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
|
/*
Copyright 2013 Michael Pavone
This file is part of BlastEm.
BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "render.h"
#include "blastem.h"
#include "io.h"
#include "util.h"
#ifndef DISABLE_OPENGL
#include <GL/glew.h>
#endif
SDL_Window *main_window;
SDL_Renderer *main_renderer;
SDL_Texture *main_texture;
SDL_Rect main_clip;
SDL_GLContext *main_context;
int main_width, main_height, is_fullscreen;
uint8_t render_dbg = 0;
uint8_t debug_pal = 0;
uint8_t render_gl = 1;
uint8_t scanlines = 0;
uint32_t last_frame = 0;
uint32_t min_delay;
uint32_t frame_delay = 1000/60;
int16_t * current_psg = NULL;
int16_t * current_ym = NULL;
uint32_t buffer_samples, sample_rate;
uint32_t missing_count;
SDL_mutex * audio_mutex;
SDL_cond * audio_ready;
SDL_cond * psg_cond;
SDL_cond * ym_cond;
uint8_t quitting = 0;
void audio_callback(void * userdata, uint8_t *byte_stream, int len)
{
//puts("audio_callback");
int16_t * stream = (int16_t *)byte_stream;
int samples = len/(sizeof(int16_t)*2);
int16_t * psg_buf, * ym_buf;
uint8_t local_quit;
SDL_LockMutex(audio_mutex);
psg_buf = NULL;
ym_buf = NULL;
do {
if (!psg_buf) {
psg_buf = current_psg;
current_psg = NULL;
SDL_CondSignal(psg_cond);
}
if (!ym_buf) {
ym_buf = current_ym;
current_ym = NULL;
SDL_CondSignal(ym_cond);
}
if (!quitting && (!psg_buf || !ym_buf)) {
SDL_CondWait(audio_ready, audio_mutex);
}
} while(!quitting && (!psg_buf || !ym_buf));
local_quit = quitting;
SDL_UnlockMutex(audio_mutex);
if (!local_quit) {
for (int i = 0; i < samples; i++) {
*(stream++) = psg_buf[i] + *(ym_buf++);
*(stream++) = psg_buf[i] + *(ym_buf++);
}
}
}
void render_close_audio()
{
SDL_LockMutex(audio_mutex);
quitting = 1;
SDL_CondSignal(audio_ready);
SDL_UnlockMutex(audio_mutex);
SDL_CloseAudio();
}
static SDL_Joystick * joysticks[MAX_JOYSTICKS];
int render_width()
{
return main_width;
}
int render_height()
{
return main_height;
}
int render_fullscreen()
{
return is_fullscreen;
}
uint32_t render_map_color(uint8_t r, uint8_t g, uint8_t b)
{
return 255 << 24 | r << 16 | g << 8 | b;
}
#ifndef DISABLE_OPENGL
GLuint textures[3], buffers[2], vshader, fshader, program, un_textures[2], un_width, at_pos;
GLfloat vertex_data[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
};
const GLushort element_data[] = {0, 1, 2, 3};
GLuint load_shader(char * fname, GLenum shader_type)
{
char const * parts[] = {get_home_dir(), "/.config/blastem/shaders/", fname};
char * shader_path = alloc_concat_m(3, parts);
FILE * f = fopen(shader_path, "rb");
free(shader_path);
if (!f) {
parts[0] = get_exe_dir();
parts[1] = "/shaders/";
shader_path = alloc_concat_m(3, parts);
f = fopen(shader_path, "rb");
free(shader_path);
if (!f) {
warning("Failed to open shader file %s for reading\n", fname);
return 0;
}
}
long fsize = file_size(f);
GLchar * text = malloc(fsize);
if (fread(text, 1, fsize, f) != fsize) {
warning("Error reading from shader file %s\n", fname);
free(text);
return 0;
}
GLuint ret = glCreateShader(shader_type);
glShaderSource(ret, 1, (const GLchar **)&text, (const GLint *)&fsize);
free(text);
glCompileShader(ret);
GLint compile_status, loglen;
glGetShaderiv(ret, GL_COMPILE_STATUS, &compile_status);
if (!compile_status) {
glGetShaderiv(ret, GL_INFO_LOG_LENGTH, &loglen);
text = malloc(loglen);
glGetShaderInfoLog(ret, loglen, NULL, text);
warning("Shader %s failed to compile:\n%s\n", fname, text);
free(text);
glDeleteShader(ret);
return 0;
}
return ret;
}
#endif
void render_alloc_surfaces(vdp_context * context)
{
static uint8_t texture_init;
context->oddbuf = context->framebuf = malloc(512 * 256 * 4 * 2);
memset(context->oddbuf, 0, 512 * 256 * 4 * 2);
context->evenbuf = ((char *)context->oddbuf) + 512 * 256 * 4;
if (texture_init) {
return;
}
texture_init = 1;
#ifndef DISABLE_OPENGL
if (render_gl) {
glGenTextures(3, textures);
for (int i = 0; i < 3; i++)
{
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if (i < 2) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, i ? context->evenbuf : context->oddbuf);
} else {
uint32_t blank = 255 << 24;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_BGRA, GL_UNSIGNED_BYTE, &blank);
}
}
glGenBuffers(2, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(element_data), element_data, GL_STATIC_DRAW);
tern_val def = {.ptrval = "default.v.glsl"};
vshader = load_shader(tern_find_path_default(config, "video\0vertex_shader\0", def).ptrval, GL_VERTEX_SHADER);
def.ptrval = "default.f.glsl";
fshader = load_shader(tern_find_path_default(config, "video\0fragment_shader\0", def).ptrval, GL_FRAGMENT_SHADER);
program = glCreateProgram();
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glLinkProgram(program);
GLint link_status;
glGetProgramiv(program, GL_LINK_STATUS, &link_status);
if (!link_status) {
fputs("Failed to link shader program\n", stderr);
exit(1);
}
un_textures[0] = glGetUniformLocation(program, "textures[0]");
un_textures[1] = glGetUniformLocation(program, "textures[1]");
un_width = glGetUniformLocation(program, "width");
at_pos = glGetAttribLocation(program, "pos");
} else {
#endif
/* height=480 to fit interlaced output */
main_texture = SDL_CreateTexture(main_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 480);
#ifndef DISABLE_OPENGL
}
#endif
}
void render_free_surfaces(vdp_context *context)
{
free(context->framebuf);
}
char * caption = NULL;
static void render_quit()
{
render_close_audio();
#ifdef DISABLE_OPENGL
SDL_DestroyTexture(main_texture);
#endif
}
void render_init(int width, int height, char * title, uint32_t fps, uint8_t fullscreen)
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
fatal_error("Unable to init SDL: %s\n", SDL_GetError());
}
atexit(SDL_Quit);
printf("width: %d, height: %d\n", width, height);
uint32_t flags = 0;
if (fullscreen) {
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
SDL_DisplayMode mode;
//TODO: Multiple monitor support
SDL_GetCurrentDisplayMode(0, &mode);
//the SDL2 migration guide suggests setting width and height to 0 when using SDL_WINDOW_FULLSCREEN_DESKTOP
//but that doesn't seem to work right when using OpenGL, at least on Linux anyway
width = mode.w;
height = mode.h;
}
main_width = width;
main_height = height;
is_fullscreen = fullscreen;
render_gl = 0;
tern_val def = {.ptrval = "off"};
char *vsync = tern_find_path_default(config, "video\0vsync\0", def).ptrval;
#ifndef DISABLE_OPENGL
flags |= SDL_WINDOW_OPENGL;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
main_window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, flags);
if (!main_window) {
fatal_error("Unable to create SDL window: %s\n", SDL_GetError());
}
main_context = SDL_GL_CreateContext(main_window);
GLenum res = glewInit();
if (res != GLEW_OK) {
warning("Initialization of GLEW failed with code %d\n", res);
}
if (res == GLEW_OK && GLEW_VERSION_2_0) {
render_gl = 1;
if (!strcmp("tear", vsync)) {
if (SDL_GL_SetSwapInterval(-1) < 0) {
warning("late tear is not available (%s), using normal vsync\n", SDL_GetError());
vsync = "on";
} else {
vsync = NULL;
}
}
if (vsync) {
if (SDL_GL_SetSwapInterval(!strcmp("on", vsync)) < 0) {
warning("Failed to set vsync to %s: %s\n", vsync, SDL_GetError());
}
}
} else {
warning("OpenGL 2.0 is unavailable, falling back to SDL2 renderer\n");
#endif
flags = SDL_RENDERER_ACCELERATED;
if (!strcmp("on", vsync) || !strcmp("tear", vsync)) {
flags |= SDL_RENDERER_PRESENTVSYNC;
}
main_renderer = SDL_CreateRenderer(main_window, -1, flags);
if (!main_window || !main_renderer) {
fatal_error("unable to create SDL window: %s\n", SDL_GetError());
}
main_clip.x = main_clip.y = 0;
main_clip.w = width;
main_clip.h = height;
#ifndef DISABLE_OPENGL
}
#endif
SDL_GetWindowSize(main_window, &width, &height);
printf("Window created with size: %d x %d\n", width, height);
float src_aspect = 4.0/3.0;
float aspect = (float)width / height;
def.ptrval = "normal";
int stretch = fabs(aspect - src_aspect) > 0.01 && !strcmp(tern_find_path_default(config, "video\0aspect\0", def).ptrval, "stretch");
if (!stretch) {
#ifndef DISABLE_OPENGL
if (render_gl) {
for (int i = 0; i < 4; i++)
{
if (aspect > src_aspect) {
vertex_data[i*2] *= src_aspect/aspect;
} else {
vertex_data[i*2+1] *= aspect/src_aspect;
}
}
} else {
#endif
float scale_x = (float)width / 320.0;
float scale_y = (float)height / 240.0;
float scale = scale_x > scale_y ? scale_y : scale_x;
main_clip.w = 320.0 * scale;
main_clip.h = 240.0 * scale;
main_clip.x = (width - main_clip.w) / 2;
main_clip.y = (height - main_clip.h) / 2;
#ifndef DISABLE_OPENGL
}
#endif
}
def.ptrval = "off";
scanlines = !strcmp(tern_find_path_default(config, "video\0scanlines\0", def).ptrval, "on");
caption = title;
min_delay = 0;
for (int i = 0; i < 100; i++) {
uint32_t start = SDL_GetTicks();
SDL_Delay(1);
uint32_t delay = SDL_GetTicks()-start;
if (delay > min_delay) {
min_delay = delay;
}
}
if (!min_delay) {
min_delay = 1;
}
printf("minimum delay: %d\n", min_delay);
frame_delay = 1000/fps;
audio_mutex = SDL_CreateMutex();
psg_cond = SDL_CreateCond();
ym_cond = SDL_CreateCond();
audio_ready = SDL_CreateCond();
SDL_AudioSpec desired, actual;
char * rate_str = tern_find_path(config, "audio\0rate\0").ptrval;
int rate = rate_str ? atoi(rate_str) : 0;
if (!rate) {
rate = 48000;
}
desired.freq = rate;
desired.format = AUDIO_S16SYS;
desired.channels = 2;
char * samples_str = tern_find_path(config, "audio\0buffer\0").ptrval;
int samples = samples_str ? atoi(samples_str) : 0;
if (!samples) {
samples = 512;
}
printf("config says: %d\n", samples);
desired.samples = samples*2;
desired.callback = audio_callback;
desired.userdata = NULL;
if (SDL_OpenAudio(&desired, &actual) < 0) {
fatal_error("Unable to open SDL audio: %s\n", SDL_GetError());
}
buffer_samples = actual.samples;
sample_rate = actual.freq;
printf("Initialized audio at frequency %d with a %d sample buffer\n", actual.freq, actual.samples);
SDL_PauseAudio(0);
SDL_JoystickEventState(SDL_ENABLE);
atexit(render_quit);
}
void render_update_caption(char *title)
{
caption = title;
}
void render_context(vdp_context * context)
{
int width = context->regs[REG_MODE_4] & BIT_H40 ? 320.0f : 256.0f;
int height = 240;
last_frame = SDL_GetTicks();
#ifndef DISABLE_OPENGL
if (render_gl) {
glBindTexture(GL_TEXTURE_2D, textures[context->framebuf == context->oddbuf ? 0 : 1]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 320, 240, GL_BGRA, GL_UNSIGNED_BYTE, context->framebuf);;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glUniform1i(un_textures[0], 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[(context->regs[REG_MODE_4] & BIT_INTERLACE) ? 1 : scanlines ? 2 : 0]);
glUniform1i(un_textures[1], 1);
glUniform1f(un_width, width);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glVertexAttribPointer(at_pos, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat[2]), (void *)0);
glEnableVertexAttribArray(at_pos);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, (void *)0);
glDisableVertexAttribArray(at_pos);
SDL_GL_SwapWindow(main_window);
} else {
#endif
SDL_Rect area;
area.x = area.y = 0;
area.w = width;
area.h = height;
if (context->regs[REG_MODE_4] & BIT_INTERLACE) {
unsigned skip;
uint32_t *src = (uint32_t*)context->framebuf;
uint8_t *dst;
int i;
area.h *= 2;
SDL_LockTexture(main_texture, &area, (void**)&dst, &skip);
if (context->framebuf == context->evenbuf)
dst += skip;
skip *= 2;
for (i = 0; i < 240; ++i) {
memcpy(dst, src, width*sizeof(uint32_t));
src += 320;
dst += skip;
}
SDL_UnlockTexture(main_texture);
}
else /* possibly faster path for non-interlaced output */
SDL_UpdateTexture(main_texture, &area, context->framebuf, 320*sizeof(uint32_t));
SDL_RenderClear(main_renderer);
SDL_RenderCopy(main_renderer, main_texture, &area, &main_clip);
SDL_RenderPresent(main_renderer);
#ifndef DISABLE_OPENGL
}
#endif
if (context->regs[REG_MODE_4] & BIT_INTERLACE) {
context->framebuf = context->framebuf == context->oddbuf ? context->evenbuf : context->oddbuf;
}
}
void render_wait_quit(vdp_context * context)
{
SDL_Event event;
while(SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_LEFTBRACKET) {
render_dbg++;
if (render_dbg == 4) {
render_dbg = 0;
}
render_context(context);
} else if(event.key.keysym.sym == SDLK_RIGHTBRACKET) {
debug_pal++;
if (debug_pal == 4) {
debug_pal = 0;
}
}
break;
case SDL_QUIT:
return;
}
}
}
void render_debug_mode(uint8_t mode)
{
if (mode < 4) {
render_dbg = mode;
}
}
void render_debug_pal(uint8_t pal)
{
if (pal < 4) {
debug_pal = pal;
}
}
int find_joystick_index(SDL_JoystickID instanceID)
{
for (int i = 0; i < MAX_JOYSTICKS; i++) {
if (joysticks[i] && SDL_JoystickInstanceID(joysticks[i]) == instanceID) {
return i;
}
}
return -1;
}
int lowest_unused_joystick_index()
{
for (int i = 0; i < MAX_JOYSTICKS; i++) {
if (!joysticks[i]) {
return i;
}
}
return -1;
}
int32_t handle_event(SDL_Event *event)
{
switch (event->type) {
case SDL_KEYDOWN:
handle_keydown(event->key.keysym.sym);
break;
case SDL_KEYUP:
handle_keyup(event->key.keysym.sym);
break;
case SDL_JOYBUTTONDOWN:
handle_joydown(find_joystick_index(event->jbutton.which), event->jbutton.button);
break;
case SDL_JOYBUTTONUP:
handle_joyup(find_joystick_index(event->jbutton.which), event->jbutton.button);
break;
case SDL_JOYHATMOTION:
handle_joy_dpad(find_joystick_index(event->jbutton.which), event->jhat.hat, event->jhat.value);
break;
case SDL_JOYDEVICEADDED:
if (event->jdevice.which < MAX_JOYSTICKS) {
int index = lowest_unused_joystick_index();
if (index >= 0) {
SDL_Joystick * joy = joysticks[index] = SDL_JoystickOpen(event->jdevice.which);
if (joy) {
printf("Joystick %d added: %s\n", index, SDL_JoystickName(joy));
printf("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
}
}
}
break;
case SDL_JOYDEVICEREMOVED: {
int index = find_joystick_index(event->jdevice.which);
if (index >= 0) {
SDL_JoystickClose(joysticks[index]);
joysticks[index] = NULL;
printf("Joystick %d removed\n", index);
} else {
printf("Failed to find removed joystick with instance ID: %d\n", index);
}
break;
}
case SDL_MOUSEMOTION:
handle_mouse_moved(event->motion.which, event->motion.x, event->motion.y, event->motion.xrel, event->motion.yrel);
break;
case SDL_MOUSEBUTTONDOWN:
handle_mousedown(event->button.which, event->button.button);
break;
case SDL_MOUSEBUTTONUP:
handle_mouseup(event->button.which, event->button.button);
break;
case SDL_QUIT:
puts("");
exit(0);
}
return 0;
}
char * fps_caption = NULL;
uint32_t frame_counter = 0;
uint32_t start = 0;
#ifdef __ANDROID__
#define FPS_INTERVAL 10000
#else
#define FPS_INTERVAL 1000
#endif
int wait_render_frame(vdp_context * context, int frame_limit)
{
SDL_Event event;
int ret = 0;
while(SDL_PollEvent(&event)) {
ret = handle_event(&event);
}
if (frame_limit) {
//TODO: Adjust frame delay so we actually get 60 FPS rather than 62.5 FPS
uint32_t current = SDL_GetTicks();
uint32_t desired = last_frame + frame_delay;
if (current < desired) {
uint32_t delay = last_frame + frame_delay - current;
if (delay > min_delay) {
SDL_Delay((delay/min_delay)*min_delay);
}
while ((desired) >= SDL_GetTicks()) {
}
}
}
render_context(context);
frame_counter++;
if ((last_frame - start) > FPS_INTERVAL) {
if (start && (last_frame-start)) {
#ifdef __ANDROID__
info_message("%s - %.1f fps", caption, ((float)frame_counter) / (((float)(last_frame-start)) / 1000.0));
#else
if (!fps_caption) {
fps_caption = malloc(strlen(caption) + strlen(" - 100000000.1 fps") + 1);
}
sprintf(fps_caption, "%s - %.1f fps", caption, ((float)frame_counter) / (((float)(last_frame-start)) / 1000.0));
SDL_SetWindowTitle(main_window, fps_caption);
#endif
}
start = last_frame;
frame_counter = 0;
}
return ret;
}
void process_events()
{
SDL_Event event;
while(SDL_PollEvent(&event)) {
handle_event(&event);
}
}
void render_wait_psg(psg_context * context)
{
SDL_LockMutex(audio_mutex);
while (current_psg != NULL) {
SDL_CondWait(psg_cond, audio_mutex);
}
current_psg = context->audio_buffer;
SDL_CondSignal(audio_ready);
context->audio_buffer = context->back_buffer;
context->back_buffer = current_psg;
SDL_UnlockMutex(audio_mutex);
context->buffer_pos = 0;
}
void render_wait_ym(ym2612_context * context)
{
SDL_LockMutex(audio_mutex);
while (current_ym != NULL) {
SDL_CondWait(ym_cond, audio_mutex);
}
current_ym = context->audio_buffer;
SDL_CondSignal(audio_ready);
context->audio_buffer = context->back_buffer;
context->back_buffer = current_ym;
SDL_UnlockMutex(audio_mutex);
context->buffer_pos = 0;
}
void render_fps(uint32_t fps)
{
frame_delay = 1000/fps;
}
uint32_t render_audio_buffer()
{
return buffer_samples;
}
uint32_t render_sample_rate()
{
return sample_rate;
}
void render_errorbox(char *title, char *message)
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, message, NULL);
}
void render_warnbox(char *title, char *message)
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, title, message, NULL);
}
void render_infobox(char *title, char *message)
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, title, message, NULL);
}
|