-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
121 lines (94 loc) · 3.68 KB
/
main.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
#include <SDL.h>
#include <chrono>
#include <iostream>
#include "Player/Player.h"
#include "World/World.h"
#include "StateController/StateController.h"
#define FRAME_RATE 60.0f
int main ( int argc, char** argv )
{
for (auto i = 0; i < argc; ++i)
{
std::cout << "Arg " << i << ": " << argv[i] << std::endl;
}
SDL_version compiled;
SDL_version linked;
SDL_VERSION(&compiled);
SDL_GetVersion(&linked);
std::cout << "We compiled against SDL version " << static_cast<int>(compiled.major) << "." << static_cast<int>(compiled.minor) << "." << static_cast<int>(compiled.patch) << std::endl;
std::cout << "But we are linking against SDL version " << static_cast<int>(linked.major) << "." << static_cast<int>(linked.minor) << "." << static_cast<int>(linked.patch) << std::endl;
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
std::cout << "Unable to init SDL: " << SDL_GetError() << std::endl;
return 1;
}
// create a new window
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP, &sdlWindow, &sdlRenderer);
SDL_GL_SetSwapInterval(1);
if ( !sdlWindow )
{
std::cout << "Unable to set 640x480 video: " << SDL_GetError() << std::endl;
return 1;
}
World world(sdlRenderer);
StateController stateController;
stateController.CreateMachine(std::shared_ptr<IStateMachine>(new Player(sdlRenderer)));
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::time_point lastUpdate = start;
int updateCount = 0;
// program main loop
bool done = false;
while (!done)
{
std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
float waitTime = std::chrono::duration_cast<std::chrono::microseconds>(now - lastUpdate).count();
if (waitTime > 1000000/FRAME_RATE)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
// exit if the window is closed
if (event.type == SDL_QUIT) {
done = true;
}
// check for key presses
if (event.type == SDL_KEYDOWN) {
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end of message processing
stateController.Update(waitTime);
// DRAWING STARTS HERE
// clear screen
world.Render();
stateController.Render();
// DRAWING ENDS HERE
SDL_RenderPresent(sdlRenderer);
lastUpdate = now;
updateCount++;
}
else
{
if (waitTime - 1000000/FRAME_RATE > 15)
{
Uint32 delay = 10;
SDL_Delay(delay);
}
}
} // end main loop
SDL_Quit();
std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
std::cout << "Total run time: " << std::chrono::duration_cast<std::chrono::seconds>(now - start).count() << " seconds" << std::endl;
std::cout << "Update Count: " << updateCount << std::endl;
std::cout << "Average Frame rate: " << (updateCount / (std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count()/1000.0f)) << std::endl;
// all is well ;)
std::cout << "Exited cleanly" << std::endl;
return 0;
}