Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_audio.h>
- #include <allegro5/allegro_acodec.h>
- int musicVol = 255;
- int musicEnable = 1;
- int soundVol = 255;
- int soundEnable = 1;
- ALLEGRO_DISPLAY *d;
- //audio stuff
- //Opening Theme
- ALLEGRO_SAMPLE *roetitle = NULL;
- ALLEGRO_SAMPLE_INSTANCE *music = NULL;
- //Sound Effects
- ALLEGRO_SAMPLE *smpl_tick = NULL;
- ALLEGRO_SAMPLE *smpl_hits = NULL;
- ALLEGRO_SAMPLE *smpl_hitns = NULL;
- ALLEGRO_SAMPLE *smpl_beam = NULL;
- ALLEGRO_SAMPLE *smpl_miss = NULL;
- ALLEGRO_SAMPLE *smpl_alert = NULL;
- ALLEGRO_SAMPLE *smpl_open= NULL;
- ALLEGRO_SAMPLE *smpl_error = NULL;
- ALLEGRO_SAMPLE *smpl_destr = NULL;
- ALLEGRO_SAMPLE *smpl_send = NULL;
- ALLEGRO_SAMPLE *smpl_ping = NULL;
- ALLEGRO_SAMPLE *smpl_shields = NULL;
- ALLEGRO_SAMPLE *smpl_received = NULL;
- ALLEGRO_SAMPLE *smpl_panel = NULL;
- ALLEGRO_SAMPLE *smpl_sdown = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_tick = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_hits = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_hitns = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_beam = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_miss = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_alert = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_open = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_error = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_destr = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_send = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_ping = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_shields = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_received = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_panel = NULL;
- ALLEGRO_SAMPLE_INSTANCE *sfx_sdown = NULL;
- ALLEGRO_MIXER *master_mixer = NULL;
- ALLEGRO_MIXER *music_mixer = NULL;
- ALLEGRO_MIXER *sfx_mixer = NULL;
- ALLEGRO_VOICE *voice = NULL;
- void startup()
- {
- if (!al_init()) {
- fprintf(stderr, "failed to initialize allegro!\n");
- exit(1);
- }
- if (!al_install_keyboard()) {
- fprintf(stderr, "Error installing keyboard.\n");
- exit(1);
- }
- if (!al_init_primitives_addon()) {
- fprintf(stderr, "Error installing prims.\n");
- exit(1);
- }
- if (!al_init_image_addon()) {
- fprintf(stderr, "Error installing image addon.\n");
- exit(1);
- }
- d = al_create_display(1024, 768);
- }
- /*!
- * \brief Loads music to be played
- */
- void MusicInit(void)
- {
- roetitle = al_load_sample("res/roetitle.ogg");
- music = al_create_sample_instance(roetitle);
- al_attach_sample_instance_to_mixer(music, music_mixer);
- al_attach_mixer_to_mixer(music_mixer, master_mixer);
- }
- /*!
- * \brief Terminate music driver
- * \note just stops the music - driver is unloded at game termination
- */
- void MusicTerm(void)
- {
- // al_set_sample_instance_playing(music, false);
- }
- /*!
- * \brief Stops music playback
- */
- void MusicStop(void)
- {
- al_set_sample_instance_playing(music, false);
- }
- /*!
- * \brief Returns MIDI mark
- * \return 0
- * \warning Not implmented - not used in game
- */
- int MusicMark(void)
- {
- return (0);
- }
- /*!
- * \brief Waits for music to stop playing
- */
- void MusicWait(void)
- {
- while (al_get_sample_instance_playing(music));
- }
- /*!
- * \brief Plays MIDI song
- * \param i Music number to play
- */
- void MusicPlay(int i)
- {
- float volume = 1;
- volume = musicVol / 255;
- if (musicEnable == 0) { volume = 0; }
- al_set_mixer_gain(music_mixer, volume);
- switch (i) {
- case 1:
- al_set_sample_instance_playing(music, true);
- }
- }
- /*!
- * \brief Stops MIDI song
- * \note Needs to be migrated to allegro
- * \warning Not implmented
- */
- void SoundStop(void)
- {
- printf("TODO: SoundStop()\n");
- }
- /*!
- * \brief Initialzes sound driver
- * \note Terminates if driver fails
- */
- void SoundInit(void)
- {
- if (!al_install_audio()) {
- fprintf(stderr, "failed to initialize audio!\n");
- exit(-1);
- }
- if (!al_init_acodec_addon()) {
- fprintf(stderr, "failed to initialize audio codecs!\n");
- exit(-1);
- }
- if (!al_reserve_samples(1)) {
- fprintf(stderr, "failed to reserve samples!\n");
- exit(-1);
- }
- music_mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
- sfx_mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
- master_mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
- voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
- al_attach_mixer_to_voice(master_mixer, voice);
- }
- /*!
- * \brief loads sound effects from HDD
- */
- void SoundLoad(void)
- {
- smpl_beam = al_load_sample("res/beam.ogg");
- sfx_beam = al_create_sample_instance(smpl_beam);
- al_attach_sample_instance_to_mixer(sfx_beam, sfx_mixer);
- smpl_tick = al_load_sample("res/tick.ogg");
- sfx_tick = al_create_sample_instance(smpl_tick);
- al_attach_sample_instance_to_mixer(sfx_tick, sfx_mixer);
- smpl_hits = al_load_sample("res\hits.ogg");
- sfx_hits = al_create_sample_instance(smpl_hits);
- al_attach_sample_instance_to_mixer(sfx_hits, sfx_mixer);
- smpl_hitns = al_load_sample("res\hitns.ogg");
- sfx_hitns = al_create_sample_instance(smpl_hitns);
- al_attach_sample_instance_to_mixer(sfx_hitns, sfx_mixer);
- smpl_miss = al_load_sample("res\miss.ogg");
- sfx_miss = al_create_sample_instance(smpl_miss);
- al_attach_sample_instance_to_mixer(sfx_miss, sfx_mixer);
- smpl_alert = al_load_sample("res\alert.ogg");
- sfx_alert = al_create_sample_instance(smpl_alert);
- al_attach_sample_instance_to_mixer(sfx_alert, sfx_mixer);
- smpl_open = al_load_sample("res\open.ogg");
- sfx_open = al_create_sample_instance(smpl_open);
- al_attach_sample_instance_to_mixer(sfx_open, sfx_mixer);
- smpl_error = al_load_sample("res\error.ogg");
- sfx_error = al_create_sample_instance(smpl_error);
- al_attach_sample_instance_to_mixer(sfx_tick, sfx_mixer);
- smpl_destr = al_load_sample("res\destr.ogg");
- sfx_destr = al_create_sample_instance(smpl_destr);
- al_attach_sample_instance_to_mixer(sfx_error, sfx_mixer);
- smpl_send = al_load_sample("res\send.ogg");
- sfx_send = al_create_sample_instance(smpl_send);
- al_attach_sample_instance_to_mixer(sfx_send, sfx_mixer);
- smpl_ping = al_load_sample("res\ping.ogg");
- sfx_ping = al_create_sample_instance(smpl_ping);
- al_attach_sample_instance_to_mixer(sfx_ping, sfx_mixer);
- smpl_shields = al_load_sample("res\shields.ogg");
- sfx_shields = al_create_sample_instance(smpl_shields);
- al_attach_sample_instance_to_mixer(sfx_shields, sfx_mixer);
- smpl_received = al_load_sample("res\received.ogg");
- sfx_received = al_create_sample_instance(smpl_received);
- al_attach_sample_instance_to_mixer(sfx_received, sfx_mixer);
- smpl_panel = al_load_sample("res\panel.ogg");
- sfx_panel = al_create_sample_instance(smpl_panel);
- al_attach_sample_instance_to_mixer(sfx_panel, sfx_mixer);
- smpl_sdown = al_load_sample("res\sdown.ogg");
- sfx_sdown = al_create_sample_instance(smpl_sdown);
- al_attach_sample_instance_to_mixer(sfx_sdown, sfx_mixer);
- al_attach_mixer_to_mixer(sfx_mixer, master_mixer);
- }
- /*!
- * \brief Plays back sound
- * \param i Sound to playback
- */
- void SoundPlay(int i)
- {
- float volume;
- volume = soundVol / 255;
- if (soundEnable == 0) { volume = 0; }
- al_set_mixer_gain(sfx_mixer, volume);
- switch (i) {
- case 2:
- al_set_sample_instance_playing(sfx_tick, true);
- break;
- case 3:
- al_set_sample_instance_playing(sfx_hits, true);
- break;
- case 4:
- al_set_sample_instance_playing(sfx_hitns, true);
- break;
- case 5:
- al_set_sample_instance_playing(sfx_beam, true);
- break;
- case 6:
- al_set_sample_instance_playing(sfx_miss, true);
- break;
- case 7:
- al_set_sample_instance_playing(sfx_alert, true);
- break;
- case 8:
- al_set_sample_instance_playing(sfx_open, true);
- break;
- case 9:
- al_set_sample_instance_playing(sfx_error, true);
- break;
- case 10:
- al_set_sample_instance_playing(sfx_destr, true);
- break;
- case 11:
- al_set_sample_instance_playing(sfx_send, true);
- break;
- case 13:
- al_set_sample_instance_playing(sfx_ping, true);
- break;
- case 14:
- al_set_sample_instance_playing(sfx_shields, true);
- break;
- case 15:
- al_set_sample_instance_playing(sfx_received, true);
- break;
- case 16:
- al_set_sample_instance_playing(sfx_panel, true);
- break;
- case 17:
- al_set_sample_instance_playing(sfx_sdown, true);
- break;
- default:
- return;
- }
- }
- int main(int argc, char **argv) {
- int test = 0;
- startup();
- SoundInit();
- SoundLoad();
- MusicInit();
- MusicPlay(1);
- al_rest(1);
- MusicStop();
- al_rest(1);
- for (test = 0; test < 20; test++)
- {
- SoundPlay(test);
- printf("Playing sound %d\n", test);
- al_rest(2);
- }
- while (1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement