/* closes down the sound system */ void shutdown_sound() { if (!al_is_audio_installed()) return; al_destroy_thread(sound_update_thread); al_uninstall_timer(ping_timer); al_stop_samples(); al_destroy_sample(zap); al_destroy_sample(bang); al_destroy_sample(bigbang); al_destroy_sample(ping); if (!no_music) { al_uninstall_timer(music_timer); al_destroy_sample_instance(part_voice[0]); al_destroy_sample_instance(part_voice[1]); al_destroy_sample_instance(part_voice[2]); al_destroy_sample_instance(part_voice[3]); al_destroy_sample(sine); al_destroy_sample(square); al_destroy_sample(saw); al_destroy_sample(bd); al_destroy_sample(snare); al_destroy_sample(hihat); } }
bool SampleResource::load(void) { if (!al_is_audio_installed()) { debug_message("Skipped loading sample %s\n", filename.c_str()); return true; } sample_data = al_load_sample(filename.c_str()); if (!sample_data) { debug_message("Error loading sample %s\n", filename.c_str()); return false; } return true; }
bool Sound :: open(char *path2) { if( !al_is_audio_installed() ) { al_install_audio(); al_init_acodec_addon(); al_reserve_samples(MAX_SAMPLES); } path = (char *)malloc(StringLengh(path2) + 1); strcpy(path, path2); audio = al_load_sample(path); if(audio == NULL) return (false); return (true); }
bool StreamResource::load(void) { if (!al_is_audio_installed()) { debug_message("Skipped loading stream %s\n", filename.c_str()); return true; } stream = al_load_audio_stream(filename.c_str(), 4, 1024); if (!stream) { debug_message("Error creating stream\n"); return false; } al_set_audio_stream_playing(stream, false); al_set_audio_stream_playmode(stream, ALLEGRO_PLAYMODE_LOOP); al_attach_audio_stream_to_mixer(stream, al_get_default_mixer()); return true; }
/* plays a ping sound effect */ void sfx_ping(int times) { if (!al_is_audio_installed()) return; if (times) { if (times > 1) { ping_vol = 255; ping_freq = 500; } else { ping_vol = 128; ping_freq = 1000; } ping_count = times; play_sample(ping, ping_vol, 128, ping_freq, FALSE); al_start_timer(ping_timer); } else play_sample(ping, 255, 128, 500, FALSE); }
int main(int argc, char **argv){ if(argc==3){ if(strcmp(argv[1],"rand")==0 && strcmp(argv[1],"map/maps.txt")!=0){ map_aleatoire(argv[2], 1000,100); printf("map cree\n"); return 0; } } /* Creation de l ecran */ ALLEGRO_DISPLAY *display = NULL; /* Creation du timer */ ALLEGRO_TIMER *timer; /* Creation de la liste d'evenement */ ALLEGRO_EVENT_QUEUE *queue; bool redraw = true; /*bool boucle_jeu=true;*/ /* Initialisation d allegro */ if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_init_image_addon()) { fprintf(stderr, "failed to initialize allegro image addon!\n"); return -1; } if(!al_init_primitives_addon()) { fprintf(stderr, "failed to initialize allegro primitive addon!\n"); return -1; } if(!al_install_mouse()) { fprintf(stderr, "failed to initialize allegro mouse !\n"); return -1; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize allegro keyboard!\n"); return -1; } if(!al_install_joystick()){ fprintf(stderr, "failed to initialize allegro joystick!\n"); return -1; } if(!al_install_audio()) { fprintf(stderr, "failed to initialize allegro audio!\n"); /*return -1;*/ } if(!al_init_acodec_addon()){ fprintf(stderr, "failed to initialize allegro audio codec!\n"); /*return -1;*/ } if (!al_reserve_samples(2)){ fprintf(stderr, "failed to initialize allegro reserve sample 1!\n"); /*return -1;*/ } al_init_font_addon(); if(!al_init_ttf_addon()){ fprintf(stderr, "failed to initialize allegro ttf addon!\n"); return -1; } Game* game; /* Instantiation de l ecran */ display = al_create_display(DISPLAY_W,DISPLAY_H); if(!display) { fprintf(stderr, "failed to create display!\n"); return -1; } bool recommencer = true; while(recommencer){ recommencer = false; game = game_init(); al_hide_mouse_cursor(display); /* Instantiation de la liste d evenement */ queue = al_create_event_queue(); /* Instantiation du timer de jeu */ timer = al_create_timer(1.0/FPS); al_start_timer(timer); /* Enregistrement des evenement */ al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_joystick_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); /* ECRAN TITRE */ al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap_region(game->jeu->map->background,250,200,game->jeu->map->w*LENGTH_SPRITE,game->jeu->map->h*LENGTH_SPRITE,0,0,0); al_draw_text(game->jeu->font_jeu,al_map_rgb(0,0,0),280,150,ALLEGRO_ALIGN_LEFT,"Titre du jeu"); al_draw_text(game->jeu->font_jeu,al_map_rgb(0,0,0),280,190,ALLEGRO_ALIGN_LEFT,"Press enter"); if(al_is_audio_installed()){ ALLEGRO_SAMPLE* main_musique; main_musique = al_load_sample("musique/musique_titre.ogg"); if (!main_musique){ fprintf(stderr, "Erreur lors du chargement musique d'intro\n"); } al_play_sample(main_musique, 0.1, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL); } al_flip_display(); bool debut = false; Event* event_titre; ALLEGRO_EVENT event_titre_event; event_titre = event_init(EVENT_PERIPH_XBOX); while(debut != true){ al_wait_for_event(queue, &event_titre_event); if(event_get_ok_court(event_titre)){ debut = true; } event_update(event_titre,&event_titre_event); } if(al_is_audio_installed()){ al_stop_samples(); } /* Ecran de chargement du premier niveau */ char num_niveau[4]; sprintf(num_niveau,"%d",game->current_map_i); al_clear_to_color(al_map_rgb(0,0,0)); if(game->size == game->current_map_i){ al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),280,190,ALLEGRO_ALIGN_LEFT,"Loading final stage"); }else{ al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),300,190,ALLEGRO_ALIGN_LEFT,"Loading level "); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),440,190,ALLEGRO_ALIGN_LEFT,num_niveau); } al_flip_display(); sleep(2); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); /* Début de la boucle de jeu */ game->jeu->ev->son = true; map_set_frame(game->jeu->map,DISPLAY_W/2-200,100,200,DISPLAY_H-300); while(game->jeu->boucle_jeu){ game_next_map_test(game); ALLEGRO_EVENT event; al_wait_for_event(queue, &event); /* On demande a quitter le jeu */ if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE ){ game->jeu->boucle_jeu=false; } event_update(game->jeu->ev,&event); /* Evenement timer */ if(event.type == ALLEGRO_EVENT_TIMER){ redraw=true; } /* if(event.type == ALLEGRO_EVENT_DISPLAY_RESIZE){ al_acknowledge_resize(display); redraw = true; } */ /* affichage */ if(redraw && al_is_event_queue_empty(queue)){ redraw=false; game->jeu->boucle_jeu = jeu_update_event(game->jeu); jeu_draw(game->jeu); } } /* Ecran de fin du jeu */ if(game->success == true){ al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,100,ALLEGRO_ALIGN_CENTER,"Félicitation, vous avez terminés les premiers niveaux du jeu !"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,120,ALLEGRO_ALIGN_CENTER,"Ce jeu à été créé à l'occasion du premier projet de C de"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,140,ALLEGRO_ALIGN_CENTER,"la promotion 2016 de Télécom Nancy"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,160,ALLEGRO_ALIGN_CENTER,"par"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,180,ALLEGRO_ALIGN_CENTER,"Giannini Valentin et Eric Perlinski"); al_flip_display(); sleep(5); al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,150,ALLEGRO_ALIGN_CENTER,"Appuyez sur [ESC] pour quitter, ou sur [Entrer] pour recommencer"); al_flip_display(); }else{ al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,100,ALLEGRO_ALIGN_CENTER,"Vous avez épuisés toutes les vies de votre personnage !"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,120,ALLEGRO_ALIGN_CENTER,"Ce jeu à été créé à l'occasion du premier projet de C de"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,140,ALLEGRO_ALIGN_CENTER,"la promotion 2016 de Télécom Nancy"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,160,ALLEGRO_ALIGN_CENTER,"par"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,180,ALLEGRO_ALIGN_CENTER,"Gianini Valentin et Eric Perlinski"); al_flip_display(); sleep(5); al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,150,ALLEGRO_ALIGN_CENTER,"Appuyez sur [ESC] pour quitter, ou sur [Entrer] pour recommencer"); al_flip_display(); } /* TODO Chargement écran de fin */ bool action = false; Event* event_credits; ALLEGRO_EVENT event_credits_event; event_credits = event_init(EVENT_PERIPH_XBOX); while(action != true){ al_wait_for_event(queue, &event_credits_event); if(event_get_ok_court(event_credits)){ action = true; recommencer = true; }else if (event_get_menu(event_credits)){ action = true; recommencer = false; } event_update(event_credits,&event_credits_event); } } jeu_dest(game->jeu); al_destroy_display(display); return 0; }
/* plays a player explosion sound effect */ void sfx_explode_player() { if (al_is_audio_installed()) play_sample(bigbang, 255, 128, 1000, FALSE); }
/* plays a block explosion sound effect */ void sfx_explode_block() { if (al_is_audio_installed()) play_sample(bang, 224, 128, 400, FALSE); }
/* plays an alien explosion sound effect */ void sfx_explode_alien() { if (al_is_audio_installed()) play_sample(bang, 192, 128, 1000, FALSE); }
/* plays a shoot sound effect */ void sfx_shoot() { if (al_is_audio_installed()) play_sample(zap, 64, 128, 1000, FALSE); }
/* initialises the sound system */ void init_sound() { float f, osc1, osc2, freq1, freq2, vol, val; char *p; int len; int i; if (!al_is_audio_installed()) return; /* zap (firing sound) consists of multiple falling saw waves */ len = 8192; zap = create_sample_u8(22050, len); p = (char *)al_get_sample_data(zap); osc1 = 0; freq1 = 0.02; osc2 = 0; freq2 = 0.025; for (i=0; i<len; i++) { vol = (float)(len - i) / (float)len * 127; *p = 128 + (fmod(osc1, 1) + fmod(osc2, 1) - 1) * vol; osc1 += freq1; freq1 -= 0.000001; osc2 += freq2; freq2 -= 0.00000125; p++; } /* bang (explosion) consists of filtered noise */ len = 8192; bang = create_sample_u8(22050, len); p = (char *)al_get_sample_data(bang); val = 0; for (i=0; i<len; i++) { vol = (float)(len - i) / (float)len * 255; val = (val * 0.75) + (RAND * 0.25); *p = 128 + val * vol; p++; } /* big bang (explosion) consists of noise plus rumble */ len = 24576; bigbang = create_sample_u8(11025, len); p = (char *)al_get_sample_data(bigbang); val = 0; osc1 = 0; osc2 = 0; for (i=0; i<len; i++) { vol = (float)(len - i) / (float)len * 128; f = 0.5 + ((float)i / (float)len * 0.4); val = (val * f) + (RAND * (1-f)); *p = 128 + (val + (sin(osc1) + sin(osc2)) / 4) * vol; osc1 += 0.03; osc2 += 0.04; p++; } /* ping consists of two sine waves */ len = 8192; ping = create_sample_u8(22050, len); p = (char *)al_get_sample_data(ping); osc1 = 0; osc2 = 0; for (i=0; i<len; i++) { vol = (float)(len - i) / (float)len * 31; *p = 128 + (sin(osc1) + sin(osc2) - 1) * vol; osc1 += 0.2; osc2 += 0.3; p++; } ping_timer = al_install_timer(0.3); /* set up my lurvely music player :-) */ if (!no_music) { init_music(); al_start_timer(music_timer); } sound_update_thread = al_create_thread(sound_update_proc, NULL); al_start_thread(sound_update_thread); }
/* this code is sick */ static void init_music() { float vol, val; char *p; int i; if (!al_is_audio_installed()) return; /* sine waves (one straight and one with oscillator sync) for the bass */ sine = create_sample_u8(22050, 64); p = (char *)al_get_sample_data(sine); for (i=0; i<64; i++) { *p = 128 + (sin((float)i * M_PI / 32.0) + sin((float)i * M_PI / 12.0)) * 8.0; p++; } /* square wave for melody #1 */ square = create_sample_u8(22050, 64); p = (char *)al_get_sample_data(square); for (i=0; i<64; i++) { *p = (i < 32) ? 120 : 136; p++; } /* saw wave for melody #2 */ saw = create_sample_u8(22050, 64); p = (char *)al_get_sample_data(saw); for (i=0; i<64; i++) { *p = 120 + (i*4 & 255) / 16; p++; } /* bass drum */ bd = create_sample_u8(22050, 1024); p = (char *)al_get_sample_data(bd); for (i=0; i<1024; i++) { vol = (float)(1024-i) / 16.0; *p = 128 + (sin((float)i / 48.0) + sin((float)i / 32.0)) * vol; p++; } /* snare drum */ snare = create_sample_u8(22050, 3072); p = (char *)al_get_sample_data(snare); val = 0; for (i=0; i<3072; i++) { vol = (float)(3072-i) / 24.0; val = (val * 0.9) + (RAND * 0.1); *p = 128 + val * vol; p++; } /* hihat */ hihat = create_sample_u8(22050, 1024); p = (char *)al_get_sample_data(hihat); for (i=0; i<1024; i++) { vol = (float)(1024-i) / 192.0; *p = 128 + (sin((float)i / 4.2) + RAND) * vol; p++; } /* start up the player */ for (i=0; i<256; i++) freq_table[i] = (int)(350.0 * pow(2.0, (float)i/12.0)); for (i=0; i<NUM_PARTS; i++) { part_pos[i] = part_ptr[i]; part_time[i] = 0; } part_voice[0] = al_create_sample_instance(sine); part_voice[1] = al_create_sample_instance(square); part_voice[2] = al_create_sample_instance(saw); part_voice[3] = al_create_sample_instance(bd); al_attach_sample_instance_to_mixer(part_voice[0], al_get_default_mixer()); al_attach_sample_instance_to_mixer(part_voice[1], al_get_default_mixer()); al_attach_sample_instance_to_mixer(part_voice[2], al_get_default_mixer()); al_attach_sample_instance_to_mixer(part_voice[3], al_get_default_mixer()); al_set_sample_instance_playmode(part_voice[0], ALLEGRO_PLAYMODE_LOOP); al_set_sample_instance_playmode(part_voice[1], ALLEGRO_PLAYMODE_LOOP); al_set_sample_instance_playmode(part_voice[2], ALLEGRO_PLAYMODE_LOOP); al_set_sample_instance_playmode(part_voice[3], ALLEGRO_PLAYMODE_ONCE); al_set_sample_instance_gain(part_voice[0], 192/255.0); al_set_sample_instance_gain(part_voice[1], 192/255.0); al_set_sample_instance_gain(part_voice[2], 192/255.0); al_set_sample_instance_gain(part_voice[3], 255/255.0); al_set_sample_instance_pan(part_voice[0], PAN(128)); al_set_sample_instance_pan(part_voice[1], PAN(224)); al_set_sample_instance_pan(part_voice[2], PAN(32)); al_set_sample_instance_pan(part_voice[3], PAN(128)); music_timer = al_install_timer(ALLEGRO_BPS_TO_SECS(22)); }