Styling fixes, fixed bugs caused by a rebellious brace, removed debug prints

This commit is contained in:
Lior Halphon 2018-05-11 13:29:58 +03:00
parent 9a29beb189
commit 4527d9ee39

View File

@ -10,9 +10,9 @@
#ifndef _WIN32 #ifndef _WIN32
#define AUDIO_FREQUENCY 96000 #define AUDIO_FREQUENCY 96000
#else #else
/* LIJI32 says: Windows (well, at least my VM) can't handle 96KHz sound well :( /* Windows (well, at least my VM) can't handle 96KHz sound well :( */
felsqualle says: For SDL 2.0.6+ using the WASAPI driver, the highest freq. /* felsqualle says: For SDL 2.0.6+ using the WASAPI driver, the highest freq.
we can get is 48000. 96000 also works, but always has some faint crackling in we can get is 48000. 96000 also works, but always has some faint crackling in
the audio, no matter how high or low I set the buffer length... the audio, no matter how high or low I set the buffer length...
Not quite satisfied with that solution, because acc. to SDL2 docs, Not quite satisfied with that solution, because acc. to SDL2 docs,
@ -31,7 +31,7 @@ static char *filename = NULL;
static bool should_free_filename = false; static bool should_free_filename = false;
static char *battery_save_path_ptr; static char *battery_save_path_ptr;
SDL_AudioDeviceID deviceId; SDL_AudioDeviceID device_id;
void set_filename(const char *new_filename, bool new_should_free) void set_filename(const char *new_filename, bool new_should_free)
{ {
@ -141,13 +141,13 @@ static void handle_events(GB_gameboy_t *gb)
} }
else { else {
bool audio_playing = SDL_GetAudioDeviceStatus(deviceId) == SDL_AUDIO_PLAYING; bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING;
if (audio_playing) { if (audio_playing) {
SDL_PauseAudioDevice(deviceId, 1); SDL_PauseAudioDevice(device_id, 1);
} }
run_gui(true); run_gui(true);
if (!audio_playing) { if (!audio_playing) {
SDL_PauseAudioDevice(deviceId, 0); SDL_PauseAudioDevice(device_id, 0);
} }
GB_set_color_correction_mode(gb, configuration.color_correction_mode); GB_set_color_correction_mode(gb, configuration.color_correction_mode);
GB_set_highpass_filter_mode(gb, configuration.highpass_mode); GB_set_highpass_filter_mode(gb, configuration.highpass_mode);
@ -169,13 +169,13 @@ static void handle_events(GB_gameboy_t *gb)
case SDL_KEYDOWN: case SDL_KEYDOWN:
switch (event.key.keysym.scancode) { switch (event.key.keysym.scancode) {
case SDL_SCANCODE_ESCAPE: { case SDL_SCANCODE_ESCAPE: {
bool audio_playing = SDL_GetAudioDeviceStatus(deviceId) == SDL_AUDIO_PLAYING; bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING;
if (audio_playing) { if (audio_playing) {
SDL_PauseAudioDevice(deviceId, 1); SDL_PauseAudioDevice(device_id, 1);
} }
run_gui(true); run_gui(true);
if (!audio_playing) { if (!audio_playing) {
SDL_PauseAudioDevice(deviceId, 0); SDL_PauseAudioDevice(device_id, 0);
} }
GB_set_color_correction_mode(gb, configuration.color_correction_mode); GB_set_color_correction_mode(gb, configuration.color_correction_mode);
GB_set_highpass_filter_mode(gb, configuration.highpass_mode); GB_set_highpass_filter_mode(gb, configuration.highpass_mode);
@ -208,12 +208,13 @@ static void handle_events(GB_gameboy_t *gb)
break; break;
} }
#endif #endif
bool audio_playing = SDL_GetAudioDeviceStatus(deviceId) == SDL_AUDIO_PLAYING; bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING;
if (audio_playing) { if (audio_playing) {
SDL_PauseAudioDevice(deviceId, 1); SDL_PauseAudioDevice(device_id, 1);
} }
else if (!audio_playing) { else if (!audio_playing) {
SDL_PauseAudioDevice(deviceId, 0); SDL_PauseAudioDevice(device_id, 0);
}
} }
break; break;
@ -244,7 +245,6 @@ static void handle_events(GB_gameboy_t *gb)
} }
break; break;
} }
}
case SDL_KEYUP: // Fallthrough case SDL_KEYUP: // Fallthrough
if (event.key.keysym.scancode == configuration.keys[8]) { if (event.key.keysym.scancode == configuration.keys[8]) {
GB_set_turbo_mode(gb, event.type == SDL_KEYDOWN, false); GB_set_turbo_mode(gb, event.type == SDL_KEYDOWN, false);
@ -475,7 +475,6 @@ int main(int argc, char **argv)
want_aspec.freq = AUDIO_FREQUENCY; want_aspec.freq = AUDIO_FREQUENCY;
want_aspec.format = AUDIO_S16SYS; want_aspec.format = AUDIO_S16SYS;
want_aspec.channels = 2; want_aspec.channels = 2;
printf("SDL version is %d\n", SDL_COMPILEDVERSION);
#if SDL_COMPILEDVERSION >= 2005 && defined(__APPLE__) #if SDL_COMPILEDVERSION >= 2005 && defined(__APPLE__)
/* SDL 2.0.5 on macOS introduced a bug where certain combinations of buffer lengths and frequencies /* SDL 2.0.5 on macOS introduced a bug where certain combinations of buffer lengths and frequencies
fail to produce audio correctly. */ fail to produce audio correctly. */
@ -487,20 +486,18 @@ int main(int argc, char **argv)
#if SDL_COMPILEDVERSION >= 2006 && defined(_WIN32) #if SDL_COMPILEDVERSION >= 2006 && defined(_WIN32)
/* SDL 2.0.6 offers WASAPI support which allows for much lower audio buffer lengths which at least /* SDL 2.0.6 offers WASAPI support which allows for much lower audio buffer lengths which at least
theoretically reduces lagging. */ theoretically reduces lagging. */
printf("SDL 2.0.6+ detected, reducing audio buffer to 32 samples\n");
want_aspec.samples = 32; want_aspec.samples = 32;
#endif #endif
#if SDL_COMPILEDVERSION <= 2005 && defined(_WIN32) #if SDL_COMPILEDVERSION <= 2005 && defined(_WIN32)
/* Since WASAPI audio was introduced in SDL 2.0.6, we have to lower the audio frequency /* Since WASAPI audio was introduced in SDL 2.0.6, we have to lower the audio frequency
to 44100 because otherwise we would get garbled audio output.*/ to 44100 because otherwise we would get garbled audio output.*/
printf("Fallback: SDL 2.0.5 detected, lowering audio freqency to 44100\n");
want_aspec.freq = 44100; want_aspec.freq = 44100;
#endif #endif
want_aspec.callback = audio_callback; want_aspec.callback = audio_callback;
want_aspec.userdata = &gb; want_aspec.userdata = &gb;
deviceId = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); device_id = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
/* Start Audio */ /* Start Audio */
@ -526,7 +523,7 @@ int main(int argc, char **argv)
if (filename == NULL) { if (filename == NULL) {
run_gui(false); run_gui(false);
} }
SDL_PauseAudioDevice(deviceId, 0); SDL_PauseAudioDevice(device_id, 0);
run(); // Never returns run(); // Never returns
return 0; return 0;
} }