diff options
| author | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-02 20:11:53 -0400 |
|---|---|---|
| committer | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-02 20:11:53 -0400 |
| commit | 84c305819930713603124d984acb51df87761246 (patch) | |
| tree | 435bbb75392311eea42d5d46d6b00caa36b35a8a /gamestate.cpp | |
| parent | a59fcafb2e81f3cb40ff320b106030e8fed4bd66 (diff) | |
| download | mazeoflife-84c305819930713603124d984acb51df87761246.tar.gz mazeoflife-84c305819930713603124d984acb51df87761246.tar.bz2 mazeoflife-84c305819930713603124d984acb51df87761246.zip | |
emscripten port emscripten
Diffstat (limited to 'gamestate.cpp')
| -rw-r--r-- | gamestate.cpp | 53 |
1 files changed, 34 insertions, 19 deletions
| diff --git a/gamestate.cpp b/gamestate.cpp index 953d35b..4a0f701 100644 --- a/gamestate.cpp +++ b/gamestate.cpp | |||
| @@ -9,9 +9,11 @@ | |||
| 9 | #include <iostream> | 9 | #include <iostream> |
| 10 | #include <list> | 10 | #include <list> |
| 11 | #include <memory> | 11 | #include <memory> |
| 12 | #include <mutex> | ||
| 12 | #include <random> | 13 | #include <random> |
| 13 | #include <set> | 14 | #include <set> |
| 14 | #include <sstream> | 15 | #include <sstream> |
| 16 | #include <thread> | ||
| 15 | #include <tuple> | 17 | #include <tuple> |
| 16 | #include <unordered_map> | 18 | #include <unordered_map> |
| 17 | #include <unordered_set> | 19 | #include <unordered_set> |
| @@ -450,34 +452,31 @@ std::unique_ptr<State> startNewLevel(int level, | |||
| 450 | 452 | ||
| 451 | class LoadGameState : public State { | 453 | class LoadGameState : public State { |
| 452 | public: | 454 | public: |
| 453 | LoadGameState(int level) : level_(level) {} | 455 | LoadGameState(Game& game, int level) : level_(level) { |
| 454 | |||
| 455 | std::unique_ptr<State> operator()(Game& game) { | ||
| 456 | std::ostringstream wintitle; | 456 | std::ostringstream wintitle; |
| 457 | wintitle << "Maze Of Life - Level " << level_; | 457 | wintitle << "Maze Of Life - Level " << level_; |
| 458 | SDL_SetWindowTitle(game.window.get(), wintitle.str().c_str()); | 458 | SDL_SetWindowTitle(game.window.get(), wintitle.str().c_str()); |
| 459 | 459 | ||
| 460 | // Randomly place the player in a corner | 460 | // Randomly place the player in a corner |
| 461 | int playerx, playery; | ||
| 462 | switch (std::uniform_int_distribution(0, 3)(game.rng)) { | 461 | switch (std::uniform_int_distribution(0, 3)(game.rng)) { |
| 463 | case 0: { | 462 | case 0: { |
| 464 | playerx = 1; | 463 | playerx_ = 1; |
| 465 | playery = 1; | 464 | playery_ = 1; |
| 466 | break; | 465 | break; |
| 467 | } | 466 | } |
| 468 | case 1: { | 467 | case 1: { |
| 469 | playerx = 1; | 468 | playerx_ = 1; |
| 470 | playery = HEIGHT - 2; | 469 | playery_ = HEIGHT - 2; |
| 471 | break; | 470 | break; |
| 472 | } | 471 | } |
| 473 | case 2: { | 472 | case 2: { |
| 474 | playerx = WIDTH - 2; | 473 | playerx_ = WIDTH - 2; |
| 475 | playery = HEIGHT - 2; | 474 | playery_ = HEIGHT - 2; |
| 476 | break; | 475 | break; |
| 477 | } | 476 | } |
| 478 | case 3: { | 477 | case 3: { |
| 479 | playerx = WIDTH - 2; | 478 | playerx_ = WIDTH - 2; |
| 480 | playery = 1; | 479 | playery_ = 1; |
| 481 | break; | 480 | break; |
| 482 | } | 481 | } |
| 483 | } | 482 | } |
| @@ -503,18 +502,34 @@ class LoadGameState : public State { | |||
| 503 | SDL_RenderPresent(game.renderer.get()); | 502 | SDL_RenderPresent(game.renderer.get()); |
| 504 | 503 | ||
| 505 | // Do 50 gens of Conway | 504 | // Do 50 gens of Conway |
| 506 | std::unique_ptr<GameBoard> board = | 505 | std::thread([&] { |
| 507 | std::make_unique<GameBoard>(game, level_, playerx, playery); | 506 | std::unique_ptr<GameBoard> board = |
| 507 | std::make_unique<GameBoard>(game, level_, playerx_, playery_); | ||
| 508 | |||
| 509 | std::lock_guard boardGuard(boardMutex_); | ||
| 510 | board_.swap(board); | ||
| 511 | }).detach(); | ||
| 512 | } | ||
| 513 | |||
| 514 | std::unique_ptr<State> operator()(Game& game) { | ||
| 515 | std::lock_guard boardGuard(boardMutex_); | ||
| 516 | if (board_) { | ||
| 517 | return startNewLevel(level_, std::move(board_), playerx_, playery_); | ||
| 518 | } | ||
| 508 | 519 | ||
| 509 | // Wait a bit | 520 | // Wait a bit |
| 510 | SDL_Delay(500); | 521 | // SDL_Delay(500); |
| 511 | 522 | ||
| 512 | // Start the level | 523 | // Start the level |
| 513 | return startNewLevel(level_, std::move(board), playerx, playery); | 524 | return nullptr; |
| 514 | } | 525 | } |
| 515 | 526 | ||
| 516 | private: | 527 | private: |
| 517 | int level_; | 528 | int level_; |
| 529 | int playerx_; | ||
| 530 | int playery_; | ||
| 531 | std::unique_ptr<GameBoard> board_; | ||
| 532 | std::mutex boardMutex_; | ||
| 518 | }; | 533 | }; |
| 519 | 534 | ||
| 520 | class PlayGameState : public State { | 535 | class PlayGameState : public State { |
| @@ -600,7 +615,7 @@ class PlayGameState : public State { | |||
| 600 | } | 615 | } |
| 601 | 616 | ||
| 602 | if (trymove && move(to_x, to_y)) { | 617 | if (trymove && move(to_x, to_y)) { |
| 603 | return std::make_unique<LoadGameState>(level_ + 1); | 618 | return std::make_unique<LoadGameState>(game, level_ + 1); |
| 604 | } | 619 | } |
| 605 | } | 620 | } |
| 606 | } | 621 | } |
| @@ -641,6 +656,6 @@ std::unique_ptr<State> startNewLevel(int level, | |||
| 641 | playery); | 656 | playery); |
| 642 | } | 657 | } |
| 643 | 658 | ||
| 644 | std::unique_ptr<State> GameState::operator()(Game&) { | 659 | std::unique_ptr<State> GameState::operator()(Game& game) { |
| 645 | return std::make_unique<LoadGameState>(0); | 660 | return std::make_unique<LoadGameState>(game, 0); |
| 646 | } | 661 | } |
