-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
executable file
·85 lines (75 loc) · 2.02 KB
/
Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include "Game.h"
#include "ResourceManager.h"
#include "GameState.h"
#include "MenuState.h"
#include "RunningState.h"
#include <SDL/SDL_thread.h>
Game* Game::SINGLETON = 0;
Game::Game():screen_width(1024),screen_height(780),screen_bpp(32),menuState(new MenuState){
screen = SDL_SetVideoMode( screen_width,
screen_height,
screen_bpp,
SDL_SWSURFACE );
//menuState = new MenuState;
runningState = new RunningState;
gameState = NULL;
}
Game::~Game(){
}
void Game::setState(STATE state){
switch(state){
case MENU_STATE: setState(menuState.get()); break;
case RUNNING_STATE: setState(runningState); break;
case QUIT_STATE: running=false; break;
}
}
void Game::setState(GameState* gameState){
if(this->gameState != NULL){
this->gameState->uninstall();
}
this->gameState = gameState;
this->gameState->install();
}
void Game::update(){
gameState->update();
}
void Game::render(SDL_Surface* screen){
gameState->render(screen);
}
void Game::start(){
setState(menuState.get());
running = true;
Uint32 currTime;
Uint32 prevTime;
Uint32 elapsedTime;
Uint32 overRunTime=0;
while(running){
prevTime = SDL_GetTicks();
update();
if(overRunTime <= 16){
render(screen);
} else {
overRunTime = 0; //drop the frame and reset overRunTime
}
SDL_Flip(screen);
if(event.type == SDL_QUIT){
running = false;
break;
}
currTime = SDL_GetTicks();
elapsedTime = currTime-prevTime;
if(elapsedTime > 16){
overRunTime += 16 - elapsedTime;
}
else if(elapsedTime < 16 && running != false)
SDL_Delay(33 -elapsedTime);
SDL_PollEvent(&event);
}
}
Game* Game::INSTANCE(){
if(SINGLETON == NULL){
SINGLETON = new Game();
}
return SINGLETON;
}