-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cpp
More file actions
117 lines (90 loc) · 2.21 KB
/
Application.cpp
File metadata and controls
117 lines (90 loc) · 2.21 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
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
#include "Application.h"
#include <iostream>
Application::Application()
: running(false)
{
}
void Application::run()
{
if (!initialize()) {
return;
}
Uint64 frameTime = SDL_GetPerformanceFrequency() / 1;
Uint64 time = SDL_GetPerformanceCounter();
// Display first frame
render();
while (running) {
frameCounter += 1;
handleEvents();
int u = 1;
update();
while ((SDL_GetPerformanceCounter() - time) < frameTime) {
++u;
update();
}
time = SDL_GetPerformanceCounter();
std::cout << "updates: " << u << std::endl;
render();
}
cleanup();
}
bool Application::initialize()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return false;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
window = SDL_CreateWindow("Evolution",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (!window) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return false;
}
glContext = SDL_GL_CreateContext(window);
if (!glContext) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return false;
}
std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;
std::cout << "Version: " << glGetString(GL_VERSION) << std::endl;
std::cout << "Shading Language Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
if (SDL_GL_SetSwapInterval(1) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
}
running = true;
frameCounter = 1;
updateCounter = 0;
glewExperimental = GL_TRUE;
glewInit();
//glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
return world.initialize();
}
void Application::handleEvents()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
}
void Application::update()
{
updateCounter += 1;
world.update();
}
void Application::render()
{
glClear(GL_COLOR_BUFFER_BIT);
world.render();
SDL_GL_SwapWindow(window);
}
void Application::cleanup()
{
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
SDL_Quit();
}