-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cpp
71 lines (52 loc) · 1.23 KB
/
Engine.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
#include "Engine.h"
Engine::Engine() {
window = nullptr;
renderer == nullptr;
}
bool Engine::initialize() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return false;
}
window = SDL_CreateWindow("Modular Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr) {
return false;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr) {
return false;
}
world.initialize();
return true;
}
void Engine::close() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void Engine::handleEvents(SDL_Event& e, bool& quit) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
}
}
void Engine::gameLoop() {
SDL_Event e;
bool quit = false;
while (!quit) {
handleEvents(e, quit);
world.update();
world.render(renderer);
SDL_Delay(DELTA_TIME);
}
}
void Engine::startEngine() {
if (!initialize()) {
std::cerr << "Failed to initialize!\n";
close();
return;
}
setupEngine();
gameLoop();
close();
}