forked from rroa/GeometryWars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SDLWrapper.cpp
356 lines (286 loc) · 9.29 KB
/
SDLWrapper.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "SDLWrapper.hpp"
#include "TimeManager.hpp"
#include "package.h"
// Constants
//
const float DESIRED_FRAME_RATE = 60.0f;
const float DESIRED_FRAME_TIME = 1.0f / DESIRED_FRAME_RATE;
namespace Geometry
{
SDLWrapper::SDLWrapper( const std::string& title, const int& width, const int& height )
: m_title ( title )
, m_width ( width )
, m_height ( height )
, m_nUpdates ( 0 )
, m_deltaTime ( 0.0 )
{
// Initializing the main window pointer
//
m_mainwindow = 0;
// Setting the Simulation state
//
m_state = SimulationState::RUNNING;
// Initializing members
//
m_timer = new TimeManager;
}
SDLWrapper::~SDLWrapper( )
{
OnCleanup( );
}
bool SDLWrapper::OnInit( )
{
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return false;
}
// Setting the windows title
//
SDL_WM_SetCaption( m_title.c_str(), "icon goes here." );
// Setting GL attributes
//
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32 );
SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 8 );
/*
* There's something about multi-sampling that my VM doesn't like
* SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
* SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 2 );
*/
// Creating the drawing canvas and GL context at the same time
// got to admit even though is a simple function call to the API
// this feels very, very, very, very weird. Like 1990's weird.
// http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlsetvideomode.html
//
/*
SDL_HWSURFACE - Create the video surface in video memory
SDL_GL_DOUBLEBUFFER - Enable hardware double buffering; only valid with SDL_HWSURFACE
SDL_OPENGL - Create an OpenGL rendering context.
*/
if(( m_mainwindow = SDL_SetVideoMode( m_width, m_height, 32,
SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL)) == NULL)
{
#if !EMSCRIPTEN
std::cerr << "Unable to set video mode: " << SDL_GetError() << std::endl;
#endif
return false;
}
GLenum glew_status = glewInit();
if (GLEW_OK != glew_status) {
#if !EMSCRIPTEN
std::cerr << stderr, "Error: %s\n", glewGetErrorString(glew_status);
#endif
return false;
}
if (!GLEW_VERSION_2_0) {
#if !EMSCRIPTEN
std::cerr << stderr, "No support for OpenGL 2.0 found\n";
#endif
return false;
}
GameRoot::getInstance()->onInitView();
return true;
}
void SDLWrapper::OnCleanup( )
{
// SDL Cleanup
//
SDL_FreeSurface( m_mainwindow );
SDL_Quit( );
delete m_timer;
m_timer = NULL;
}
void SDLWrapper::OnResize( int width, int height )
{
m_width = width;
m_height = height;
}
void SDLWrapper::OnExit( )
{
m_state = SimulationState::QUIT;
}
void SDLWrapper::OnExecute( )
{
// Frame starts
//
double startTime = m_timer->GetElapsedTime( );
// Input
//
OnInput( );
// Update
//
OnUpdate( );
// Draw
//
OnRender( );
// Time Management
//
double endTime = m_timer->GetElapsedTime( );
double nextTimeFrame = startTime + DESIRED_FRAME_TIME;
while( endTime < nextTimeFrame )
{
// Spin lock
//
endTime = m_timer->GetElapsedTime( );
}
// Frame ends
//
++m_nUpdates;
m_deltaTime = m_nUpdates / static_cast< double >( DESIRED_FRAME_RATE );
}
void SDLWrapper::OnInput( )
{
SDL_Event event;
if( SDL_PollEvent( &event ))
{
// Clean exit if window is closed
//
if( event.type == SDL_QUIT )
{
OnExit( );
}
switch( event.type )
{
case SDL_KEYDOWN:
OnKeyDown( event.key );
break;
case SDL_KEYUP:
OnKeyUp( event.key );
break;
case SDL_MOUSEMOTION:
OnMouseMove( event.motion.x,
event.motion.y,
event.motion.xrel,
event.motion.yrel,
( event.motion.state & SDL_BUTTON( SDL_BUTTON_LEFT )) != 0, // Left button clicked
( event.motion.state & SDL_BUTTON( SDL_BUTTON_RIGHT )) != 0, // Right button clicked
( event.motion.state & SDL_BUTTON( SDL_BUTTON_MIDDLE )) != 0 ); // Middle button clicked
break;
case SDL_MOUSEBUTTONDOWN:
HandleMouseButtonDownEvents( &event );
break;
case SDL_MOUSEBUTTONUP:
HandleMouseButtonUpEvents( &event );
break;
#if EMSCRIPTEN
case SDL_FINGERMOTION:
OnFingerMotion( &event );
break;
case SDL_FINGERDOWN:
OnFingerDown( &event );
break;
case SDL_FINGERUP:
OnFingerUp( &event );
break;
#endif
}
}
}
void SDLWrapper::HandleMouseButtonDownEvents( SDL_Event* event )
{
switch( event->button.button )
{
case SDL_BUTTON_LEFT:
OnLButtonDown( event->button.x, event->button.y );
break;
case SDL_BUTTON_RIGHT:
OnRButtonDown( event->button.x, event->button.y );
break;
case SDL_BUTTON_MIDDLE:
OnMButtonDown( event->button.x, event->button.y );
break;
}
}
void SDLWrapper::HandleMouseButtonUpEvents( SDL_Event* event )
{
switch( event->button.button )
{
case SDL_BUTTON_LEFT:
OnLButtonUp( event->button.x, event->button.y );
break;
case SDL_BUTTON_RIGHT:
OnRButtonUp(event->button.x, event->button.y);
break;
case SDL_BUTTON_MIDDLE:
OnMButtonUp(event->button.x, event->button.y);
break;
}
}
void SDLWrapper::OnMouseMove( int x, int y, int relx, int rely, bool left, bool right, bool middle )
{
Input::getInstance()->onTouch(tTouchEvent(tTouchEvent::kTouchMove,
tPoint2f(x, y), 0));
}
void SDLWrapper::OnLButtonDown( int x, int y)
{
Input::getInstance()->onTouch(tTouchEvent(tTouchEvent::kTouchBegin,
tPoint2f(x, y), 0));
}
void SDLWrapper::OnRButtonDown( int x, int y )
{
}
void SDLWrapper::OnMButtonDown( int x, int y )
{
}
void SDLWrapper::OnLButtonUp( int x, int y )
{
Input::getInstance()->onTouch(tTouchEvent(tTouchEvent::kTouchEnd,
tPoint2f(x, y), 0));
}
void SDLWrapper::OnRButtonUp( int x, int y )
{
}
void SDLWrapper::OnMButtonUp( int x, int y )
{
}
void SDLWrapper::OnFingerDown( SDL_Event* touchFingerEvent )
{
#if EMSCRIPTEN
SDL_TouchFingerEvent *t = (SDL_TouchFingerEvent*)touchFingerEvent;
#endif
}
void SDLWrapper::OnFingerUp( SDL_Event* touchFingerEvent )
{
#if EMSCRIPTEN
SDL_TouchFingerEvent *t = (SDL_TouchFingerEvent*)touchFingerEvent;
#endif
}
void SDLWrapper::OnFingerMotion( SDL_Event* touchFingerEvent )
{
#if EMSCRIPTEN
SDL_TouchFingerEvent *t = (SDL_TouchFingerEvent*)touchFingerEvent;
#endif
}
void SDLWrapper::OnLoop( )
{
}
void SDLWrapper::OnUpdate( )
{
}
void SDLWrapper::OnKeyDown( SDL_KeyboardEvent keyBoardEvent )
{
Input::getInstance()->onKeyboard(tKeyboardEvent(tKeyboardEvent::kKeyDown, keyBoardEvent.keysym.sym ));
}
void SDLWrapper::OnKeyUp( SDL_KeyboardEvent keyBoardEvent )
{
if( keyBoardEvent.keysym.sym == SDLK_ESCAPE )
{
OnExit( );
}
Input::getInstance()->onKeyboard(tKeyboardEvent(tKeyboardEvent::kKeyUp, keyBoardEvent.keysym.sym ));
}
void SDLWrapper::OnRender( )
{
GameRoot::getInstance()->onRedrawView( tTimer::getTimeMS() );
// Bringing the back buffer to the front
//
SDL_GL_SwapBuffers();
}
}