Skip to content

Commit

Permalink
Fix issue liballeg#1547 for shooter demo: Segfault after failed audio…
Browse files Browse the repository at this point in the history
… 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?
  • Loading branch information
tstoeter committed Jun 18, 2024
1 parent 529adee commit 4aba9a2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
70 changes: 40 additions & 30 deletions demos/shooter/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions demos/shooter/title.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 4aba9a2

Please sign in to comment.