-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.cpp
91 lines (74 loc) · 2.22 KB
/
App.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
86
87
88
89
90
91
#include <cmath>
#include "App.h"
#include "Utility/Config.h"
#include <iostream>
App* App::selfInstance = nullptr;
App::App() {
appQuit = false;
windowInstance = nullptr;
eventsHandler = nullptr;
Config::initConfig();
//if(!Config::getInstance()->isCorrect()) appQuit = true;
Window::initWindow();
windowInstance = Window::getInstance();
if(!windowInstance->isRunning()) appQuit = true;
startScreen = StartScreen::getInstance();
eventsHandler = Events::getEvents();
Textures::initTextures();
surfaceGame = Surface::getInstance();
}
App::~App() {
StartScreen::destroyScreen();
startScreen = nullptr;
Config::destroyConfig();
Textures::destroyTextures();
Window::destroyWindow();
windowInstance = nullptr;
Events::destroyEvents();
eventsHandler = nullptr;
Surface::destroySurface();
surfaceGame = nullptr;
}
App *App::getApp() {
if(selfInstance == nullptr) selfInstance = new App();
return selfInstance;
}
void App::runningApp() {
Uint64 realTimeTicks = 0;
const double oneFrameTime = 1000.0/60;
while(!appQuit){
realTimeTicks = SDL_GetPerformanceCounter();
appQuit = !handleEvent();
update();
render();
windowInstance->updateSurface();
realTimeTicks = SDL_GetPerformanceCounter() - realTimeTicks;
SDL_Delay((Uint32) floor((Uint64) oneFrameTime - (realTimeTicks/SDL_GetPerformanceFrequency() * 1000.0f)));
}
}
void App::destroyApp() {
delete selfInstance;
selfInstance = nullptr;
}
bool App::handleEvent() {
eventsHandler->updateKeyState();
if(eventsHandler->isQUIT()) return false;
if(startScreen->running) return startScreen->handleEvents();
return surfaceGame->handleEvents();
}
void App::update() {
if(startScreen->running) {
startScreen->update();
}
else
surfaceGame->update();
}
void App::render() {
SDL_RenderClear(Window::getInstance()->getSDLRenderer());
if(startScreen->running) {
startScreen->render();
}
else
surfaceGame->render();
SDL_RenderPresent(Window::getInstance()->getSDLRenderer());
}