From 84c305819930713603124d984acb51df87761246 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 2 Nov 2023 20:11:53 -0400 Subject: emscripten port --- gamestate.cpp | 53 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 19 deletions(-) (limited to 'gamestate.cpp') diff --git a/gamestate.cpp b/gamestate.cpp index 953d35b..4a0f701 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -9,9 +9,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -450,34 +452,31 @@ std::unique_ptr startNewLevel(int level, class LoadGameState : public State { public: - LoadGameState(int level) : level_(level) {} - - std::unique_ptr operator()(Game& game) { + LoadGameState(Game& game, int level) : level_(level) { std::ostringstream wintitle; wintitle << "Maze Of Life - Level " << level_; SDL_SetWindowTitle(game.window.get(), wintitle.str().c_str()); // Randomly place the player in a corner - int playerx, playery; switch (std::uniform_int_distribution(0, 3)(game.rng)) { case 0: { - playerx = 1; - playery = 1; + playerx_ = 1; + playery_ = 1; break; } case 1: { - playerx = 1; - playery = HEIGHT - 2; + playerx_ = 1; + playery_ = HEIGHT - 2; break; } case 2: { - playerx = WIDTH - 2; - playery = HEIGHT - 2; + playerx_ = WIDTH - 2; + playery_ = HEIGHT - 2; break; } case 3: { - playerx = WIDTH - 2; - playery = 1; + playerx_ = WIDTH - 2; + playery_ = 1; break; } } @@ -503,18 +502,34 @@ class LoadGameState : public State { SDL_RenderPresent(game.renderer.get()); // Do 50 gens of Conway - std::unique_ptr board = - std::make_unique(game, level_, playerx, playery); + std::thread([&] { + std::unique_ptr board = + std::make_unique(game, level_, playerx_, playery_); + + std::lock_guard boardGuard(boardMutex_); + board_.swap(board); + }).detach(); + } + + std::unique_ptr operator()(Game& game) { + std::lock_guard boardGuard(boardMutex_); + if (board_) { + return startNewLevel(level_, std::move(board_), playerx_, playery_); + } // Wait a bit - SDL_Delay(500); + // SDL_Delay(500); // Start the level - return startNewLevel(level_, std::move(board), playerx, playery); + return nullptr; } private: int level_; + int playerx_; + int playery_; + std::unique_ptr board_; + std::mutex boardMutex_; }; class PlayGameState : public State { @@ -600,7 +615,7 @@ class PlayGameState : public State { } if (trymove && move(to_x, to_y)) { - return std::make_unique(level_ + 1); + return std::make_unique(game, level_ + 1); } } } @@ -641,6 +656,6 @@ std::unique_ptr startNewLevel(int level, playery); } -std::unique_ptr GameState::operator()(Game&) { - return std::make_unique(0); +std::unique_ptr GameState::operator()(Game& game) { + return std::make_unique(game, 0); } -- cgit 1.4.1