dEngine
Simple 2D C++ game engine
Game.h
Go to the documentation of this file.
1 #ifndef DENGINE_GAME_H
2 #define DENGINE_GAME_H
3 
4 #include <stdio.h>
5 #include <SDL.h>
6 #include "Time.h"
7 #include <string>
8 #include <SDL_image.h>
9 #include <vector>
10 #include <memory>
11 #include <stack>
12 #include <SDL_ttf.h>
13 #include "GameStates.h"
14 #include "GameLevel.h"
15 #include "../Utils/Serializer.h"
16 #include "../Universal.h"
17 #include "../UI/UI.h"
18 #include "EventSystem.h"
19 
20 using namespace dengine_UI;
21 
22 namespace dengine {
28  class Game {
29  public:
30  Game();
31 
35  GameLevel &GetCurrentState();
36 
40  void Push(GameLevel *state);
44  void Pop();
45 
49  void loop();
50 
54  void cleanup();
55 
59  static Game &GetInstance();
60 
64  SDL_Renderer *GetRenderer();
65 
69  SDL_Window *window = NULL;
70 
71  template<typename T>
77  bool SaveState(Serializer<T>* serializer);
78 
79  template<typename T>
85  bool LoadState(Serializer<T>* serializer);
86 
92  UI *GetUI();
93 
99  EventSystem *GetEventSystem();
100  private:
101  UI *ui;
103  SDL_Renderer *renderer;
104  std::stack <std::unique_ptr<GameLevel>> stateStack;
106  static Game *instance;
107  };
108 
109  template<typename T>
110  bool Game::SaveState(Serializer<T>* serializer) {
111  T* level = dynamic_cast<T*>(&GetCurrentState());
112  return serializer->saveToFile(level);
113  }
114 
115  template<typename T>
116  bool Game::LoadState(Serializer<T>* serializer) {
117  T level = serializer->loadFromFile();
118  Pop();
119  Push(new T(level));
120  return true;
121  }
122 }
123 #endif //DENGINE_GAME_H
UI * ui
Definition: Game.h:101
std::stack< std::unique_ptr< GameLevel > > stateStack
Definition: Game.h:104
A level of the game.
Definition: GameLevel.h:19
Used to manage the UI of a game See the code example below for a quick reference on how to use the UI...
Definition: UI.h:25
EventSystem * eventSystem
Definition: Game.h:102
T loadFromFile()
Load data from a file and deserialize it back to an object.
Definition: Serializer.h:87
The base level of the engine.
Definition: Game.h:28
Event System.
Definition: EventSystem.h:26
Definition: Circle.h:11
static Game * instance
Definition: Game.h:106
A serializer.
Definition: Serializer.h:19
SDL_Renderer * renderer
Definition: Game.h:103
The namespace containing the engine&#39;s code.
Definition: Collider.h:9
GameLevel * storedState
Definition: Game.h:105
bool saveToFile(const T *obj)
Serialize and object and save it to a file.
Definition: Serializer.h:81