-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.cpp
executable file
·203 lines (161 loc) · 4.06 KB
/
Core.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "Core.h"
namespace Engine
{
Core::Core()
:
isWindow( false ),
isRunning( false ),
window( nullptr ),
renderer(),
inputHandler(),
frameRate( 5000 ),
frameTime( 1.0 / (double)frameRate ),
loadFunction( nullptr ),
updateFunction( nullptr ),
renderFunction( nullptr )
{
Time t( &frameTime );
}
Core::~Core()
{
cleanup();
}
void Core::cleanup()
{
delete window;
}
int Core::createWindow( int w, int h, std::string title, bool fullscreen )
{
if( isWindow )
{
fputs( "Cannot create window: A window already exists!\n", stdout );
return -1;
}
if( !window )
window = new Window();
window->createWindow( w, h, title, fullscreen );
isWindow = true;
return 0;
}
void Core::destroyWindow()
{
if( isWindow )
window->destroyWindow();
isWindow = false;
}
void Core::swapWindow()
{
if( isWindow )
this->window->swapBuffers();
}
void Core::setVsync( bool Vsync )
{
glfwSwapInterval( Vsync );
}
void Core::setFrameRate( int rate )
{
this->frameRate = rate;
}
void Core::start()
{
if( !isWindow )
{
fputs( "Create a window before running!\n", stderr );
return;
}
if( isRunning )
{
fputs( "Already running!\n", stdout );
return;
}
run();
}
void Core::stop()
{
if( isRunning )
isRunning = false;
}
void Core::run()
{
isRunning = true;
load();
int frames = 0;
double frameCounter = 0.0;
int lastFrameRate = frameRate;
frameTime = 1.0 / (double)frameRate;
double lastTime = Time::getTime();
double deltaTime;
double currTime;
double unprocessedTime = 0.0;
bool shouldRender;
while( isRunning )
{
if( lastFrameRate != frameRate )
{
frameTime = 1.0 / (double)frameRate;
lastFrameRate = frameRate;
}
shouldRender = false;
currTime = Time::getTime();
deltaTime = currTime - lastTime;
lastTime = currTime;
unprocessedTime += deltaTime;
frameCounter += deltaTime;
while( unprocessedTime > frameTime )
{
shouldRender = true;
unprocessedTime -= frameTime;
if( window->shouldClose() )
stop();
glfwPollEvents();
update();
inputHandler.update();
if( frameCounter > Time::TICKS_PER_SEC )
{
fprintf( stdout, "FPS: %i\n", frames );
frames = 0;
frameCounter = 0.0;
}
}
if( shouldRender )
{
render();
frames++;
}
else
{
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
}
}
}
void Core::load()
{
renderer.load();
inputHandler.refreshJoysticks();
if( this->loadFunction )
loadFunction( inputHandler );
}
void Core::update()
{
if( this->updateFunction )
updateFunction();
}
void Core::render()
{
if( this->renderFunction )
renderFunction( this->renderer );
swapWindow();
}
void Core::loadFunc( void (*loadFunct)( InputHandler& ) )
{
this->loadFunction = loadFunct;
}
void Core::updateFunc( void (*updFunct)() )
{
this->updateFunction = updFunct;
}
void Core::renderFunc( void (*rndrFunct)( Renderer& ) )
{
this->renderFunction = rndrFunct;
}
}