use mixers for audio

This commit is contained in:
Sebastian Krzyszkowiak 2012-03-05 21:07:42 +01:00
parent 2f118db351
commit e4e848b175
9 changed files with 110 additions and 48 deletions

3
TODO
View file

@ -1,6 +1,7 @@
- moar documentation
- rewrite music handling into mixers
- control settings
- video settings
- playable levels :D
- get allegro 5.1 Windows build and use al_set_mixer_gain

View file

@ -36,7 +36,7 @@ void About_Draw(struct Game *game) {
}
void About_Load(struct Game *game) {
if (game->music) al_play_sample(game->about.sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
al_play_sample_instance(game->about.music);
ALLEGRO_EVENT ev;
int fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
@ -63,6 +63,11 @@ void About_Preload(struct Game *game) {
game->about.letter = LoadScaledBitmap("letter.png", al_get_display_width(game->display), al_get_display_height(game->display) );
game->about.sample = al_load_sample( "data/about.flac" );
game->about.music = al_create_sample_instance(game->about.sample);
al_attach_sample_instance_to_mixer(game->about.music, game->audio.music);
al_set_sample_instance_playmode(game->about.music, ALLEGRO_PLAYMODE_LOOP);
game->about.font = al_load_ttf_font("data/ShadowsIntoLight.ttf",al_get_display_height(game->display)*0.035,0 );
game->about.x = -0.1;
if (!game->about.sample){
@ -157,6 +162,7 @@ void About_Unload(struct Game *game) {
al_destroy_bitmap(game->about.letter);
al_destroy_bitmap(game->about.fade_bitmap);
al_destroy_bitmap(game->about.text_bitmap);
al_destroy_sample_instance(game->about.music);
al_destroy_sample(game->about.sample);
al_destroy_font(game->about.font);
}

View file

@ -29,7 +29,7 @@ void FillPage(struct Game *game, int page) {
//if (game->intro.audiostream) al_destroy_audio_stream(game->intro.audiostream);
game->intro.audiostream = al_load_audio_stream(filename, 4, 1024);
al_attach_audio_stream_to_mixer(game->intro.audiostream, al_get_default_mixer());
al_attach_audio_stream_to_mixer(game->intro.audiostream, game->audio.fx);
al_set_audio_stream_playing(game->intro.audiostream, false);
al_set_audio_stream_gain(game->intro.audiostream, 1.75);
@ -135,7 +135,7 @@ void Intro_Draw(struct Game *game) {
}
void Intro_Load(struct Game *game) {
if (game->music) al_play_sample(game->intro.sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
al_play_sample_instance(game->intro.music);
ALLEGRO_EVENT ev;
int fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
@ -177,6 +177,10 @@ void Intro_Preload(struct Game *game) {
game->intro.sample = al_load_sample( "data/intro.flac" );
game->intro.music = al_create_sample_instance(game->intro.sample);
al_attach_sample_instance_to_mixer(game->intro.music, game->audio.music);
al_set_sample_instance_playmode(game->intro.music, ALLEGRO_PLAYMODE_LOOP);
if (!game->intro.sample){
fprintf(stderr, "Audio clip sample not loaded!\n" );
exit(-1);
@ -206,6 +210,7 @@ void Intro_Unload(struct Game *game) {
}
al_destroy_bitmap(game->intro.table);
al_destroy_font(game->intro.font);
al_destroy_sample_instance(game->intro.music);
al_destroy_sample(game->intro.sample);
al_destroy_bitmap(game->intro.table_bitmap);
}

View file

@ -54,7 +54,7 @@ void Level_Draw(struct Game *game) {
}
void Level_Load(struct Game *game) {
if (game->music) al_play_sample_instance(game->level.music);
al_play_sample_instance(game->level.music);
ALLEGRO_EVENT ev;
int fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
@ -93,7 +93,7 @@ void Level_Preload(struct Game *game) {
}
game->level.music = al_create_sample_instance(game->level.sample);
al_attach_sample_instance_to_mixer(game->level.music, al_get_default_mixer());
al_attach_sample_instance_to_mixer(game->level.music, game->audio.music);
al_set_sample_instance_playmode(game->level.music, ALLEGRO_PLAYMODE_LOOP);
game->level.derpy = al_create_bitmap(al_get_display_width(game->display)*0.1953125, al_get_display_height(game->display)*0.25);
@ -128,5 +128,6 @@ void Level_Unload(struct Game *game) {
al_destroy_bitmap(game->level.derpy);
al_destroy_bitmap(game->level.derpy_walkcycle);
al_destroy_bitmap(game->level.fade_bitmap);
al_destroy_sample_instance(game->level.music);
al_destroy_sample(game->level.sample);
}

View file

@ -293,11 +293,11 @@ int main(int argc, char **argv){
return -1;
}
if (!al_reserve_samples(10)){
/* if (!al_reserve_samples(10)){
fprintf(stderr, "failed to reserve samples!\n");
return -1;
}
*/
al_init_font_addon();
if(!al_init_ttf_addon()){
@ -338,10 +338,12 @@ int main(int argc, char **argv){
}
game.audio.voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
game.audio.mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
game.audio.fx = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
game.audio.music = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
al_attach_mixer_to_voice(game.audio.fx, game.audio.voice);
al_attach_mixer_to_voice(game.audio.music, game.audio.voice);
al_attach_mixer_to_voice(game.audio.mixer, game.audio.voice);
if (game.fx) al_attach_mixer_to_mixer(game.audio.fx, game.audio.mixer);
if (game.music) al_attach_mixer_to_mixer(game.audio.music, game.audio.mixer);
al_register_event_source(game.event_queue, al_get_display_event_source(game.display));
al_register_event_source(game.event_queue, al_get_timer_event_source(game.timer));
@ -422,6 +424,7 @@ int main(int argc, char **argv){
al_destroy_font(game.font_console);
al_destroy_mixer(game.audio.fx);
al_destroy_mixer(game.audio.music);
al_destroy_mixer(game.audio.mixer);
al_destroy_voice(game.audio.voice);
al_uninstall_audio();
DeinitConfig();

View file

@ -75,6 +75,9 @@ struct Menu {
ALLEGRO_SAMPLE *sample;
ALLEGRO_SAMPLE *rain_sample;
ALLEGRO_SAMPLE *click_sample;
ALLEGRO_SAMPLE_INSTANCE *music; /*!< Sample instance with background music. */
ALLEGRO_SAMPLE_INSTANCE *rain_sound; /*!< Sample instance with rain sound. */
ALLEGRO_SAMPLE_INSTANCE *click; /*!< Sample instance with click sound. */
ALLEGRO_FONT *font_title;
ALLEGRO_FONT *font_subtitle;
ALLEGRO_FONT *font;
@ -106,6 +109,7 @@ struct About {
ALLEGRO_BITMAP *text_bitmap;
ALLEGRO_BITMAP *letter;
ALLEGRO_SAMPLE *sample;
ALLEGRO_SAMPLE_INSTANCE *music; /*!< Sample instance with background music. */
ALLEGRO_FONT *font;
float x;
};
@ -122,6 +126,8 @@ struct Map {
float arrowpos;
ALLEGRO_SAMPLE *sample;
ALLEGRO_SAMPLE *click_sample;
ALLEGRO_SAMPLE_INSTANCE *music; /*!< Sample instance with background music. */
ALLEGRO_SAMPLE_INSTANCE *click; /*!< Sample instance with click sound. */
};
/*! \brief Resources used by Intro state. */
@ -133,6 +139,7 @@ struct Intro {
ALLEGRO_BITMAP *table_bitmap;
ALLEGRO_FONT *font;
ALLEGRO_SAMPLE *sample;
ALLEGRO_SAMPLE_INSTANCE *music; /*!< Sample instance with background music. */
ALLEGRO_AUDIO_STREAM *audiostream;
};
@ -164,6 +171,7 @@ struct Game {
struct Pause pause;
struct {
ALLEGRO_VOICE *voice;
ALLEGRO_MIXER *mixer;
ALLEGRO_MIXER *music;
ALLEGRO_MIXER *fx;
} audio;

View file

@ -57,7 +57,7 @@ void Map_Draw(struct Game *game) {
}
void Map_Load(struct Game *game) {
if (game->music) al_play_sample(game->map.sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
al_play_sample_instance(game->map.music);
ALLEGRO_EVENT ev;
int fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
@ -72,14 +72,14 @@ void Map_Load(struct Game *game) {
int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if ((((game->map.selected<4) || (game->map.selected==6)) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) || ((game->map.selected>4) && (game->map.selected!=6) && (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT)) || ((game->map.selected==4) && (ev->keyboard.keycode==ALLEGRO_KEY_UP)) || ((game->map.selected==6) && (ev->keyboard.keycode==ALLEGRO_KEY_DOWN))) {
game->map.selected--;
if (game->fx) al_play_sample(game->map.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->map.click);
} else if (((game->map.selected<3) && (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT)) || ((game->map.selected==4) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) || ((game->map.selected==3) && (ev->keyboard.keycode==ALLEGRO_KEY_DOWN)) || ((game->map.selected==5) && (ev->keyboard.keycode==ALLEGRO_KEY_UP))) {
game->map.selected++;
if (game->fx) al_play_sample(game->map.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->map.click);
} else if ((ev->keyboard.keycode==ALLEGRO_KEY_LEFT) || (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT) || (ev->keyboard.keycode==ALLEGRO_KEY_UP) || (ev->keyboard.keycode==ALLEGRO_KEY_DOWN)) {
if (game->fx) al_play_sample(game->map.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->map.click);
} else if (ev->keyboard.keycode==ALLEGRO_KEY_ENTER) {
if (game->fx) al_play_sample(game->map.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->map.click);
game->level.current_level = game->map.selected;
PrintConsole(game, "Selecting level %d...", game->map.selected);
UnloadGameState(game);
@ -114,6 +114,14 @@ void Map_Preload(struct Game *game) {
game->map.click_sample = al_load_sample( "data/click.flac" );
game->map.sample = al_load_sample( "data/map.flac" );
game->map.music = al_create_sample_instance(game->map.sample);
al_attach_sample_instance_to_mixer(game->map.music, game->audio.music);
al_set_sample_instance_playmode(game->map.music, ALLEGRO_PLAYMODE_LOOP);
game->map.click = al_create_sample_instance(game->map.click_sample);
al_attach_sample_instance_to_mixer(game->map.click, game->audio.fx);
al_set_sample_instance_playmode(game->map.click, ALLEGRO_PLAYMODE_ONCE);
if (!game->map.sample){
fprintf(stderr, "Audio clip sample not loaded!\n" );
exit(-1);
@ -144,6 +152,8 @@ void Map_Unload(struct Game *game) {
al_destroy_bitmap(game->map.map_bg);
al_destroy_bitmap(game->map.highlight);
al_destroy_bitmap(game->map.arrow);
al_destroy_sample_instance(game->map.music);
al_destroy_sample(game->map.sample);
al_destroy_sample_instance(game->map.click);
al_destroy_sample(game->map.click_sample);
}

View file

@ -99,6 +99,18 @@ void Menu_Preload(struct Game *game) {
game->menu.click_sample = al_load_sample( "data/click.flac" );
game->menu.mountain_position = al_get_display_width(game->display)*0.7;
game->menu.music = al_create_sample_instance(game->menu.sample);
al_attach_sample_instance_to_mixer(game->menu.music, game->audio.music);
al_set_sample_instance_playmode(game->menu.music, ALLEGRO_PLAYMODE_LOOP);
game->menu.rain_sound = al_create_sample_instance(game->menu.rain_sample);
al_attach_sample_instance_to_mixer(game->menu.rain_sound, game->audio.fx);
al_set_sample_instance_playmode(game->menu.rain_sound, ALLEGRO_PLAYMODE_LOOP);
game->menu.click = al_create_sample_instance(game->menu.click_sample);
al_attach_sample_instance_to_mixer(game->menu.click, game->audio.fx);
al_set_sample_instance_playmode(game->menu.click, ALLEGRO_PLAYMODE_ONCE);
game->menu.font_title = al_load_ttf_font("data/ShadowsIntoLight.ttf",al_get_display_height(game->display)*0.16,0 );
game->menu.font_subtitle = al_load_ttf_font("data/ShadowsIntoLight.ttf",al_get_display_height(game->display)*0.08,0 );
game->menu.font = al_load_ttf_font("data/ShadowsIntoLight.ttf",al_get_display_height(game->display)*0.05,0 );
@ -153,7 +165,8 @@ void Menu_Stop(struct Game* game) {
DrawConsole(game);
al_flip_display();
}
al_stop_samples();
al_stop_sample_instance(game->menu.music);
al_stop_sample_instance(game->menu.rain_sound);
}
void Menu_Unload(struct Game *game) {
@ -171,23 +184,22 @@ void Menu_Unload(struct Game *game) {
al_destroy_font(game->menu.font_subtitle);
al_destroy_font(game->menu.font);
al_destroy_font(game->menu.font_selected);
al_destroy_sample_instance(game->menu.music);
al_destroy_sample_instance(game->menu.rain_sound);
al_destroy_sample_instance(game->menu.click);
al_destroy_sample(game->menu.sample);
al_destroy_sample(game->menu.rain_sample);
al_destroy_sample(game->menu.click_sample);
}
void play_samples(struct Game *game) {
if (game->music) al_play_sample(game->menu.sample, 0.8, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
if (game->fx) al_play_sample(game->menu.rain_sample, 0.7, -0.3, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
}
void Menu_Load(struct Game *game) {
game->menu.cloud_position = 100;
game->menu.cloud2_position = 100;
game->menu.options = false;
game->menu.selected = 0;
play_samples(game);
al_play_sample_instance(game->menu.music);
al_play_sample_instance(game->menu.rain_sound);
game->menu.menu_fade_bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display));
al_set_target_bitmap(game->menu.menu_fade_bitmap);
al_clear_to_color(al_map_rgb(0,0,0));
@ -208,36 +220,44 @@ void Menu_Load(struct Game *game) {
int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if (ev->keyboard.keycode==ALLEGRO_KEY_UP) {
game->menu.selected--;
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
} else if (ev->keyboard.keycode==ALLEGRO_KEY_DOWN) {
game->menu.selected++;
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
} else if ((!game->menu.options) && (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.selected==3)) || (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) {
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
return 1;
} else if ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (!game->menu.options) && (game->menu.selected==0)) {
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
UnloadGameState(game);
game->gamestate = GAMESTATE_LOADING;
game->loadstate = GAMESTATE_INTRO;
} else if ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (!game->menu.options) && (game->menu.selected==2)) {
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
UnloadGameState(game);
game->gamestate = GAMESTATE_LOADING;
game->loadstate = GAMESTATE_ABOUT;
} else if (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (!game->menu.options) && (game->menu.selected==1)) || (((game->menu.options) && ((ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) || (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.selected==3))))) {
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
game->menu.options=!game->menu.options;
game->menu.selected=0;
PrintConsole(game, "options state changed %d", game->menu.options);
} else if ((game->menu.options) && (game->menu.selected==2)) {
al_stop_samples();
if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); }
else if (game->fx) { game->music=1; game->fx=0; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "0"); }
else if (game->music) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); }
else { game->music=1; game->fx=1; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "1"); }
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
play_samples(game);
if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0");
al_detach_mixer(game->audio.music);
}
else if (game->fx) { game->music=1; game->fx=0; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "0");
al_attach_mixer_to_mixer(game->audio.music, game->audio.mixer);
al_detach_mixer(game->audio.fx);
}
else if (game->music) { game->music=0; SetConfigOption("SuperDerpy", "music", "0");
al_detach_mixer(game->audio.music);
}
else { game->music=1; game->fx=1; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "1");
al_attach_mixer_to_mixer(game->audio.fx, game->audio.mixer);
al_attach_mixer_to_mixer(game->audio.music, game->audio.mixer);
}
al_play_sample_instance(game->menu.click);
}
if (game->menu.selected==-1) game->menu.selected=3;
if (game->menu.selected==4) game->menu.selected=0;

View file

@ -37,26 +37,34 @@ int Pause_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
game->loadstate = GAMESTATE_MAP;
} else if (ev->keyboard.keycode==ALLEGRO_KEY_UP) {
game->pause.selected--;
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
} else if (ev->keyboard.keycode==ALLEGRO_KEY_DOWN) {
game->pause.selected++;
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
} else if ((!game->pause.options) && (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->pause.selected==3)) || (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) {
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
return 1;
} else if (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (!game->pause.options) && (game->pause.selected==2)) || (((game->pause.options) && ((ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) || (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->pause.selected==3))))) {
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
al_play_sample_instance(game->menu.click);
game->pause.options=!game->pause.options;
game->pause.selected=0;
PrintConsole(game, "options state changed %d", game->pause.options);
} else if ((game->pause.options) && (game->pause.selected==2)) {
al_stop_samples();
if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); }
else if (game->fx) { game->music=1; game->fx=0; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "0"); }
else if (game->music) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); }
else { game->music=1; game->fx=1; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "1"); }
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
//play_samples(game);
if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0");
al_detach_mixer(game->audio.music);
}
else if (game->fx) { game->music=1; game->fx=0; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "0");
al_attach_mixer_to_mixer(game->audio.music, game->audio.mixer);
al_detach_mixer(game->audio.fx);
}
else if (game->music) { game->music=0; SetConfigOption("SuperDerpy", "music", "0");
al_detach_mixer(game->audio.music);
}
else { game->music=1; game->fx=1; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "1");
al_attach_mixer_to_mixer(game->audio.fx, game->audio.mixer);
al_attach_mixer_to_mixer(game->audio.music, game->audio.mixer);
}
al_play_sample_instance(game->menu.click);
}
if (game->pause.selected==-1) game->pause.selected=3;
if (game->pause.selected==4) game->pause.selected=0;