From 4aba9a265d848abacd1445c180b8beb18a443934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20St=C3=B6ter?= Date: Tue, 18 Jun 2024 07:36:51 +0200 Subject: [PATCH] Fix issue #1547 for shooter demo: Segfault after failed audio subsystem installation on Linux. The segfault happened in the function calls `play_midi`, `al_stop_sample`, and `al_unlock_sample_id`, which have now been guarded by `if (al_is_audio_installed())`. Potentially move this check into the body of these functions? --- demos/shooter/game.c | 70 ++++++++++++++++++++++++------------------- demos/shooter/title.c | 6 ++-- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/demos/shooter/game.c b/demos/shooter/game.c index 0e3ce21e8c..269392cc23 100644 --- a/demos/shooter/game.c +++ b/demos/shooter/game.c @@ -132,26 +132,30 @@ static void move_everyone(void) if ((key[ALLEGRO_KEY_UP])) { // || (joy[0].stick[0].axis[1].d1)) { /* firing thrusters */ if (yspeed < MAX_SPEED) { - if (yspeed == 0) { - al_stop_sample(&engine); - al_play_sample(data[ENGINE_SPL].dat, 0.9, -1 + 2 * PAN(player_x_pos >> SPEED_SHIFT) / 255.0, - 1.0, ALLEGRO_PLAYMODE_LOOP, &engine); - } - else { - /* fade in sample while speeding up */ - ALLEGRO_SAMPLE_INSTANCE *si = al_lock_sample_id(&engine); - al_set_sample_instance_gain(si, yspeed * 64 / MAX_SPEED / 255.0); - al_set_sample_instance_pan(si, -1 + 2 * PAN(player_x_pos >> SPEED_SHIFT) / 255.0); - al_unlock_sample_id(&engine); - } + if (al_is_audio_installed()) { + if (yspeed == 0) { + al_stop_sample(&engine); + al_play_sample(data[ENGINE_SPL].dat, 0.9, -1 + 2 * PAN(player_x_pos >> SPEED_SHIFT) / 255.0, + 1.0, ALLEGRO_PLAYMODE_LOOP, &engine); + } + else { + /* fade in sample while speeding up */ + ALLEGRO_SAMPLE_INSTANCE *si = al_lock_sample_id(&engine); + al_set_sample_instance_gain(si, yspeed * 64 / MAX_SPEED / 255.0); + al_set_sample_instance_pan(si, -1 + 2 * PAN(player_x_pos >> SPEED_SHIFT) / 255.0); + al_unlock_sample_id(&engine); + } + } yspeed++; } else { - /* adjust pan while the sample is looping */ - ALLEGRO_SAMPLE_INSTANCE *si = al_lock_sample_id(&engine); - al_set_sample_instance_gain(si, 64 / 255.0); - al_set_sample_instance_pan(si, -1 + 2 * PAN(player_x_pos >> SPEED_SHIFT) / 255.0); - al_unlock_sample_id(&engine); + if (al_is_audio_installed()) { + /* adjust pan while the sample is looping */ + ALLEGRO_SAMPLE_INSTANCE *si = al_lock_sample_id(&engine); + al_set_sample_instance_gain(si, 64 / 255.0); + al_set_sample_instance_pan(si, -1 + 2 * PAN(player_x_pos >> SPEED_SHIFT) / 255.0); + al_unlock_sample_id(&engine); + } } ship_burn = TRUE; @@ -161,17 +165,19 @@ static void move_everyone(void) /* not firing thrusters */ if (yspeed) { yspeed--; - if (yspeed == 0) { - al_stop_sample(&engine); - } - else { - /* fade out and reduce frequency when slowing down */ - ALLEGRO_SAMPLE_INSTANCE *si = al_lock_sample_id(&engine); - al_set_sample_instance_gain(si, yspeed * 64 / MAX_SPEED / 255.0); - al_set_sample_instance_pan(si, -1 + 2 * PAN(player_x_pos >> SPEED_SHIFT) / 255.0); - al_set_sample_instance_speed(si, (500 + yspeed * 500 / MAX_SPEED) / 1000.0); - al_unlock_sample_id(&engine); - } + if (al_is_audio_installed()) { + if (yspeed == 0) { + al_stop_sample(&engine); + } + else { + /* fade out and reduce frequency when slowing down */ + ALLEGRO_SAMPLE_INSTANCE *si = al_lock_sample_id(&engine); + al_set_sample_instance_gain(si, yspeed * 64 / MAX_SPEED / 255.0); + al_set_sample_instance_pan(si, -1 + 2 * PAN(player_x_pos >> SPEED_SHIFT) / 255.0); + al_set_sample_instance_speed(si, (500 + yspeed * 500 / MAX_SPEED) / 1000.0); + al_unlock_sample_id(&engine); + } + } } ship_burn = FALSE; @@ -343,7 +349,10 @@ void play_game(void) /* introduction synced to the music */ draw_intro_item(INTRO_BMP_1, 5); - play_midi(data[GAME_MUSIC].dat, TRUE); + + if (al_is_audio_installed()) + play_midi(data[GAME_MUSIC].dat, TRUE); + clear_keybuf(); if (fade_intro_item(-1, 2)) @@ -447,7 +456,8 @@ void play_game(void) } /* cleanup */ - al_stop_sample(&engine); + if (al_is_audio_installed()) + al_stop_sample(&engine); while (bullet_list) delete_bullet(bullet_list); diff --git a/demos/shooter/title.c b/demos/shooter/title.c index 5125e72b44..eac9074106 100644 --- a/demos/shooter/title.c +++ b/demos/shooter/title.c @@ -274,8 +274,10 @@ int title_screen(void) text_pix = 0; text_width = 0; - play_midi(data[TITLE_MUSIC].dat, TRUE); - play_sample(data[WELCOME_SPL].dat, 255, 127, 1000, FALSE); + if (al_is_audio_installed()) { + play_midi(data[TITLE_MUSIC].dat, TRUE); + play_sample(data[WELCOME_SPL].dat, 255, 127, 1000, FALSE); + } load_credits();