| 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 "MenuSystem.h" |
| 25 | #include "replayhandler.hpp" |
| 26 | #include "common.h" |
| 27 | #include "global.hpp" |
| 28 | #include "scopeHelpers.hpp" |
| 29 | #include "icon.hpp" |
| 30 | #include "os.hpp" |
| 31 | |
| 32 | #include <string.h> |
| 33 | #include <fmt/core.h> |
| 34 | |
| 35 | #include "version.h" |
| 36 | |
| 37 | #define WITH_SDL 1 |
| 38 | |
| 39 | #include "sago/SagoSpriteHolder.hpp" |
| 40 | #include "sago/SagoTextBox.hpp" |
| 41 | #include "sago/SagoMisc.hpp" |
| 42 | #include <iostream> |
| 43 | #include <stdlib.h> |
| 44 | #include <time.h> //Used for srand() |
| 45 | #include <sstream> //Still used by std::to_string2 |
| 46 | #include <string> |
| 47 | #include "SDL.h" //The SDL libary, used for most things |
| 48 | #include <SDL_mixer.h> //Used for sound & music |
| 49 | #include <SDL_image.h> //To load PNG images! |
| 50 | #include <physfs.h> //Abstract file system. To use containers |
| 51 | #include <vector> |
| 52 | #include "puzzlehandler.hpp" |
| 53 | #include "stageclearhandler.hpp" |
| 54 | #include <memory> |
| 55 | #include "ScoresDisplay.hpp" |
| 56 | |
| 57 | #include "highscore.h" //Stores highscores |
| 58 | #include "ReadKeyboard.h" //Reads text from keyboard |
| 59 | #include "stats.h" //Saves general stats |
| 60 | |
| 61 | |
| 62 | #include "common.h" |
| 63 | #include "gamecontroller.h" |
| 64 | #include <boost/algorithm/string.hpp> |
| 65 | #include <boost/program_options.hpp> |
| 66 | #include <fstream> |
| 67 | #include "levelselect.hpp" |
| 68 | |
| 69 | #include "ExplosionManager.hpp" |
| 70 | |
| 71 | #include "editor/SagoTextureSelector.hpp" |
| 72 | #include "puzzle_editor/PuzzleEditorState.hpp" |
| 73 | #include "SagoImGui.hpp" |
| 74 | |
| 75 | /******************************************************************************* |
| 76 | * All variables and constant has been moved to mainVars.inc for the overview. * |
| 77 | *******************************************************************************/ |
| 78 | #include "mainVars.inc" |
| 79 | |
| 80 | GlobalData globalData; |
| 81 | |
| 82 | static int InitImages(sago::SagoSpriteHolder& holder); |
| 83 | |
| 84 | static void FsSearchParthMainAppend(std::vector<std::string>& paths) { |
| 85 | paths.push_back((std::string)SHAREDIR+"/blockattack.data"); |
| 86 | paths.push_back((std::string)PHYSFS_getBaseDir()+"/blockattack.data"); |
| 87 | paths.push_back((std::string)PHYSFS_getBaseDir()+"/data"); |
| 88 | } |
| 89 | |
| 90 | static void FsSearchPathModAppend(std::vector<std::string>& paths, const std::vector<std::string>& modlist) { |
| 91 | for (const std::string& mod : modlist) { |
| 92 | std::string filename = std::string(SHAREDIR)+"/mods/"+mod+".data"; |
| 93 | paths.push_back(filename); |
| 94 | filename = std::string(PHYSFS_getBaseDir())+"/mods/"+mod+".data"; |
| 95 | paths.push_back(filename); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static void PhysFsSetSearchPath(const std::vector<std::string>& paths, const std::string& savepath) { |
| 100 | for (const std::string& path : paths) { |
| 101 | PHYSFS_mount(path.c_str(), "/", 0); |
| 102 | } |
| 103 | PHYSFS_mount(savepath.c_str(), "/", 0); |
| 104 | PHYSFS_setWriteDir(savepath.c_str()); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void PhysFsCreateFolders() { |
| 109 | //PHYSFS_mkdir("screenshots"); |
| 110 | PHYSFS_mkdir("replays"); |
| 111 | PHYSFS_mkdir("puzzles"); |
| 112 | } |
| 113 | |
| 114 | static void setGameOverFont(const sago::SagoDataHolder* holder, sago::SagoTextBox& field, const char* text) { |
| 115 | field.SetHolder(holder); |
| 116 | field.SetFont("freeserif"); |
| 117 | field.SetColor({0,0,255,255}); |
| 118 | field.SetFontSize(60); |
| 119 | field.SetOutline(3, {192,192,255,255}); |
| 120 | field.SetMaxWidth(280); |
| 121 | field.SetText(text); |
| 122 | } |
| 123 | |
| 124 | //Load all image files to memory |
| 125 | static int InitImages(sago::SagoSpriteHolder& holder) { |
| 126 | globalData.bricks[0] = holder.GetSprite("block_blue"); |
| 127 | globalData.bricks[1] = holder.GetSprite("block_green"); |
| 128 | globalData.bricks[2] = holder.GetSprite("block_purple"); |
| 129 | globalData.bricks[3] = holder.GetSprite("block_red"); |
| 130 | globalData.bricks[4] = holder.GetSprite("block_yellow"); |
| 131 | globalData.bricks[5] = holder.GetSprite("block_turkish"); |
| 132 | globalData.bricks[6] = holder.GetSprite("block_grey"); |
| 133 | globalData.bomb = holder.GetSprite("block_bomb"); |
| 134 | globalData.bHighScore = holder.GetSprite("b_highscore"); |
| 135 | globalData.bBack = holder.GetSprite("b_blank"); |
| 136 | bForward = holder.GetSprite("b_forward"); |
| 137 | globalData.blackLine = holder.GetSprite("black_line"); |
| 138 | globalData.stageBobble = holder.GetSprite("i_stage_clear_limit"); |
| 139 | globalData.crossover = holder.GetSprite("crossover"); |
| 140 | globalData.balls[0] = holder.GetSprite("ball_blue"); |
| 141 | globalData.balls[1] = holder.GetSprite("ball_green"); |
| 142 | globalData.balls[2] = holder.GetSprite("ball_purple"); |
| 143 | globalData.balls[3] = holder.GetSprite("ball_red"); |
| 144 | globalData.balls[4] = holder.GetSprite("ball_yellow"); |
| 145 | globalData.balls[5] = holder.GetSprite("ball_turkish"); |
| 146 | globalData.balls[6] = holder.GetSprite("ball_gray"); |
| 147 | globalData.yellow_star = holder.GetSprite("star_yellow"); |
| 148 | globalData.cursor = holder.GetSprite("cursor"); |
| 149 | globalData.ready = holder.GetSprite("block_ready"); |
| 150 | globalData.explosion[0] = holder.GetSprite("explosion0"); |
| 151 | globalData.explosion[1] = holder.GetSprite("explosion1"); |
| 152 | globalData.explosion[2] = holder.GetSprite("explosion2"); |
| 153 | globalData.explosion[3] = holder.GetSprite("explosion3"); |
| 154 | globalData.counter[0] = holder.GetSprite("counter_1"); |
| 155 | globalData.counter[1] = holder.GetSprite("counter_2"); |
| 156 | globalData.counter[2] = holder.GetSprite("counter_3"); |
| 157 | setGameOverFont(&holder.GetDataHolder(),globalData.tbGameOver, _("GAME OVER")); |
| 158 | setGameOverFont(&holder.GetDataHolder(),globalData.tbWinner, _("WINNER")); |
| 159 | setGameOverFont(&holder.GetDataHolder(),globalData.tbDraw, _("DRAW")); |
| 160 | globalData.iChainFrame = holder.GetSprite("chain_frame"); |
| 161 | globalData.iLevelCheck = holder.GetSprite("i_level_check"); |
| 162 | globalData.iLevelCheckBox = holder.GetSprite("i_level_check_box"); |
| 163 | globalData.iLevelCheckBoxMarked = holder.GetSprite("i_level_check_box_marked"); |
| 164 | globalData.iCheckBoxArea = holder.GetSprite("i_check_box_area"); |
| 165 | globalData.boardBackBack = holder.GetSprite("board_back_back"); |
| 166 | globalData.garbageTL = holder.GetSprite("garbage_tl"); |
| 167 | globalData.garbageT = holder.GetSprite("garbage_t"); |
| 168 | globalData.garbageTR = holder.GetSprite("garbage_tr"); |
| 169 | globalData.garbageR = holder.GetSprite("garbage_r"); |
| 170 | globalData.garbageBR = holder.GetSprite("garbage_br"); |
| 171 | globalData.garbageB = holder.GetSprite("garbage_b"); |
| 172 | globalData.garbageBL = holder.GetSprite("garbage_bl"); |
| 173 | globalData.garbageL = holder.GetSprite("garbage_l"); |
| 174 | globalData.garbageFill = holder.GetSprite("garbage_fill"); |
| 175 | globalData.garbageML = holder.GetSprite("garbage_ml"); |
| 176 | globalData.garbageM = holder.GetSprite("garbage_m"); |
| 177 | globalData.garbageMR = holder.GetSprite("garbage_mr"); |
| 178 | globalData.garbageGM = holder.GetSprite("garbage_gm"); |
| 179 | globalData.garbageGML = holder.GetSprite("garbage_gml"); |
| 180 | globalData.garbageGMR = holder.GetSprite("garbage_gmr"); |
| 181 | globalData.transCover = holder.GetSprite("trans_cover"); |
| 182 | bExit = holder.GetSprite("b_exit"); |
| 183 | globalData.bSkip = holder.GetSprite("b_blank"); |
| 184 | globalData.bNext = holder.GetSprite("b_blank"); |
| 185 | globalData.bRetry = holder.GetSprite("b_blank"); |
| 186 | globalData.mouse = holder.GetSprite("mouse"); |
| 187 | |
| 188 | globalData.cordNextButton.x = 3*bsize+(3*bsize-buttonXsize)/2; |
| 189 | globalData.cordNextButton.y = 10*bsize; |
| 190 | globalData.cordNextButton.xsize = buttonXsize; |
| 191 | globalData.cordNextButton.ysize = buttonYsize; |
| 192 | |
| 193 | |
| 194 | globalData.cordRetryButton.x = (3*bsize-buttonXsize)/2; |
| 195 | globalData.cordRetryButton.y = 10*bsize; |
| 196 | globalData.cordRetryButton.xsize = buttonXsize; |
| 197 | globalData.cordRetryButton.ysize = buttonYsize; |
| 198 | |
| 199 | |
| 200 | //Loads the sound if sound present |
| 201 | if (!globalData.NoSound) { |
| 202 | //And here the music: |
| 203 | bgMusic = holder.GetDataHolder().getMusicHandler("bgmusic"); |
| 204 | highbeatMusic = holder.GetDataHolder().getMusicHandler("highbeat"); |
| 205 | //the music... we just hope it exists, else the user won't hear anything |
| 206 | //Same goes for the sounds |
| 207 | globalData.boing = holder.GetDataHolder().getSoundHandler("pop"); |
| 208 | globalData.applause = holder.GetDataHolder().getSoundHandler("applause"); |
| 209 | globalData.photoClick = holder.GetDataHolder().getSoundHandler("cameraclick"); |
| 210 | globalData.typingChunk = holder.GetDataHolder().getSoundHandler("typing"); |
| 211 | globalData.counterChunk = holder.GetDataHolder().getSoundHandler("counter"); |
| 212 | globalData.counterFinalChunk = holder.GetDataHolder().getSoundHandler("counter_final"); |
| 213 | Config::getInstance()->setDefault("volume_sound", "24"); //0-128 |
| 214 | int soundVolume = Config::getInstance()->getInt("volume_sound"); |
| 215 | Mix_VolumeChunk(globalData.boing.get(), soundVolume); |
| 216 | Mix_VolumeChunk(globalData.applause.get(), soundVolume); |
| 217 | Mix_VolumeChunk(globalData.photoClick.get(), soundVolume); |
| 218 | Mix_VolumeChunk(globalData.typingChunk.get(), soundVolume); |
| 219 | Mix_VolumeChunk(globalData.counterChunk.get(), soundVolume); |
| 220 | Mix_VolumeChunk(globalData.counterFinalChunk.get(), soundVolume); |
| 221 | } //All sound has been loaded or not |
| 222 | return 0; |
| 223 | } //InitImages() |
| 224 | |
| 225 | /*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/ |
| 226 | void DrawIMG(const sago::SagoSprite& sprite, SDL_Renderer* target, int x, int y) { |
| 227 | sprite.Draw(target, SDL_GetTicks(),x,y, &globalData.logicalResize); |
| 228 | } |
| 229 | |
| 230 | void DrawIMG_Bounded(const sago::SagoSprite& sprite, SDL_Renderer* target, int x, int y, int minx, int miny, int maxx, int maxy) { |
| 231 | SDL_Rect bounds; |
| 232 | bounds.x = minx; |
| 233 | bounds.y = miny; |
| 234 | bounds.w = maxx-minx; |
| 235 | bounds.h = maxy-miny; |
| 236 | sprite.DrawBounded(target, SDL_GetTicks(),x,y,bounds, &globalData.logicalResize); |
| 237 | } |
| 238 | |
| 239 | SDL_Window* sdlWindow; |
| 240 | |
| 241 | sago::SagoDataHolder dataHolder; |
| 242 | |
| 243 | void ResetFullscreen() { |
| 244 | Mix_HaltMusic(); //We need to reload all data in case the screen type changes. Music must be stopped before unload. |
| 245 | if (globalData.bFullscreen) { |
| 246 | SDL_DisplayMode dm; |
| 247 | globalData.xsize = SIXTEEN_NINE_WIDTH; |
| 248 | globalData.ysize = SCREEN_HIGHT; |
| 249 | if (SDL_GetDesktopDisplayMode(0, &dm) == 0) { |
| 250 | globalData.xsize = globalData.ysize*dm.w/(double)dm.h; |
| 251 | } |
| 252 | SDL_SetWindowFullscreen(sdlWindow, SDL_WINDOW_FULLSCREEN_DESKTOP); |
| 253 | } |
| 254 | else { |
| 255 | globalData.xsize = FOUR_THREE_WIDTH; |
| 256 | globalData.ysize = SCREEN_HIGHT; |
| 257 | SDL_SetWindowFullscreen(sdlWindow, 0); |
| 258 | } |
| 259 | if (globalData.alwaysSixteenNine || globalData.xsize > SIXTEEN_NINE_WIDTH) { |
| 260 | globalData.xsize = SIXTEEN_NINE_WIDTH; |
| 261 | } |
| 262 | if (globalData.xsize < FOUR_THREE_WIDTH) { |
| 263 | globalData.xsize = FOUR_THREE_WIDTH; |
| 264 | } |
| 265 | |
| 266 | //SDL_RenderSetLogicalSize(globalData.screen, globalData.xsize, globalData.ysize); |
| 267 | globalData.logicalResize = sago::SagoLogicalResize(globalData.xsize, globalData.ysize); |
| 268 | dataHolder.invalidateAll(globalData.screen); |
| 269 | globalData.spriteHolder.reset(new sago::SagoSpriteHolder( dataHolder ) ); |
| 270 | globalData.dataHolder = &dataHolder; |
| 271 | globalData.spriteHolder->ReadSprites(globalData.modinfo.getModSpriteFiles()); |
| 272 | if (sago::FileExists("sprites/custom_backgrounds.sprite")) { |
| 273 | std::vector<std::string> custom_backgrounds = { "custom_backgrounds" }; |
| 274 | globalData.spriteHolder->ReadSprites(custom_backgrounds); |
| 275 | } |
| 276 | InitImages(*(globalData.spriteHolder.get()) ); |
| 277 | SDL_ShowCursor(SDL_DISABLE); |
| 278 | } |
| 279 | |
| 280 | void DrawBackground(SDL_Renderer* target) { |
| 281 | SDL_RenderClear(target); |
| 282 | int w=1; |
| 283 | int h=1; |
| 284 | SDL_GetRendererOutputSize(target, &w, &h); |
| 285 | globalData.logicalResize.SetPhysicalSize(w, h); |
| 286 | sago::SagoSprite background = globalData.spriteHolder->GetSprite(globalData.theme.background.background_sprite); |
| 287 | if ( (double)globalData.xsize/globalData.ysize > 1.5 && globalData.theme.background.background_sprite_16x9.length()) { |
| 288 | background = globalData.spriteHolder->GetSprite(globalData.theme.background.background_sprite_16x9); |
| 289 | } |
| 290 | size_t ticks = SDL_GetTicks(); |
| 291 | if (globalData.theme.background.background_scale == ImgScale::Tile) { |
| 292 | int nextX = 0; |
| 293 | if (globalData.theme.background.tileMoveSpeedX) { |
| 294 | nextX -= (ticks/globalData.theme.background.tileMoveSpeedX)%background.GetWidth(); |
| 295 | } |
| 296 | while (nextX < w) { |
| 297 | int nextY = 0; |
| 298 | if (globalData.theme.background.tileMoveSpeedY) { |
| 299 | nextY -= (ticks/globalData.theme.background.tileMoveSpeedY)%background.GetWidth(); |
| 300 | } |
| 301 | while (nextY < h ) { |
| 302 | background.Draw(target, ticks, nextX, nextY); |
| 303 | nextY += background.GetHeight(); |
| 304 | } |
| 305 | nextX += background.GetWidth(); |
| 306 | } |
| 307 | return; |
| 308 | } |
| 309 | SDL_Rect r = {0,0,globalData.xsize,globalData.ysize}; |
| 310 | globalData.logicalResize.LogicalToPhysical(r); |
| 311 | background.DrawScaled(target, ticks, r.x, r.y, r.w, r.h); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * This function reads the mouse coordinates from a relevant event. |
| 316 | * Unlike SDL_GetMouseState this works even if SDL_RenderSetLogicalSize is used |
| 317 | * @param event |
| 318 | * @param mousex |
| 319 | * @param mousey |
| 320 | */ |
| 321 | void UpdateMouseCoordinates(const SDL_Event& event, int& mousex, int& mousey) { |
| 322 | switch (event.type) { |
| 323 | case SDL_MOUSEBUTTONDOWN: |
| 324 | case SDL_MOUSEBUTTONUP: |
| 325 | mousex = event.button.x; |
| 326 | mousey = event.button.y; |
| 327 | break; |
| 328 | case SDL_MOUSEMOTION: |
| 329 | mousex = event.motion.x; |
| 330 | mousey = event.motion.y; |
| 331 | break; |
| 332 | default: |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | BallManager theBallManager; |
| 338 | |
| 339 | ExplosionManager theExplosionManager; |
| 340 | |
| 341 | |
| 342 | //Here comes the Block Game object |
| 343 | #include "BlockGame.hpp" |
| 344 | #include "os.hpp" |
| 345 | #include "sago/SagoMiscSdl2.hpp" |
| 346 | #include "ScoresDisplay.hpp" |
| 347 | |
| 348 | #include "BlockGameSdl.hpp" |
| 349 | #include "sago/SagoMisc.hpp" |
| 350 | #include "ReplayPlayer.hpp" |
| 351 | #include "platform_folders.h" |
| 352 | |
| 353 | std::string pathToScreenShots() { |
| 354 | Config::getInstance()->setDefault("screenshot_dir", _("Block Attack - Rise of the Blocks")); |
| 355 | std::string screenshot_dir = Config::getInstance()->getString("screenshot_dir"); |
| 356 | if (OsPathIsRelative(screenshot_dir)) { |
| 357 | return sago::getPicturesFolder() + "/" + screenshot_dir; |
| 358 | } |
| 359 | return screenshot_dir; |
| 360 | } |
| 361 | |
| 362 | //writeScreenShot saves the screen as a bmp file, it uses the time to get a unique filename |
| 363 | void writeScreenShot() { |
| 364 | if (globalData.verboseLevel) { |
| 365 | std::cout << "Saving screenshot" << "\n"; |
| 366 | } |
| 367 | int rightNow = (int)time(nullptr); |
| 368 | int w, h; |
| 369 | SDL_GetRendererOutputSize(globalData.screen, &w, &h); |
| 370 | SDL_Surface* sreenshotSurface = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); |
| 371 | SDL_RenderReadPixels(globalData.screen, NULL, SDL_PIXELFORMAT_ARGB8888, sreenshotSurface->pixels, sreenshotSurface->pitch); |
| 372 | OsCreateFolder(pathToScreenShots()); |
| 373 | std::string buf = pathToScreenShots() + "/screenshot"+std::to_string(rightNow)+".bmp"; |
| 374 | SDL_SaveBMP(sreenshotSurface, buf.c_str()); |
| 375 | SDL_FreeSurface(sreenshotSurface); |
| 376 | if (!globalData.NoSound) { |
| 377 | if (globalData.SoundEnabled) { |
| 378 | Mix_PlayChannel(1, globalData.photoClick.get(), 0); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | //Function to return the name of a key, to be displayed... |
| 384 | std::string getKeyName(SDL_Keycode key); |
| 385 | |
| 386 | |
| 387 | void RunGameState(sago::GameStateInterface& state ) { |
| 388 | bool done = false; //We are done! |
| 389 | while (!done && !Config::getInstance()->isShuttingDown()) { |
| 390 | state.Draw(globalData.screen); |
| 391 | |
| 392 | SDL_Delay(1); |
| 393 | SDL_Event event; |
| 394 | |
| 395 | bool mustWriteScreenshot = false; |
| 396 | |
| 397 | while ( SDL_PollEvent(&event) ) { |
| 398 | UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey); |
| 399 | if ( event.type == SDL_QUIT ) { |
| 400 | Config::getInstance()->setShuttingDown(5); |
| 401 | done = true; |
| 402 | } |
| 403 | |
| 404 | if ( event.key.keysym.sym == SDLK_F9 ) { |
| 405 | mustWriteScreenshot = true; |
| 406 | } |
| 407 | |
| 408 | bool processed = false; |
| 409 | state.ProcessInput(event, processed); |
| 410 | |
| 411 | } |
| 412 | |
| 413 | state.Update(); |
| 414 | |
| 415 | globalData.mouse.Draw(globalData.screen, SDL_GetTicks(), globalData.mousex, globalData.mousey); |
| 416 | SDL_RenderPresent(globalData.screen); |
| 417 | if (mustWriteScreenshot) { |
| 418 | writeScreenShot(); |
| 419 | } |
| 420 | |
| 421 | if (!state.IsActive()) { |
| 422 | done = true; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void RunImGuiGameState(sago::GameStateInterface& state ) { |
| 428 | bool done = false; //We are done! |
| 429 | while (!done && !Config::getInstance()->isShuttingDown()) { |
| 430 | SDL_SetRenderDrawColor(globalData.screen, 0, 0, 0, 0); |
| 431 | SDL_RenderClear(globalData.screen); |
| 432 | ImGui_ImplSDLRenderer2_NewFrame(); |
| 433 | ImGui_ImplSDL2_NewFrame(); |
| 434 | ImGui::NewFrame(); |
| 435 | state.Draw(globalData.screen); |
| 436 | ImGui::Render(); |
| 437 | ImGui_ImplSDLRenderer2_RenderDrawData( ImGui::GetDrawData(), globalData.screen ); |
| 438 | |
| 439 | //While using Dear ImGui we do not draw the mouse ourself. This is gone: globalData.mouse.Draw(globalData.screen, SDL_GetTicks(), globalData.mousex, globalData.mousey); |
| 440 | SDL_RenderPresent(globalData.screen); |
| 441 | |
| 442 | SDL_Delay(1); |
| 443 | SDL_Event event; |
| 444 | bool mustWriteScreenshot = false; |
| 445 | |
| 446 | while ( SDL_PollEvent(&event) ) { |
| 447 | if ( event.type == SDL_QUIT ) { |
| 448 | Config::getInstance()->setShuttingDown(5); |
| 449 | done = true; |
| 450 | } |
| 451 | |
| 452 | if (event.type == SDL_WINDOWEVENT) { |
| 453 | if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { |
| 454 | std::cout << event.window.data1 << ", " << event.window.data2 << "\n"; |
| 455 | SDL_GetRendererOutputSize(globalData.screen, &globalData.xsize, &globalData.ysize); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | if (event.type == SDL_KEYDOWN) { |
| 460 | if ( event.key.keysym.sym == SDLK_F9 ) { |
| 461 | mustWriteScreenshot = true; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (mustWriteScreenshot) { |
| 466 | writeScreenShot(); |
| 467 | } |
| 468 | bool processed = false; |
| 469 | ImGui_ImplSDL2_ProcessEvent(&event); |
| 470 | state.ProcessInput(event, processed); |
| 471 | |
| 472 | } |
| 473 | |
| 474 | |
| 475 | state.Update(); |
| 476 | |
| 477 | //SDL_RenderPresent(globalData.screen); |
| 478 | |
| 479 | |
| 480 | if (!state.IsActive()) { |
| 481 | done = true; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | void OpenScoresDisplay() { |
| 487 | ScoresDisplay d; |
| 488 | d.scoreX = buttonXsize*2; |
| 489 | d.scoreY = 0; |
| 490 | d.buttonXsize = buttonXsize; |
| 491 | d.buttonYsize = buttonYsize; |
| 492 | RunGameState(d); |
| 493 | } |
| 494 | |
| 495 | |
| 496 | static sago::SagoTextField* getSmallInt(size_t number) { |
| 497 | static std::vector<std::shared_ptr<sago::SagoTextField> > smallFontCache; |
| 498 | if (smallFontCache.size() < number+1) { |
| 499 | smallFontCache.resize(number+1); |
| 500 | } |
| 501 | if (!smallFontCache[number]) { |
| 502 | std::shared_ptr<sago::SagoTextField> newNumber = std::make_shared<sago::SagoTextField>(); |
| 503 | newNumber->SetHolder(&globalData.spriteHolder->GetDataHolder()); |
| 504 | newNumber->SetFont("freeserif"); |
| 505 | newNumber->SetFontSize(16); |
| 506 | newNumber->SetColor({255,0,0,255}); |
| 507 | newNumber->SetText(std::to_string(number)); |
| 508 | smallFontCache[number] = newNumber; |
| 509 | } |
| 510 | return smallFontCache[number].get(); |
| 511 | } |
| 512 | |
| 513 | //Draws the balls and explosions |
| 514 | static void DrawBalls() { |
| 515 | for (size_t i = 0; i< theBallManager.ballArray.size(); i++) { |
| 516 | if (theBallManager.ballArray[i].inUse) { |
| 517 | DrawIMG(globalData.balls[theBallManager.ballArray[i].getColor()], globalData.screen, theBallManager.ballArray[i].getX(), theBallManager.ballArray[i].getY()); |
| 518 | } //if used |
| 519 | } |
| 520 | for (size_t i = 0; i< theExplosionManager.explosionArray.size(); i++) { |
| 521 | if (theExplosionManager.explosionArray[i].inUse) { |
| 522 | DrawIMG(globalData.explosion[theExplosionManager.explosionArray[i].getFrame()], globalData.screen, theExplosionManager.explosionArray[i].getX(), theExplosionManager.explosionArray[i].getY()); |
| 523 | } |
| 524 | } //for |
| 525 | for (size_t i = 0; i < globalData.theTextManager.textArray.size(); ++i) { |
| 526 | if (globalData.theTextManager.textArray[i].inUse) { |
| 527 | int x = globalData.theTextManager.textArray[i].getX()-12; |
| 528 | int y = globalData.theTextManager.textArray[i].getY()-12; |
| 529 | DrawIMG(globalData.iChainFrame, globalData.screen, x, y); |
| 530 | |
| 531 | getSmallInt(globalData.theTextManager.textArray[i].getText())->Draw(globalData.screen, x+12, y+7, |
| 532 | sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize); |
| 533 | } |
| 534 | } |
| 535 | } //DrawBalls |
| 536 | |
| 537 | void sagoTextSetBlueFont(sago::SagoTextField& field) { |
| 538 | field.SetHolder(&globalData.spriteHolder->GetDataHolder()); |
| 539 | field.SetFont("freeserif"); |
| 540 | field.SetFontSize(30); |
| 541 | field.SetColor({0,0,255,255}); |
| 542 | field.SetOutline(1, {128,128,255,255}); |
| 543 | } |
| 544 | |
| 545 | //draws everything |
| 546 | void DrawEverything(int xsize, int ysize,BlockGameSdl* theGame, BlockGameSdl* theGame2) { |
| 547 | SDL_ShowCursor(SDL_DISABLE); |
| 548 | DrawBackground(globalData.screen); |
| 549 | theGame->DoPaintJob(); |
| 550 | theGame2->DoPaintJob(); |
| 551 | std::string strHolder; |
| 552 | strHolder = std::to_string(theGame->GetScore()+theGame->GetHandicap()); |
| 553 | /*player1score.SetText(strHolder); |
| 554 | player1score.Draw(globalData.screen, theGame->GetTopX()+310,theGame->GetTopY()+100); |
| 555 | if (theGame->GetAIenabled()) { |
| 556 | player1name.SetText(_("AI")); |
| 557 | } |
| 558 | else if (singlePuzzle) { |
| 559 | player1name.SetText(_("Playing field")); |
| 560 | } |
| 561 | else { |
| 562 | player1name.SetText(globalData.player1name); |
| 563 | } |
| 564 | player1name.Draw(globalData.screen, theGame->GetTopX()+10,theGame->GetTopY()-34); |
| 565 | if (theGame->isTimeTrial()) { |
| 566 | int tid = (int)SDL_GetTicks()-theGame->GetGameStartedAt(); |
| 567 | int minutes; |
| 568 | int seconds; |
| 569 | if (tid>=0) { |
| 570 | minutes = (2*60*1000-(abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))/60/1000; |
| 571 | seconds = ((2*60*1000-(abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))%(60*1000))/1000; |
| 572 | } |
| 573 | else { |
| 574 | minutes = ((abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))/60/1000; |
| 575 | seconds = (((abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))%(60*1000))/1000; |
| 576 | } |
| 577 | if (theGame->isGameOver()) { |
| 578 | minutes=0; |
| 579 | } |
| 580 | if (theGame->isGameOver()) { |
| 581 | seconds=0; |
| 582 | } |
| 583 | if (seconds>9) { |
| 584 | strHolder = std::to_string(minutes)+":"+std::to_string(seconds); |
| 585 | } |
| 586 | else { |
| 587 | strHolder = std::to_string(minutes)+":0"+std::to_string(seconds); |
| 588 | } |
| 589 | player1time.SetText(strHolder); |
| 590 | } |
| 591 | else { |
| 592 | int minutes = ((abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))/60/1000; |
| 593 | int seconds = (((abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))%(60*1000))/1000; |
| 594 | if (theGame->isGameOver()) { |
| 595 | minutes=(theGame->GetGameEndedAt()/1000/60)%100; |
| 596 | } |
| 597 | if (theGame->isGameOver()) { |
| 598 | seconds=(theGame->GetGameEndedAt()/1000)%60; |
| 599 | } |
| 600 | if (seconds>9) { |
| 601 | strHolder = std::to_string(minutes)+":"+std::to_string(seconds); |
| 602 | } |
| 603 | else { |
| 604 | strHolder = std::to_string(minutes)+":0"+std::to_string(seconds); |
| 605 | } |
| 606 | player1time.SetText(strHolder); |
| 607 | } |
| 608 | player1time.Draw(globalData.screen, theGame->GetTopX()+310,theGame->GetTopY()+150); |
| 609 | strHolder = std::to_string(theGame->GetChains()); |
| 610 | player1chain.SetText(strHolder); |
| 611 | player1chain.Draw(globalData.screen, theGame->GetTopX()+310,theGame->GetTopY()+200); |
| 612 | //drawspeedLevel: |
| 613 | strHolder = std::to_string(theGame->GetSpeedLevel()); |
| 614 | player1speed.SetText(strHolder); |
| 615 | player1speed.Draw(globalData.screen, theGame->GetTopX()+310,theGame->GetTopY()+250); |
| 616 | if ((theGame->isStageClear()) &&(theGame->GetTopY()+700+50*(theGame->GetStageClearLimit()-theGame->GetLinesCleared())-theGame->GetPixels()-1<600+theGame->GetTopY())) { |
| 617 | oldBubleX = theGame->GetTopX()+280; |
| 618 | oldBubleY = theGame->GetTopY()+650+50*(theGame->GetStageClearLimit()-theGame->GetLinesCleared())-theGame->GetPixels()-1; |
| 619 | DrawIMG(globalData.stageBobble,globalData.screen,theGame->GetTopX()+280,theGame->GetTopY()+650+50*(theGame->GetStageClearLimit()-theGame->GetLinesCleared())-theGame->GetPixels()-1); |
| 620 | }*/ |
| 621 | //player1 finnish, player2 start |
| 622 | #if 0 |
| 623 | if (true ) { |
| 624 | /* |
| 625 | *If single player mode (and not VS) |
| 626 | */ |
| 627 | if (!twoPlayers && !theGame->isGameOver()) { |
| 628 | //Blank player2's board: |
| 629 | globalData.spriteHolder->GetSprite(globalData.theme.back_board).DrawScaled(globalData.screen, SDL_GetTicks(), theGame2->GetTopX(),theGame2->GetTopY(), BOARD_WIDTH, BOARD_HEIGHT); |
| 630 | //Write a description: |
| 631 | std::string gametypeName; |
| 632 | std::string infostring; |
| 633 | if (theGame->isTimeTrial()) { |
| 634 | gametypeName = _("Time Trial"); |
| 635 | infostring = _("Score as much as possible in 2 minutes"); |
| 636 | |
| 637 | } |
| 638 | else if (theGame->isStageClear()) { |
| 639 | gametypeName = _("Stage Clear"); |
| 640 | infostring = _("You must clear a number of lines. Speed is rapidly increased."); |
| 641 | } |
| 642 | else if (theGame->isPuzzleMode()) { |
| 643 | gametypeName = _("Puzzle"); |
| 644 | infostring = _("Clear the entire board with a limited number of moves."); |
| 645 | } |
| 646 | else { |
| 647 | gametypeName = _("Endless"); |
| 648 | infostring = _("Score as much as possible. No time limit."); |
| 649 | } |
| 650 | if (infostring.length() > 0) { |
| 651 | static sago::SagoTextBox infoBox; |
| 652 | static sago::SagoTextField objectiveField; |
| 653 | static sago::SagoTextField gametypeNameField; |
| 654 | sagoTextSetHelpFont(infoBox); |
| 655 | infoBox.SetMaxWidth(290); |
| 656 | infoBox.SetText(infostring); |
| 657 | sagoTextSetHelpFont(objectiveField); |
| 658 | objectiveField.SetText(_("Objective:")); |
| 659 | sagoTextSetHelpFont(gametypeNameField); |
| 660 | gametypeNameField.SetText(gametypeName); |
| 661 | gametypeNameField.Draw(globalData.screen, theGame2->GetTopX()+7,theGame2->GetTopY()+10); |
| 662 | objectiveField.Draw(globalData.screen, theGame2->GetTopX()+7, theGame2->GetTopY()+160); |
| 663 | infoBox.Draw(globalData.screen, theGame2->GetTopX()+7, theGame2->GetTopY()+160+32); |
| 664 | } |
| 665 | |
| 666 | //Write the keys that are in use |
| 667 | int y = theGame2->GetTopY()+400; |
| 668 | std::string controldBoxText = std::string(_("Movement keys:"))+"\n"+getKeyName(keySettings[0].left)+", "+getKeyName(keySettings[0].right)+",\n" |
| 669 | + getKeyName(keySettings[0].up)+", "+getKeyName(keySettings[0].down)+"\n" |
| 670 | + _("Switch: ") + getKeyName(keySettings[0].change); |
| 671 | if (theGame->isPuzzleMode()) { |
| 672 | controldBoxText += std::string("\n") + _("Restart: ")+getKeyName(keySettings[0].push); |
| 673 | } |
| 674 | else { |
| 675 | controldBoxText += std::string("\n") + _("Push line: ")+getKeyName(keySettings[0].push); |
| 676 | } |
| 677 | static sago::SagoTextBox controldBox; |
| 678 | controldBox.SetHolder(&globalData.spriteHolder->GetDataHolder()); |
| 679 | sagoTextSetHelpFont(controldBox); |
| 680 | controldBox.SetMaxWidth(290); |
| 681 | controldBox.SetText(controldBoxText); |
| 682 | controldBox.Draw(globalData.screen, theGame2->GetTopX()+7,y); |
| 683 | } |
| 684 | strHolder = std::to_string(theGame2->GetScore()+theGame2->GetHandicap()); |
| 685 | player2score.SetText(strHolder); |
| 686 | player2score.Draw(globalData.screen, theGame2->GetTopX()+310, theGame2->GetTopY()+100); |
| 687 | if (theGame2->GetAIenabled()) { |
| 688 | player2name.SetText(_("AI")); |
| 689 | } |
| 690 | else { |
| 691 | player2name.SetText(theGame2->name); |
| 692 | } |
| 693 | player2name.Draw(globalData.screen, theGame2->GetTopX()+10,theGame2->GetTopY()-34); |
| 694 | if (theGame2->isTimeTrial()) { |
| 695 | int tid = (int)SDL_GetTicks()-theGame2->GetGameStartedAt(); |
| 696 | int minutes; |
| 697 | int seconds; |
| 698 | if (tid>=0) { |
| 699 | minutes = (2*60*1000-(abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())))/60/1000; |
| 700 | seconds = ((2*60*1000-(abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())))%(60*1000))/1000; |
| 701 | } |
| 702 | else { |
| 703 | minutes = ((abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())))/60/1000; |
| 704 | seconds = (((abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())))%(60*1000))/1000; |
| 705 | } |
| 706 | if (theGame2->isGameOver()) { |
| 707 | minutes=0; |
| 708 | } |
| 709 | if (theGame2->isGameOver()) { |
| 710 | seconds=0; |
| 711 | } |
| 712 | if (seconds>9) { |
| 713 | strHolder = std::to_string(minutes)+":"+std::to_string(seconds); |
| 714 | } |
| 715 | else { |
| 716 | strHolder = std::to_string(minutes)+":0"+std::to_string(seconds); |
| 717 | } |
| 718 | } |
| 719 | else { |
| 720 | int minutes = (abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt()))/60/1000; |
| 721 | int seconds = (abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())%(60*1000))/1000; |
| 722 | if (theGame2->isGameOver()) { |
| 723 | minutes=(theGame2->GetGameEndedAt()/1000/60)%100; |
| 724 | } |
| 725 | if (theGame2->isGameOver()) { |
| 726 | seconds=(theGame2->GetGameEndedAt()/1000)%60; |
| 727 | } |
| 728 | if (seconds>9) { |
| 729 | strHolder = std::to_string(minutes)+":"+std::to_string(seconds); |
| 730 | } |
| 731 | else { |
| 732 | strHolder = std::to_string(minutes)+":0"+std::to_string(seconds); |
| 733 | } |
| 734 | } |
| 735 | player2time.SetText(strHolder); |
| 736 | player2time.Draw(globalData.screen, theGame2->GetTopX()+310,theGame2->GetTopY()+150); |
| 737 | strHolder = std::to_string(theGame2->GetChains()); |
| 738 | player2chain.SetText(strHolder); |
| 739 | player2chain.Draw(globalData.screen, theGame2->GetTopX()+310,theGame2->GetTopY()+200); |
| 740 | strHolder = std::to_string(theGame2->GetSpeedLevel()); |
| 741 | player2speed.SetText(strHolder); |
| 742 | player2speed.Draw(globalData.screen, theGame2->GetTopX()+310,theGame2->GetTopY()+250); |
| 743 | } |
| 744 | //player2 finnish |
| 745 | #endif |
| 746 | |
| 747 | |
| 748 | //draw exit |
| 749 | bExit.Draw(globalData.screen,SDL_GetTicks(), xsize-bExitOffset, ysize-bExitOffset, &globalData.logicalResize); |
| 750 | DrawBalls(); |
| 751 | |
| 752 | #if DEBUG |
| 753 | static sago::SagoTextField fpsField; |
| 754 | sagoTextSetBlueFont(fpsField); |
| 755 | Frames++; |
| 756 | if (SDL_GetTicks() >= Ticks + 1000) { |
| 757 | if (Frames > 999) { |
| 758 | Frames=999; |
| 759 | } |
| 760 | snprintf(FPS, sizeof(FPS), "%lu fps", Frames); |
| 761 | Frames = 0; |
| 762 | Ticks = SDL_GetTicks(); |
| 763 | } |
| 764 | fpsField.SetText(FPS); |
| 765 | fpsField.Draw(globalData.screen, globalData.xsize - 4, 4, sago::SagoTextField::Alignment::right, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize); |
| 766 | #endif |
| 767 | } |
| 768 | |
| 769 | static BlockGameSdl* player1; |
| 770 | static BlockGameSdl* player2; |
| 771 | |
| 772 | static bool registerEndlessHighscore = false; |
| 773 | static bool registerTTHighscorePlayer1 = false; |
| 774 | static bool registerTTHighscorePlayer2 = false; |
| 775 | static bool saveReplay = false; |
| 776 | |
| 777 | /** |
| 778 | * startSpeed is a value from 0 to 4 |
| 779 | * */ |
| 780 | static void StartSinglePlayerEndless(int startSpeed) { |
| 781 | //1 player - endless |
| 782 | BlockGameStartInfo startInfo; |
| 783 | startInfo.ticks = SDL_GetTicks(); |
| 784 | startInfo.startBlocks = startInfo.ticks; |
| 785 | startInfo.gameSpeed = startSpeed; |
| 786 | startInfo.basicBlockVariants = Config::getInstance()->getInt("basic_block_variants", 6); |
| 787 | player1->NewGame(startInfo); |
| 788 | twoPlayers =false; |
| 789 | BlockGameAction a; |
| 790 | a.action = BlockGameAction::Action::SET_GAME_OVER; |
| 791 | a.tick = startInfo.ticks; |
| 792 | player2->DoAction(a); |
| 793 | player1->name = globalData.player1name; |
| 794 | player2->name = globalData.player2name; |
| 795 | registerEndlessHighscore = true; |
| 796 | } |
| 797 | |
| 798 | static void StartSinglePlayerTimeTrial() { |
| 799 | BlockGameStartInfo startInfo; |
| 800 | startInfo.ticks = SDL_GetTicks(); |
| 801 | startInfo.timeTrial = true; |
| 802 | startInfo.basicBlockVariants = Config::getInstance()->getInt("basic_block_variants", 6); |
| 803 | player1->NewGame(startInfo); |
| 804 | twoPlayers =false; |
| 805 | BlockGameAction a; |
| 806 | a.action = BlockGameAction::Action::SET_GAME_OVER; |
| 807 | a.tick = startInfo.ticks; |
| 808 | player2->DoAction(a); |
| 809 | //vsMode = false; |
| 810 | player1->name = globalData.player1name; |
| 811 | player2->name = globalData.player2name; |
| 812 | registerTTHighscorePlayer1 = true; |
| 813 | saveReplay = true; |
| 814 | } |
| 815 | |
| 816 | static int StartSinglePlayerPuzzle() { |
| 817 | BlockGameStartInfo startInfo; |
| 818 | startInfo.ticks = SDL_GetTicks(); |
| 819 | startInfo.puzzleMode = true; |
| 820 | startInfo.level = PuzzleLevelSelect(0); |
| 821 | if (startInfo.level == -1) { |
| 822 | return 1; |
| 823 | } |
| 824 | player1->NewGame(startInfo); |
| 825 | DrawBackground(globalData.screen); |
| 826 | twoPlayers = false; |
| 827 | BlockGameAction a; |
| 828 | a.action = BlockGameAction::Action::SET_GAME_OVER; |
| 829 | a.tick = startInfo.ticks; |
| 830 | player2->DoAction(a); |
| 831 | player1->name = globalData.player1name; |
| 832 | player2->name = globalData.player2name; |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | |
| 837 | static void StarTwoPlayerTimeTrial() { |
| 838 | BlockGameStartInfo startInfo; |
| 839 | startInfo.ticks = SDL_GetTicks(); |
| 840 | startInfo.timeTrial = true; |
| 841 | startInfo.basicBlockVariants = Config::getInstance()->getInt("basic_block_variants", 6); |
| 842 | BlockGameStartInfo startInfo2 = startInfo; |
| 843 | registerTTHighscorePlayer1 = true; |
| 844 | registerTTHighscorePlayer2 = true; |
| 845 | if (player1AI) { |
| 846 | startInfo.AI = true; |
| 847 | startInfo.level = player1AIlevel; |
| 848 | registerTTHighscorePlayer1 = false; |
| 849 | } |
| 850 | if (player2AI) { |
| 851 | startInfo2.AI = true; |
| 852 | startInfo2.level = player2AIlevel; |
| 853 | registerTTHighscorePlayer2 = false; |
| 854 | } |
| 855 | startInfo.gameSpeed = player1Speed; |
| 856 | startInfo2.gameSpeed = player2Speed; |
| 857 | startInfo.handicap = player1handicap; |
| 858 | startInfo2.handicap = player2handicap; |
| 859 | player1->NewGame(startInfo); |
| 860 | player2->NewGame(startInfo2); |
| 861 | twoPlayers = true; |
| 862 | player1->name = globalData.player1name; |
| 863 | player2->name = globalData.player2name; |
| 864 | } |
| 865 | |
| 866 | static void StartTwoPlayerVs() { |
| 867 | //2 player - VsMode |
| 868 | BlockGameStartInfo startInfo; |
| 869 | startInfo.ticks = SDL_GetTicks(); |
| 870 | startInfo.vsMode = true; |
| 871 | startInfo.startBlocks = startInfo.ticks; |
| 872 | startInfo.basicBlockVariants = Config::getInstance()->getInt("basic_block_variants", 6); |
| 873 | BlockGameStartInfo startInfo2 = startInfo; |
| 874 | if (player1AI) { |
| 875 | startInfo.AI = true; |
| 876 | startInfo.level = player1AIlevel; |
| 877 | } |
| 878 | if (player2AI) { |
| 879 | startInfo2.AI = true; |
| 880 | startInfo2.level = player2AIlevel; |
| 881 | } |
| 882 | startInfo.gameSpeed = player1Speed; |
| 883 | startInfo2.gameSpeed = player2Speed; |
| 884 | startInfo.handicap = player1handicap; |
| 885 | startInfo2.handicap = player2handicap; |
| 886 | player1->NewGame(startInfo); |
| 887 | player2->NewGame(startInfo2); |
| 888 | twoPlayers = true; |
| 889 | player1->name = globalData.player1name; |
| 890 | player2->name = globalData.player2name; |
| 891 | } |
| 892 | |
| 893 | static void MoveBlockGameSdls( BlockGameSdl& game1, BlockGameSdl& game2 ) { |
| 894 | game1.SetTopXY(globalData.xsize/2-440, globalData.ysize/2-284); |
| 895 | game2.SetTopXY(globalData.xsize/2+40, globalData.ysize/2-284); |
| 896 | } |
| 897 | |
| 898 | struct globalConfig { |
| 899 | std::string savepath; |
| 900 | std::vector<std::string> search_paths; |
| 901 | std::string puzzleName; |
| 902 | bool allowResize = true; |
| 903 | bool autoScale = true; |
| 904 | bool softwareRenderer = false; |
| 905 | }; |
| 906 | |
| 907 | static void ParseArguments(int argc, char* argv[], globalConfig& conf) { |
| 908 | int consoleWidth = boost::program_options::options_description::m_default_line_length; |
| 909 | const char* columnsEnv = getenv("COLUMNS"); // Allows using "COLUMNS=300 help2man" for generating the man page without bad line breaks. |
| 910 | if (columnsEnv) { |
| 911 | consoleWidth = sago::StrToLong(columnsEnv); |
| 912 | } |
| 913 | const char* commandname = "blockattack"; |
| 914 | if (argv[0]) { |
| 915 | //NULL on Windows |
| 916 | commandname = argv[0]; |
| 917 | } |
| 918 | boost::program_options::options_description desc("Options", consoleWidth); |
| 919 | desc.add_options() |
| 920 | ("help,h", "Displays this message") |
| 921 | ("version", "Display the version information") |
| 922 | ("config,c", boost::program_options::value<std::vector<std::string> >(), "Read a config file with the values. Can be given multiple times") |
| 923 | ("nosound", "Disables the sound. Can be used if sound errors prevents you from starting") |
| 924 | ("priority", "Causes the game to not sleep between frames.") |
| 925 | ("software-renderer", "Asks SDL2 to use software renderer") |
| 926 | ("verbose-basic", "Enables basic verbose messages") |
| 927 | ("verbose-game-controller", "Enables verbose messages regarding controllers") |
| 928 | ("print-search-path", "Prints the search path and quits") |
| 929 | ("no-auto-scale", "Do not automatically auto scale") |
| 930 | ("always-sixteen-nine", "Use 16:9 format even in Window mode") |
| 931 | ("editor", "Start the sprite editor/browser") |
| 932 | ("puzzle-editor", "Start the build in puzzle editor") |
| 933 | ("puzzle-level-file", boost::program_options::value<std::string>(), "Sets the default puzzle file to load") |
| 934 | ("puzzle-single-level", boost::program_options::value<int>(), "Start the specific puzzle level directly") |
| 935 | #ifdef REPLAY_IMPLEMENTED |
| 936 | ("play-replay", boost::program_options::value<std::string>(), "Start a replay") |
| 937 | #endif |
| 938 | ("bind-text-domain", boost::program_options::value<std::string>(), fmt::format("Overwrites the bind text domain used for finding translations. " |
| 939 | "Default: \"{}\"", LOCALEDIR).c_str()) |
| 940 | ("homepath", boost::program_options::value<std::string>(), fmt::format("Set the home folder where settings are saved. The directory will be created if it does not exist." |
| 941 | " Default: \"{}\"", getPathToSaveFiles()).c_str()) |
| 942 | ("mod,m", boost::program_options::value<std::vector<std::string> >(), "Loads a mod. Later mods have preference") |
| 943 | |
| 944 | ; |
| 945 | boost::program_options::variables_map vm; |
| 946 | try { |
| 947 | boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); |
| 948 | boost::program_options::notify(vm); |
| 949 | } |
| 950 | catch (std::exception& e) { |
| 951 | std::cerr << e.what() << "\n"; |
| 952 | std::cerr << desc << "\n"; |
| 953 | throw; |
| 954 | } |
| 955 | if (vm.count("config")) { |
| 956 | std::vector<std::string> config_filenames = vm["config"].as<std::vector<std::string> >(); |
| 957 | for ( const std::string& s : config_filenames) { |
| 958 | std::ifstream config_file(s); |
| 959 | store(parse_config_file(config_file, desc), vm); |
| 960 | notify(vm); |
| 961 | } |
| 962 | } |
| 963 | if (vm.count("bind-text-domain")) { |
| 964 | std::string s = vm["bind-text-domain"].as<std::string>(); |
| 965 | bindtextdomain (PACKAGE, s.c_str()); |
| 966 | } |
| 967 | if (vm.count("homepath")) { |
| 968 | std::string s = vm["homepath"].as<std::string>(); |
| 969 | setPathToSaveFiles(s); |
| 970 | conf.savepath = getPathToSaveFiles(); |
| 971 | } |
| 972 | if (vm.count("help")) { |
| 973 | std::cout << fmt::format("Block Attack - Rise of the blocks {}\n\n" |
| 974 | "Block Attack - Rise of the Blocks is a puzzle/blockfall game inspired by Tetris Attack for the SNES.\n\n" |
| 975 | "{}\n\n", VERSION_NUMBER, "www.blockattack.net"); |
| 976 | std::cout << "Usage: "<< commandname << " [OPTION]..." << "\n"; |
| 977 | std::cout << desc << "\n"; |
| 978 | std::cout << "Examples:" << "\n"; |
| 979 | std::cout << "\tblockattack \tStart the game normally" << "\n"; |
| 980 | std::cout << "\tblockattack --nosound\tStart the game without sound. Can be used if sound problems prevents the game from starting" << "\n"; |
| 981 | std::cout << "\tblockattack --puzzle-level-file puzzle.levels --puzzle-single-level 3\tStart the game with the default puzzles in level 3" << "\n"; |
| 982 | std::cout << "\tblockattack --bind-text-domain /dev/null\t Disables translations" << "\n"; |
| 983 | std::cout << "\n"; |
| 984 | std::cout << "Report bugs to the issue tracker here: <https://github.com/blockattack/blockattack-game/issues>" << "\n"; |
| 985 | exit(0); |
| 986 | } |
| 987 | if (vm.count("version")) { |
| 988 | std::cout << "blockattack " << VERSION_NUMBER << "\n"; |
| 989 | std::cout << "\n"; |
| 990 | std::cout << "Copyright (C) 2005-2022 Poul Sander" << "\n"; |
| 991 | std::cout << "License GPLv2+: GNU GPL version 2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html> or later <http://gnu.org/licenses/gpl.html>" << "\n"; |
| 992 | std::cout << "This is free software: you are free to change and redistribute it." << "\n"; |
| 993 | std::cout << "There is NO WARRANTY, to the extent permitted by law." << "\n"; |
| 994 | exit(0); |
| 995 | } |
| 996 | if (vm.count("nosound")) { |
| 997 | globalData.NoSound = true; |
| 998 | } |
| 999 | if (vm.count("priority")) { |
| 1000 | globalData.highPriority = true; |
| 1001 | } |
| 1002 | if (vm.count("software-renderer")) { |
| 1003 | conf.softwareRenderer = true; |
| 1004 | } |
| 1005 | if (vm.count("verbose-basic")) { |
| 1006 | globalData.verboseLevel++; |
| 1007 | } |
| 1008 | if (vm.count("verbose-game-controller")) { |
| 1009 | GameControllerSetVerbose(true); |
| 1010 | } |
| 1011 | if (vm.count("print-search-path")) { |
| 1012 | for (const std::string& s : conf.search_paths) { |
| 1013 | std::cout << s << "\n"; |
| 1014 | } |
| 1015 | std::cout << conf.savepath << "\n"; |
| 1016 | exit(0); |
| 1017 | } |
| 1018 | if (vm.count("puzzle-single-level")) { |
| 1019 | singlePuzzle = true; |
| 1020 | singlePuzzleNr = vm["puzzle-single-level"].as<int>(); |
| 1021 | } |
| 1022 | if (vm.count("no-auto-scale")) { |
| 1023 | conf.autoScale = false; |
| 1024 | } |
| 1025 | if (vm.count("always-sixteen-nine")) { |
| 1026 | globalData.alwaysSixteenNine = true; |
| 1027 | } |
| 1028 | if (vm.count("editor")) { |
| 1029 | editor = true; |
| 1030 | } |
| 1031 | if (vm.count("puzzle-editor")) { |
| 1032 | puzzleEditor = true; |
| 1033 | } |
| 1034 | if (vm.count("puzzle-level-file")) { |
| 1035 | conf.puzzleName = vm["puzzle-level-file"].as<std::string>(); |
| 1036 | } |
| 1037 | if (vm.count("play-replay")) { |
| 1038 | globalData.replayArgument = vm["play-replay"].as<std::string>(); |
| 1039 | } |
| 1040 | if (vm.count("mod")) { |
| 1041 | globalData.modList = vm["mod"].as<std::vector<std::string> >(); |
| 1042 | } |
| 1043 | |
| 1044 | } |
| 1045 | |
| 1046 | //Physfs 2.0.z does not have PHYSFS_unmount |
| 1047 | #if (PHYSFS_VER_MAJOR <= 2) && (PHYSFS_VER_MINOR < 1) |
| 1048 | #define PHYSFS_unmount PHYSFS_removeFromSearchPath |
| 1049 | #endif |
| 1050 | |
| 1051 | static void writeStateFile(const char* executable, const char* basedir) { |
| 1052 | std::string path = getPathToStateFiles(); |
| 1053 | OsCreateFolder(path); |
| 1054 | std::ofstream stateFile; |
| 1055 | stateFile.open (path+"/game.txt"); |
| 1056 | if (executable) { |
| 1057 | stateFile << "executable " << executable << "\n"; |
| 1058 | } |
| 1059 | stateFile << "basedir " << basedir << "\n"; |
| 1060 | stateFile << "SHAREDIR " << SHAREDIR << "\n"; |
| 1061 | } |
| 1062 | |
| 1063 | //Warning: the arguments to main must be "int argc, char* argv[]" NO CONST! or SDL_main will fail to find it |
| 1064 | int main(int argc, char* argv[]) { |
| 1065 | try { |
| 1066 | //Init the file system abstraction layer |
| 1067 | PHYSFS_init(argv[0]); |
| 1068 | globalConfig config; |
| 1069 | config.puzzleName = "puzzle.levels"; |
| 1070 | FsSearchParthMainAppend(config.search_paths); |
| 1071 | config.savepath = getPathToSaveFiles(); |
| 1072 | globalData.highPriority = false; //if true the game will take most resources, but increase framerate. |
| 1073 | globalData.bFullscreen = false; |
| 1074 | //Set default Config variables: |
| 1075 | setlocale (LC_ALL, ""); |
| 1076 | bindtextdomain (PACKAGE, LOCALEDIR); |
| 1077 | bind_textdomain_codeset(PACKAGE, "utf-8"); |
| 1078 | textdomain (PACKAGE); |
| 1079 | ParseArguments(argc, argv, config); |
| 1080 | OsCreateSaveFolder(); |
| 1081 | writeStateFile(argv[0], PHYSFS_getBaseDir()); |
| 1082 | PhysFsSetSearchPath(config.search_paths, config.savepath); |
| 1083 | /*if (globalData.modList.empty() && sago::FileExists(MODLIST_TXT)) { |
| 1084 | std::string modString = sago::GetFileContent(MODLIST_TXT); |
| 1085 | boost::split(globalData.modList, modString, boost::is_any_of(",")); |
| 1086 | }*/ |
| 1087 | globalData.modinfo.InitModList(globalData.modList); |
| 1088 | if (sago::FileExists(MODLIST_TXT)) { |
| 1089 | globalData.modinfo.ParseModFile(sago::GetFileContent(MODLIST_TXT)); |
| 1090 | globalData.modList = globalData.modinfo.getModList(); |
| 1091 | } |
| 1092 | std::cout << "Mod list: "; |
| 1093 | for (const std::string& s : globalData.modList) { |
| 1094 | std::cout << s << ","; |
| 1095 | } |
| 1096 | std::cout << "\n"; |
| 1097 | if (globalData.modList.size()>0) { |
| 1098 | PHYSFS_unmount(config.savepath.c_str()); |
| 1099 | FsSearchPathModAppend(config.search_paths, globalData.modList); |
| 1100 | PhysFsSetSearchPath(config.search_paths, config.savepath); |
| 1101 | } |
| 1102 | //Os create folders must be after the parameters because they can change the home folder |
| 1103 | PhysFsCreateFolders(); |
| 1104 | bool gameShutdownProperly = true; |
| 1105 | if (sago::FileExists("gameRunning")) { |
| 1106 | gameShutdownProperly = false; |
| 1107 | } |
| 1108 | sago::WriteFileContent("gameRunning", "Started"); |
| 1109 | globalData.SoundEnabled = true; |
| 1110 | globalData.MusicEnabled = true; |
| 1111 | twoPlayers = false; //true if two players splitscreen |
| 1112 | drawBalls = true; |
| 1113 | puzzleLoaded = false; |
| 1114 | theBallManager = BallManager(); |
| 1115 | theExplosionManager = ExplosionManager(); |
| 1116 | PuzzleSetName(config.puzzleName); |
| 1117 | //Init SDL |
| 1118 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
| 1119 | sago::SagoFatalErrorF("Unable to init SDL: %s", SDL_GetError()); |
| 1120 | } |
| 1121 | if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER ) != 0) { |
| 1122 | std::cerr << "Warning: Game controller failed to initialize. Reason: " << SDL_GetError() << "\n"; |
| 1123 | } |
| 1124 | InitGameControllers(); |
| 1125 | TTF_Init(); |
| 1126 | atexit(SDL_Quit); //quits SDL when the game stops for some reason (like you hit exit or Esc) |
| 1127 | globalData.theTextManager = TextManager(); |
| 1128 | |
| 1129 | //Open Audio |
| 1130 | if (!globalData.NoSound) { |
| 1131 | //If sound has not been disabled, then load the sound system |
| 1132 | if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0) { |
| 1133 | std::cerr << "Warning: Couldn't set 44100 Hz 16-bit audio - Reason: " << SDL_GetError() << "\n" |
| 1134 | << "Sound will be disabled!" << "\n"; |
| 1135 | globalData.NoSound = true; //Tries to stop all sound from playing/loading |
| 1136 | } |
| 1137 | } |
| 1138 | Config::getInstance()->setDefault("volume_music", "20"); //0-128 |
| 1139 | |
| 1140 | |
| 1141 | if (globalData.verboseLevel) { |
| 1142 | //Copyright notice: |
| 1143 | std::cout << "Block Attack - Rise of the Blocks (" << VERSION_NUMBER << ")" << "\n" << "http://www.blockattack.net" << "\n" << "Copyright 2004-2022 Poul Sander" << "\n" << |
| 1144 | "A SDL2 based game (see www.libsdl.org)" << "\n" << |
| 1145 | "The game is available under the GPL, see COPYING for details." << "\n"; |
| 1146 | std::cout << "-------------------------------------------" << "\n"; |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | keySettings[player1keys].up= SDLK_UP; |
| 1151 | keySettings[player1keys].down = SDLK_DOWN; |
| 1152 | keySettings[player1keys].left = SDLK_LEFT; |
| 1153 | keySettings[player1keys].right = SDLK_RIGHT; |
| 1154 | keySettings[player1keys].change = SDLK_RCTRL; |
| 1155 | keySettings[player1keys].push = SDLK_RSHIFT; |
| 1156 | |
| 1157 | keySettings[player2keys].up= SDLK_w; |
| 1158 | keySettings[player2keys].down = SDLK_s; |
| 1159 | keySettings[player2keys].left = SDLK_a; |
| 1160 | keySettings[player2keys].right = SDLK_d; |
| 1161 | keySettings[player2keys].change = SDLK_LCTRL; |
| 1162 | keySettings[player2keys].push = SDLK_LSHIFT; |
| 1163 | |
| 1164 | globalData.player1name = defaultPlayerName(); |
| 1165 | globalData.player2name = _("Player 2"); |
| 1166 | |
| 1167 | Config* configSettings = Config::getInstance(); |
| 1168 | //configSettings->setString("aNumber"," A string"); |
| 1169 | //configSettings->save(); |
| 1170 | int screenHeight = 768; |
| 1171 | if (configSettings->exists("fullscreen")) { //Test if an configFile exists |
| 1172 | globalData.bFullscreen = (bool)configSettings->getInt("fullscreen"); |
| 1173 | globalData.MusicEnabled = (bool)configSettings->getInt("musicenabled"); |
| 1174 | globalData.SoundEnabled = (bool)configSettings->getInt("soundenabled"); |
| 1175 | |
| 1176 | if (configSettings->exists("sdl2_player1keyup")) { |
| 1177 | keySettings[0].up = (SDL_Keycode)configSettings->getInt("sdl2_player1keyup"); |
| 1178 | } |
| 1179 | if (configSettings->exists("sdl2_player1keydown")) { |
| 1180 | keySettings[0].down = (SDL_Keycode)configSettings->getInt("sdl2_player1keydown"); |
| 1181 | } |
| 1182 | if (configSettings->exists("sdl2_player1keyleft")) { |
| 1183 | keySettings[0].left = (SDL_Keycode)configSettings->getInt("sdl2_player1keyleft"); |
| 1184 | } |
| 1185 | if (configSettings->exists("sdl2_player1keyright")) { |
| 1186 | keySettings[0].right = (SDL_Keycode)configSettings->getInt("sdl2_player1keyright"); |
| 1187 | } |
| 1188 | if (configSettings->exists("sdl2_player1keychange")) { |
| 1189 | keySettings[0].change = (SDL_Keycode)configSettings->getInt("sdl2_player1keychange"); |
| 1190 | } |
| 1191 | if (configSettings->exists("sdl2_player1keypush")) { |
| 1192 | keySettings[0].push = (SDL_Keycode)configSettings->getInt("sdl2_player1keypush"); |
| 1193 | } |
| 1194 | |
| 1195 | if (configSettings->exists("sdl2_player2keyup")) { |
| 1196 | keySettings[2].up = (SDL_Keycode)configSettings->getInt("sdl2_player2keyup"); |
| 1197 | } |
| 1198 | if (configSettings->exists("sdl2_player2keydown")) { |
| 1199 | keySettings[2].down = (SDL_Keycode)configSettings->getInt("sdl2_player2keydown"); |
| 1200 | } |
| 1201 | if (configSettings->exists("sdl2_player2keyleft")) { |
| 1202 | keySettings[2].left = (SDL_Keycode)configSettings->getInt("sdl2_player2keyleft"); |
| 1203 | } |
| 1204 | if (configSettings->exists("sdl2_player2keyright")) { |
| 1205 | keySettings[2].right = (SDL_Keycode)configSettings->getInt("sdl2_player2keyright"); |
| 1206 | } |
| 1207 | if (configSettings->exists("sdl2_player2keychange")) { |
| 1208 | keySettings[2].change = (SDL_Keycode)configSettings->getInt("sdl2_player2keychange"); |
| 1209 | } |
| 1210 | if (configSettings->exists("sdl2_player2keypush")) { |
| 1211 | keySettings[2].push = (SDL_Keycode)configSettings->getInt("sdl2_player2keypush"); |
| 1212 | } |
| 1213 | if (configSettings->exists("player1name")) { |
| 1214 | globalData.player1name = configSettings->getString("player1name"); |
| 1215 | } |
| 1216 | if (configSettings->exists("player2name")) { |
| 1217 | globalData.player2name = configSettings->getString("player2name"); |
| 1218 | } |
| 1219 | if (configSettings->exists("xsize")) { |
| 1220 | globalData.xsize = configSettings->getInt("xsize"); |
| 1221 | } |
| 1222 | if (configSettings->exists("ysize")) { |
| 1223 | globalData.ysize = configSettings->getInt("ysize"); |
| 1224 | } |
| 1225 | if (configSettings->exists("screenHeight")) { |
| 1226 | screenHeight = configSettings->getInt("screenHeight"); |
| 1227 | } |
| 1228 | if (globalData.verboseLevel) { |
| 1229 | std::cout << "Data loaded from config file" << "\n"; |
| 1230 | } |
| 1231 | } |
| 1232 | else { |
| 1233 | if (globalData.verboseLevel) { |
| 1234 | std::cout << "Unable to load options file, using default values" << "\n"; |
| 1235 | } |
| 1236 | } |
| 1237 | if (configSettings->getInt("always-software")) { |
| 1238 | config.softwareRenderer = true; |
| 1239 | } |
| 1240 | if (!gameShutdownProperly) { |
| 1241 | std::cerr << "Game not shotdown. Using software renderer.\n"; |
| 1242 | config.softwareRenderer = true; |
| 1243 | } |
| 1244 | |
| 1245 | // "Block Attack - Rise of the Blocks" |
| 1246 | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"); |
| 1247 | //Open video |
| 1248 | int createWindowParams = 0; |
| 1249 | if (config.allowResize) { |
| 1250 | createWindowParams |= SDL_WINDOW_RESIZABLE; |
| 1251 | } |
| 1252 | globalData.xsize = FOUR_THREE_WIDTH; |
| 1253 | if (globalData.alwaysSixteenNine) { |
| 1254 | globalData.xsize = SIXTEEN_NINE_WIDTH; |
| 1255 | } |
| 1256 | globalData.ysize = SCREEN_HIGHT; |
| 1257 | globalData.logicalResize = sago::SagoLogicalResize(globalData.xsize, globalData.ysize); |
| 1258 | sdlWindow = SDL_CreateWindow("Block Attack - Rise of the Blocks " VERSION_NUMBER, |
| 1259 | SDL_WINDOWPOS_UNDEFINED, |
| 1260 | SDL_WINDOWPOS_UNDEFINED, |
| 1261 | (screenHeight)*globalData.xsize/globalData.ysize, screenHeight, |
| 1262 | createWindowParams ); |
| 1263 | dieOnNullptr(sdlWindow, "Unable to create window"); |
| 1264 | int rendererFlags = 0; |
| 1265 | if (config.softwareRenderer) { |
| 1266 | rendererFlags |= SDL_RENDERER_SOFTWARE; |
| 1267 | } |
| 1268 | SDL_Renderer* renderer = SDL_CreateRenderer(sdlWindow, -1, rendererFlags); |
| 1269 | dieOnNullptr(renderer, "Unable to create render"); |
| 1270 | |
| 1271 | if (globalData.verboseLevel) { |
| 1272 | SDL_RendererInfo info; |
| 1273 | SDL_GetRendererInfo(renderer, &info); |
| 1274 | std::cout << "Renderer: " << info.name << "\n"; |
| 1275 | } |
| 1276 | globalData.screen = renderer; |
| 1277 | globalData.theme= ThemesGet(ThemesGetNumber(Config::getInstance()->getString("theme"))); |
| 1278 | |
| 1279 | ResetFullscreen(); |
| 1280 | SetSDLIcon(sdlWindow); |
| 1281 | |
| 1282 | if (globalData.verboseLevel) { |
| 1283 | std::cout << "Images loaded" << "\n"; |
| 1284 | } |
| 1285 | |
| 1286 | BlockGameSdl theGame = BlockGameSdl(globalData.xsize/2-426, 100, &globalData.spriteHolder->GetDataHolder()); //creates game objects |
| 1287 | BlockGameSdl theGame2 = BlockGameSdl(globalData.xsize/2+40, 100, &globalData.spriteHolder->GetDataHolder()); |
| 1288 | player1 = &theGame; |
| 1289 | player2 = &theGame2; |
| 1290 | |
| 1291 | BlockGameAction a; |
| 1292 | a.action = BlockGameAction::Action::SET_GAME_OVER; |
| 1293 | theGame.DoAction(a); |
| 1294 | theGame2.DoAction(a); |
| 1295 | |
| 1296 | |
| 1297 | //Takes names from file instead |
| 1298 | theGame.name = globalData.player1name; |
| 1299 | theGame2.name = globalData.player2name; |
| 1300 | |
| 1301 | if (singlePuzzle) { |
| 1302 | LoadPuzzleStages(); |
| 1303 | BlockGameStartInfo s; |
| 1304 | s.puzzleMode = true; |
| 1305 | s.level = singlePuzzleNr; |
| 1306 | s.singlePuzzle = true; |
| 1307 | theGame.NewGame(s); |
| 1308 | } |
| 1309 | DrawBackground(globalData.screen); |
| 1310 | MoveBlockGameSdls(theGame, theGame2); |
| 1311 | DrawEverything(globalData.xsize, globalData.ysize, &theGame, &theGame2); |
| 1312 | SDL_RenderPresent(globalData.screen); |
| 1313 | if (singlePuzzle) { |
| 1314 | runGame(Gametype::Puzzle, singlePuzzleNr); |
| 1315 | } |
| 1316 | else if (editor) { |
| 1317 | InitImGui(sdlWindow, renderer, globalData.xsize, globalData.ysize); |
| 1318 | ImGuiIO& io = ImGui::GetIO(); |
| 1319 | io.IniFilename = nullptr; |
| 1320 | std::string imgui_inifile = getPathToSaveFiles() + "/imgui.ini"; |
| 1321 | ImGui::LoadIniSettingsFromDisk(imgui_inifile.c_str()); |
| 1322 | SagoTextureSelector sts; |
| 1323 | sts.Init(); |
| 1324 | RunImGuiGameState(sts); |
| 1325 | ImGui::SaveIniSettingsToDisk(imgui_inifile.c_str()); |
| 1326 | } |
| 1327 | else if (puzzleEditor) { |
| 1328 | InitImGui(sdlWindow, renderer, globalData.xsize, globalData.ysize); |
| 1329 | ImGuiIO& io = ImGui::GetIO(); |
| 1330 | io.IniFilename = nullptr; |
| 1331 | std::string imgui_inifile = getPathToSaveFiles() + "/imgui.ini"; |
| 1332 | ImGui::LoadIniSettingsFromDisk(imgui_inifile.c_str()); |
| 1333 | PuzzleEditorState s; |
| 1334 | s.Init(); |
| 1335 | RunImGuiGameState(s); |
| 1336 | ImGui::SaveIniSettingsToDisk(imgui_inifile.c_str()); |
| 1337 | } |
| 1338 | else if (globalData.replayArgument.length()) { |
| 1339 | ReplayPlayer rp; |
| 1340 | RunGameState(rp); |
| 1341 | } |
| 1342 | else { |
| 1343 | if (!gameShutdownProperly) { |
| 1344 | SafeModeMenu(); |
| 1345 | } |
| 1346 | //game loop |
| 1347 | MainMenu(); |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | //Saves options |
| 1353 | if (true) { |
| 1354 | configSettings->setInt("fullscreen",(int)globalData.bFullscreen); |
| 1355 | configSettings->setInt("musicenabled",(int)globalData.MusicEnabled); |
| 1356 | configSettings->setInt("soundenabled",(int)globalData.SoundEnabled); |
| 1357 | |
| 1358 | configSettings->setInt("sdl2_player1keyup",(int)keySettings[0].up); |
| 1359 | configSettings->setInt("sdl2_player1keydown",(int)keySettings[0].down); |
| 1360 | configSettings->setInt("sdl2_player1keyleft",(int)keySettings[0].left); |
| 1361 | configSettings->setInt("sdl2_player1keyright",(int)keySettings[0].right); |
| 1362 | configSettings->setInt("sdl2_player1keychange",(int)keySettings[0].change); |
| 1363 | configSettings->setInt("sdl2_player1keypush",(int)keySettings[0].push); |
| 1364 | |
| 1365 | configSettings->setInt("sdl2_player2keyup",(int)keySettings[2].up); |
| 1366 | configSettings->setInt("sdl2_player2keydown",(int)keySettings[2].down); |
| 1367 | configSettings->setInt("sdl2_player2keyleft",(int)keySettings[2].left); |
| 1368 | configSettings->setInt("sdl2_player2keyright",(int)keySettings[2].right); |
| 1369 | configSettings->setInt("sdl2_player2keychange",(int)keySettings[2].change); |
| 1370 | configSettings->setInt("sdl2_player2keypush",(int)keySettings[2].push); |
| 1371 | |
| 1372 | configSettings->setString("player1name", globalData.player1name); |
| 1373 | configSettings->setString("player2name", globalData.player2name); |
| 1374 | if (!globalData.bFullscreen) { |
| 1375 | //Store physical height of window |
| 1376 | int height = 0; |
| 1377 | SDL_GetWindowSize(sdlWindow, nullptr, &height); |
| 1378 | configSettings->setInt("screenHeight", height); |
| 1379 | } |
| 1380 | configSettings->save(); |
| 1381 | } |
| 1382 | |
| 1383 | //calculate uptime: |
| 1384 | //int hours, mins, secs, |
| 1385 | commonTime ct = TimeHandler::ms2ct(SDL_GetTicks()); |
| 1386 | |
| 1387 | if (globalData.verboseLevel) { |
| 1388 | std::cout << fmt::format("Block Attack - Rise of the Blocks ran for: {} hours {:02} minutes and {:02} seconds", ct.hours, ct.minutes, ct.seconds) << "\n"; |
| 1389 | } |
| 1390 | |
| 1391 | /*std::string modListString; |
| 1392 | if (globalData.modList.size()>0) { |
| 1393 | modListString = globalData.modList.at(0); |
| 1394 | for (size_t i = 1; i < globalData.modList.size(); ++i) { |
| 1395 | modListString += std::string(",")+globalData.modList[i]; |
| 1396 | } |
| 1397 | }*/ |
| 1398 | ct = TimeHandler::addTime("totalTime",ct); |
| 1399 | if (globalData.verboseLevel) { |
| 1400 | std::cout << fmt::format("Total run time is now: {} days, {} hours, {:02} minutes and {:02} seconds\n", ct.days, ct.hours, ct.minutes, ct.seconds); |
| 1401 | //std::cout << "Mods loaded: " << modListString << "\n"; |
| 1402 | } |
| 1403 | //sago::WriteFileContent(MODLIST_TXT, modListString); |
| 1404 | Stats::getInstance()->save(); |
| 1405 | Config::getInstance()->save(); |
| 1406 | } |
| 1407 | catch (std::exception& e) { |
| 1408 | sago::SagoFatalError(e.what()); |
| 1409 | } |
| 1410 | PHYSFS_delete("gameRunning"); |
| 1411 | //Close file system Apstraction layer! |
| 1412 | PHYSFS_deinit(); |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | void SetSinglePuzzleMode(int level) { |
| 1417 | singlePuzzle = true; |
| 1418 | singlePuzzleNr = level; |
| 1419 | } |
| 1420 | |
| 1421 | int runGame(Gametype gametype, int level) { |
| 1422 | drawBalls = true; |
| 1423 | puzzleLoaded = false; |
| 1424 | bool bNearDeath = false; //Play music faster or louder while tru |
| 1425 | |
| 1426 | theBallManager = BallManager(); |
| 1427 | theExplosionManager = ExplosionManager(); |
| 1428 | BlockGameSdl theGame = BlockGameSdl(globalData.xsize/2-426, 100, &globalData.spriteHolder->GetDataHolder()); //creates game objects |
| 1429 | BlockGameSdl theGame2 = BlockGameSdl(globalData.xsize/2+4, 100, &globalData.spriteHolder->GetDataHolder()); |
| 1430 | player1 = &theGame; |
| 1431 | player2 = &theGame2; |
| 1432 | theGame.DoPaintJob(); //Makes sure what there is something to paint |
| 1433 | theGame2.DoPaintJob(); |
| 1434 | BlockGameAction a; |
| 1435 | a.action = BlockGameAction::Action::SET_GAME_OVER; |
| 1436 | theGame.DoAction(a); |
| 1437 | theGame2.DoAction(a); |
| 1438 | |
| 1439 | //Takes names from file instead |
| 1440 | theGame.name = globalData.player1name; |
| 1441 | theGame2.name = globalData.player2name; |
| 1442 | |
| 1443 | bool mustsetupgame = true; |
| 1444 | |
| 1445 | if (singlePuzzle) { |
| 1446 | LoadPuzzleStages(); |
| 1447 | BlockGameStartInfo s; |
| 1448 | s.puzzleMode = true; |
| 1449 | s.level = singlePuzzleNr; |
| 1450 | s.singlePuzzle = true; |
| 1451 | theGame.NewGame(s); |
| 1452 | mustsetupgame = false; |
| 1453 | } |
| 1454 | //game loop |
| 1455 | int done = 0; |
| 1456 | if (globalData.verboseLevel) { |
| 1457 | std::cout << "Starting game loop" << "\n"; |
| 1458 | } |
| 1459 | |
| 1460 | |
| 1461 | while (done == 0) { |
| 1462 | if (mustsetupgame) { |
| 1463 | registerEndlessHighscore = false; |
| 1464 | registerTTHighscorePlayer1 = false; |
| 1465 | registerTTHighscorePlayer2 = false; |
| 1466 | switch (gametype) { |
| 1467 | case Gametype::SinglePlayerTimeTrial: |
| 1468 | StartSinglePlayerTimeTrial(); |
| 1469 | break; |
| 1470 | case Gametype::StageClear: { |
| 1471 | int myLevel = PuzzleLevelSelect(1); |
| 1472 | if (myLevel == -1) { |
| 1473 | return 1; |
| 1474 | } |
| 1475 | BlockGameStartInfo s; |
| 1476 | s.ticks = SDL_GetTicks(); |
| 1477 | s.stageClear = true; |
| 1478 | s.level = myLevel; |
| 1479 | theGame.NewGame(s); |
| 1480 | DrawBackground(globalData.screen); |
| 1481 | twoPlayers =false; |
| 1482 | BlockGameAction a; |
| 1483 | a.action = BlockGameAction::Action::SET_GAME_OVER; |
| 1484 | theGame2.DoAction(a); |
| 1485 | theGame.name = globalData.player1name; |
| 1486 | theGame2.name = globalData.player2name; |
| 1487 | } |
| 1488 | break; |
| 1489 | case Gametype::Puzzle: |
| 1490 | if (StartSinglePlayerPuzzle()) { |
| 1491 | return 1; |
| 1492 | } |
| 1493 | break; |
| 1494 | case Gametype::SinglePlayerVs: { |
| 1495 | //1 player - Vs mode |
| 1496 | int theAIlevel = level; //startSingleVs(); |
| 1497 | BlockGameStartInfo startInfo; |
| 1498 | startInfo.ticks = SDL_GetTicks(); |
| 1499 | startInfo.vsMode = true; |
| 1500 | startInfo.vsAI = true; |
| 1501 | startInfo.level = theAIlevel; |
| 1502 | startInfo.basicBlockVariants = Config::getInstance()->getInt("basic_block_variants", 6); |
| 1503 | theGame.NewGame(startInfo); |
| 1504 | startInfo.AI = true; |
| 1505 | theGame2.NewGame(startInfo); |
| 1506 | DrawBackground(globalData.screen); |
| 1507 | twoPlayers = true; //Single player, but AI plays |
| 1508 | theGame.name = globalData.player1name; |
| 1509 | theGame2.name = globalData.player2name; |
| 1510 | } |
| 1511 | break; |
| 1512 | case Gametype::TwoPlayerTimeTrial: |
| 1513 | StarTwoPlayerTimeTrial(); |
| 1514 | break; |
| 1515 | case Gametype::TwoPlayerVs: |
| 1516 | StartTwoPlayerVs(); |
| 1517 | break; |
| 1518 | case Gametype::SinglePlayerEndless: |
| 1519 | default: |
| 1520 | StartSinglePlayerEndless(level); |
| 1521 | }; |
| 1522 | if (!twoPlayers) { |
| 1523 | std::string gametypeName; |
| 1524 | std::string infostring; |
| 1525 | if (theGame.isTimeTrial()) { |
| 1526 | gametypeName = _("Time Trial"); |
| 1527 | infostring = _("Score as much as possible in 2 minutes"); |
| 1528 | |
| 1529 | } |
| 1530 | else if (theGame.isStageClear()) { |
| 1531 | gametypeName = _("Stage Clear"); |
| 1532 | infostring = _("You must clear a number of lines. Speed is rapidly increased."); |
| 1533 | } |
| 1534 | else if (theGame.isPuzzleMode()) { |
| 1535 | gametypeName = _("Puzzle"); |
| 1536 | infostring = _("Clear the entire board with a limited number of moves."); |
| 1537 | } |
| 1538 | else { |
| 1539 | gametypeName = _("Endless"); |
| 1540 | infostring = _("Score as much as possible. No time limit."); |
| 1541 | } |
| 1542 | theGame2.infostring = infostring; |
| 1543 | theGame2.infostringName = gametypeName; |
| 1544 | |
| 1545 | std::string controldBoxText = std::string(_("Movement keys:"))+"\n"+getKeyName(keySettings[0].left)+", "+getKeyName(keySettings[0].right)+",\n" |
| 1546 | + getKeyName(keySettings[0].up)+", "+getKeyName(keySettings[0].down)+"\n" |
| 1547 | + _("Switch: ") + getKeyName(keySettings[0].change); |
| 1548 | if (theGame.isPuzzleMode()) { |
| 1549 | controldBoxText += std::string("\n") + _("Restart: ")+getKeyName(keySettings[0].push); |
| 1550 | } |
| 1551 | else { |
| 1552 | controldBoxText += std::string("\n") + _("Push line: ")+getKeyName(keySettings[0].push); |
| 1553 | } |
| 1554 | theGame2.controldBoxText = controldBoxText; |
| 1555 | } |
| 1556 | mustsetupgame = false; |
| 1557 | DrawBackground(globalData.screen); |
| 1558 | MoveBlockGameSdls(theGame, theGame2); |
| 1559 | DrawEverything(globalData.xsize, globalData.ysize, &theGame, &theGame2); |
| 1560 | SDL_RenderPresent(globalData.screen); |
| 1561 | } |
| 1562 | |
| 1563 | if (!(globalData.highPriority)) { |
| 1564 | SDL_Delay(1); |
| 1565 | } |
| 1566 | |
| 1567 | DrawBackground(globalData.screen); |
| 1568 | //updates the balls and explosions:g |
| 1569 | theBallManager.update(); |
| 1570 | theExplosionManager.update(); |
| 1571 | globalData.theTextManager.update(); |
| 1572 | |
| 1573 | bool mustWriteScreenshot = false; |
| 1574 | int mousex; |
| 1575 | int mousey; |
| 1576 | globalData.logicalResize.PhysicalToLogical(globalData.mousex, globalData.mousey, mousex, mousey); |
| 1577 | |
| 1578 | BlockGameAction a; |
| 1579 | a.tick = SDL_GetTicks(); |
| 1580 | if (true) { |
| 1581 | SDL_Event event; |
| 1582 | |
| 1583 | while ( SDL_PollEvent(&event) ) { |
| 1584 | UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey); |
| 1585 | int mousex; |
| 1586 | int mousey; |
| 1587 | globalData.logicalResize.PhysicalToLogical(globalData.mousex, globalData.mousey, mousex, mousey); |
| 1588 | if ( event.type == SDL_QUIT ) { |
| 1589 | Config::getInstance()->setShuttingDown(5); |
| 1590 | done = 1; |
| 1591 | } |
| 1592 | |
| 1593 | if (theGame.isGameOver() && isEscapeEvent(event)) { |
| 1594 | done = 1; |
| 1595 | } |
| 1596 | |
| 1597 | if (event.type == SDL_CONTROLLERBUTTONDOWN) { |
| 1598 | if ( event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK ) { |
| 1599 | done=1; |
| 1600 | DrawBackground(globalData.screen); |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | if ( event.type == SDL_KEYDOWN ) { |
| 1605 | if ( event.key.keysym.sym == SDLK_ESCAPE || ( event.key.keysym.sym == SDLK_RETURN && theGame.isGameOver() ) ) { |
| 1606 | done=1; |
| 1607 | DrawBackground(globalData.screen); |
| 1608 | } |
| 1609 | if (!theGame.GetAIenabled()) { |
| 1610 | //player1: |
| 1611 | if ( event.key.keysym.sym == keySettings[player1keys].up ) { |
| 1612 | a.action = BlockGameAction::Action::MOVE; |
| 1613 | a.way = 'N'; |
| 1614 | theGame.DoAction(a); |
| 1615 | } |
| 1616 | if ( event.key.keysym.sym == keySettings[player1keys].down ) { |
| 1617 | a.action = BlockGameAction::Action::MOVE; |
| 1618 | a.way = 'S'; |
| 1619 | theGame.DoAction(a); |
| 1620 | } |
| 1621 | if ( event.key.keysym.sym == keySettings[player1keys].left ) { |
| 1622 | a.action = BlockGameAction::Action::MOVE; |
| 1623 | a.way = 'W'; |
| 1624 | theGame.DoAction(a); |
| 1625 | } |
| 1626 | if ( event.key.keysym.sym == keySettings[player1keys].right ) { |
| 1627 | a.action = BlockGameAction::Action::MOVE; |
| 1628 | a.way = 'E'; |
| 1629 | theGame.DoAction(a); |
| 1630 | } |
| 1631 | if ( event.key.keysym.sym == keySettings[player1keys].push ) { |
| 1632 | a.action = BlockGameAction::Action::PUSH; |
| 1633 | theGame.DoAction(a); |
| 1634 | } |
| 1635 | if ( event.key.keysym.sym == keySettings[player1keys].change ) { |
| 1636 | a.action = BlockGameAction::Action::SWITCH; |
| 1637 | theGame.DoAction(a); |
| 1638 | } |
| 1639 | } |
| 1640 | if (!theGame2.GetAIenabled()) { |
| 1641 | //player2: |
| 1642 | if ( event.key.keysym.sym == keySettings[player2keys].up ) { |
| 1643 | a.action = BlockGameAction::Action::MOVE; |
| 1644 | a.way = 'N'; |
| 1645 | theGame2.DoAction(a); |
| 1646 | } |
| 1647 | if ( event.key.keysym.sym == keySettings[player2keys].down ) { |
| 1648 | a.action = BlockGameAction::Action::MOVE; |
| 1649 | a.way = 'S'; |
| 1650 | theGame2.DoAction(a); |
| 1651 | } |
| 1652 | if ( event.key.keysym.sym == keySettings[player2keys].left ) { |
| 1653 | a.action = BlockGameAction::Action::MOVE; |
| 1654 | a.way = 'W'; |
| 1655 | theGame2.DoAction(a); |
| 1656 | } |
| 1657 | if ( event.key.keysym.sym == keySettings[player2keys].right ) { |
| 1658 | a.action = BlockGameAction::Action::MOVE; |
| 1659 | a.way = 'E'; |
| 1660 | theGame2.DoAction(a); |
| 1661 | } |
| 1662 | if ( event.key.keysym.sym == keySettings[player2keys].push ) { |
| 1663 | a.action = BlockGameAction::Action::PUSH; |
| 1664 | theGame2.DoAction(a); |
| 1665 | } |
| 1666 | if ( event.key.keysym.sym == keySettings[player2keys].change ) { |
| 1667 | a.action = BlockGameAction::Action::SWITCH; |
| 1668 | theGame2.DoAction(a); |
| 1669 | } |
| 1670 | } |
| 1671 | //common: |
| 1672 | if (!singlePuzzle) { |
| 1673 | if ( event.key.keysym.sym == SDLK_F2 ) { |
| 1674 | /*#if NETWORK |
| 1675 | if ((!showOptions)&&(!networkActive)){ |
| 1676 | #else |
| 1677 | if ((!showOptions)){ |
| 1678 | #endif |
| 1679 | StartSinglePlayerEndless(); |
| 1680 | } |
| 1681 | */ |
| 1682 | mustsetupgame = true; |
| 1683 | } |
| 1684 | if ( event.key.keysym.sym == SDLK_F10 ) { |
| 1685 | //StartReplay("/home/poul/.gamesaves/blockattack/quicksave"); |
| 1686 | } |
| 1687 | if ( event.key.keysym.sym == SDLK_F9 ) { |
| 1688 | mustWriteScreenshot = true; |
| 1689 | } |
| 1690 | if ( event.key.keysym.sym == SDLK_F5 ) { |
| 1691 | } |
| 1692 | if ( event.key.keysym.sym == SDLK_F11 ) { |
| 1693 | globalData.theme = ThemesGetNext(); |
| 1694 | } //F11 |
| 1695 | } |
| 1696 | if ( event.key.keysym.sym == SDLK_F12 ) { |
| 1697 | done=1; |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | if (GameControllerExtIsPlayerUpEvent(1, event)) { |
| 1702 | a.action = BlockGameAction::Action::MOVE; |
| 1703 | a.way = 'N'; |
| 1704 | theGame.DoAction(a); |
| 1705 | } |
| 1706 | if (GameControllerExtIsPlayerDownEvent(1, event)) { |
| 1707 | a.action = BlockGameAction::Action::MOVE; |
| 1708 | a.way = 'S'; |
| 1709 | theGame.DoAction(a); |
| 1710 | } |
| 1711 | if (GameControllerExtIsPlayerLeftEvent(1, event)) { |
| 1712 | a.action = BlockGameAction::Action::MOVE; |
| 1713 | a.way = 'W'; |
| 1714 | theGame.DoAction(a); |
| 1715 | } |
| 1716 | if (GameControllerExtIsPlayerRightEvent (1, event)) { |
| 1717 | a.action = BlockGameAction::Action::MOVE; |
| 1718 | a.way = 'E'; |
| 1719 | theGame.DoAction(a); |
| 1720 | } |
| 1721 | if (GameControllerExtIsPlayerSwitchEvent(1, event)) { |
| 1722 | a.action = BlockGameAction::Action::SWITCH; |
| 1723 | theGame.DoAction(a); |
| 1724 | } |
| 1725 | if (GameControllerExtIsPlayerPushEvent(1, event)) { |
| 1726 | a.action = BlockGameAction::Action::PUSH; |
| 1727 | theGame.DoAction(a); |
| 1728 | } |
| 1729 | if (GameControllerExtIsPlayerUpEvent(2, event)) { |
| 1730 | a.action = BlockGameAction::Action::MOVE; |
| 1731 | a.way = 'N'; |
| 1732 | theGame2.DoAction(a); |
| 1733 | } |
| 1734 | if (GameControllerExtIsPlayerDownEvent(2, event)) { |
| 1735 | a.action = BlockGameAction::Action::MOVE; |
| 1736 | a.way = 'S'; |
| 1737 | theGame2.DoAction(a); |
| 1738 | } |
| 1739 | if (GameControllerExtIsPlayerLeftEvent(2, event)) { |
| 1740 | a.action = BlockGameAction::Action::MOVE; |
| 1741 | a.way = 'W'; |
| 1742 | theGame2.DoAction(a); |
| 1743 | } |
| 1744 | if (GameControllerExtIsPlayerRightEvent (2, event)) { |
| 1745 | a.action = BlockGameAction::Action::MOVE; |
| 1746 | a.way = 'E'; |
| 1747 | theGame2.DoAction(a); |
| 1748 | } |
| 1749 | if (GameControllerExtIsPlayerSwitchEvent(2, event)) { |
| 1750 | a.action = BlockGameAction::Action::SWITCH; |
| 1751 | theGame2.DoAction(a); |
| 1752 | } |
| 1753 | if (GameControllerExtIsPlayerPushEvent(2, event)) { |
| 1754 | a.action = BlockGameAction::Action::PUSH; |
| 1755 | theGame2.DoAction(a); |
| 1756 | } |
| 1757 | static int mouseDownX = 0; |
| 1758 | static int mouseDownY = 0; |
| 1759 | if (event.type == SDL_MOUSEBUTTONDOWN) { |
| 1760 | if (event.button.button == SDL_BUTTON_LEFT) { |
| 1761 | bool pressed = false; |
| 1762 | int x = 0; |
| 1763 | int y = 0; |
| 1764 | |
| 1765 | theGame.GetBrickCoordinateFromMouse(pressed, mousex, mousey, x, y); |
| 1766 | if (pressed) { |
| 1767 | a.action = BlockGameAction::Action::MOUSE_DOWN; |
| 1768 | a.x = x; |
| 1769 | a.y = y; |
| 1770 | theGame.DoAction(a); |
| 1771 | } |
| 1772 | theGame2.GetBrickCoordinateFromMouse(pressed, mousex, mousey, x, y); |
| 1773 | if (pressed) { |
| 1774 | a.action = BlockGameAction::Action::MOUSE_DOWN; |
| 1775 | a.x = x; |
| 1776 | a.y = y; |
| 1777 | theGame2.DoAction(a); |
| 1778 | } |
| 1779 | mouseDownX = mousex; |
| 1780 | mouseDownY = mousey; |
| 1781 | } |
| 1782 | if (event.button.button == SDL_BUTTON_RIGHT) { |
| 1783 | bool pressed = false; |
| 1784 | int x = 0; |
| 1785 | int y = 0; |
| 1786 | theGame.GetBrickCoordinateFromMouse(pressed, mousex, mousey, x, y); |
| 1787 | if (pressed) { |
| 1788 | a.action = BlockGameAction::Action::PUSH; |
| 1789 | theGame.DoAction(a); |
| 1790 | } |
| 1791 | theGame2.GetBrickCoordinateFromMouse(pressed, mousex, mousey, x, y); |
| 1792 | if (pressed) { |
| 1793 | a.action = BlockGameAction::Action::PUSH; |
| 1794 | theGame2.DoAction(a); |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | if (event.type == SDL_MOUSEBUTTONUP) { |
| 1799 | if (event.button.button == SDL_BUTTON_LEFT) { |
| 1800 | int x = mousex; |
| 1801 | int y = mousey; |
| 1802 | a.action = BlockGameAction::Action::MOUSE_UP; |
| 1803 | theGame.DoAction(a); |
| 1804 | theGame2.DoAction(a); |
| 1805 | if (theGame.IsInTheBoard(x,y) && theGame.IsUnderTheBoard(mouseDownX, mouseDownY)) { |
| 1806 | a.action = BlockGameAction::Action::PUSH; |
| 1807 | theGame.DoAction(a); |
| 1808 | } |
| 1809 | if (theGame2.IsInTheBoard(x,y) && theGame2.IsUnderTheBoard(mouseDownX, mouseDownY)) { |
| 1810 | a.action = BlockGameAction::Action::PUSH; |
| 1811 | theGame2.DoAction(a); |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | if (event.type == SDL_MOUSEMOTION) { |
| 1816 | //cout << "Moved" << "\n"; |
| 1817 | bool pressed = false; |
| 1818 | int x = 0; |
| 1819 | int y = 0; |
| 1820 | theGame.GetMouseCursor(pressed, x, y); |
| 1821 | if (pressed) { |
| 1822 | int mx = 0; |
| 1823 | int my = 0; |
| 1824 | theGame.GetBrickCoordinateFromMouse(pressed, mousex, mousey, mx, my); |
| 1825 | if (pressed) { |
| 1826 | if (mx != x) { |
| 1827 | a.action = BlockGameAction::Action::MOUSE_MOVE; |
| 1828 | a.x = mx; |
| 1829 | theGame.DoAction(a); |
| 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | theGame2.GetMouseCursor(pressed, x, y); |
| 1834 | if (pressed) { |
| 1835 | int mx = 0; |
| 1836 | int my = 0; |
| 1837 | theGame2.GetBrickCoordinateFromMouse(pressed, mousex, mousey, mx, my); |
| 1838 | if (pressed) { |
| 1839 | if (mx != x) { |
| 1840 | a.action = BlockGameAction::Action::MOUSE_MOVE; |
| 1841 | a.x = mx; |
| 1842 | theGame2.DoAction(a); |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | } |
| 1848 | } //while event PollEvent - read keys |
| 1849 | |
| 1850 | // If the mouse button is released, make bMouseUp equal true |
| 1851 | if (! (SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) { |
| 1852 | bMouseUp=true; |
| 1853 | } |
| 1854 | |
| 1855 | // If the mouse button 2 is released, make bMouseUp2 equal true |
| 1856 | if ((SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(3))!=SDL_BUTTON(3)) { |
| 1857 | bMouseUp2=true; |
| 1858 | } |
| 1859 | |
| 1860 | if (true ) { |
| 1861 | //read mouse events |
| 1862 | if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) { |
| 1863 | //This is the mouse events |
| 1864 | bMouseUp = false; |
| 1865 | DrawBackground(globalData.screen); |
| 1866 | |
| 1867 | if (stageButtonStatus != SBdontShow && (mousex > theGame.GetTopX()+globalData.cordNextButton.x) |
| 1868 | && (mousex < theGame.GetTopX()+globalData.cordNextButton.x+globalData.cordNextButton.xsize) |
| 1869 | && (mousey > theGame.GetTopY()+globalData.cordNextButton.y) |
| 1870 | && (mousey < theGame.GetTopY()+globalData.cordNextButton.y+globalData.cordNextButton.ysize)) { |
| 1871 | //Clicked the next button after a stage clear or puzzle |
| 1872 | nextLevel(theGame, SDL_GetTicks()); |
| 1873 | } |
| 1874 | |
| 1875 | if (stageButtonStatus != SBdontShow && (mousex > theGame.GetTopX()+globalData.cordRetryButton .x) |
| 1876 | &&(mousex < theGame.GetTopX()+globalData.cordRetryButton.x+globalData.cordRetryButton.xsize) |
| 1877 | &&(mousey > theGame.GetTopY()+globalData.cordRetryButton.y) |
| 1878 | &&(mousey < theGame.GetTopY()+globalData.cordRetryButton.y+globalData.cordRetryButton.ysize)) { |
| 1879 | //Clicked the retry button |
| 1880 | retryLevel(theGame, SDL_GetTicks()); |
| 1881 | } |
| 1882 | |
| 1883 | if (mousex > globalData.xsize-bExitOffset && mousex < globalData.xsize-bExitOffset+bExitSize && |
| 1884 | mousey > globalData.ysize-bExitOffset && mousey < globalData.ysize-bExitOffset+bExitSize) { |
| 1885 | done = 1; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | //Mouse button 2: |
| 1890 | if ((SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(3))==SDL_BUTTON(3) && bMouseUp2) { |
| 1891 | bMouseUp2=false; //The button is pressed |
| 1892 | } |
| 1893 | } |
| 1894 | } //if !bScreenBocked; |
| 1895 | |
| 1896 | |
| 1897 | //Sees if music is stopped and if music is enabled |
| 1898 | if ((!globalData.NoSound)&&(!Mix_PlayingMusic())&&(globalData.MusicEnabled)&&(!bNearDeath)) { |
| 1899 | // then starts playing it. |
| 1900 | Mix_PlayMusic(bgMusic.get(), -1); //music loop |
| 1901 | int musicVolume = Config::getInstance()->getInt("volume_music"); |
| 1902 | Mix_VolumeMusic(musicVolume); |
| 1903 | } |
| 1904 | |
| 1905 | if (bNearDeath!=bNearDeathPrev) { |
| 1906 | int musicVolume = Config::getInstance()->getInt("volume_music"); |
| 1907 | if (bNearDeath) { |
| 1908 | if (!globalData.NoSound &&(globalData.MusicEnabled)) { |
| 1909 | Mix_PlayMusic(highbeatMusic.get(), 1); |
| 1910 | Mix_VolumeMusic(musicVolume); |
| 1911 | } |
| 1912 | } |
| 1913 | else { |
| 1914 | if (!globalData.NoSound &&(globalData.MusicEnabled)) { |
| 1915 | Mix_PlayMusic(bgMusic.get(), -1); |
| 1916 | Mix_VolumeMusic(musicVolume); |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | bNearDeathPrev = bNearDeath; |
| 1922 | |
| 1923 | |
| 1924 | //set bNearDeath to false theGame*.Update() will change to true as needed |
| 1925 | bNearDeath = theGame.IsNearDeath() || theGame2.IsNearDeath(); |
| 1926 | //Updates the objects |
| 1927 | a.action = BlockGameAction::Action::UPDATE; |
| 1928 | theGame.DoAction(a); |
| 1929 | theGame2.DoAction(a); |
| 1930 | |
| 1931 | //see if anyone has won (two players only) |
| 1932 | #if NETWORK |
| 1933 | if (!networkPlay) |
| 1934 | #endif |
| 1935 | if (twoPlayers) { |
| 1936 | if (theGame.isGameOver() && theGame2.isGameOver() && !theGame.GetIsWinner() && !theGame2.GetIsWinner() ) { |
| 1937 | if (theGame.GetScore()+theGame.GetHandicap()>theGame2.GetScore()+theGame2.GetHandicap()) { |
| 1938 | BlockGameAction a; |
| 1939 | a.action = BlockGameAction::Action::SET_WON; |
| 1940 | theGame.DoAction(a); |
| 1941 | } |
| 1942 | else if (theGame.GetScore()+theGame.GetHandicap()<theGame2.GetScore()+theGame2.GetHandicap()) { |
| 1943 | BlockGameAction a; |
| 1944 | a.action = BlockGameAction::Action::SET_WON; |
| 1945 | theGame2.DoAction(a); |
| 1946 | } |
| 1947 | else { |
| 1948 | BlockGameAction a; |
| 1949 | a.action = BlockGameAction::Action::SET_DRAW; |
| 1950 | theGame.DoAction(a); |
| 1951 | theGame2.DoAction(a); |
| 1952 | } |
| 1953 | //twoPlayers = false; |
| 1954 | } |
| 1955 | if ((theGame.isGameOver()) && (!theGame2.isGameOver())) { |
| 1956 | BlockGameAction a; |
| 1957 | a.action = BlockGameAction::Action::SET_WON; |
| 1958 | theGame2.DoAction(a); |
| 1959 | } |
| 1960 | if ((!theGame.isGameOver()) && (theGame2.isGameOver())) { |
| 1961 | BlockGameAction a; |
| 1962 | a.action = BlockGameAction::Action::SET_WON; |
| 1963 | theGame.DoAction(a); |
| 1964 | } |
| 1965 | std::vector<GarbageStruct> gs; |
| 1966 | theGame.PopSendGarbage(gs); |
| 1967 | for (const GarbageStruct& g : gs ) { |
| 1968 | BlockGameAction a; |
| 1969 | a.action = BlockGameAction::Action::PUSH_GARBAGE; |
| 1970 | a.garbage.push_back(g); |
| 1971 | theGame2.DoAction(a); |
| 1972 | } |
| 1973 | gs.clear(); |
| 1974 | theGame2.PopSendGarbage(gs); |
| 1975 | for (const GarbageStruct& g : gs ) { |
| 1976 | BlockGameAction a; |
| 1977 | a.action = BlockGameAction::Action::PUSH_GARBAGE; |
| 1978 | a.garbage.push_back(g); |
| 1979 | theGame.DoAction(a); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | if (theGame.isGameOver() && (registerTTHighscorePlayer1 || registerTTHighscorePlayer2)) { |
| 1984 | std::string highscore_name = "timetrial"; |
| 1985 | int block_variants = theGame.GetBasicBlockVariants(); |
| 1986 | // If the block variants are not the default 6, add the number of block variants to the highscore name |
| 1987 | if (block_variants != 6) { |
| 1988 | highscore_name += "_block" + std::to_string(block_variants); |
| 1989 | } |
| 1990 | Highscore theTopScoresTimeTrial = Highscore(highscore_name, 0.5); |
| 1991 | if (registerTTHighscorePlayer1) { |
| 1992 | registerTTHighscorePlayer1 = false; |
| 1993 | theTopScoresTimeTrial.addScore(theGame.name, theGame.GetScore()); |
| 1994 | } |
| 1995 | if (theGame2.isGameOver() && registerTTHighscorePlayer2) { |
| 1996 | registerTTHighscorePlayer2 = false; |
| 1997 | theTopScoresTimeTrial.addScore(theGame2.name, theGame2.GetScore()); |
| 1998 | } |
| 1999 | } |
| 2000 | if (theGame.isGameOver() && registerEndlessHighscore) { |
| 2001 | std::string highscore_name = "endless"; |
| 2002 | int block_variants = theGame.GetBasicBlockVariants(); |
| 2003 | // If the block variants are not the default 6, add the number of block variants to the highscore name |
| 2004 | if (block_variants != 6) { |
| 2005 | highscore_name += "_block" + std::to_string(block_variants); |
| 2006 | } |
| 2007 | Highscore theTopScoresEndless = Highscore(highscore_name.c_str(), theGame.GetBaseSpeed()); |
| 2008 | registerEndlessHighscore = false; |
| 2009 | theTopScoresEndless.addScore(theGame.name, theGame.GetScore()); |
| 2010 | theGame.EndlessHighscoreEvent(); |
| 2011 | } |
| 2012 | #ifdef REPLAY_IMPLEMENTED |
| 2013 | if (theGame.isGameOver() && saveReplay) { |
| 2014 | if (twoPlayers && theGame2.isGameOver()) { |
| 2015 | saveReplay = false; |
| 2016 | SaveReplay(theGame.GetBlockGameInfo(), theGame2.GetBlockGameInfo()); |
| 2017 | } |
| 2018 | if (!twoPlayers) { |
| 2019 | saveReplay = false; |
| 2020 | SaveReplay(theGame.GetBlockGameInfo()); |
| 2021 | } |
| 2022 | } |
| 2023 | #endif |
| 2024 | |
| 2025 | //Once evrything has been checked, update graphics |
| 2026 | MoveBlockGameSdls(theGame, theGame2); |
| 2027 | DrawEverything(globalData.xsize, globalData.ysize, &theGame, &theGame2); |
| 2028 | //Draw the mouse: |
| 2029 | globalData.mouse.Draw(globalData.screen, SDL_GetTicks(), globalData.mousex, globalData.mousey); |
| 2030 | SDL_RenderPresent(globalData.screen); |
| 2031 | if (mustWriteScreenshot) { |
| 2032 | writeScreenShot(); |
| 2033 | } |
| 2034 | } //game loop |
| 2035 | return 0; |
| 2036 | } |