Space Walk board game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

76 lines
1.2 KiB

  1. #ifndef GAMESTATE
  2. #define GAMESTATE
  3. #include <chrono> // time
  4. #include <thread> // sleep_for
  5. #include "inputoutput.h"
  6. #include "window.h"
  7. class GameState
  8. {
  9. public:
  10. // Makes sure that the inherited class's destructor is called
  11. virtual ~GameState() {}
  12. virtual void initialize() {}
  13. virtual void update() {}
  14. virtual void render() {}
  15. virtual void destroy() {}
  16. static void sleep(int amount) {
  17. // Sleep for amount ms
  18. std::this_thread::sleep_for(std::chrono::milliseconds(amount));
  19. }
  20. };
  21. class GameStateManager
  22. {
  23. public:
  24. GameStateManager() : state(nullptr) {
  25. Window::initialize();
  26. }
  27. ~GameStateManager() {
  28. if(this->state != nullptr) {
  29. this->state->destroy();
  30. delete this->state;
  31. }
  32. Window::destroy();
  33. }
  34. void setState(GameState* state) {
  35. if(this->state != nullptr) {
  36. this->state->destroy();
  37. delete this->state;
  38. }
  39. this->state = state;
  40. if(this->state != nullptr) {
  41. this->state->initialize();
  42. }
  43. }
  44. void update() {
  45. GameState::sleep(15);
  46. if(this->state != nullptr) {
  47. this->state->update();
  48. }
  49. }
  50. void render() {
  51. if(this->state != nullptr) {
  52. this->state->render();
  53. }
  54. }
  55. private:
  56. GameState *state;
  57. };
  58. extern GameStateManager gameStateManager;
  59. #endif // GAMESTATE