| 1 | /* |
| 2 | =========================================================================== |
| 3 | blockattack - Block Attack - Rise of the Blocks |
| 4 | Copyright (C) 2005-2016 Poul Sander |
| 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see http://www.gnu.org/licenses/ |
| 18 | |
| 19 | Source information and contacts persons can be found at |
| 20 | https://blockattack.net |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | #include "levelselect.hpp" |
| 25 | #include "SDL.h" |
| 26 | #include "common.h" |
| 27 | #include "global.hpp" |
| 28 | #include "puzzlehandler.hpp" |
| 29 | #include "stageclearhandler.hpp" |
| 30 | #include "MenuSystem.h" |
| 31 | #include <fmt/core.h> |
| 32 | |
| 33 | |
| 34 | static bool bMouseUp; //true if the mouse(1) is unpressed |
| 35 | |
| 36 | static std::map<std::string, std::shared_ptr<sago::SagoTextField> > fieldCache; |
| 37 | |
| 38 | static sago::SagoTextField* getCachedText(const std::string& text) { |
| 39 | std::shared_ptr<sago::SagoTextField> ptr = fieldCache[text]; |
| 40 | if (!ptr) { |
| 41 | std::shared_ptr<sago::SagoTextField> newText = std::make_shared<sago::SagoTextField>(); |
| 42 | sagoTextSetBlueFont(*newText.get()); |
| 43 | newText->SetText(text); |
| 44 | fieldCache[text] = newText; |
| 45 | } |
| 46 | return fieldCache[text].get(); |
| 47 | } |
| 48 | |
| 49 | static void Write(SDL_Renderer* target, int x, int y, const char* text) { |
| 50 | getCachedText(text)->Draw(target, x, y, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize); |
| 51 | } |
| 52 | |
| 53 | //The function that allows the player to choose PuzzleLevel |
| 54 | int PuzzleLevelSelect(int Type) { |
| 55 | const int xplace = 200; |
| 56 | const int yplace = 300; |
| 57 | int levelNr = 0; |
| 58 | int oldmousex = 0; |
| 59 | int oldmousey = 0; |
| 60 | bool levelSelected = false; |
| 61 | int nrOfLevels = 0; |
| 62 | Uint32 totalScore = 0; |
| 63 | Uint32 totalTime = 0; |
| 64 | int selected = 0; |
| 65 | fieldCache.clear(); |
| 66 | |
| 67 | //Loads the levels, if they havn't been loaded: |
| 68 | if (Type == 0) { |
| 69 | LoadPuzzleStages(); |
| 70 | nrOfLevels = PuzzleGetNumberOfPuzzles(); |
| 71 | } |
| 72 | if (Type == 1) { |
| 73 | LoadStageClearStages(); |
| 74 | totalScore = GetTotalScore(); |
| 75 | totalTime = GetTotalTime(); |
| 76 | nrOfLevels = GetNrOfLevels(); |
| 77 | } |
| 78 | |
| 79 | while (!levelSelected) { |
| 80 | SDL_Delay(1); |
| 81 | auto ticks = SDL_GetTicks(); |
| 82 | DrawBackground(globalData.screen); |
| 83 | int mousex; |
| 84 | int mousey; |
| 85 | globalData.logicalResize.PhysicalToLogical(globalData.mousex, globalData.mousey, mousex, mousey); |
| 86 | globalData.iCheckBoxArea.Draw(globalData.screen,ticks,xplace,yplace, &globalData.logicalResize); |
| 87 | if (Type == 0) { |
| 88 | Write(globalData.screen, xplace+12,yplace+2,_("Select Puzzle") ); |
| 89 | } |
| 90 | if (Type == 1) { |
| 91 | Write(globalData.screen, xplace+12,yplace+2, _("Stage Clear Level Select") ); |
| 92 | } |
| 93 | //Now drow the fields you click in (and a V if clicked): |
| 94 | for (int i = 0; i < nrOfLevels; i++) { |
| 95 | globalData.iLevelCheckBox.Draw(globalData.screen, ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50, &globalData.logicalResize); |
| 96 | if (i==selected) { |
| 97 | globalData.iLevelCheckBoxMarked.Draw(globalData.screen, ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50, &globalData.logicalResize); |
| 98 | } |
| 99 | if (Type == 0 && PuzzleIsCleared(i)) { |
| 100 | globalData.iLevelCheck.Draw(globalData.screen,ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50, &globalData.logicalResize); |
| 101 | } |
| 102 | if (Type == 1) { |
| 103 | if (GetStageScores(i) >= GetStageParScore(i)) { |
| 104 | globalData.yellow_star.Draw(globalData.screen, ticks, xplace+10+(i%10)*50+2, yplace+60+(i/10)*50+2, &globalData.logicalResize); |
| 105 | } |
| 106 | if (IsStageCleared(i)) { |
| 107 | globalData.iLevelCheck.Draw(globalData.screen, ticks, xplace+10+(i%10)*50, yplace+60+(i/10)*50, &globalData.logicalResize); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | SDL_Event event; |
| 113 | while ( SDL_PollEvent(&event) ) { |
| 114 | UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey); |
| 115 | globalData.logicalResize.PhysicalToLogical(globalData.mousex, globalData.mousey, mousex, mousey); |
| 116 | |
| 117 | if ( event.type == SDL_QUIT ) { |
| 118 | Config::getInstance()->setShuttingDown(5); |
| 119 | levelNr = -1; |
| 120 | levelSelected = true; |
| 121 | } |
| 122 | if (isEscapeEvent(event)) { |
| 123 | levelNr = -1; |
| 124 | levelSelected = true; |
| 125 | } |
| 126 | if (isConfirmEvent(event)) { |
| 127 | levelNr = selected; |
| 128 | levelSelected = true; |
| 129 | } |
| 130 | if (isRightEvent(event)) { |
| 131 | ++selected; |
| 132 | if (selected >= nrOfLevels) { |
| 133 | selected = 0; |
| 134 | } |
| 135 | } |
| 136 | if (isLeftEvent(event)) { |
| 137 | --selected; |
| 138 | if (selected < 0) { |
| 139 | selected = nrOfLevels-1; |
| 140 | } |
| 141 | } |
| 142 | if (isDownEvent(event)) { |
| 143 | selected+=10; |
| 144 | if (selected >= nrOfLevels) { |
| 145 | selected-=10; |
| 146 | } |
| 147 | } |
| 148 | if (isUpEvent(event)) { |
| 149 | selected-=10; |
| 150 | if (selected < 0) { |
| 151 | selected+=10; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | SDL_GetKeyboardState(nullptr); |
| 157 | |
| 158 | if (mousex != oldmousex || mousey != oldmousey) { |
| 159 | int tmpSelected = -1; |
| 160 | int j; |
| 161 | for (j = 0; (tmpSelected == -1) && ( (j<nrOfLevels/10)||((j<nrOfLevels/10+1)&&(nrOfLevels%10 != 0)) ); j++) { |
| 162 | if ((60+j*50<mousey-yplace)&&(mousey-yplace<j*50+92)) { |
| 163 | tmpSelected = j*10; |
| 164 | } |
| 165 | } |
| 166 | if (tmpSelected != -1) { |
| 167 | for (int k = 0; (( (!(nrOfLevels%10) || k<nrOfLevels-10*(j-1)) )&&(k<10)); k++) { |
| 168 | if ((10+k*50<mousex-xplace)&&(mousex-xplace<k*50+42)) { |
| 169 | tmpSelected +=k; |
| 170 | selected = tmpSelected; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | oldmousey = mousey; |
| 176 | oldmousex= mousex; |
| 177 | |
| 178 | // If the mouse button is released, make bMouseUp equal true |
| 179 | if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) { |
| 180 | bMouseUp=true; |
| 181 | } |
| 182 | |
| 183 | if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) { |
| 184 | bMouseUp = false; |
| 185 | |
| 186 | int levelClicked = -1; |
| 187 | int i; |
| 188 | for (i = 0; (i<nrOfLevels/10)||((i<nrOfLevels/10+1)&&(nrOfLevels%10 != 0)); i++) |
| 189 | if ((60+i*50<mousey-yplace)&&(mousey-yplace<i*50+92)) { |
| 190 | levelClicked = i*10; |
| 191 | } |
| 192 | i++; |
| 193 | if (levelClicked != -1) |
| 194 | for (int j = 0; ((j<nrOfStageLevels%(i*10))&&(j<10)); j++) |
| 195 | if ((10+j*50<mousex-xplace)&&(mousex-xplace<j*50+42)) { |
| 196 | levelClicked +=j; |
| 197 | levelSelected = true; |
| 198 | levelNr = levelClicked; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (Type == 1) { |
| 203 | std::string scoreString = fmt::format(_("Best score: {}"), GetStageScores(selected)) ; |
| 204 | std::string timeString = fmt::format(_("Time used: {}"),"-- : --"); |
| 205 | std::string parScoreString = fmt::format(_("Par score: {}"), GetStageParScore(selected)); |
| 206 | |
| 207 | if (GetStageTime(selected)>0) { |
| 208 | timeString = fmt::format(_("Time used: {} : {:02}"), GetStageTime(selected)/1000/60, (GetStageTime(selected)/1000)%60); |
| 209 | } |
| 210 | |
| 211 | Write(globalData.screen, 200,200,scoreString.c_str()); |
| 212 | Write(globalData.screen, 500, 200, parScoreString.c_str()); |
| 213 | Write(globalData.screen, 200,250,timeString.c_str()); |
| 214 | std::string totalString = fmt::format(_("Total score: {} in {}:{:02}"), totalScore, totalTime/1000/60, ((totalTime/1000)%60) ); |
| 215 | Write(globalData.screen, 200,600,totalString.c_str()); |
| 216 | } |
| 217 | |
| 218 | globalData.mouse.Draw(globalData.screen, SDL_GetTicks(), globalData.mousex, globalData.mousey); |
| 219 | SDL_RenderPresent(globalData.screen); //draws it all to the screen |
| 220 | |
| 221 | } |
| 222 | DrawBackground(globalData.screen); |
| 223 | return levelNr; |
| 224 | } |