Mercurial > repos > blastem
comparison render_sdl.c @ 2663:568c1c22f3e3
Allow changing at least some settings in web build without breaking
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 06 Mar 2025 01:33:03 -0800 |
parents | c5c9498ff279 |
children | 36ae207af490 |
comparison
equal
deleted
inserted
replaced
2662:5e2d41f0d2ba | 2663:568c1c22f3e3 |
---|---|
1071 static int source_frame; | 1071 static int source_frame; |
1072 static int source_frame_count; | 1072 static int source_frame_count; |
1073 static int frame_repeat[60]; | 1073 static int frame_repeat[60]; |
1074 | 1074 |
1075 static uint32_t sample_rate; | 1075 static uint32_t sample_rate; |
1076 static void init_audio() | 1076 typedef struct { |
1077 int rate; | |
1078 int samples; | |
1079 uint16_t format; | |
1080 } audio_config; | |
1081 | |
1082 static audio_config get_audio_config(void) | |
1083 { | |
1084 audio_config ret; | |
1085 char * rate_str = tern_find_path(config, "audio\0rate\0", TVAL_PTR).ptrval; | |
1086 ret.rate = rate_str ? atoi(rate_str) : 0; | |
1087 if (!ret.rate) { | |
1088 ret.rate = 48000; | |
1089 } | |
1090 char *config_format = tern_find_path_default(config, "audio\0format\0", (tern_val){.ptrval="f32"}, TVAL_PTR).ptrval; | |
1091 ret.format = !strcmp(config_format, "s16") ? AUDIO_S16SYS : AUDIO_F32SYS; | |
1092 char * samples_str = tern_find_path(config, "audio\0buffer\0", TVAL_PTR).ptrval; | |
1093 ret.samples = samples_str ? atoi(samples_str) : 0; | |
1094 if (!ret.samples) { | |
1095 ret.samples = 512; | |
1096 } | |
1097 return ret; | |
1098 } | |
1099 | |
1100 static audio_config current_audio_config; | |
1101 static void init_audio(void) | |
1077 { | 1102 { |
1078 SDL_AudioSpec desired, actual; | 1103 SDL_AudioSpec desired, actual; |
1079 char * rate_str = tern_find_path(config, "audio\0rate\0", TVAL_PTR).ptrval; | 1104 audio_config ac = get_audio_config(); |
1080 int rate = rate_str ? atoi(rate_str) : 0; | 1105 desired.freq = ac.rate; |
1081 if (!rate) { | 1106 desired.format = ac.format; |
1082 rate = 48000; | |
1083 } | |
1084 desired.freq = rate; | |
1085 char *config_format = tern_find_path_default(config, "audio\0format\0", (tern_val){.ptrval="f32"}, TVAL_PTR).ptrval; | |
1086 desired.format = !strcmp(config_format, "s16") ? AUDIO_S16SYS : AUDIO_F32SYS; | |
1087 desired.channels = 2; | 1107 desired.channels = 2; |
1088 char * samples_str = tern_find_path(config, "audio\0buffer\0", TVAL_PTR).ptrval; | 1108 desired.samples = ac.samples * 2; |
1089 int samples = samples_str ? atoi(samples_str) : 0; | |
1090 if (!samples) { | |
1091 samples = 512; | |
1092 } | |
1093 debug_message("config says: %d\n", samples); | |
1094 desired.samples = samples*2; | |
1095 switch (sync_src) | 1109 switch (sync_src) |
1096 { | 1110 { |
1097 case SYNC_AUDIO: | 1111 case SYNC_AUDIO: |
1098 desired.callback = audio_callback; | 1112 desired.callback = audio_callback; |
1099 break; | 1113 break; |
1106 desired.userdata = NULL; | 1120 desired.userdata = NULL; |
1107 | 1121 |
1108 if (SDL_OpenAudio(&desired, &actual) < 0) { | 1122 if (SDL_OpenAudio(&desired, &actual) < 0) { |
1109 fatal_error("Unable to open SDL audio: %s\n", SDL_GetError()); | 1123 fatal_error("Unable to open SDL audio: %s\n", SDL_GetError()); |
1110 } | 1124 } |
1125 current_audio_config = ac; | |
1111 sample_rate = actual.freq; | 1126 sample_rate = actual.freq; |
1112 debug_message("Initialized audio at frequency %d with a %d sample buffer, ", actual.freq, actual.samples); | 1127 debug_message("Initialized %d channel audio at frequency %d with a %d sample buffer, ", actual.channels, actual.freq, actual.samples); |
1113 render_audio_format format = RENDER_AUDIO_UNKNOWN; | 1128 render_audio_format format = RENDER_AUDIO_UNKNOWN; |
1114 if (actual.format == AUDIO_S16SYS) { | 1129 if (actual.format == AUDIO_S16SYS) { |
1115 debug_message("signed 16-bit int format\n"); | 1130 debug_message("signed 16-bit int format\n"); |
1116 format = RENDER_AUDIO_S16; | 1131 format = RENDER_AUDIO_S16; |
1117 } else if (actual.format == AUDIO_F32SYS) { | 1132 } else if (actual.format == AUDIO_F32SYS) { |
1440 main_height = windowed_height; | 1455 main_height = windowed_height; |
1441 } | 1456 } |
1442 if (on_ui_fb_resized) { | 1457 if (on_ui_fb_resized) { |
1443 on_ui_fb_resized(); | 1458 on_ui_fb_resized(); |
1444 } | 1459 } |
1460 uint8_t old_sync_src = sync_src; | |
1445 | 1461 |
1446 window_setup(); | 1462 window_setup(); |
1447 update_aspect(); | 1463 update_aspect(); |
1448 #ifndef DISABLE_OPENGL | 1464 #ifndef DISABLE_OPENGL |
1449 //need to check render_gl again after window_setup as render option could have changed | 1465 //need to check render_gl again after window_setup as render option could have changed |
1450 if (render_gl && on_context_created) { | 1466 if (render_gl && on_context_created) { |
1451 on_context_created(); | 1467 on_context_created(); |
1452 } | 1468 } |
1453 #endif | 1469 #endif |
1454 | 1470 |
1455 uint8_t was_paused = SDL_GetAudioStatus() == SDL_AUDIO_PAUSED; | 1471 |
1456 render_close_audio(); | 1472 uint8_t was_paused = 1; |
1457 quitting = 0; | 1473 uint8_t do_audio_reinit = sync_src != old_sync_src; |
1458 init_audio(); | 1474 if (!do_audio_reinit) { |
1475 audio_config ac = get_audio_config(); | |
1476 do_audio_reinit = ac.rate != current_audio_config.rate || | |
1477 ac.samples != current_audio_config.samples || ac.format != current_audio_config.format; | |
1478 } | |
1479 if (do_audio_reinit) { | |
1480 was_paused = SDL_GetAudioStatus() == SDL_AUDIO_PAUSED; | |
1481 render_close_audio(); | |
1482 quitting = 0; | |
1483 init_audio(); | |
1484 } | |
1459 render_set_video_standard(video_standard); | 1485 render_set_video_standard(video_standard); |
1460 | 1486 |
1461 drain_events(); | 1487 drain_events(); |
1462 in_toggle = 0; | 1488 in_toggle = 0; |
1463 if (!was_paused) { | 1489 if (!was_paused) { |
1754 glVertexAttribPointer(extra->at_uv, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat[2]), (void *)0); | 1780 glVertexAttribPointer(extra->at_uv, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat[2]), (void *)0); |
1755 glEnableVertexAttribArray(extra->at_uv); | 1781 glEnableVertexAttribArray(extra->at_uv); |
1756 | 1782 |
1757 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, extra->gl_buffers[1]); | 1783 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, extra->gl_buffers[1]); |
1758 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, (void *)0); | 1784 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, (void *)0); |
1785 glDisableVertexAttribArray(extra->at_pos); | |
1786 glDisableVertexAttribArray(extra->at_uv); | |
1759 } | 1787 } |
1760 #endif | 1788 #endif |
1761 | 1789 |
1762 uint8_t render_static_image(uint8_t window, uint8_t *buffer, uint32_t size) | 1790 uint8_t render_static_image(uint8_t window, uint8_t *buffer, uint32_t size) |
1763 { | 1791 { |