-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
68 lines (54 loc) · 1.29 KB
/
game.cpp
File metadata and controls
68 lines (54 loc) · 1.29 KB
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
#include "game.h"
#include "graphics.h"
#include "input.h"
#include <SDL.h>
/* Game class
* Holds all information for main game loop
*/
namespace{
const int FPS = 50;
const int MAX_FRAME_TIME = 5 * 1000 / FPS;
}
Game::Game() {
SDL_Init(SDL_INIT_EVERYTHING);
this->gameLoop();
}
Game::~Game() {
}
void Game::gameLoop() {
Graphics graphics;
SDL_Event event;
Input input;
int LAST_UPDATE_TIME = SDL_GetTicks();
while (true) {
input.beginNewFrame();
if (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
input.keyDownEvent(event);
}
else if (event.type == SDL_KEYUP) {
input.keyUpEvent(event);
}
if (event.type == SDL_QUIT) {
return;
}
}
if (input.wasKeyPressed(SDL_SCANCODE_ESCAPE)) {
return;
}
const int CURRENT_TIME_MS = SDL_GetTicks();
int ELAPSED_TIME_MS = CURRENT_TIME_MS - LAST_UPDATE_TIME;
this->update(min(ELAPSED_TIME_MS, MAX_FRAME_TIME));
LAST_UPDATE_TIME = CURRENT_TIME_MS;
}
}
void Game::draw(Graphics& graphics) {
}
void Game::update(float elapsedTime) {
}
int Game::min(int a, int b) {
if (b > a) {
return a;
}
return b;
}