git repos / blockattack-game

commit c6c637cc

sago007 · 2016-11-12 17:35
c6c637ccd95ac3ebfbd57666b8b88bc91e25b86d patch · browse files
parent fa8b055b99bd97724e4537222d7109bfc73d920c

Moved the LevelSelect function out from main.cpp. Also added a small delay to stop massive CPU usage.

Changed files

M source/code/global.hpp before
A source/code/levelselect.cpp
A source/code/levelselect.hpp
M source/code/main.cpp before
M source/code/mainVars.inc before
diff --git a/source/code/global.hpp b/source/code/global.hpp index e18d029..6dffbca 100644 --- a/source/code/global.hpp +++ b/source/code/global.hpp
@@ -45,6 +45,10 @@ struct GlobalData {
sago::SagoSprite bHighScore;
sago::SagoSprite bBack;
sago::SagoSprite bNext;
+ sago::SagoSprite iLevelCheck; //To the level select screen
+ sago::SagoSprite iLevelCheckBox;
+ sago::SagoSprite iLevelCheckBoxMarked;
+ sago::SagoSprite iCheckBoxArea;
NFont nf_scoreboard_font;
NFont nf_standard_blue_font;
NFont nf_button_font;
diff --git a/source/code/levelselect.cpp b/source/code/levelselect.cpp new file mode 100644 index 0000000..821d669 --- /dev/null +++ b/source/code/levelselect.cpp
@@ -0,0 +1,206 @@
+/*
+===========================================================================
+blockattack - Block Attack - Rise of the Blocks
+Copyright (C) 2005-2016 Poul Sander
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see http://www.gnu.org/licenses/
+
+Source information and contacts persons can be found at
+http://www.blockattack.net
+===========================================================================
+*/
+
+#include "levelselect.hpp"
+#include "SDL.h"
+#include "common.h"
+#include "global.hpp"
+#include "puzzlehandler.hpp"
+#include "stageclearhandler.hpp"
+#include "MenuSystem.h"
+
+
+using std::string;
+using std::cerr;
+using std::cout;
+using std::exception;
+using std::vector;
+
+static bool bMouseUp; //true if the mouse(1) is unpressed
+
+static void NFont_Write(SDL_Renderer* target, int x, int y, const std::string& text) {
+ globalData.nf_standard_blue_font.draw(target, x, y, "%s", text.c_str());
+}
+
+//The function that allows the player to choose PuzzleLevel
+int PuzzleLevelSelect(int Type) {
+ const int xplace = 200;
+ const int yplace = 300;
+ int levelNr = 0;
+ int oldmousex = 0;
+ int oldmousey = 0;
+ bool levelSelected = false;
+ int nrOfLevels = 0;
+ Uint32 totalScore = 0;
+ Uint32 totalTime = 0;
+ int selected = 0;
+
+ //Loads the levels, if they havn't been loaded:
+ if (Type == 0) {
+ LoadPuzzleStages();
+ }
+ if (Type == 0) {
+ nrOfLevels = PuzzleGetNumberOfPuzzles();
+ }
+ if (Type == 1) {
+ LoadStageClearStages();
+ totalScore = GetTotalScore();
+ totalTime = GetTotalTime();
+ nrOfLevels = GetNrOfLevels();
+ }
+
+ while (!levelSelected) {
+ SDL_Delay(1);
+ auto ticks = SDL_GetTicks();
+ DrawBackground(globalData.screen);
+ globalData.iCheckBoxArea.Draw(globalData.screen,ticks,xplace,yplace);
+ if (Type == 0) {
+ NFont_Write(globalData.screen, xplace+12,yplace+2,_("Select Puzzle") );
+ }
+ if (Type == 1) {
+ NFont_Write(globalData.screen, xplace+12,yplace+2, _("Stage Clear Level Select") );
+ }
+ //Now drow the fields you click in (and a V if clicked):
+ for (int i = 0; i < nrOfLevels; i++) {
+ globalData.iLevelCheckBox.Draw(globalData.screen, ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50);
+ if (i==selected) {
+ globalData.iLevelCheckBoxMarked.Draw(globalData.screen, ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50);
+ }
+ if (Type == 0 && PuzzleIsCleared(i)) {
+ globalData.iLevelCheck.Draw(globalData.screen,ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50);
+ }
+ if (Type == 1 && IsStageCleared(i)) {
+ globalData.iLevelCheck.Draw(globalData.screen, ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50);
+ }
+ }
+
+ SDL_Event event;
+ while ( SDL_PollEvent(&event) ) {
+ UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey);
+
+ if ( event.type == SDL_QUIT ) {
+ Config::getInstance()->setShuttingDown(5);
+ levelNr = -1;
+ levelSelected = true;
+ }
+ if (isEscapeEvent(event)) {
+ levelNr = -1;
+ levelSelected = true;
+ }
+ if (isConfirmEvent(event)) {
+ levelNr = selected;
+ levelSelected = true;
+ }
+ if (isRightEvent(event)) {
+ ++selected;
+ if (selected >= nrOfLevels) {
+ selected = 0;
+ }
+ }
+ if (isLeftEvent(event)) {
+ --selected;
+ if (selected < 0) {
+ selected = nrOfLevels-1;
+ }
+ }
+ if (isDownEvent(event)) {
+ selected+=10;
+ if (selected >= nrOfLevels) {
+ selected-=10;
+ }
+ }
+ if (isUpEvent(event)) {
+ selected-=10;
+ if (selected < 0) {
+ selected+=10;
+ }
+ }
+ }
+
+ SDL_GetKeyboardState(nullptr);
+
+ if (globalData.mousex != oldmousex || globalData.mousey != oldmousey) {
+ int tmpSelected = -1;
+ int j;
+ for (j = 0; (tmpSelected == -1) && ( (j<nrOfLevels/10)||((j<nrOfLevels/10+1)&&(nrOfLevels%10 != 0)) ); j++) {
+ if ((60+j*50<globalData.mousey-yplace)&&(globalData.mousey-yplace<j*50+92)) {
+ tmpSelected = j*10;
+ }
+ }
+ if (tmpSelected != -1) {
+ for (int k = 0; (( (!(nrOfLevels%10) || k<nrOfLevels-10*(j-1)) )&&(k<10)); k++) {
+ if ((10+k*50<globalData.mousex-xplace)&&(globalData.mousex-xplace<k*50+42)) {
+ tmpSelected +=k;
+ selected = tmpSelected;
+ }
+ }
+ }
+ }
+ oldmousey = globalData.mousey;
+ oldmousex= globalData.mousex;
+
+ // If the mouse button is released, make bMouseUp equal true
+ if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
+ bMouseUp=true;
+ }
+
+ if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
+ bMouseUp = false;
+
+ int levelClicked = -1;
+ int i;
+ for (i = 0; (i<nrOfLevels/10)||((i<nrOfLevels/10+1)&&(nrOfLevels%10 != 0)); i++)
+ if ((60+i*50<globalData.mousey-yplace)&&(globalData.mousey-yplace<i*50+92)) {
+ levelClicked = i*10;
+ }
+ i++;
+ if (levelClicked != -1)
+ for (int j = 0; ((j<nrOfStageLevels%(i*10))&&(j<10)); j++)
+ if ((10+j*50<globalData.mousex-xplace)&&(globalData.mousex-xplace<j*50+42)) {
+ levelClicked +=j;
+ levelSelected = true;
+ levelNr = levelClicked;
+ }
+ }
+
+ if (Type == 1) {
+ string scoreString = SPrintStringF(_("Best score: %i"), GetStageScores(selected)) ;
+ string timeString = SPrintStringF(_("Time used: %s"),"-- : --");
+
+ if (GetStageTime(selected)>0) {
+ timeString = SPrintStringF(_("Time used: %d : %02d"), GetStageTime(selected)/1000/60, (GetStageTime(selected)/1000)%60);
+ }
+
+ NFont_Write(globalData.screen, 200,200,scoreString.c_str());
+ NFont_Write(globalData.screen, 200,250,timeString.c_str());
+ string totalString = (boost::format(_("Total score: %1% in %2%:%3%"))%totalScore%(totalTime/1000/60)%((totalTime/1000)%60)).str();
+ NFont_Write(globalData.screen, 200,600,totalString.c_str());
+ }
+
+ globalData.mouse.Draw(globalData.screen, SDL_GetTicks(), globalData.mousex, globalData.mousey);
+ SDL_RenderPresent(globalData.screen); //draws it all to the screen
+
+ }
+ DrawBackground(globalData.screen);
+ return levelNr;
+}
\ No newline at end of file
diff --git a/source/code/levelselect.hpp b/source/code/levelselect.hpp new file mode 100644 index 0000000..7f86173 --- /dev/null +++ b/source/code/levelselect.hpp
@@ -0,0 +1,30 @@
+/*
+===========================================================================
+blockattack - Block Attack - Rise of the Blocks
+Copyright (C) 2005-2016 Poul Sander
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see http://www.gnu.org/licenses/
+
+Source information and contacts persons can be found at
+http://www.blockattack.net
+===========================================================================
+*/
+
+#ifndef LEVELSELECT_HPP
+#define LEVELSELECT_HPP
+
+int PuzzleLevelSelect(int Type);
+
+#endif /* LEVELSELECT_HPP */
+
diff --git a/source/code/main.cpp b/source/code/main.cpp index 7870694..40db252 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -73,6 +73,7 @@ http://www.blockattack.net
#include "gamecontroller.h"
#include <boost/program_options.hpp>
#include <fstream>
+#include "levelselect.hpp"
/*******************************************************************************
* All variables and constant has been moved to mainVars.inc for the overview. *
@@ -148,10 +149,10 @@ static int InitImages(sago::SagoSpriteHolder& holder) {
iDraw = holder.GetSprite("i_draw");
iLoser = holder.GetSprite("i_loser");
iChainFrame = holder.GetSprite("chain_frame");
- iLevelCheck = holder.GetSprite("i_level_check");
- iLevelCheckBox = holder.GetSprite("i_level_check_box");
- iLevelCheckBoxMarked = holder.GetSprite("i_level_check_box_marked");
- iCheckBoxArea = holder.GetSprite("i_check_box_area");
+ globalData.iLevelCheck = holder.GetSprite("i_level_check");
+ globalData.iLevelCheckBox = holder.GetSprite("i_level_check_box");
+ globalData.iLevelCheckBoxMarked = holder.GetSprite("i_level_check_box_marked");
+ globalData.iCheckBoxArea = holder.GetSprite("i_check_box_area");
boardBackBack = holder.GetSprite("board_back_back");
garbageTL = holder.GetSprite("garbage_tl");
garbageT = holder.GetSprite("garbage_t");
@@ -880,167 +881,6 @@ void DrawEverything(int xsize, int ysize,BlockGameSdl* theGame, BlockGameSdl* th
#endif
}
-//The function that allows the player to choose PuzzleLevel
-int PuzzleLevelSelect(int Type) {
- const int xplace = 200;
- const int yplace = 300;
- int levelNr = 0;
- int oldmousex = 0;
- int oldmousey = 0;
- bool levelSelected = false;
- int nrOfLevels = 0;
- Uint32 totalScore = 0;
- Uint32 totalTime = 0;
- int selected = 0;
-
- //Loads the levels, if they havn't been loaded:
- if (Type == 0) {
- LoadPuzzleStages();
- }
- if (Type == 0) {
- nrOfLevels = PuzzleGetNumberOfPuzzles();
- }
- if (Type == 1) {
- LoadStageClearStages();
- totalScore = GetTotalScore();
- totalTime = GetTotalTime();
- nrOfLevels = GetNrOfLevels();
- }
-
- while (!levelSelected) {
- DrawBackground(globalData.screen);
- DrawIMG(iCheckBoxArea,globalData.screen,xplace,yplace);
- if (Type == 0) {
- NFont_Write(globalData.screen, xplace+12,yplace+2,_("Select Puzzle") );
- }
- if (Type == 1) {
- NFont_Write(globalData.screen, xplace+12,yplace+2, _("Stage Clear Level Select") );
- }
- //Now drow the fields you click in (and a V if clicked):
- for (int i = 0; i < nrOfLevels; i++) {
- DrawIMG(iLevelCheckBox,globalData.screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
- if (i==selected) {
- DrawIMG(iLevelCheckBoxMarked,globalData.screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
- }
- if (Type == 0 && PuzzleIsCleared(i)) {
- DrawIMG(iLevelCheck,globalData.screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
- }
- if (Type == 1 && IsStageCleared(i)) {
- DrawIMG(iLevelCheck,globalData.screen,xplace+10+(i%10)*50, yplace+60+(i/10)*50);
- }
- }
-
- SDL_Event event;
- while ( SDL_PollEvent(&event) ) {
- UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey);
-
- if ( event.type == SDL_QUIT ) {
- Config::getInstance()->setShuttingDown(5);
- levelNr = -1;
- levelSelected = true;
- }
- if (isEscapeEvent(event)) {
- levelNr = -1;
- levelSelected = true;
- }
- if (isConfirmEvent(event)) {
- levelNr = selected;
- levelSelected = true;
- }
- if (isRightEvent(event)) {
- ++selected;
- if (selected >= nrOfLevels) {
- selected = 0;
- }
- }
- if (isLeftEvent(event)) {
- --selected;
- if (selected < 0) {
- selected = nrOfLevels-1;
- }
- }
- if (isDownEvent(event)) {
- selected+=10;
- if (selected >= nrOfLevels) {
- selected-=10;
- }
- }
- if (isUpEvent(event)) {
- selected-=10;
- if (selected < 0) {
- selected+=10;
- }
- }
- }
-
- SDL_GetKeyboardState(nullptr);
-
- if (globalData.mousex != oldmousex || globalData.mousey != oldmousey) {
- int tmpSelected = -1;
- int j;
- for (j = 0; (tmpSelected == -1) && ( (j<nrOfLevels/10)||((j<nrOfLevels/10+1)&&(nrOfLevels%10 != 0)) ); j++) {
- if ((60+j*50<globalData.mousey-yplace)&&(globalData.mousey-yplace<j*50+92)) {
- tmpSelected = j*10;
- }
- }
- if (tmpSelected != -1) {
- for (int k = 0; (( (!(nrOfLevels%10) || k<nrOfLevels-10*(j-1)) )&&(k<10)); k++) {
- if ((10+k*50<globalData.mousex-xplace)&&(globalData.mousex-xplace<k*50+42)) {
- tmpSelected +=k;
- selected = tmpSelected;
- }
- }
- }
- }
- oldmousey = globalData.mousey;
- oldmousex= globalData.mousex;
-
- // If the mouse button is released, make bMouseUp equal true
- if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
- bMouseUp=true;
- }
-
- if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
- bMouseUp = false;
-
- int levelClicked = -1;
- int i;
- for (i = 0; (i<nrOfLevels/10)||((i<nrOfLevels/10+1)&&(nrOfLevels%10 != 0)); i++)
- if ((60+i*50<globalData.mousey-yplace)&&(globalData.mousey-yplace<i*50+92)) {
- levelClicked = i*10;
- }
- i++;
- if (levelClicked != -1)
- for (int j = 0; ((j<nrOfStageLevels%(i*10))&&(j<10)); j++)
- if ((10+j*50<globalData.mousex-xplace)&&(globalData.mousex-xplace<j*50+42)) {
- levelClicked +=j;
- levelSelected = true;
- levelNr = levelClicked;
- }
- }
-
- if (Type == 1) {
- string scoreString = SPrintStringF(_("Best score: %i"), GetStageScores(selected)) ;
- string timeString = SPrintStringF(_("Time used: %s"),"-- : --");
-
- if (GetStageTime(selected)>0) {
- timeString = SPrintStringF(_("Time used: %d : %02d"), GetStageTime(selected)/1000/60, (GetStageTime(selected)/1000)%60);
- }
-
- NFont_Write(globalData.screen, 200,200,scoreString.c_str());
- NFont_Write(globalData.screen, 200,250,timeString.c_str());
- string totalString = (boost::format(_("Total score: %1% in %2%:%3%"))%totalScore%(totalTime/1000/60)%((totalTime/1000)%60)).str();
- NFont_Write(globalData.screen, 200,600,totalString.c_str());
- }
-
- globalData.mouse.Draw(globalData.screen, SDL_GetTicks(), globalData.mousex, globalData.mousey);
- SDL_RenderPresent(globalData.screen); //draws it all to the screen
-
- }
- DrawBackground(globalData.screen);
- return levelNr;
-}
-
static BlockGameSdl* player1;
static BlockGameSdl* player2;
diff --git a/source/code/mainVars.inc b/source/code/mainVars.inc index 41f449e..947745b 100644 --- a/source/code/mainVars.inc +++ b/source/code/mainVars.inc
@@ -70,10 +70,7 @@ static sago::SagoSprite counter[3]; //Counts down from 3
static sago::SagoSprite bricks[7]; //The bricks, saved in an array of pointers
static sago::SagoSprite crossover; //Cross the bricks that will be cleared soon
static sago::SagoSprite balls[7]; //The balls (the small ones that jump around)
-static sago::SagoSprite iLevelCheck; //To the level select screen
-static sago::SagoSprite iLevelCheckBox;
-static sago::SagoSprite iLevelCheckBoxMarked;
-static sago::SagoSprite iCheckBoxArea;
+
static sago::SagoSprite boardBackBack;
static sago::SagoSprite garbageTL; //the Garbage Blocks
static sago::SagoSprite garbageT;