void SoundManager::Update() { if(music==NULL) return; if(musicEnabled && !paused) al_set_audio_stream_playing(music, true); else if(!musicEnabled || paused) al_set_audio_stream_playing(music, false); al_set_audio_stream_gain(music, musicVolume); }
void stop_sound(sound_t* sound, bool rewind) { al_set_audio_stream_playing(sound->stream, false); if (rewind) al_rewind_audio_stream(sound->stream); }
bool AllegroMusicSample5::Stop() { if ( !m_pInstance ) return false; return al_set_audio_stream_playing( m_pInstance, false ); }
void Framework::StopMusic() { if( voice != 0 && musicStream != 0 ) { al_set_audio_stream_playing( musicStream, false ); } }
static int allua_audio_stream_set_playing(lua_State * L) { ALLUA_audio_stream audio_stream = allua_check_audio_stream(L, 1); float val = lua_toboolean(L, 2); lua_pushboolean(L, al_set_audio_stream_playing(audio_stream, val)); return 1; }
void removeStage(stage *s){ al_set_audio_stream_playing(s -> stageAudio, false); al_set_audio_stream_playing(s -> bossAudio, false); al_drain_audio_stream(s -> stageAudio); al_drain_audio_stream(s -> bossAudio); al_detach_audio_stream(s -> stageAudio); al_detach_audio_stream(s -> bossAudio); al_destroy_bitmap(s -> stageBackground); al_destroy_audio_stream(s -> stageAudio); al_destroy_audio_stream(s -> bossAudio); free(s); }
void Audio::PlayMusic( std::string Filename, bool Loop ) { if( audioVoice == 0 || audioMixer == 0 ) { return; } if( musicStream != nullptr ) { StopMusic(); } // Only play if Music is set if( FRAMEWORK->Settings->GetQuickBooleanValue( "Audio.Music", true ) ) { #ifdef WRITE_LOG fprintf( FRAMEWORK->LogFile, "Framework: Start audio file %s\n", Filename.c_str() ); #endif musicStream = al_load_audio_stream( Filename.c_str(), 4, 2048 ); if( musicStream != nullptr ) { al_attach_audio_stream_to_mixer( musicStream, audioMixer ); al_set_audio_stream_playmode( musicStream, ( Loop ? ALLEGRO_PLAYMODE_LOOP : ALLEGRO_PLAYMODE_ONCE ) ); FRAMEWORK->RegisterEventSource( al_get_audio_stream_event_source( musicStream ) ); al_set_audio_stream_playing( musicStream, true ); } else { #ifdef WRITE_LOG fprintf( FRAMEWORK->LogFile, "Framework: Could not load music '%s'\n", Filename.c_str() ); #endif } } }
void dumba5_pause_duh(DUMBA5_PLAYER * dp) { if (dp && dp->sigrenderer && (dp->flags & ADP_PLAYING)) { al_set_audio_stream_playing(dp->stream, false); dp->flags &= ~ADP_PLAYING; } }
bool reload_sound(sound_t* sound) { ALLEGRO_AUDIO_STREAM* new_stream; if (!(new_stream = al_load_audio_stream(sound->path, 4, 1024))) return false; if (sound->stream != NULL) { al_set_audio_stream_playing(sound->stream, false); al_destroy_audio_stream(sound->stream); } sound->stream = new_stream; al_set_audio_stream_gain(sound->stream, 1.0); al_attach_audio_stream_to_mixer(sound->stream, al_get_default_mixer()); al_set_audio_stream_playing(sound->stream, false); return true; }
bool GameLogic::LoadAssets() { Bitmap.Logo = al_load_bitmap("assets/images/daklutz.png"); Bitmap.Title = al_load_bitmap("assets/images/Bitmap.Title.png"); if(!Bitmap.Title || !Bitmap.Logo) { fprintf(stderr, "Failed to load image files!\n"); return false; } if(Debug) { fprintf(stderr, "Bitmaps loaded.\n"); } // actually load the font, I loaded the same font at different sizes. Font.Small = al_load_font("assets/fonts/Geo-Regular.ttf", 24, 0); Font.Medium = al_load_font("assets/fonts/Geo-Regular.ttf", 38, 0); Font.Big = al_load_font("assets/fonts/Geo-Regular.ttf", 70, 0); if(!Font.Small || !Font.Medium || !Font.Big) { fprintf(stderr, "Failed to load fonts!\n"); return false; } if (!al_reserve_samples(2)) { fprintf(stderr, "Could not reserve samples!\n"); return false; } if(Debug) { fprintf(stderr, "Samples reserved.\n"); } Sound.HitWall = al_load_sample("assets/sounds/wall_collision.ogg"); Sound.HitPaddle = al_load_sample("assets/sounds/paddle_collision.ogg"); Sound.BackgroundMusic = al_load_audio_stream("assets/sounds/Sound.BackgroundMusic-tech.ogg", 4, 2048); if (!Sound.HitWall || !Sound.HitPaddle || !Sound.BackgroundMusic) { fprintf(stderr, "Failed to load sound files!\n"); return false; } if(Debug) { fprintf(stderr, "Sound files loaded.\n"); } if(!al_attach_audio_stream_to_mixer(Sound.BackgroundMusic, Mixer) ) { fprintf(stderr, "Failed to attach stream to default Mixer!\n"); return 1; } if(Debug) { fprintf(stderr, "Stream attached to Mixer.\n"); } if(!al_set_mixer_playing(Mixer, true)) { fprintf(stderr, "Failed to play Mixer!\n"); } if(Debug) { fprintf(stderr, "Mixer playing.\n"); } if(!al_set_audio_stream_playmode(Sound.BackgroundMusic, ALLEGRO_PLAYMODE_LOOP)) { fprintf(stderr, "Failed to loop stream!\n"); } if(Debug) { fprintf(stderr, "Stream is set to loop.\n"); } if(!al_set_audio_stream_playing(Sound.BackgroundMusic, true)) { fprintf(stderr, "Failed to play stream!\n"); } if(Debug) { fprintf(stderr, "Stream playing.\n"); } return true; }
bool AllegroMusicSample5::Play() { if ( !m_pInstance ) return false; al_seek_audio_stream_secs( m_pInstance, 0 ); return al_set_audio_stream_playing( m_pInstance, true ); }
void dumba5_resume_duh(DUMBA5_PLAYER * dp) { if (dp && dp->sigrenderer && !(dp->flags & ADP_PLAYING)) { al_set_audio_stream_playing(dp->stream, true); // voice_start(dp->stream->voice); dp->flags |= ADP_PLAYING; } }
bool Letter(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { if (state == TM_ACTIONSTATE_INIT) { float* f = (float*)malloc(sizeof(float)); *f = 0; ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)malloc(sizeof(ALLEGRO_AUDIO_STREAM*)); *stream = al_load_audio_stream(GetDataFilePath(GetLevelFilename(game, "levels/?/letter.flac")), 4, 1024); al_attach_audio_stream_to_mixer(*stream, game->audio.voice); al_set_audio_stream_playing(*stream, false); al_set_audio_stream_gain(*stream, 2.00); action->arguments = TM_AddToArgs(action->arguments, (void*)f); action->arguments = TM_AddToArgs(action->arguments, (void*)stream); action->arguments->next->next = NULL; } else if (state == TM_ACTIONSTATE_DESTROY) { ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; al_set_audio_stream_playing(*stream, false); al_destroy_audio_stream(*stream); free(action->arguments->next->value); free(action->arguments->value); TM_DestroyArgs(action->arguments); } else if (state == TM_ACTIONSTATE_DRAW) { float* f = (float*)action->arguments->value; al_draw_tinted_bitmap(game->level.letter, al_map_rgba(*f,*f,*f,*f), (game->viewportWidth-al_get_bitmap_width(game->level.letter))/2.0, al_get_bitmap_height(game->level.letter)*-0.05, 0); return false; } else if (state == TM_ACTIONSTATE_PAUSE) { ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; al_set_audio_stream_playing(*stream, false); } else if ((state == TM_ACTIONSTATE_RESUME) || (state == TM_ACTIONSTATE_START)) { ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; al_set_audio_stream_playing(*stream, true); } if (state != TM_ACTIONSTATE_RUNNING) return false; float* f = (float*)action->arguments->value; *f+=5; if (*f>255) *f=255; al_draw_tinted_bitmap(game->level.letter, al_map_rgba(*f,*f,*f,*f), (game->viewportWidth-al_get_bitmap_width(game->level.letter))/2.0, al_get_bitmap_height(game->level.letter)*-0.05, 0); struct ALLEGRO_KEYBOARD_STATE keyboard; al_get_keyboard_state(&keyboard); // FIXME: do it the proper way if (al_key_down(&keyboard, ALLEGRO_KEY_ENTER)) { return true; } return false; }
void pauseMusic(void) { /* pause game music */ if (music != NULL) { al_set_audio_stream_playing(music, false); } }
void t3f_resume_music(void) { if(t3f_stream && t3f_music_mutex) { al_lock_mutex(t3f_music_mutex); al_set_audio_stream_playing(t3f_stream, true); al_unlock_mutex(t3f_music_mutex); t3f_set_music_state(T3F_MUSIC_STATE_PLAYING); } }
void t3f_pause_music(void) { if(t3f_stream && t3f_music_mutex) { al_lock_mutex(t3f_music_mutex); al_set_audio_stream_playing(t3f_stream, false); al_unlock_mutex(t3f_music_mutex); t3f_set_music_state(T3F_MUSIC_STATE_PAUSED); } }
void unPauseMusic(void) { /* unpause game music */ if (music != NULL) { al_set_audio_stream_playing(music, true); } }
void playBGM_Stream(const char *str) { if (!str) return; stream = al_load_audio_stream(str, 2, 1024); if (!stream) return; al_set_audio_stream_playing(stream, true); al_set_audio_stream_playmode(stream, ALLEGRO_PLAYMODE_LOOP); al_attach_audio_stream_to_mixer(stream, al_get_default_mixer()); }
void done(void) { // Free resources al_stop_samples(); ResourceManager& rm = ResourceManager::getInstance(); for (int i = RES_STREAM_START; i < RES_STREAM_END; i++) { ALLEGRO_AUDIO_STREAM *s = (ALLEGRO_AUDIO_STREAM *)rm.getData(i); if (s) al_set_audio_stream_playing(s, false); } ResourceManager::getInstance().destroy(); }
/* Function: al_drain_audio_stream */ void al_drain_audio_stream(ALLEGRO_AUDIO_STREAM *stream) { bool playing; if (!al_get_audio_stream_attached(stream)) { al_set_audio_stream_playing(stream, false); return; } stream->is_draining = true; do { al_rest(0.01); playing = al_get_audio_stream_playing(stream); } while (playing); stream->is_draining = false; }
void FillPage(struct Game *game, int page) { char filename[30] = { }; sprintf(filename, "intro/%d.flac", page); game->intro.audiostream = al_load_audio_stream(GetDataFilePath(filename), 4, 1024); al_attach_audio_stream_to_mixer(game->intro.audiostream, game->audio.voice); al_set_audio_stream_playing(game->intro.audiostream, false); al_set_audio_stream_gain(game->intro.audiostream, 1.75); al_set_target_bitmap(game->intro.table); float y = 0.2; float oldx = -1; void draw_text(int page, char* text) { float x = 0.45; if (page!=oldx) { y=0.2; oldx=page; } al_draw_text_with_shadow(game->intro.font, al_map_rgb(255,255,255), game->viewportWidth*x, game->viewportHeight*y, ALLEGRO_ALIGN_LEFT, text); y+=0.07; }
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; }
void Audio::StopMusic() { if( audioVoice == 0 || audioMixer == 0 ) { return; } #ifdef WRITE_LOG fprintf( FRAMEWORK->LogFile, "Framework: Stop audio\n" ); #endif if( musicStream != nullptr ) { FRAMEWORK->UnregisterEventSource( al_get_audio_stream_event_source( musicStream ) ); al_set_audio_stream_playing( musicStream, false ); al_detach_audio_stream( musicStream ); // Causes game to hang!? // al_destroy_audio_stream( musicStream ); musicStream = nullptr; } }
void Framework::PlayMusic( std::string Filename, ALLEGRO_PLAYMODE Mode ) { #ifdef WRITE_LOG printf( "Framework: Play Music '%s'\n", Filename.c_str() ); #endif if( voice != 0 ) { musicStream = audioMgr->GetMusic( Filename ); if( musicStream != 0 ) { al_set_audio_stream_playmode( musicStream, Mode ); al_attach_audio_stream_to_mixer( musicStream, mixer ); al_set_audio_stream_playing( musicStream, true ); } else { #ifdef WRITE_LOG printf( "Framework: Could not load music '%s'\n", Filename.c_str() ); #endif } } }
void menuLoop() { bool redraw = true; al_start_timer(timer); al_attach_audio_stream_to_mixer(music, al_get_default_mixer()); al_set_audio_stream_playing(music, true); al_set_audio_stream_playmode(music, ALLEGRO_PLAYMODE_LOOP); while (!menuDone) { ALLEGRO_EVENT event; al_wait_for_event(eventMenuQueue, &event); if (event.type == ALLEGRO_EVENT_TIMER) { redraw = true; //update_logic(); } else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { menuDone = true; } if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) { changeGameState(Game); } if (event.keyboard.keycode == ALLEGRO_KEY_UP){ changeGameState(Menu); } //get_user_input(); } if (redraw && al_is_event_queue_empty(eventMenuQueue)) { redraw = false; al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_tinted_bitmap(background, al_map_rgba(255, 255, 255, 255), 0, 0, 0); al_draw_text(font, al_map_rgb(0, 0, 0), 0, 0, 0, "BREGA HERO"); //update_graphics(); al_flip_display(); } } }
void AudioStream::resume() { al_set_audio_stream_playing(m_stream, true); assert(al_seek_audio_stream_secs(m_stream, m_lastPause)); }
int main(int argc, char *argv[]) { // -------- VARIÁVEIS DO JOGO -------- ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font20 = NULL; ALLEGRO_SAMPLE *ataque = NULL; ALLEGRO_SAMPLE *morte_inimigo = NULL; ALLEGRO_SAMPLE *morte_personagem = NULL; ALLEGRO_SAMPLE *hit = NULL; ALLEGRO_SAMPLE *item = NULL; ALLEGRO_AUDIO_STREAM *musica = NULL; ALLEGRO_AUDIO_STREAM *track_menu = NULL; ALLEGRO_BITMAP *start = NULL; bool fim = false; bool menu = true; int flag = 0; bool desenha = true; bool game_over = false; bool teclas[] = {false, false, false, false, false, false, false}; int i; int dificuldade; // ___________________________________ // -------- INICIALIZAÇÃO DE OBJETOS -------- dificuldade = 0; Personagem personagem_principal; Projetil* balas = new Projetil [NUM_BALAS]; Projetil* balas_2 = new Projetil [NUM_BALAS]; Inimigo* inimigos = new Inimigo[NUM_INIMIGOS]; Inimigo* inimigos2 = new Inimigo [NUM_INIMIGOS]; Coracao* coracoes = new Coracao[NUM_ITENS]; Speed* speed = new Speed[NUM_ITENS]; ItemPontos* pontos = new ItemPontos[NUM_ITENS]; Estrelas estrelas_pf[NUM_PLANOS][NUM_ESTRELAS]; Selecionar select; // __________________________________________ // -------- INICIALIZAÇÃO DA ALLEGRO E DO DISPLAY -------- ALLEGRO_DISPLAY *display = NULL; if (!al_init()) { al_show_native_message_box(NULL, "AVISO!", "ERRO!", "ERRO AO INICIALIZAR A ALLEGRO!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return -1; } display = al_create_display(LARGURA_T, ALTURA_T); if (!display) { al_show_native_message_box(NULL, "AVISO!", "ERRO!", "ERRO AO CRIAR O DISPLAY!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return -1; } al_set_window_title(display, "Cosmos Guardian"); // ____________________________________________________ // -------- INICIALIZAÇÃO DE ADDONS E INSTALAÇÕES -------- al_init_primitives_addon(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_install_audio(); al_init_acodec_addon(); al_reserve_samples(100); // _______________________________________________________ // -------- CRIAÇÃO DE FILAS E DEMAIS DISPOSITIVOS -------- fila_eventos = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); font20 = al_load_font("BADABB__.ttf", 20, 0); // ________________________________________________________ // -------- REGISTRO DE SOURCES -------- al_register_event_source(fila_eventos, al_get_display_event_source(display)); al_register_event_source(fila_eventos, al_get_keyboard_event_source()); al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); // _____________________________________ // -------- FUNÇÕES INICIAIS -------- srand(time(NULL)); personagem_principal.InitPersonagem(); select.InitSelecionar(); //Inicialização de projeteis for (i = 0; i < NUM_BALAS; i++) { balas[i].InitBalas(); balas_2[i].InitBalas(); } //Inicialização de inimigos for (i = 0; i < NUM_INIMIGOS; i++) { inimigos[i].InitInimigo(7, 46, 85, 1 , 0); inimigos2[i].InitInimigo(3, 55, 94, 3, 0); } //Inicialização de itens for (i = 0; i < NUM_ITENS; i++) { coracoes[i].InitItem(); speed[i].InitItem(); pontos[i].InitItem(); } //Inicialização do Plano de Fundo InitPlanoFundo(estrelas_pf, NUM_PLANOS, NUM_ESTRELAS); //Setando Sons e Imagens select.bmp = al_load_bitmap("select.png"); ataque = al_load_sample("laser.wav"); morte_inimigo = al_load_sample("dead.wav"); morte_personagem = al_load_sample("death.wav"); hit = al_load_sample("hit.wav"); item = al_load_sample("item.wav"); musica = al_load_audio_stream("trilha_sonora.ogg", 4, 1024); track_menu = al_load_audio_stream("menu.ogg", 4, 1024); personagem_principal.bmp = al_load_bitmap("ship.png"); for (i = 0; i < NUM_BALAS; i++) { balas[i].bmp = al_load_bitmap("bala.png"); balas_2[i].bmp = al_load_bitmap("bala.png"); } for (i = 0; i < NUM_INIMIGOS; i++){ inimigos[i].bmp = al_load_bitmap("enemyRed.png"); inimigos2[i].bmp = al_load_bitmap("enemyWhite.png"); } start = al_load_bitmap("start.jpg"); for (i = 0; i < NUM_ITENS; i++){ coracoes[i].imagem = al_load_bitmap("heart.png"); speed[i].imagem = al_load_bitmap("speed.png"); pontos[i].imagem = al_load_bitmap("pontos.png"); } // __________________________________ // ----------------- LOOP PRINCIPAL ----------------- al_start_timer(timer); while(!fim) { ALLEGRO_EVENT ev; al_wait_for_event(fila_eventos, &ev); // -------- EVENTOS E LÓGICA DO JOGO -------- if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { fim = true; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: fim = true; break; case ALLEGRO_KEY_UP: teclas[CIMA] = true; break; case ALLEGRO_KEY_DOWN: teclas[BAIXO] = true; break; case ALLEGRO_KEY_LEFT: teclas[ESQUERDA] = true; break; case ALLEGRO_KEY_RIGHT: teclas[DIREITA] = true; break; case ALLEGRO_KEY_SPACE: teclas[ESPACO] = true; AtiraBalas(balas, NUM_BALAS, personagem_principal, personagem_principal.y + 12); AtiraBalas(balas_2, NUM_BALAS, personagem_principal, personagem_principal.y + 70); al_play_sample(ataque, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL); break; case ALLEGRO_KEY_ENTER: teclas[ENTER] = true; break; case ALLEGRO_KEY_BACKSPACE: teclas[BACKSPACE] = true; break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: teclas[CIMA] = false; break; case ALLEGRO_KEY_DOWN: teclas[BAIXO] = false; break; case ALLEGRO_KEY_LEFT: teclas[ESQUERDA] = false; break; case ALLEGRO_KEY_RIGHT: teclas[DIREITA] = false; break; case ALLEGRO_KEY_ENTER: teclas[ENTER] = false; break; case ALLEGRO_KEY_BACKSPACE: teclas[BACKSPACE] = false; break; } } else if (ev.type == ALLEGRO_EVENT_TIMER) { desenha = true; if (teclas[CIMA]) personagem_principal.MoveCima(); if (teclas[BAIXO]) personagem_principal.MoveBaixo(ALTURA_T); if (teclas[ESQUERDA]) personagem_principal.MoveEsquerda(); if (teclas[DIREITA]) personagem_principal.MoveDireita(LARGURA_T); if (teclas[ESPACO]) { for (i = 0; i < NUM_BALAS; i++) { balas[i].AtualizaBalas(0); balas_2[i].AtualizaBalas(0); } } // Movimentação no menu if(menu) { al_attach_audio_stream_to_mixer(track_menu, al_get_default_mixer()); al_set_audio_stream_playing(track_menu, true); if (teclas[CIMA] && select.y!=235) select.y -= 70; if (teclas[BAIXO] && select.y!=305) select.y += 70; if (teclas[ENTER] && select.y==235) { menu = false; al_set_audio_stream_playing(track_menu, false); } } // Acontecimentos do Jogo if(!game_over && !menu) { al_attach_audio_stream_to_mixer(musica, al_get_default_mixer()); al_set_audio_stream_playing(musica, true); AtualizaPlanoFundo(estrelas_pf, NUM_PLANOS, NUM_ESTRELAS); { //Gera e atualiza inimigos for (i = 0; i < NUM_INIMIGOS; i++) { inimigos[i].GeraInimigos(); inimigos2[i].GeraInimigos(); inimigos[i].AtualizaInimigos(); inimigos[i].InimigoColidido(personagem_principal, hit); inimigos2[i].AtualizaInimigos(); inimigos2[i].InimigoColidido(personagem_principal, hit); } } //Checa colisões de projeteis BalaColidida(balas, NUM_BALAS, inimigos, NUM_INIMIGOS, personagem_principal, dificuldade, morte_inimigo); BalaColidida(balas_2, NUM_BALAS, inimigos, NUM_INIMIGOS, personagem_principal, dificuldade, morte_inimigo); BalaColidida(balas, NUM_BALAS, inimigos2, NUM_INIMIGOS, personagem_principal, dificuldade, morte_inimigo); BalaColidida(balas_2, NUM_BALAS, inimigos2, NUM_INIMIGOS, personagem_principal, dificuldade, morte_inimigo); // Faz os testes relacionado aos itens for (i = 0; i < NUM_ITENS; i++) { coracoes[i].GeraItens(inimigos[i]); coracoes[i].AtualizaItens(); coracoes[i].ItemColidido(personagem_principal, item); speed[i].GeraItens(inimigos[i]); speed[i].AtualizaItens(); speed[i].ItemColidido(personagem_principal, item); pontos[i].GeraItens(inimigos[i]); pontos[i].AtualizaItens(); pontos[i].ItemColidido(personagem_principal, item); } if ((dificuldade+1)%16 == 0) // Dificuldade aumenta a cada 15 pontos. { for (i = 0; i < NUM_INIMIGOS; i++){ inimigos[i].velocidade++; inimigos2[i].velocidade++; } dificuldade = 0; } if (personagem_principal.vidas <= 0) { al_play_sample(morte_personagem, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL); game_over = true; } } // Reinicializa o jogo else if (!menu) { al_set_audio_stream_playing(musica, false); if (teclas[ENTER]) { al_destroy_audio_stream(musica); personagem_principal.InitPersonagem(); for (i = 0; i < NUM_BALAS; i++) { balas[i].InitBalas(); balas_2[i].InitBalas(); } for (i = 0; i < NUM_INIMIGOS; i++) { inimigos[i].InitInimigo(7, 46, 85, 1 , 0); inimigos2[i].InitInimigo(3, 55, 94, 3, 0); } for (i = 0; i < NUM_ITENS; i++) { coracoes[i].InitItem(); speed[i].InitItem(); pontos[i].InitItem(); } personagem_principal.bmp = al_load_bitmap("ship.png"); musica = al_load_audio_stream("trilha_sonora.ogg", 4, 1024); for (i = 0; i < NUM_BALAS; i++) { balas[i].bmp = al_load_bitmap("bala.png"); balas_2[i].bmp = al_load_bitmap("bala.png"); } for (i = 0; i < NUM_INIMIGOS; i++) { inimigos[i].bmp = al_load_bitmap("enemyRed.png"); inimigos2[i].bmp = al_load_bitmap("enemyWhite.png"); coracoes[i].imagem = al_load_bitmap("heart.png"); speed[i].imagem = al_load_bitmap("heart.png"); } game_over = false; } } } // _________________________________________ // ---------------- DESENHO ---------------- if(desenha && al_is_event_queue_empty(fila_eventos)) { desenha = false; // Desenhos da Tela do Menu if(menu) { al_draw_bitmap(start, 0, 0, 0); if (select.ativo) al_draw_bitmap(select.bmp, select.x, select.y, 0); if (teclas[ENTER] && flag == 0) { flag = 1; teclas[BACKSPACE] = false; select.ativo = false; start = al_load_bitmap("como_jogar.jpg"); al_draw_bitmap(start, 0, 0, 0); } else if (teclas[BACKSPACE] && flag == 1) { flag = 0; select.ativo = true; start = al_load_bitmap("start.jpg"); al_draw_bitmap(start, 0, 0, 0); } } // Jogo normal, desenho de todos os objetos if(!game_over && !menu) { DesenhaPlanoFundo(estrelas_pf, NUM_PLANOS, NUM_ESTRELAS); personagem_principal.DesenhaPersonagem(); for (i = 0; i < NUM_BALAS; i++) { balas[i].DesenhaBalas(); balas_2[i].DesenhaBalas(); } for (i = 0; i < NUM_INIMIGOS; i++){ inimigos[i].DesenhaInimigos(); inimigos2[i].DesenhaInimigos(); } for (i = 0; i < NUM_ITENS; i++) { coracoes[i].DesenhaItens(); speed[i].DesenhaItens(); pontos[i].DesenhaItens(); } al_draw_textf(font20, al_map_rgb(255, 255, 255), 0, 0, 0, "VIDAS: %d / PONTOS: %d", personagem_principal.vidas, personagem_principal.pontos); } //Tela de fim de jogo else if (!menu) { al_draw_textf(font20, al_map_rgb(255, 255, 255), LARGURA_T / 2, ALTURA_T / 2, ALLEGRO_ALIGN_CENTRE, "FIM DE JOGO. SEUS PONTOS FORAM: %d. TECLE ENTER PARA JOGAR NOVAMENTE OU ESC PARA SAIR DO JOGO.", personagem_principal.pontos); } al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); } // _________________________________________ } // _________________________________________________ // -------- FINALIZAÇÕES DO PROGRAMA -------- delete[] inimigos; delete[] inimigos2; delete[] balas; delete[] balas_2; delete[] coracoes; delete[] speed; delete[] pontos; al_destroy_bitmap(start); al_destroy_sample(ataque); al_destroy_sample(morte_inimigo); al_destroy_sample(morte_personagem); al_destroy_sample(hit); al_destroy_sample(item); al_destroy_audio_stream(musica); al_destroy_audio_stream(track_menu); al_destroy_display(display); al_destroy_timer(timer); al_destroy_font(font20); al_destroy_event_queue(fila_eventos); //___________________________________________ return 0; }
int main(int argc, char **argv) { ALLEGRO_AUDIO_RECORDER *r; ALLEGRO_AUDIO_STREAM *s; ALLEGRO_EVENT_QUEUE *q; ALLEGRO_DISPLAY *d; ALLEGRO_FILE *fp = NULL; ALLEGRO_PATH *tmp_path = NULL; int prev = 0; bool is_recording = false; int n = 0; /* number of samples written to disk */ (void) argc; (void) argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } if (!al_init_primitives_addon()) { abort_example("Unable to initialize primitives addon"); } if (!al_install_keyboard()) { abort_example("Unable to install keyboard"); } if (!al_install_audio()) { abort_example("Unable to initialize audio addon"); } if (!al_init_acodec_addon()) { abort_example("Unable to initialize acodec addon"); } /* Note: increasing the number of channels will break this demo. Other * settings can be changed by modifying the constants at the top of the * file. */ r = al_create_audio_recorder(1000, samples_per_fragment, frequency, audio_depth, ALLEGRO_CHANNEL_CONF_1); if (!r) { abort_example("Unable to create audio recorder"); } s = al_create_audio_stream(playback_fragment_count, playback_samples_per_fragment, frequency, audio_depth, ALLEGRO_CHANNEL_CONF_1); if (!s) { abort_example("Unable to create audio stream"); } al_reserve_samples(0); al_set_audio_stream_playing(s, false); al_attach_audio_stream_to_mixer(s, al_get_default_mixer()); q = al_create_event_queue(); /* Note: the following two options are referring to pixel samples, and have * nothing to do with audio samples. */ al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); d = al_create_display(320, 256); if (!d) { abort_example("Error creating display\n"); } al_set_window_title(d, "SPACE to record. P to playback."); al_register_event_source(q, al_get_audio_recorder_event_source(r)); al_register_event_source(q, al_get_audio_stream_event_source(s)); al_register_event_source(q, al_get_display_event_source(d)); al_register_event_source(q, al_get_keyboard_event_source()); al_start_audio_recorder(r); while (true) { ALLEGRO_EVENT e; al_wait_for_event(q, &e); if (e.type == ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT) { /* We received an incoming fragment from the microphone. In this * example, the recorder is constantly recording even when we aren't * saving to disk. The display is updated every time a new fragment * comes in, because it makes things more simple. If the fragments * are coming in faster than we can update the screen, then it will be * a problem. */ ALLEGRO_AUDIO_RECORDER_EVENT *re = al_get_audio_recorder_event(&e); audio_buffer_t input = (audio_buffer_t) re->buffer; int sample_count = re->samples; const int R = sample_count / 320; int i, gain = 0; /* Calculate the volume, and display it regardless if we are actively * recording to disk. */ for (i = 0; i < sample_count; ++i) { if (gain < abs(input[i] - sample_center)) gain = abs(input[i] - sample_center); } al_clear_to_color(al_map_rgb(0,0,0)); if (is_recording) { /* Save raw bytes to disk. Assumes everything is written * succesfully. */ if (fp && n < frequency / (float) samples_per_fragment * max_seconds_to_record) { al_fwrite(fp, input, sample_count * sample_size); ++n; } /* Draw a pathetic visualization. It draws exactly one fragment * per frame. This means the visualization is dependent on the * various parameters. A more thorough implementation would use this * event to copy the new data into a circular buffer that holds a * few seconds of audio. The graphics routine could then always * draw that last second of audio, which would cause the * visualization to appear constant across all different settings. */ for (i = 0; i < 320; ++i) { int j, c = 0; /* Take the average of R samples so it fits on the screen */ for (j = i * R; j < i * R + R && j < sample_count; ++j) { c += input[j] - sample_center; } c /= R; /* Draws a line from the previous sample point to the next */ al_draw_line(i - 1, 128 + ((prev - min_sample_val) / (float) sample_range) * 256 - 128, i, 128 + ((c - min_sample_val) / (float) sample_range) * 256 - 128, al_map_rgb(255,255,255), 1.2); prev = c; } } /* draw volume bar */ al_draw_filled_rectangle((gain / (float) max_sample_val) * 320, 251, 0, 256, al_map_rgba(0, 255, 0, 128)); al_flip_display(); } else if (e.type == ALLEGRO_EVENT_AUDIO_STREAM_FRAGMENT) { /* This event is received when we are playing back the audio clip. * See ex_saw.c for an example dedicated to playing streams. */ if (fp) { audio_buffer_t output = al_get_audio_stream_fragment(s); if (output) { /* Fill the buffer from the data we have recorded into the file. * If an error occurs (or end of file) then silence out the * remainder of the buffer and stop the playback. */ const size_t bytes_to_read = playback_samples_per_fragment * sample_size; size_t bytes_read = 0, i; do { bytes_read += al_fread(fp, (uint8_t *)output + bytes_read, bytes_to_read - bytes_read); } while (bytes_read < bytes_to_read && !al_feof(fp) && !al_ferror(fp)); /* silence out unused part of buffer (end of file) */ for (i = bytes_read / sample_size; i < bytes_to_read / sample_size; ++i) { output[i] = sample_center; } al_set_audio_stream_fragment(s, output); if (al_ferror(fp) || al_feof(fp)) { al_drain_audio_stream(s); al_fclose(fp); fp = NULL; } } } } else if (e.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (e.type == ALLEGRO_EVENT_KEY_CHAR) { if (e.keyboard.unichar == 27) { /* pressed ESC */ break; } else if (e.keyboard.unichar == ' ') { if (!is_recording) { /* Start the recording */ is_recording = true; if (al_get_audio_stream_playing(s)) { al_drain_audio_stream(s); } /* Reuse the same temp file for all recordings */ if (!tmp_path) { fp = al_make_temp_file("alrecXXX.raw", &tmp_path); } else { if (fp) al_fclose(fp); fp = al_fopen(al_path_cstr(tmp_path, '/'), "w"); } n = 0; } else { is_recording = false; if (fp) { al_fclose(fp); fp = NULL; } } } else if (e.keyboard.unichar == 'p') { /* Play the previously recorded wav file */ if (!is_recording) { if (tmp_path) { fp = al_fopen(al_path_cstr(tmp_path, '/'), "r"); if (fp) { al_set_audio_stream_playing(s, true); } } } } } } /* clean up */ al_destroy_audio_recorder(r); al_destroy_audio_stream(s); if (fp) al_fclose(fp); if (tmp_path) { al_remove_filename(al_path_cstr(tmp_path, '/')); al_destroy_path(tmp_path); } return 0; }
int main() { bool menu = true; char pontuacao[100]; char vida[100]; ALLEGRO_COLOR font_color; ALLEGRO_FONT *font,*font2; ALLEGRO_AUDIO_STREAM *musica = NULL; camera *cam = camera_inicializa(0); if(!cam) erro("erro na inicializacao da camera\n"); int x = 0, y = 0; int largura = cam->largura; int altura = cam->altura; int fps = 0,tempo = 5; int ndisco = 9; if(!al_init()) erro("erro na inicializacao do allegro\n"); if(!al_init_image_addon()) erro("erro na inicializacao do adicional de imagem\n"); al_init_font_addon(); al_init_ttf_addon(); font_color = al_map_rgb(0, 0, 0); font = al_load_ttf_font("Fontes/Blokletters-Viltstift.ttf", 20, 0); font2 = al_load_ttf_font("Fontes/Blokletters-Viltstift.ttf", 50, 0); if(!al_init_primitives_addon()) erro("erro na inicializacao do adicional de primitivas\n"); ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS); if(!timer) erro("erro na criacao do relogio\n"); ALLEGRO_DISPLAY *display = al_create_display(2 * largura,altura); if(!display) erro("erro na criacao da janela\n"); ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue(); if(!queue) erro("erro na criacao da fila\n"); if (!al_install_audio()) { fprintf(stderr, "Falha ao inicializar áudio.\n"); return false; } if (!al_init_acodec_addon()) { fprintf(stderr, "Falha ao inicializar codecs de áudio.\n"); return false; } if (!al_reserve_samples(1)) { fprintf(stderr, "Falha ao alocar canais de áudio.\n"); return false; } musica = al_load_audio_stream("Audio/elementary.ogg", 4, 1024); if(!musica) erro("Erro na alocação da musica de fundo\n"); al_attach_audio_stream_to_mixer(musica, al_get_default_mixer()); al_set_audio_stream_playing(musica, true); al_register_event_source(queue, al_get_timer_event_source(timer)); al_register_event_source(queue, al_get_display_event_source(display)); al_start_timer(timer); unsigned char ***matriz = camera_aloca_matriz(cam); ALLEGRO_BITMAP *buffer = al_get_backbuffer(display); ALLEGRO_BITMAP *fundo = al_load_bitmap("Imagens/Elementary2.png"); if(!fundo) erro("erro ao carregar Elementary.png"); ALLEGRO_BITMAP *esquerda = al_create_sub_bitmap(buffer, 0, 0, largura, altura); ALLEGRO_BITMAP *direita = al_create_sub_bitmap(buffer, largura, 0, largura, altura); /**********/ Disco *discos[9]; bool perdeu = false; carregarDiscos(discos); int pontos=0,velo = 1; int aux1 = 1; int vidas = 10; int ultimoDisco = 0; int distance = 0; int desenhar = 0; int terminar = 0; al_set_target_bitmap(esquerda); al_draw_bitmap(fundo,0,0,0); while(1) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); switch(event.type) { case ALLEGRO_EVENT_TIMER: desenhar = 1; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: terminar = 1; break; } if(terminar) break; if(desenhar && al_is_event_queue_empty(queue)) { desenhar = 0; camera_atualiza(cam); mediana(cam); /**********/ al_set_target_bitmap(esquerda); al_draw_bitmap(fundo,0,0,0); if(!menu){ while(aux1 <= ndisco){ if(discos[aux1]->status == false){ if(distance > 50 || ultimoDisco==0 ){ printf("%d\n",aux1 ); al_draw_bitmap(discos[aux1]->elemento,discos[aux1]->pos_x,discos[aux1]->pos_y,0); discos[aux1]->status = true; distance = 0; ultimoDisco = aux1; break; }else aux1++; }else{ discos[aux1]->pos_y+=(10+velo); al_draw_bitmap(discos[aux1]->elemento,discos[aux1]->pos_x,discos[aux1]->pos_y,0); aux1++; } } distance = discos[ultimoDisco]->pos_y; for(aux1 = 1;aux1<ndisco;aux1++){ if(discos[aux1]->pos_x >= x-30 && discos[aux1]->pos_x <= x+30 && discos[aux1]->pos_y >= y-30 && discos[aux1]->pos_y <= y+30){ if(discos[aux1]->tipo == 2){ // Tipo do fogo(Necessario para vencer o jogo) pontos +=10; velo += 1; discos[aux1]->pos_x = rand()%9 * 55; discos[aux1]->pos_y = 0; discos[aux1]->status = false; }else if(discos[aux1]->tipo == 1){ //Tipo da agua(Perde o jogo se destruir esse disco) discos[aux1]->pos_x = rand()%9 * 55; discos[aux1]->pos_y = 0; discos[aux1]->status = false; al_flip_display(); vidas--; }else if(discos[aux1]->tipo == 3){//Tipo planta(Aumenta velocidade de queda das peças) velo *= 2; discos[aux1]->pos_x = rand()%9 * 55; discos[aux1]->pos_y = 0; discos[aux1]->status = false; } }else if( discos[aux1]->pos_y > 480){ if(discos[aux1]->tipo == 2){ //Tipo da agua e Planta(Não perde se deixar cair) discos[aux1]->pos_x = rand()%9 * 55; discos[aux1]->pos_y = 0; discos[aux1]->status = false; al_flip_display(); vidas--; }else{ discos[aux1]->pos_x = rand()%9 * 55; discos[aux1]->pos_y = 0; discos[aux1]->status = false; } } } aux1 = 1; sprintf(pontuacao,"PONTUAÇÃO: %d",pontos); al_draw_text(font, al_map_rgb(255, 255, 255), 50, 5, 0,pontuacao); sprintf(vida,"VIDAS: %d",vidas); al_draw_text(font, al_map_rgb(255, 255, 255), 300, 5, 0,vida); al_flip_display(); } if(perdeu){ al_draw_text(font2, al_map_rgb(255, 0, 0), 50, 100, 0,"PONTUAÇÃO FINAL"); sprintf(pontuacao,"%d",pontos); al_draw_text(font2, al_map_rgb(255, 0, 0), 250, 170, 0,pontuacao); al_flip_display(); al_rest(3); break; } if(vidas == 0){ perdeu = true; } if(menu){ if(abrirJogo(x,y,&fps,&tempo,font,font2, font_color)){ fundo = al_load_bitmap("Imagens/galaxia.png"); menu = false; } } cameraRastreia(cam,&x,&y); al_set_target_bitmap(direita); camera_copia(cam, cam->quadro, direita); al_flip_display(); } } al_destroy_bitmap(direita); al_destroy_bitmap(fundo); al_destroy_bitmap(esquerda); camera_libera_matriz(cam, matriz); int fri = 9; while(fri != 0){ free(discos[fri]); fri--; } al_stop_timer(timer); al_unregister_event_source(queue, al_get_display_event_source(display)); al_unregister_event_source(queue, al_get_timer_event_source(timer)); al_destroy_event_queue(queue); al_destroy_display(display); al_destroy_timer(timer); al_destroy_audio_stream(musica); al_shutdown_primitives_addon(); al_shutdown_image_addon(); al_uninstall_system(); camera_finaliza(cam); return EXIT_SUCCESS; }
static void event_handler(const ALLEGRO_EVENT * event) { int i; switch (event->type) { /* Was the X button on the window pressed? */ case ALLEGRO_EVENT_DISPLAY_CLOSE: exiting = true; break; /* Was a key pressed? */ case ALLEGRO_EVENT_KEY_CHAR: if (event->keyboard.keycode == ALLEGRO_KEY_LEFT) { double pos = al_get_audio_stream_position_secs(music_stream); pos -= 5.0; if (pos < 0.0) pos = 0.0; al_seek_audio_stream_secs(music_stream, pos); } else if (event->keyboard.keycode == ALLEGRO_KEY_RIGHT) { double pos = al_get_audio_stream_position_secs(music_stream); pos += 5.0; if (!al_seek_audio_stream_secs(music_stream, pos)) log_printf("seek error!\n"); } else if (event->keyboard.keycode == ALLEGRO_KEY_SPACE) { bool playing; playing = al_get_audio_stream_playing(music_stream); playing = !playing; al_set_audio_stream_playing(music_stream, playing); } else if (event->keyboard.keycode == ALLEGRO_KEY_ESCAPE) { exiting = true; } break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: mouse_button[event->mouse.button] = 1; maybe_fiddle_sliders(event->mouse.x, event->mouse.y); break; case ALLEGRO_EVENT_MOUSE_AXES: maybe_fiddle_sliders(event->mouse.x, event->mouse.y); break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: mouse_button[event->mouse.button] = 0; break; case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY: for (i = 0; i < 16; i++) mouse_button[i] = 0; break; /* Is it time for the next timer tick? */ case ALLEGRO_EVENT_TIMER: logic(); render(); break; case ALLEGRO_EVENT_AUDIO_STREAM_FINISHED: log_printf("Stream finished.\n"); break; } }