-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEngine.cpp
More file actions
77 lines (71 loc) · 1.66 KB
/
Engine.cpp
File metadata and controls
77 lines (71 loc) · 1.66 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
69
70
71
72
73
74
75
76
77
#include "Engine.h"
#include "SDL.h"
#include <iostream>
using namespace std;
Engine::Engine(){}
Engine::~Engine(){}
void Engine::destroyGame(){}
void Engine::initContollers()
{
sceneController = new SceneController();
resourceController = new ResourceController();
animationController = new AnimationController();
sceneController->init();
resourceController->init(sceneController->getRenderer());
sceneController->setAnimationController(animationController);
}
bool Engine::initGame()
{
bool success = false;
srand(time(NULL));
if (resourceController->loadResources() == false)
{
success = false;
}
SDL_Texture* bg = resourceController->getResource(BACKGROUND);
sceneController->setBackground(bg);
sceneController->generateLevel();
return success;
}
void Engine::startGame(){
//gameloop
bool quit = false;
double previous = SDL_GetTicks();
double lag = 0.0f;
sceneController->setGameStartTime(previous);
while (!quit)
{
if (sceneController->quit)
{
quit = true;
}
double current = SDL_GetTicks();
double elapsed = current - previous;
previous = current;
lag += elapsed;
while (lag >= MS_60_MS)
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_WINDOWEVENT) {
if (event.window.type == SDL_WINDOWEVENT_CLOSE)
{
quit = true;
}
}
if (event.type == SDL_QUIT) {
quit = true;
}
sceneController->handleEvent(&event);
}
sceneController->update();
lag -= MS_60_MS;
}
sceneController->renderScene();
}
sceneController->cleanup();
resourceController->cleanup();
delete animationController;
delete sceneController;
delete resourceController;
}