The Game Engine  1
Game.h
Go to the documentation of this file.
1 #ifndef __GAME_H__
2 #define __GAME_H__
3 #include "Texture.h"
4 #include "Level.h"
5 #include "Sprite.h"
6 #include "Camera.h"
7 #include "ParticleSystem.h"
8 #include <vector>
9 #include <string>
10 
24 class Game
25 {
26 public:
32  static Game* game();
33 
35  static const int FRAMES_PER_SECOND = 60;
37  static const int TICKS_PER_SECOND = 30;//can use 25 for small weaker hardware
38 
41 
43  bool ShowFPS;
44 
47 
49  bool Paused;
50 
52  bool Focused;
53 
56 
58  friend class Camera;
59 
62 
65 
68 
71 
74 
77 
80 
86 
91  void setCurrentLevel(string name);
92 
97  Level* loadLevel(string name);
98 
103  string getLevelKey();
104 
109  int getLevelCount();
110 
119  Font* getFont( FontType type, int size);
120 
126  void removeFont(FontType type, int size);
127 
132  void removeFont(Font* font);
133 
141  Texture* getTexture(string filename, Texture* tex = NULL);
142 
148  void removeTexture(string filename);
149 
155 
160  void addAnimation(Animation* anim);
161 
167  Animation* getAnimation(string name);
168 
173  void removeAnimation(Animation* anim);
174 
181  void addActor(string name, Actor actor);
182 
187  void removeActor(string name);
188 
194  void addObject(WorldObject* sp, Level* l = NULL);
195 
201  void removeObject(WorldObject* sp, Level* l = NULL);
202 
208  void removeAllSprites(string name, Level* l = NULL);
209 
215  void removeAllParticleSystems(string name, Level* l = NULL);
216 
220  void run();
221 
225  void stop();
226 
230  Uint32 getFPS() { return avgFPS; }
231 
236 
237 protected:
238  Game();
239  ~Game();
240 
242  bool Running;
243 
245  bool poll;
246 
249 
251  SDL_TimerID timer;
252 
254  static Game* m_instance;
255 
259  void LoadResources();
260 
264  void SetStaticObjectValues();
265 
269  void addLevel(Level* l);
270 
272  map<string, Level*> mLevels;
273 
275  map<string,Actor> mActors;
276 
278  vector<Animation*> mAnims;
279 
281  SDL_Surface *mScreen;
283  map<string,Behavior*> mBehaviors;
284 
286  map<pair<FontType, int>, Font*> mFonts;
287 
289  map<string, Texture* > mTextures;
290 
292  Uint32 unfocusTime;
293 
295 
296  static const int FPSRANGE = 1;
300  Uint32 numFrames;
302  Uint32 currentFPS;
304  Uint32 avgFPS;
306  Uint32 FPSs[FPSRANGE];
309 
311 
312  static const Uint32 drawInterval = 1000/FRAMES_PER_SECOND;
314  Uint32 nextDrawTick;
315 
316 
318 
319  static const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
321  static const int MAX_FRAMESKIP = 5;
323  Uint32 nextGameTick;
326 
330  void handleInput(SDL_Event event);
331 
335  void draw();
336 
340  void startDrawTimer();
341 
345  void stopDrawTimer();
346 
352  void resize(int w , int h);
353 
357  static Uint32 pushUserEvent(Uint32 interval, void* data);
358 
362  void CalculateFramerate();
363 };
364 
365 #endif