-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameEngine.cpp
67 lines (53 loc) · 1.35 KB
/
GameEngine.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
#include "SDL/SDL.h"
#include <string>
#include <vector>
#include <iostream>
#include "Timer.h"
#include "SpriteInput.h"
#include "GameEngine.h"
#include "Globals.h"
namespace GameEng {
GameEngine::~GameEngine(){
for(unsigned int i=0; i < comps.size(); i++)
delete comps[i];
}
int GameEngine::run(){
Timer timer;
timer.start();
while( sys.run ){
SDL_Event event;
timer.start();
while( SDL_PollEvent( &event ) ) {
if( event.type == SDL_QUIT )
sys.run = false;
for(unsigned int i=0; i < comps.size(); i++) {
SpriteInput *si = dynamic_cast<SpriteInput*>(comps[i]);
if(si)
si->handle_input(event);
}
}
for(unsigned int i=0; i < comps.size(); i++) {
comps[i]->tick();
SpriteInput *si = dynamic_cast<SpriteInput*>(comps[i]);
if(si)
si->collision(comps);
}
//Fill the screen
SDL_FillRect( sys.screen, &sys.screen->clip_rect, SDL_MapRGB( sys.screen->format, 0xF4, 0x93, 0x43 ) );
for(unsigned int i=0; i < comps.size(); i++) {
comps[i]->draw();
}
//Update the screen
if( SDL_Flip( sys.screen ) == -1 ) {
return 1;
}
//Cap the frame rate
if ( timer.get_ticks() < 1000 / FRAMES_PER_SECOND ) {
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - timer.get_ticks());
}
}
}
void GameEngine::add(Sprite* spr){
comps.push_back(spr);
}
}