-
Notifications
You must be signed in to change notification settings - Fork 2
/
winsys.cpp
496 lines (422 loc) · 13.2 KB
/
winsys.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* --------------------------------------------------------------------
EXTREME TUXRACER
Copyright (C) 1999-2001 Jasmin F. Patry (Tuxracer)
Copyright (C) 2010 Extreme Tuxracer Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
---------------------------------------------------------------------*/
#include "winsys.h"
#include "ogl.h"
#include "audio.h"
#include "game_ctrl.h"
#include "font.h"
#include "score.h"
#include "textures.h"
#define USE_JOYSTICK true
#define COLOURDEPTH_RED_SIZE 5
#define COLOURDEPTH_GREEN_SIZE 6
#define COLOURDEPTH_BLUE_SIZE 5
#define COLOURDEPTH_DEPTH_SIZE 16
#if defined(HAVE_GL_GLES1)
EGLDisplay g_eglDisplay = 0;
EGLConfig g_eglConfig = 0;
EGLContext g_eglContext = 0;
EGLSurface g_eglSurface = 0;
Display* g_x11Display = NULL;
static const EGLint g_configAttribs[] = {
EGL_RED_SIZE, COLOURDEPTH_RED_SIZE,
EGL_GREEN_SIZE, COLOURDEPTH_GREEN_SIZE,
EGL_BLUE_SIZE, COLOURDEPTH_BLUE_SIZE,
EGL_DEPTH_SIZE, COLOURDEPTH_DEPTH_SIZE,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE,
EGL_NONE
};
#endif
CWinsys Winsys;
CWinsys::CWinsys () {
window = NULL;
renderer = NULL;
for (int i=0; i<NUM_GAME_MODES; i++) {
modefuncs[i].init = NULL;
modefuncs[i].loop = NULL;
modefuncs[i].term = NULL;
modefuncs[i].keyb = NULL;
modefuncs[i].mouse = NULL;
modefuncs[i].motion = NULL;
modefuncs[i].jaxis = NULL;
modefuncs[i].jbutt = NULL;
}
new_mode = NO_MODE;
lasttick = 0;
joystick = NULL;
numJoysticks = 0;
joystick_active = false;
resolution[0] = MakeRes (0, 0);
resolution[1] = MakeRes (800, 600);
resolution[2] = MakeRes (1024, 768);
resolution[3] = MakeRes (1152, 864);
resolution[4] = MakeRes (1280, 960);
resolution[5] = MakeRes (1280, 1024);
resolution[6] = MakeRes (1360, 768);
resolution[7] = MakeRes (1400, 1050);
resolution[8] = MakeRes (1440, 900);
resolution[9] = MakeRes (1680, 1050);
auto_x_resolution = 800;
auto_y_resolution = 600;
clock_time = 0;
cur_time = 0;
lasttick = 0;
elapsed_time = 0;
remain_ticks = 0;
}
CWinsys::~CWinsys () {}
TScreenRes CWinsys::MakeRes (int width, int height) {
TScreenRes res;
res.width = width;
res.height = height;
return res;
}
TScreenRes CWinsys::GetResolution (int idx) {
if (idx < 0 || idx >= NUM_RESOLUTIONS) return MakeRes (800, 600);
return resolution[idx];
}
string CWinsys::GetResName (int idx) {
string line;
if (idx < 0 || idx >= NUM_RESOLUTIONS) return "800 x 600";
if (idx == 0) return ("auto");
line = Int_StrN (resolution[idx].width);
line += " x " + Int_StrN (resolution[idx].height);
return line;
}
double CWinsys::CalcScreenScale () {
double hh = (double)param.y_resolution;
if (hh < 768) return 0.78;
else if (hh == 768) return 1.0;
else return (hh / 768);
}
void CWinsys::SetupVideoMode (TScreenRes resolution) {
int bpp = 0;
switch (param.bpp_mode) {
case 0: bpp = 0; break;
case 1: bpp = 16; break;
case 2: bpp = 32; break;
default: param.bpp_mode = 0; bpp = 0;
}
Uint32 window_flags = window_flags = ( param.fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0 );
#if !defined(HAVE_GL_GLES1)
window_flags |= SDL_WINDOW_OPENGL;
#endif
window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, resolution.width, resolution.height, window_flags);
if (NULL == window) {
Message ("couldn't initialize video", SDL_GetError());
Message ("set to 800 x 600");
window = SDL_CreateWindow (WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, window_flags);
param.res_type = 1;
SaveConfigFile ();
}
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_GetWindowSize(window, ¶m.x_resolution, ¶m.y_resolution);
if (resolution.width == 0 && resolution.height == 0) {
auto_x_resolution = param.x_resolution;
auto_y_resolution = param.y_resolution;
}
param.scale = CalcScreenScale ();
if (param.use_quad_scale) param.scale = sqrt (param.scale);
}
void CWinsys::SetupVideoMode (int idx) {
if (idx < 0 || idx >= NUM_RESOLUTIONS) SetupVideoMode (MakeRes (800, 600));
else SetupVideoMode (resolution[idx]);
}
void CWinsys::SetupVideoMode (int width, int height) {
SetupVideoMode (MakeRes (width, height));
}
void CWinsys::InitJoystick () {
if (SDL_InitSubSystem (SDL_INIT_JOYSTICK) < 0) {
Message ("Could not initialize SDL_joystick: %s", SDL_GetError());
return;
}
numJoysticks = SDL_NumJoysticks ();
if (numJoysticks < 1) {
joystick = NULL;
return;
}
SDL_JoystickEventState (SDL_ENABLE);
joystick = SDL_JoystickOpen (0); // first stick with number 0
if (joystick == NULL) {
Message ("Cannot open joystick %s", SDL_GetError ());
return;
}
joystick_active = true;
}
void CWinsys::Init () {
Uint32 sdl_flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER;
if (SDL_Init (sdl_flags) < 0) Message ("Could not initialize SDL");
#if !defined(HAVE_GL_GLES1)
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
#if defined (USE_STENCIL_BUFFER)
SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
#endif
#endif
SetupVideoMode (GetResolution (param.res_type));
#if defined(HAVE_GL_GLES1)
// use EGL to initialise GLES
g_x11Display = XOpenDisplay(NULL);
if (!g_x11Display)
{
fprintf(stderr, "ERROR: unable to get display!n");
exit(-1);
}
g_eglDisplay = eglGetDisplay((EGLNativeDisplayType)g_x11Display);
if (g_eglDisplay == EGL_NO_DISPLAY)
{
printf("Unable to initialise EGL display.");
exit(-1);
}
// Initialise egl
if (!eglInitialize(g_eglDisplay, NULL, NULL))
{
printf("Unable to initialise EGL display.");
exit(-1);
}
// Find a matching config
EGLint numConfigsOut = 0;
if (eglChooseConfig(g_eglDisplay, g_configAttribs, &g_eglConfig, 1, &numConfigsOut) != EGL_TRUE || numConfigsOut == 0)
{
fprintf(stderr, "Unable to find appropriate EGL config.");
exit(-1);
}
// Get the SDL window handle
SDL_SysWMinfo sysInfo; //Will hold our Window information
SDL_VERSION(&sysInfo.version); //Set SDL version
if(SDL_GetWindowWMInfo(window, &sysInfo) <= 0)
{
printf("Unable to get window handle");
exit(-1);
}
g_eglSurface = eglCreateWindowSurface(g_eglDisplay, g_eglConfig, (EGLNativeWindowType)sysInfo.info.x11.window, 0);
if (g_eglSurface == EGL_NO_SURFACE)
{
printf("Unable to create EGL surface!");
exit(-1);
}
// Bind GLES and create the context
eglBindAPI(EGL_OPENGL_ES_API);
EGLint contextParams[] = {EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE}; // Use GLES version 1.x
g_eglContext = eglCreateContext(g_eglDisplay, g_eglConfig, NULL, contextParams);
if (g_eglContext == EGL_NO_CONTEXT)
{
printf("Unable to create GLES context!");
exit(-1);
}
if (eglMakeCurrent(g_eglDisplay, g_eglSurface, g_eglSurface, g_eglContext) == EGL_FALSE)
{
printf("Unable to make GLES context current");
exit(-1);
}
#else
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, COLOURDEPTH_RED_SIZE);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, COLOURDEPTH_GREEN_SIZE);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, COLOURDEPTH_BLUE_SIZE);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, COLOURDEPTH_DEPTH_SIZE);
#endif
context = SDL_GL_CreateContext(window);
Reshape (param.x_resolution, param.y_resolution);
KeyRepeat (false);
if (USE_JOYSTICK) InitJoystick ();
// SDL_EnableUNICODE (1);
}
void CWinsys::KeyRepeat (bool repeat) {
//int delay = ( repeat ? SDL_DEFAULT_REPEAT_DELAY : 0 );
//int interval = ( repeat ? SDL_DEFAULT_REPEAT_INTERVAL : 0 );
//SDL_EnableKeyRepeat (delay, interval);
}
void CWinsys::SetFonttype () {
const char* font = (param.use_papercut_font > 0 ? "pc20" : "bold");
FT.SetFont (font);
}
void CWinsys::CloseJoystick () {
if (joystick_active) SDL_JoystickClose (joystick);
}
void CWinsys::Quit () {
CloseJoystick ();
Tex.FreeTextureList ();
Course.FreeCourseList ();
Course.ResetCourse ();
SaveMessages ();
Audio.Close (); // frees music and sound as well
FT.Clear ();
if (g_game.argument < 1) Players.SavePlayers ();
Score.SaveHighScore ();
#if defined(HAVE_GL_GLES1)
eglMakeCurrent(g_eglDisplay, NULL, NULL, EGL_NO_CONTEXT);
eglDestroySurface(g_eglDisplay, g_eglSurface);
eglDestroyContext(g_eglDisplay, g_eglContext);
g_eglSurface = 0;
g_eglContext = 0;
g_eglConfig = 0;
eglTerminate(g_eglDisplay);
g_eglDisplay = 0;
XCloseDisplay(g_x11Display);
g_x11Display = NULL;
#endif
SDL_Quit ();
exit (0);
}
void CWinsys::PrintJoystickInfo () {
if (joystick_active == false) {
Message ("No joystick found");
return;
}
PrintStr ("");
PrintStr (SDL_JoystickName (0));
int num_buttons = SDL_JoystickNumButtons (joystick);
printf ("Joystick has %d button%s\n", num_buttons, num_buttons == 1 ? "" : "s");
int num_axes = SDL_JoystickNumAxes (joystick);
printf ("Joystick has %d ax%ss\n\n", num_axes, num_axes == 1 ? "i" : "e");
}
void CWinsys::SwapBuffers() {
#if defined(HAVE_GL_GLES1)
eglSwapBuffers(g_eglDisplay, g_eglSurface);
#else
SDL_GL_SwapWindow(window);
#endif
}
void CWinsys::Delay(unsigned int ms) {
SDL_Delay (ms);
}
// ------------ modes -------------------------------------------------
void CWinsys::SetModeFuncs (
TGameMode mode, TInitFuncN init, TLoopFuncN loop, TTermFuncN term,
TKeybFuncN keyb, TMouseFuncN mouse, TMotionFuncN motion,
TJAxisFuncN jaxis, TJButtFuncN jbutt, TKeybFuncS keyb_spec) {
modefuncs[mode].init = init;
modefuncs[mode].loop = loop;
modefuncs[mode].term = term;
modefuncs[mode].keyb = keyb;
modefuncs[mode].mouse = mouse;
modefuncs[mode].motion = motion;
modefuncs[mode].jaxis = jaxis;
modefuncs[mode].jbutt = jbutt;
modefuncs[mode].keyb_spec = keyb_spec;
}
void CWinsys::IdleFunc () {}
bool CWinsys::ModePending () {
return g_game.mode != new_mode;
}
void CWinsys::PollEvent () {
SDL_Event event;
SDL_Keysym sym;
unsigned int key, axis;
int x, y;
float val;
while (SDL_PollEvent (&event)) {
if (ModePending()) {
IdleFunc ();
} else {
switch (event.type) {
case SDL_KEYDOWN:
if (modefuncs[g_game.mode].keyb) {
SDL_GetMouseState (&x, &y);
key = event.key.keysym.sym;
(modefuncs[g_game.mode].keyb) (key, key >= 256, false, x, y);
} else if (modefuncs[g_game.mode].keyb_spec) {
sym = event.key.keysym;
(modefuncs[g_game.mode].keyb_spec) (sym, false);
}
break;
case SDL_KEYUP:
if (modefuncs[g_game.mode].keyb) {
SDL_GetMouseState (&x, &y);
key = event.key.keysym.sym;
(modefuncs[g_game.mode].keyb) (key, key >= 256, true, x, y);
} else if (modefuncs[g_game.mode].keyb_spec) {
sym = event.key.keysym;
(modefuncs[g_game.mode].keyb_spec) (sym, true);
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if (modefuncs[g_game.mode].mouse) {
(modefuncs[g_game.mode].mouse)
(event.button.button, event.button.state,
event.button.x, event.button.y);
}
break;
case SDL_MOUSEMOTION:
if (modefuncs[g_game.mode].motion)
(modefuncs[g_game.mode].motion) (event.motion.x, event.motion.y);
break;
case SDL_JOYAXISMOTION:
if (joystick_active) {
axis = event.jaxis.axis;
if (modefuncs[g_game.mode].jaxis && axis < 2) {
val = (float)event.jaxis.value / 32768;
(modefuncs[g_game.mode].jaxis) (axis, val);
}
}
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
if (joystick_active) {
if (modefuncs[g_game.mode].jbutt) {
(modefuncs[g_game.mode].jbutt)
(event.jbutton.button, event.jbutton.state);
}
}
break;
case SDL_WINDOWEVENT:
if (SDL_WINDOWEVENT_RESIZED == event.window.event) {
param.x_resolution = event.window.data1;
param.y_resolution = event.window.data2;
SetupVideoMode (param.res_type);
Reshape (event.window.data1, event.window.data2);
}
break;
case SDL_QUIT:
Quit ();
break;
}
}
}
}
void CWinsys::ChangeMode () {
// this function is called when new_mode is set
// terminate function of previous mode
if (g_game.mode >= 0 && modefuncs[g_game.mode].term != 0)
(modefuncs[g_game.mode].term) ();
g_game.prev_mode = g_game.mode;
// init function of new mode
if (modefuncs[new_mode].init != 0) {
(modefuncs[new_mode].init) ();
clock_time = SDL_GetTicks() * 1.e-3;
}
g_game.mode = new_mode;
// new mode is now the current mode.
}
void CWinsys::CallLoopFunction () {
cur_time = SDL_GetTicks() * 1.e-3;
g_game.time_step = cur_time - clock_time;
if (g_game.time_step < 0.0001) g_game.time_step = 0.0001;
clock_time = cur_time;
if (modefuncs[g_game.mode].loop != 0)
(modefuncs[g_game.mode].loop) (g_game.time_step);
}
void CWinsys::EventLoop () {
while (true) {
PollEvent ();
if (ModePending()) ChangeMode ();
CallLoopFunction ();
Delay (g_game.loopdelay);
}
}