/* =========================================================================== 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 https://blockattack.net =========================================================================== */ #include "levelselect.hpp" #include "SDL.h" #include "common.h" #include "global.hpp" #include "puzzlehandler.hpp" #include "stageclearhandler.hpp" #include "MenuSystem.h" #include static bool bMouseUp; //true if the mouse(1) is unpressed static std::map > fieldCache; static sago::SagoTextField* getCachedText(const std::string& text) { std::shared_ptr ptr = fieldCache[text]; if (!ptr) { std::shared_ptr newText = std::make_shared(); sagoTextSetBlueFont(*newText.get()); newText->SetText(text); fieldCache[text] = newText; } return fieldCache[text].get(); } static void Write(SDL_Renderer* target, int x, int y, const char* text) { getCachedText(text)->Draw(target, x, y, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize); } //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; fieldCache.clear(); //Loads the levels, if they havn't been loaded: if (Type == 0) { LoadPuzzleStages(); nrOfLevels = PuzzleGetNumberOfPuzzles(); } if (Type == 1) { LoadStageClearStages(); totalScore = GetTotalScore(); totalTime = GetTotalTime(); nrOfLevels = GetNrOfLevels(); } while (!levelSelected) { SDL_Delay(1); auto ticks = SDL_GetTicks(); DrawBackground(globalData.screen); int mousex; int mousey; globalData.logicalResize.PhysicalToLogical(globalData.mousex, globalData.mousey, mousex, mousey); globalData.iCheckBoxArea.Draw(globalData.screen,ticks,xplace,yplace, &globalData.logicalResize); if (Type == 0) { Write(globalData.screen, xplace+12,yplace+2,_("Select Puzzle") ); } if (Type == 1) { 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, &globalData.logicalResize); if (i==selected) { globalData.iLevelCheckBoxMarked.Draw(globalData.screen, ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50, &globalData.logicalResize); } if (Type == 0 && PuzzleIsCleared(i)) { globalData.iLevelCheck.Draw(globalData.screen,ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50, &globalData.logicalResize); } if (Type == 1 && IsStageCleared(i)) { globalData.iLevelCheck.Draw(globalData.screen, ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50, &globalData.logicalResize); } } SDL_Event event; while ( SDL_PollEvent(&event) ) { UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey); globalData.logicalResize.PhysicalToLogical(globalData.mousex, globalData.mousey, mousex, 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 (mousex != oldmousex || mousey != oldmousey) { int tmpSelected = -1; int j; for (j = 0; (tmpSelected == -1) && ( (j0) { timeString = fmt::format(_("Time used: {} : {:02}"), GetStageTime(selected)/1000/60, (GetStageTime(selected)/1000)%60); } Write(globalData.screen, 200,200,scoreString.c_str()); Write(globalData.screen, 200,250,timeString.c_str()); std::string totalString = fmt::format(_("Total score: {} in {}:{:02}"), totalScore, totalTime/1000/60, ((totalTime/1000)%60) ); 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; }