Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
195 lines (154 loc) · 5.18 KB
/
Copy pathmain.cpp
File metadata and controls
195 lines (154 loc) · 5.18 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
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
// +------------------------------------------------------------+
// | University Racer |
// | Projekt do PGR a GMU, FIT VUT v Brne, 2011 |
// +------------------------------------------------------------+
// | Autori: Tomáš Kimer, xkimer00@stud.fit.vutbr.cz |
// | Tomáš Sychra, xsychr03@stud.fit.vutbr.cz |
// | David Šabata, xsabat01@stud.fit.vutbr.cz |
// +------------------------------------------------------------+
#include "main.h"
#ifdef _DEBUG
#define new MYDEBUG_NEW
#endif
using namespace std;
// Pointer to active application instance, which will recieve and handle SDL events
BaseApp* application = NULL;
SDL_Surface * init(unsigned width, unsigned height, unsigned color, unsigned depth, unsigned stencil)
{
// Set OpenGL attributes
if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, color) < 0) throw SDL_Exception();
if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depth) < 0) throw SDL_Exception();
if(SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, stencil) < 0) throw SDL_Exception();
if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) throw SDL_Exception();
// Zapnout antialiasing
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
// Create window
SDL_Surface * screen = SDL_SetVideoMode(width, height, color, SDL_OPENGL | SDL_RESIZABLE);
if(screen == NULL) throw SDL_Exception();
SDL_WM_SetCaption("University Racer", "University Racer");
#ifndef USE_GLEE
// Inicializace glew
GLenum err = glewInit();
if (GLEW_OK != err) {
// Problem: glewInit failed, something is seriously wrong.
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
//fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
#endif
// Create the Game and set as current application
application = new Game;
// Call init code
application->onInit();
application->onWindowResized(width, height);
return screen;
}
// Simple main loop
void mainLoop()
{
// Window is not minimized
bool active = true;
GameTime gameTime;
gameTime.Init(SDL_GetTicks());
FpsCounter fpsCounter;
for(;;) // Infinite loop
{
SDL_Event event;
// Wait for event
if(SDL_WaitEvent(&event) == 0) throw SDL_Exception();
// Screen needs redraw
bool redraw = false;
// aktualizovat FPS
//updateFPS(SDL_GetTicks());
gameTime.Update(SDL_GetTicks());
fpsCounter.Update(gameTime);
ostringstream text;
text << "University Racer - Diff:" << gameTime << ", FPS - " << fpsCounter << "; " << "GPU Mem: " << ((Game *)application)->statsString();
SDL_WM_SetCaption(text.str().c_str(), NULL);
// Handle all waiting events
do
{
// Call proper event handlers
switch(event.type)
{
// Stop redraw when minimized
case SDL_ACTIVEEVENT :
if(event.active.state == SDL_APPACTIVE)
active = (event.active.gain != 0);
break;
// Set redraw flag
case SDL_VIDEOEXPOSE :
redraw = true;
break;
// End main loop
case SDL_QUIT :
return;
// Pass UI events to the app
case SDL_KEYDOWN :
case SDL_KEYUP :
case SDL_MOUSEMOTION :
case SDL_MOUSEBUTTONDOWN :
case SDL_MOUSEBUTTONUP :
case SDL_VIDEORESIZE :
application->handleEvent(event);
break;
// Do nothing
default :
break;
}
} while(SDL_PollEvent(&event) == 1);
// Optionally redraw window
if (active && redraw) application->onWindowRedraw(gameTime);
// immediately fire redraw event
SDL_Event nextFrameEvent;
nextFrameEvent.type = SDL_VIDEOEXPOSE;
if(SDL_PushEvent(&nextFrameEvent) < 0) throw SDL_Exception();
}
}
int main(int /*argc*/, char ** /*argv*/)
{
#ifdef _DEBUG
#ifdef _MSC_VER
// hlida stav heapu pri kazde de/alokaci
_CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF);
#endif
#endif
try {
// Init SDL - only video subsystem will be used
if(SDL_Init(SDL_INIT_VIDEO) < 0) throw SDL_Exception();
// initialize application
init(800, 600, 24, 24, 8);
// start the main loop
mainLoop();
// cleanup
delete application;
} catch(exception & ex) {
cout << "ERROR : " << ex.what() << endl;
system("pause");
return EXIT_FAILURE;
}
SDL_Quit();
#ifdef _DEBUG
#ifdef _MSC_VER
// vypise pripadne memory leaky
_CrtDumpMemoryLeaks();
#endif
#endif
return EXIT_SUCCESS;
}
const char * getGlErrorString(GLenum error)
{
#define ERROR(e) case e : return #e
switch(error)
{
ERROR(GL_NO_ERROR);
ERROR(GL_INVALID_ENUM);
ERROR(GL_INVALID_VALUE);
ERROR(GL_INVALID_OPERATION);
ERROR(GL_INVALID_FRAMEBUFFER_OPERATION);
ERROR(GL_OUT_OF_MEMORY);
default :
return "UNKNOWN_GL_ERROR";
}
#undef ERROR
}