This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
systemstub_sdl.cpp
446 lines (420 loc) · 10.5 KB
/
systemstub_sdl.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
/*
* Another World engine rewrite
* Copyright (C) 2004-2005 Gregory Montoir ([email protected])
*/
#include <SDL.h>
#include "graphics.h"
#include "systemstub.h"
#include "util.h"
struct SystemStub_SDL : SystemStub {
static const int kJoystickIndex = 0;
static const int kJoystickCommitValue = 16384;
static const float kAspectRatio;
int _w, _h;
float _aspectRatio[4];
SDL_Window *_window;
SDL_Renderer *_renderer;
SDL_GLContext _glcontext;
int _texW, _texH;
SDL_Texture *_texture;
SDL_Joystick *_joystick;
SDL_GameController *_controller;
int _screenshot;
SystemStub_SDL();
virtual ~SystemStub_SDL() {}
virtual void init(const char *title, const DisplayMode *dm);
virtual void fini();
virtual void prepareScreen(int &w, int &h, float ar[4]);
virtual void updateScreen();
virtual void setScreenPixels555(const uint16_t *data, int w, int h);
virtual void processEvents();
virtual void sleep(uint32_t duration);
virtual uint32_t getTimeStamp();
void setAspectRatio(int w, int h);
};
const float SystemStub_SDL::kAspectRatio = 16.f / 10.f;
SystemStub_SDL::SystemStub_SDL()
: _w(0), _h(0), _window(0), _renderer(0), _texW(0), _texH(0), _texture(0) {
}
void SystemStub_SDL::init(const char *title, const DisplayMode *dm) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
SDL_ShowCursor(SDL_DISABLE);
// SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
int windowW = 0;
int windowH = 0;
int flags = dm->opengl ? SDL_WINDOW_OPENGL : 0;
if (dm->mode == DisplayMode::WINDOWED) {
flags |= SDL_WINDOW_RESIZABLE;
windowW = dm->width;
windowH = dm->height;
} else {
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
_window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowW, windowH, flags);
SDL_GetWindowSize(_window, &_w, &_h);
if (dm->opengl) {
_glcontext = SDL_GL_CreateContext(_window);
} else {
_glcontext = 0;
_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 255);
SDL_RenderClear(_renderer);
}
_aspectRatio[0] = _aspectRatio[1] = 0.;
_aspectRatio[2] = _aspectRatio[3] = 1.;
if (dm->mode == DisplayMode::FULLSCREEN_AR) {
if (dm->opengl) {
setAspectRatio(_w, _h);
} else {
SDL_RenderSetLogicalSize(_renderer, 320, 200);
}
}
_joystick = 0;
_controller = 0;
if (SDL_NumJoysticks() > 0) {
#if SDL_COMPILEDVERSION >= SDL_VERSIONNUM(2,0,2)
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
#endif
if (SDL_IsGameController(kJoystickIndex)) {
_controller = SDL_GameControllerOpen(kJoystickIndex);
}
if (!_controller) {
_joystick = SDL_JoystickOpen(kJoystickIndex);
}
}
_screenshot = 1;
_dm = *dm;
}
void SystemStub_SDL::fini() {
if (_texture) {
SDL_DestroyTexture(_texture);
_texture = 0;
}
if (_joystick) {
SDL_JoystickClose(_joystick);
_joystick = 0;
}
if (_controller) {
SDL_GameControllerClose(_controller);
_controller = 0;
}
if (_renderer) {
SDL_DestroyRenderer(_renderer);
_renderer = 0;
}
if (_glcontext) {
SDL_GL_DeleteContext(_glcontext);
_glcontext = 0;
}
SDL_DestroyWindow(_window);
SDL_Quit();
}
void SystemStub_SDL::prepareScreen(int &w, int &h, float ar[4]) {
w = _w;
h = _h;
ar[0] = _aspectRatio[0];
ar[1] = _aspectRatio[1];
ar[2] = _aspectRatio[2];
ar[3] = _aspectRatio[3];
if (_renderer) {
SDL_RenderClear(_renderer);
}
}
void SystemStub_SDL::updateScreen() {
if (_renderer) {
SDL_RenderPresent(_renderer);
} else {
SDL_GL_SwapWindow(_window);
}
}
void SystemStub_SDL::setScreenPixels555(const uint16_t *data, int w, int h) {
if (_renderer) {
if (!_texture) {
_texture = SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_RGB555, SDL_TEXTUREACCESS_STREAMING, w, h);
if (!_texture) {
return;
}
_texW = w;
_texH = h;
}
assert(w <= _texW && h <= _texH);
SDL_Rect r;
r.w = w;
r.h = h;
if (w != _texW && h != _texH) {
r.x = (_texW - w) / 2;
r.y = (_texH - h) / 2;
} else {
r.x = 0;
r.y = 0;
}
SDL_UpdateTexture(_texture, &r, data, w * sizeof(uint16_t));
SDL_RenderCopy(_renderer, _texture, 0, 0);
}
}
void SystemStub_SDL::processEvents() {
SDL_Event ev;
while(SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_QUIT:
_pi.quit = true;
break;
case SDL_WINDOWEVENT:
if (ev.window.event == SDL_WINDOWEVENT_RESIZED) {
_w = _dm.width = ev.window.data1;
_h = _dm.height = ev.window.data2;
} else if (ev.window.event == SDL_WINDOWEVENT_CLOSE) {
_pi.quit = true;
}
break;
case SDL_KEYUP:
switch (ev.key.keysym.sym) {
case SDLK_LEFT:
_pi.dirMask &= ~PlayerInput::DIR_LEFT;
break;
case SDLK_RIGHT:
_pi.dirMask &= ~PlayerInput::DIR_RIGHT;
break;
case SDLK_UP:
_pi.dirMask &= ~PlayerInput::DIR_UP;
break;
case SDLK_DOWN:
_pi.dirMask &= ~PlayerInput::DIR_DOWN;
break;
case SDLK_SPACE:
case SDLK_RETURN:
_pi.action = false;
break;
case SDLK_LSHIFT:
case SDLK_RSHIFT:
_pi.jump = false;
break;
case SDLK_s:
_pi.screenshot = true;
break;
case SDLK_c:
_pi.code = true;
break;
case SDLK_p:
_pi.pause = true;
break;
case SDLK_ESCAPE:
case SDLK_AC_BACK:
_pi.back = true;
break;
case SDLK_AC_HOME:
_pi.quit = true;
break;
default:
break;
}
break;
case SDL_KEYDOWN:
if (ev.key.keysym.mod & KMOD_ALT) {
if (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_KP_ENTER) {
} else if (ev.key.keysym.sym == SDLK_x) {
_pi.quit = true;
}
break;
} else if (ev.key.keysym.mod & KMOD_CTRL) {
if (ev.key.keysym.sym == SDLK_f) {
_pi.fastMode = true;
}
break;
}
_pi.lastChar = ev.key.keysym.sym;
switch(ev.key.keysym.sym) {
case SDLK_LEFT:
_pi.dirMask |= PlayerInput::DIR_LEFT;
break;
case SDLK_RIGHT:
_pi.dirMask |= PlayerInput::DIR_RIGHT;
break;
case SDLK_UP:
_pi.dirMask |= PlayerInput::DIR_UP;
break;
case SDLK_DOWN:
_pi.dirMask |= PlayerInput::DIR_DOWN;
break;
case SDLK_SPACE:
case SDLK_RETURN:
_pi.action = true;
break;
case SDLK_LSHIFT:
case SDLK_RSHIFT:
_pi.jump = true;
break;
default:
break;
}
break;
case SDL_JOYHATMOTION:
if (_joystick) {
_pi.dirMask = 0;
if (ev.jhat.value & SDL_HAT_UP) {
_pi.dirMask |= PlayerInput::DIR_UP;
}
if (ev.jhat.value & SDL_HAT_DOWN) {
_pi.dirMask |= PlayerInput::DIR_DOWN;
}
if (ev.jhat.value & SDL_HAT_LEFT) {
_pi.dirMask |= PlayerInput::DIR_LEFT;
}
if (ev.jhat.value & SDL_HAT_RIGHT) {
_pi.dirMask |= PlayerInput::DIR_RIGHT;
}
}
break;
case SDL_JOYAXISMOTION:
if (_joystick) {
switch (ev.jaxis.axis) {
case 0:
_pi.dirMask &= ~(PlayerInput::DIR_RIGHT | PlayerInput::DIR_LEFT);
if (ev.jaxis.value > kJoystickCommitValue) {
_pi.dirMask |= PlayerInput::DIR_RIGHT;
} else if (ev.jaxis.value < -kJoystickCommitValue) {
_pi.dirMask |= PlayerInput::DIR_LEFT;
}
break;
case 1:
_pi.dirMask &= ~(PlayerInput::DIR_UP | PlayerInput::DIR_DOWN);
if (ev.jaxis.value > kJoystickCommitValue) {
_pi.dirMask |= PlayerInput::DIR_DOWN;
} else if (ev.jaxis.value < -kJoystickCommitValue) {
_pi.dirMask |= PlayerInput::DIR_UP;
}
break;
}
}
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
if (_joystick) {
_pi.action = (ev.jbutton.state == SDL_PRESSED);
}
break;
case SDL_CONTROLLERAXISMOTION:
if (_controller) {
switch (ev.caxis.axis) {
case SDL_CONTROLLER_AXIS_LEFTX:
case SDL_CONTROLLER_AXIS_RIGHTX:
if (ev.caxis.value < -kJoystickCommitValue) {
_pi.dirMask |= PlayerInput::DIR_LEFT;
} else {
_pi.dirMask &= ~PlayerInput::DIR_LEFT;
}
if (ev.caxis.value > kJoystickCommitValue) {
_pi.dirMask |= PlayerInput::DIR_RIGHT;
} else {
_pi.dirMask &= ~PlayerInput::DIR_RIGHT;
}
break;
case SDL_CONTROLLER_AXIS_LEFTY:
case SDL_CONTROLLER_AXIS_RIGHTY:
if (ev.caxis.value < -kJoystickCommitValue) {
_pi.dirMask |= PlayerInput::DIR_UP;
} else {
_pi.dirMask &= ~PlayerInput::DIR_UP;
}
if (ev.caxis.value > kJoystickCommitValue) {
_pi.dirMask |= PlayerInput::DIR_DOWN;
} else {
_pi.dirMask &= ~PlayerInput::DIR_DOWN;
}
break;
}
}
break;
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
if (_controller) {
const bool pressed = (ev.cbutton.state == SDL_PRESSED);
switch (ev.cbutton.button) {
case SDL_CONTROLLER_BUTTON_BACK:
_pi.back = pressed;
break;
case SDL_CONTROLLER_BUTTON_GUIDE:
_pi.code = pressed;
break;
case SDL_CONTROLLER_BUTTON_START:
_pi.pause = pressed;
break;
case SDL_CONTROLLER_BUTTON_DPAD_UP:
if (pressed) {
_pi.dirMask |= PlayerInput::DIR_UP;
} else {
_pi.dirMask &= ~PlayerInput::DIR_UP;
}
break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
if (pressed) {
_pi.dirMask |= PlayerInput::DIR_DOWN;
} else {
_pi.dirMask &= ~PlayerInput::DIR_DOWN;
}
break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
if (pressed) {
_pi.dirMask |= PlayerInput::DIR_LEFT;
} else {
_pi.dirMask &= ~PlayerInput::DIR_LEFT;
}
break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
if (pressed) {
_pi.dirMask |= PlayerInput::DIR_RIGHT;
} else {
_pi.dirMask &= ~PlayerInput::DIR_RIGHT;
}
break;
case SDL_CONTROLLER_BUTTON_A:
_pi.action = pressed;
break;
case SDL_CONTROLLER_BUTTON_B:
_pi.jump = pressed;
break;
}
}
break;
default:
break;
}
}
}
void SystemStub_SDL::sleep(uint32_t duration) {
SDL_Delay(duration);
}
uint32_t SystemStub_SDL::getTimeStamp() {
return SDL_GetTicks();
}
void SystemStub_SDL::setAspectRatio(int w, int h) {
const float currentAspectRatio = w / (float)h;
if (int(currentAspectRatio * 100) == int(kAspectRatio * 100)) {
_aspectRatio[0] = 0.f;
_aspectRatio[1] = 0.f;
_aspectRatio[2] = 1.f;
_aspectRatio[3] = 1.f;
return;
}
// pillar box
if (currentAspectRatio > kAspectRatio) {
const float inset = 1.f - kAspectRatio / currentAspectRatio;
_aspectRatio[0] = inset / 2;
_aspectRatio[1] = 0.f;
_aspectRatio[2] = 1.f - inset;
_aspectRatio[3] = 1.f;
return;
}
// letter box
if (currentAspectRatio < kAspectRatio) {
const float inset = 1.f - currentAspectRatio / kAspectRatio;
_aspectRatio[0] = 0.f;
_aspectRatio[1] = inset / 2;
_aspectRatio[2] = 1.f;
_aspectRatio[3] = 1.f - inset;
return;
}
}
SystemStub *SystemStub_SDL_create() {
return new SystemStub_SDL();
}