forked from cyxx/blues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_sdl2.c
773 lines (725 loc) · 19.5 KB
/
sys_sdl2.c
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
#include <SDL.h>
#include "sys.h"
#include "util.h"
#define COPPER_BARS_H 80
#define MAX_SPRITES 256
static const int FADE_STEPS = 16;
struct spritesheet_t {
int count;
SDL_Rect *r;
SDL_Surface *surface;
SDL_Texture *texture;
};
static struct spritesheet_t _spritesheets[RENDER_SPR_COUNT];
struct sprite_t {
int sheet;
int num;
int x, y;
bool xflip;
};
static struct sprite_t _sprites[MAX_SPRITES];
static int _sprites_count;
static SDL_Rect _sprites_cliprect;
static int _screen_w, _screen_h;
static int _shake_dx, _shake_dy;
static SDL_Window *_window;
static SDL_Renderer *_renderer;
static SDL_Texture *_texture;
static SDL_PixelFormat *_fmt;
static SDL_Palette *_palette;
static uint32_t _screen_palette[256];
static uint32_t *_screen_buffer;
static int _copper_color_key;
static uint32_t _copper_palette[COPPER_BARS_H];
static SDL_GameController *_controller;
static SDL_Joystick *_joystick;
static void sdl2_init() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
SDL_ShowCursor(SDL_DISABLE);
_screen_w = _screen_h = 0;
memset(_screen_palette, 0, sizeof(_screen_palette));
_palette = SDL_AllocPalette(256);
_screen_buffer = 0;
_copper_color_key = -1;
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
_controller = 0;
const int count = SDL_NumJoysticks();
if (count > 0) {
for (int i = 0; i < count; ++i) {
if (SDL_IsGameController(i)) {
_controller = SDL_GameControllerOpen(i);
if (_controller) {
fprintf(stdout, "Using controller '%s'\n", SDL_GameControllerName(_controller));
break;
}
}
}
if (!_controller) {
_joystick = SDL_JoystickOpen(0);
if (_joystick) {
fprintf(stdout, "Using joystick '%s'\n", SDL_JoystickName(_joystick));
}
}
}
}
static void sdl2_fini() {
if (_fmt) {
SDL_FreeFormat(_fmt);
_fmt = 0;
}
if (_palette) {
SDL_FreePalette(_palette);
_palette = 0;
}
if (_texture) {
SDL_DestroyTexture(_texture);
_texture = 0;
}
if (_renderer) {
SDL_DestroyRenderer(_renderer);
_renderer = 0;
}
if (_window) {
SDL_DestroyWindow(_window);
_window = 0;
}
free(_screen_buffer);
if (_controller) {
SDL_GameControllerClose(_controller);
_controller = 0;
}
if (_joystick) {
SDL_JoystickClose(_joystick);
_joystick = 0;
}
SDL_Quit();
}
static void sdl2_set_screen_size(int w, int h, const char *caption, int scale, const char *filter, bool fullscreen) {
assert(_screen_w == 0 && _screen_h == 0); // abort if called more than once
_screen_w = w;
_screen_h = h;
if (!filter || strcmp(filter, "nearest") == 0) {
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
} else if (strcmp(filter, "linear") == 0) {
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
} else {
print_warning("Unhandled filter '%s'", filter);
}
const int window_w = w * scale;
const int window_h = h * scale;
const int flags = fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
_window = SDL_CreateWindow(caption, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_w, window_h, flags);
_renderer = SDL_CreateRenderer(_window, -1, 0);
SDL_RenderSetLogicalSize(_renderer, w, h);
print_debug(DBG_SYSTEM, "set_screen_size %d,%d", _screen_w, _screen_h);
_screen_buffer = (uint32_t *)calloc(_screen_w * _screen_h, sizeof(uint32_t));
if (!_screen_buffer) {
print_error("Failed to allocate screen buffer");
}
static const uint32_t pfmt = SDL_PIXELFORMAT_RGB888;
_texture = SDL_CreateTexture(_renderer, pfmt, SDL_TEXTUREACCESS_STREAMING, _screen_w, _screen_h);
_fmt = SDL_AllocFormat(pfmt);
_sprites_cliprect.x = 0;
_sprites_cliprect.y = 0;
_sprites_cliprect.w = w;
_sprites_cliprect.h = h;
}
static uint32_t convert_amiga_color(uint16_t color) {
uint8_t r = (color >> 8) & 15;
r |= r << 4;
uint8_t g = (color >> 4) & 15;
g |= g << 4;
uint8_t b = color & 15;
b |= b << 4;
return SDL_MapRGB(_fmt, r, g, b);
}
static void set_amiga_color(uint16_t color, SDL_Color *p) {
const uint8_t r = (color >> 8) & 15;
p->r = (r << 4) | r;
const uint8_t g = (color >> 4) & 15;
p->g = (g << 4) | g;
const uint8_t b = color & 15;
p->b = (b << 4) | b;
}
static void sdl2_set_palette_amiga(const uint16_t *colors, int offset) {
SDL_Color *palette_colors = &_palette->colors[offset];
for (int i = 0; i < 16; ++i) {
_screen_palette[offset + i] = convert_amiga_color(colors[i]);
set_amiga_color(colors[i], &palette_colors[i]);
}
}
static void sdl2_set_copper_bars(const uint16_t *data) {
if (!data) {
_copper_color_key = -1;
} else {
_copper_color_key = (data[0] - 0x180) / 2;
const uint16_t *src = data + 1;
uint32_t *dst = _copper_palette;
for (int i = 0; i < COPPER_BARS_H / 5; ++i) {
const int j = i + 1;
*dst++ = convert_amiga_color(src[j]);
*dst++ = convert_amiga_color(src[i]);
*dst++ = convert_amiga_color(src[j]);
*dst++ = convert_amiga_color(src[i]);
*dst++ = convert_amiga_color(src[j]);
}
}
}
static void sdl2_set_screen_palette(const uint8_t *colors, int offset, int count, int depth) {
SDL_Color *palette_colors = &_palette->colors[offset];
const int shift = 8 - depth;
for (int i = 0; i < count; ++i) {
int r = *colors++;
int g = *colors++;
int b = *colors++;
if (depth != 8) {
r = (r << shift) | (r >> (depth - shift));
g = (g << shift) | (g >> (depth - shift));
b = (b << shift) | (b >> (depth - shift));
}
_screen_palette[offset + i] = SDL_MapRGB(_fmt, r, g, b);
palette_colors[i].r = r;
palette_colors[i].g = g;
palette_colors[i].b = b;
}
for (int i = 0; i < ARRAYSIZE(_spritesheets); ++i) {
struct spritesheet_t *sheet = &_spritesheets[i];
if (sheet->surface) {
SDL_DestroyTexture(sheet->texture);
sheet->texture = SDL_CreateTextureFromSurface(_renderer, sheet->surface);
}
}
}
static void sdl2_set_palette_color(int i, const uint8_t *colors) {
int r = colors[0];
r = (r << 2) | (r >> 4);
int g = colors[1];
g = (g << 2) | (g >> 4);
int b = colors[2];
b = (b << 2) | (b >> 4);
_screen_palette[i] = SDL_MapRGB(_fmt, r, g, b);
}
static void fade_palette_helper(int in) {
SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND);
SDL_Rect r;
r.x = r.y = 0;
SDL_GetRendererOutputSize(_renderer, &r.w, &r.h);
for (int i = 0; i <= FADE_STEPS; ++i) {
int alpha = 255 * i / FADE_STEPS;
if (in) {
alpha = 255 - alpha;
}
SDL_SetRenderDrawColor(_renderer, 0, 0, 0, alpha);
SDL_RenderClear(_renderer);
SDL_RenderCopy(_renderer, _texture, 0, 0);
SDL_RenderFillRect(_renderer, &r);
SDL_RenderPresent(_renderer);
SDL_Delay(30);
}
}
static void sdl2_fade_in_palette() {
if (!g_sys.input.quit) {
fade_palette_helper(1);
}
}
static void sdl2_fade_out_palette() {
if (!g_sys.input.quit) {
fade_palette_helper(0);
}
}
static void sdl2_transition_screen(enum sys_transition_e type, bool open) {
const int step_w = _screen_w / FADE_STEPS;
const int step_h = _screen_h / FADE_STEPS;
SDL_Rect r;
r.x = 0;
r.w = 0;
r.y = 0;
r.h = (type == TRANSITION_CURTAIN) ? _screen_h : 0;
do {
r.x = (_screen_w - r.w) / 2;
if (r.x < 0) {
r.x = 0;
}
r.w += step_w;
if (r.x + r.w > _screen_w) {
r.w = _screen_w - r.x;
}
if (type == TRANSITION_SQUARE) {
r.y = (_screen_h - r.h) / 2;
if (r.y < 0) {
r.y = 0;
}
r.h += step_h;
if (r.y + r.h > _screen_h) {
r.h = _screen_h - r.y;
}
}
SDL_RenderClear(_renderer);
SDL_RenderCopy(_renderer, _texture, &r, &r);
SDL_RenderPresent(_renderer);
SDL_Delay(30);
} while (r.x > 0 && (type == TRANSITION_CURTAIN || r.y > 0));
}
static void sdl2_copy_bitmap(const uint8_t *p, int w, int h) {
if (w != _screen_w || h != _screen_h) {
memset(_screen_buffer, 0, _screen_w * _screen_h * sizeof(uint32_t));
const int offset_x = (_screen_w - w) / 2;
const int offset_y = (_screen_h - h) / 2;
for (int j = 0; j < h; ++j) {
for (int i = 0; i < w; ++i) {
_screen_buffer[(offset_y + j) * _screen_w + (offset_x + i)] = _screen_palette[*p++];
}
}
} else if (_copper_color_key != -1) {
for (int j = 0; j < _screen_h; ++j) {
const int color = (j * 200 / _screen_h) / 2;
if (color < COPPER_BARS_H) {
const uint32_t line_color = _copper_palette[color];
for (int i = 0; i < _screen_w; ++i) {
_screen_buffer[j * _screen_w + i] = (p[i] == _copper_color_key) ? line_color : _screen_palette[p[i]];
}
} else {
for (int i = 0; i < _screen_w; ++i) {
_screen_buffer[j * _screen_w + i] = _screen_palette[p[i]];
}
}
p += _screen_w;
}
} else {
for (int i = 0; i < _screen_w * _screen_h; ++i) {
_screen_buffer[i] = _screen_palette[p[i]];
}
}
SDL_UpdateTexture(_texture, 0, _screen_buffer, _screen_w * sizeof(uint32_t));
}
static void sdl2_update_screen() {
SDL_Rect r;
r.x = _shake_dx;
r.y = _shake_dy;
r.w = _screen_w;
r.h = _screen_h;
SDL_RenderClear(_renderer);
SDL_RenderCopy(_renderer, _texture, 0, &r);
// sprites
SDL_RenderSetClipRect(_renderer, &_sprites_cliprect);
for (int i = 0; i < _sprites_count; ++i) {
const struct sprite_t *spr = &_sprites[i];
struct spritesheet_t *sheet = &_spritesheets[spr->sheet];
if (spr->num >= sheet->count) {
continue;
}
SDL_Rect r;
r.x = spr->x + _shake_dx;
r.y = spr->y + _shake_dy;
r.w = sheet->r[spr->num].w;
r.h = sheet->r[spr->num].h;
if (!spr->xflip) {
SDL_RenderCopy(_renderer, sheet->texture, &sheet->r[spr->num], &r);
} else {
SDL_RenderCopyEx(_renderer, sheet->texture, &sheet->r[spr->num], &r, 0., 0, SDL_FLIP_HORIZONTAL);
}
}
SDL_RenderSetClipRect(_renderer, 0);
SDL_RenderPresent(_renderer);
}
static void sdl2_shake_screen(int dx, int dy) {
_shake_dx = dx;
_shake_dy = dy;
}
static void handle_keyevent(int keysym, bool keydown, struct input_t *input, bool *paused) {
switch (keysym) {
case SDLK_LEFT:
if (keydown) {
input->direction |= INPUT_DIRECTION_LEFT;
} else {
input->direction &= ~INPUT_DIRECTION_LEFT;
}
break;
case SDLK_RIGHT:
if (keydown) {
input->direction |= INPUT_DIRECTION_RIGHT;
} else {
input->direction &= ~INPUT_DIRECTION_RIGHT;
}
break;
case SDLK_UP:
if (keydown) {
input->direction |= INPUT_DIRECTION_UP;
} else {
input->direction &= ~INPUT_DIRECTION_UP;
}
break;
case SDLK_DOWN:
if (keydown) {
input->direction |= INPUT_DIRECTION_DOWN;
} else {
input->direction &= ~INPUT_DIRECTION_DOWN;
}
break;
case SDLK_RETURN:
case SDLK_SPACE:
input->space = keydown;
break;
case SDLK_LSHIFT:
case SDLK_RSHIFT:
input->jump = keydown;
break;
case SDLK_ESCAPE:
if (keydown) {
g_sys.input.quit = true;
}
break;
case SDLK_1:
input->digit1 = keydown;
break;
case SDLK_2:
input->digit2 = keydown;
break;
case SDLK_3:
input->digit3 = keydown;
break;
case SDLK_p:
if (!keydown) {
*paused = !*paused;
}
break;
}
}
static void handle_controlleraxis(int axis, int value, struct input_t *input) {
static const int THRESHOLD = 3200;
switch (axis) {
case SDL_CONTROLLER_AXIS_LEFTX:
case SDL_CONTROLLER_AXIS_RIGHTX:
if (value < -THRESHOLD) {
input->direction |= INPUT_DIRECTION_LEFT;
} else {
input->direction &= ~INPUT_DIRECTION_LEFT;
}
if (value > THRESHOLD) {
input->direction |= INPUT_DIRECTION_RIGHT;
} else {
input->direction &= ~INPUT_DIRECTION_RIGHT;
}
break;
case SDL_CONTROLLER_AXIS_LEFTY:
case SDL_CONTROLLER_AXIS_RIGHTY:
if (value < -THRESHOLD) {
input->direction |= INPUT_DIRECTION_UP;
} else {
input->direction &= ~INPUT_DIRECTION_UP;
}
if (value > THRESHOLD) {
input->direction |= INPUT_DIRECTION_DOWN;
} else {
input->direction &= ~INPUT_DIRECTION_DOWN;
}
break;
}
}
static void handle_controllerbutton(int button, bool pressed, struct input_t *input, bool *paused) {
switch (button) {
case SDL_CONTROLLER_BUTTON_A:
input->space = pressed;
break;
case SDL_CONTROLLER_BUTTON_B:
input->jump = pressed;
break;
case SDL_CONTROLLER_BUTTON_X:
input->digit1 = pressed;
break;
case SDL_CONTROLLER_BUTTON_Y:
input->digit2 = pressed;
break;
case SDL_CONTROLLER_BUTTON_START:
if (!pressed) {
*paused = !*paused;
}
break;
case SDL_CONTROLLER_BUTTON_GUIDE:
input->quit = pressed;
break;
case SDL_CONTROLLER_BUTTON_DPAD_UP:
if (pressed) {
input->direction |= INPUT_DIRECTION_UP;
} else {
input->direction &= ~INPUT_DIRECTION_UP;
}
break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
if (pressed) {
input->direction |= INPUT_DIRECTION_DOWN;
} else {
input->direction &= ~INPUT_DIRECTION_DOWN;
}
break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
if (pressed) {
input->direction |= INPUT_DIRECTION_LEFT;
} else {
input->direction &= ~INPUT_DIRECTION_LEFT;
}
break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
if (pressed) {
input->direction |= INPUT_DIRECTION_RIGHT;
} else {
input->direction &= ~INPUT_DIRECTION_RIGHT;
}
break;
}
}
static void handle_joystickhatmotion(int value, struct input_t *input) {
input->direction = 0;
if (value & SDL_HAT_UP) {
input->direction |= INPUT_DIRECTION_UP;
}
if (value & SDL_HAT_DOWN) {
input->direction |= INPUT_DIRECTION_DOWN;
}
if (value & SDL_HAT_LEFT) {
input->direction |= INPUT_DIRECTION_LEFT;
}
if (value & SDL_HAT_RIGHT) {
input->direction |= INPUT_DIRECTION_RIGHT;
}
}
static void handle_joystickaxismotion(int axis, int value, struct input_t *input) {
static const int THRESHOLD = 3200;
switch (axis) {
case 0:
input->direction &= ~(INPUT_DIRECTION_RIGHT | INPUT_DIRECTION_LEFT);
if (value > THRESHOLD) {
input->direction |= INPUT_DIRECTION_RIGHT;
} else if (value < -THRESHOLD) {
input->direction |= INPUT_DIRECTION_LEFT;
}
break;
case 1:
input->direction &= ~(INPUT_DIRECTION_UP | INPUT_DIRECTION_DOWN);
if (value > THRESHOLD) {
input->direction |= INPUT_DIRECTION_DOWN;
} else if (value < -THRESHOLD) {
input->direction |= INPUT_DIRECTION_UP;
}
break;
}
}
static void handle_joystickbutton(int button, int pressed, struct input_t *input) {
switch (button) {
case 0:
input->space = pressed;
break;
case 1:
input->jump = pressed;
break;
}
}
static int handle_event(const SDL_Event *ev, bool *paused) {
switch (ev->type) {
case SDL_QUIT:
g_sys.input.quit = true;
break;
case SDL_WINDOWEVENT:
switch (ev->window.event) {
case SDL_WINDOWEVENT_FOCUS_GAINED:
case SDL_WINDOWEVENT_FOCUS_LOST:
*paused = (ev->window.event == SDL_WINDOWEVENT_FOCUS_LOST);
break;
}
break;
case SDL_KEYUP:
handle_keyevent(ev->key.keysym.sym, 0, &g_sys.input, paused);
break;
case SDL_KEYDOWN:
handle_keyevent(ev->key.keysym.sym, 1, &g_sys.input, paused);
break;
case SDL_CONTROLLERDEVICEADDED:
if (!_controller) {
_controller = SDL_GameControllerOpen(ev->cdevice.which);
if (_controller) {
fprintf(stdout, "Using controller '%s'\n", SDL_GameControllerName(_controller));
}
}
break;
case SDL_CONTROLLERDEVICEREMOVED:
if (_controller == SDL_GameControllerFromInstanceID(ev->cdevice.which)) {
fprintf(stdout, "Removed controller '%s'\n", SDL_GameControllerName(_controller));
SDL_GameControllerClose(_controller);
_controller = 0;
}
break;
case SDL_CONTROLLERBUTTONUP:
if (_controller) {
handle_controllerbutton(ev->cbutton.button, 0, &g_sys.input, paused);
}
break;
case SDL_CONTROLLERBUTTONDOWN:
if (_controller) {
handle_controllerbutton(ev->cbutton.button, 1, &g_sys.input, paused);
}
break;
case SDL_CONTROLLERAXISMOTION:
if (_controller) {
handle_controlleraxis(ev->caxis.axis, ev->caxis.value, &g_sys.input);
}
break;
case SDL_JOYHATMOTION:
if (_joystick) {
handle_joystickhatmotion(ev->jhat.value, &g_sys.input);
}
break;
case SDL_JOYAXISMOTION:
if (_joystick) {
handle_joystickaxismotion(ev->jaxis.axis, ev->jaxis.value, &g_sys.input);
}
break;
case SDL_JOYBUTTONUP:
if (_joystick) {
handle_joystickbutton(ev->jbutton.button, 0, &g_sys.input);
}
break;
case SDL_JOYBUTTONDOWN:
if (_joystick) {
handle_joystickbutton(ev->jbutton.button, 1, &g_sys.input);
}
break;
default:
return -1;
}
return 0;
}
static void sdl2_process_events() {
bool paused = false;
while (1) {
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
const bool prev = paused;
handle_event(&ev, &paused);
if (g_sys.input.quit) {
break;
}
if (prev != paused) {
SDL_PauseAudio(paused);
}
}
if (!paused) {
break;
}
SDL_Delay(100);
}
}
static void sdl2_sleep(int duration) {
SDL_Delay(duration);
}
static uint32_t sdl2_get_timestamp() {
return SDL_GetTicks();
}
static void sdl2_start_audio(sys_audio_cb callback, void *param) {
SDL_AudioSpec desired;
memset(&desired, 0, sizeof(desired));
desired.freq = SYS_AUDIO_FREQ;
desired.format = AUDIO_S16;
desired.channels = 1;
desired.samples = 2048;
desired.callback = callback;
desired.userdata = param;
if (SDL_OpenAudio(&desired, 0) == 0) {
SDL_PauseAudio(0);
}
}
static void sdl2_stop_audio() {
SDL_CloseAudio();
}
static void sdl2_lock_audio() {
SDL_LockAudio();
}
static void sdl2_unlock_audio() {
SDL_UnlockAudio();
}
static void render_load_sprites(int spr_type, int count, const struct sys_rect_t *r, const uint8_t *data, int w, int h, uint8_t color_key, bool update_pal) {
assert(spr_type < ARRAYSIZE(_spritesheets));
struct spritesheet_t *sheet = &_spritesheets[spr_type];
sheet->count = count;
sheet->r = (SDL_Rect *)malloc(count * sizeof(SDL_Rect));
for (int i = 0; i < count; ++i) {
SDL_Rect *rect = &sheet->r[i];
rect->x = r[i].x;
rect->y = r[i].y;
rect->w = r[i].w;
rect->h = r[i].h;
}
SDL_Surface *surface = SDL_CreateRGBSurface(0, w, h, 8, 0x0, 0x0, 0x0, 0x0);
SDL_SetSurfacePalette(surface, _palette);
SDL_SetColorKey(surface, 1, color_key);
SDL_LockSurface(surface);
for (int y = 0; y < h; ++y) {
memcpy(((uint8_t *)surface->pixels) + y * surface->pitch, data + y * w, w);
}
SDL_UnlockSurface(surface);
sheet->texture = SDL_CreateTextureFromSurface(_renderer, surface);
if (update_pal) { /* update texture on palette change */
sheet->surface = surface;
} else {
SDL_FreeSurface(surface);
}
}
static void render_unload_sprites(int spr_type) {
struct spritesheet_t *sheet = &_spritesheets[spr_type];
free(sheet->r);
if (sheet->surface) {
SDL_FreeSurface(sheet->surface);
}
if (sheet->texture) {
SDL_DestroyTexture(sheet->texture);
}
memset(sheet, 0, sizeof(struct spritesheet_t));
}
static void render_add_sprite(int spr_type, int frame, int x, int y, int xflip) {
assert(_sprites_count < ARRAYSIZE(_sprites));
struct sprite_t *spr = &_sprites[_sprites_count];
spr->sheet = spr_type;
spr->num = frame;
spr->x = x;
spr->y = y;
spr->xflip = xflip;
++_sprites_count;
}
static void render_clear_sprites() {
_sprites_count = 0;
}
static void render_set_sprites_clipping_rect(int x, int y, int w, int h) {
_sprites_cliprect.x = x;
_sprites_cliprect.y = y;
_sprites_cliprect.w = w;
_sprites_cliprect.h = h;
}
static void print_log(FILE *fp, const char *s) {
}
struct sys_t g_sys = {
.init = sdl2_init,
.fini = sdl2_fini,
.set_screen_size = sdl2_set_screen_size,
.set_screen_palette = sdl2_set_screen_palette,
.set_palette_amiga = sdl2_set_palette_amiga,
.set_copper_bars = sdl2_set_copper_bars,
.set_palette_color = sdl2_set_palette_color,
.fade_in_palette = sdl2_fade_in_palette,
.fade_out_palette = sdl2_fade_out_palette,
.copy_bitmap = sdl2_copy_bitmap,
.update_screen = sdl2_update_screen,
.shake_screen = sdl2_shake_screen,
.transition_screen = sdl2_transition_screen,
.process_events = sdl2_process_events,
.sleep = sdl2_sleep,
.get_timestamp = sdl2_get_timestamp,
.start_audio = sdl2_start_audio,
.stop_audio = sdl2_stop_audio,
.lock_audio = sdl2_lock_audio,
.unlock_audio = sdl2_unlock_audio,
.render_load_sprites = render_load_sprites,
.render_unload_sprites = render_unload_sprites,
.render_add_sprite = render_add_sprite,
.render_clear_sprites = render_clear_sprites,
.render_set_sprites_clipping_rect = render_set_sprites_clipping_rect,
.print_log = print_log,
};