bool Event::Update() { ALLEGRO_EVENT Events; ALLEGRO_TIMEOUT TimeOut; al_init_timeout( &TimeOut, 0.06f ); /// Time to wait for Event al_get_mouse_state( &Mouse ); bool get_event = al_wait_for_event_until( EventQueue, &Events, &TimeOut ); if( get_event ) switch( Events.type ) { case ALLEGRO_EVENT_KEY_DOWN: if( Events.keyboard.keycode == ALLEGRO_KEY_ESCAPE ) return false; Keys[ Events.keyboard.keycode ] = true; break; case ALLEGRO_EVENT_KEY_UP: Keys[ Events.keyboard.keycode ] = false; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: return false; break; } return true; }
void PauseGame() { bool pause; FadeOut(5, 150); pause = true; ALLEGRO_EVENT evento; ALLEGRO_TIMEOUT timeout; al_init_timeout(&timeout, 0.05); al_draw_text(Fonte, al_map_rgb(255, 255, 255), (cameraPosition[0] + LARGURA_TELA / 2 - al_get_font_ascent(Fonte)), (cameraPosition[1] + ALTURA_TELA / 2 - al_get_font_ascent(Fonte)), ALLEGRO_ALIGN_CENTRE, "Pause Game! Pressione Enter."); al_flip_display(); while (pause) { int tem_eventos = al_wait_for_event_until(Fila_Eventos, &evento, &timeout); if (tem_eventos) { if (evento.keyboard.keycode == ALLEGRO_KEY_ENTER) { pause = false; } } } redraw = true; }
int main(int argc, char **argv){ ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; al_init(); display = al_create_display(640, 480); event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_display_event_source(display)); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); while(1) { ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT timeout; al_init_timeout(&timeout, 0.06); bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); } al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
static void test_absolute_timeout(ALLEGRO_EVENT_QUEUE *queue) { ALLEGRO_TIMEOUT timeout; ALLEGRO_EVENT event; float shade = 0.1; bool ret; while (true) { al_init_timeout(&timeout, 0.1); while ((ret = al_wait_for_event_until(queue, &event, &timeout))) { if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { return; } else { shade = 0.1; } } } if (!ret) { /* timed out */ shade += 0.1; if (shade > 1.0) shade = 1.0; } al_clear_to_color(al_map_rgba_f(shade, 0.5 * shade, 0.25 * shade, 0)); al_flip_display(); } }
void NPCFalando(NPC *NPC) { ALLEGRO_FONT *FonteFala = al_load_font("Arial.ttf", 16, 0); bool Sair; FadeOut(5, 150); Sair = true; int PosFala = 0; bool Redraw = true; ALLEGRO_EVENT evento; ALLEGRO_TIMEOUT timeout; al_init_timeout(&timeout, 0.05); while (Sair) { int tem_eventos = al_wait_for_event_until(Fila_Eventos, &evento, &timeout); if (tem_eventos) { if (evento.type == ALLEGRO_EVENT_KEY_DOWN && evento.keyboard.keycode == ALLEGRO_KEY_S) { if (PosFala >= NPC->QtdFalas) { Redraw = false; Sair = false; break; } else { Redraw = true; } } } if (Redraw) { Redraw = false; /* Desenha a Borda do Retangulo */ al_draw_rectangle(cameraPosition[0], cameraPosition[1] + (ALTURA_TELA - 80), cameraPosition[0] + LARGURA_TELA, cameraPosition[1] + ALTURA_TELA, al_map_rgb(255, 255, 255), 2); al_draw_filled_rectangle(cameraPosition[0], cameraPosition[1] + (ALTURA_TELA - 80), cameraPosition[0] + LARGURA_TELA, cameraPosition[1] + ALTURA_TELA, al_map_rgb(240, 255, 113)); /* Escreve o Texto na Tela */ al_draw_textf(FonteFala, al_map_rgb(0, 0, 0), cameraPosition[0] + 30, cameraPosition[1] + (ALTURA_TELA - 70), 0, "%s", NPC->FalaNPC[PosFala]); al_draw_text(Fonte12, al_map_rgb(0, 0, 0), cameraPosition[0] + (LARGURA_TELA - 80), ((cameraPosition[1] + (ALTURA_TELA - 100) + cameraPosition[1] + ALTURA_TELA) / 2) + 30, 0, "Pressione S"); al_flip_display(); PosFala++; } } redraw = true; al_destroy_font(FonteFala); }
int main(int argc, char **argv){ ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } display = al_create_display(640, 480); if(!display) { fprintf(stderr, "failed to create display!\n"); return -1; } event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_display(display); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); while(1) { ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT timeout; al_init_timeout(&timeout, 0.06); bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); if(get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); } al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
bool GameState::update( double delta ) { //simplog.writeLog( LOG_VERBOSE, "update" ); totalTime += delta; ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT timeout; al_init_timeout( &timeout, 0.01 ); bool got_event = al_wait_for_event_until( event_queue, &ev, &timeout ); if( got_event ) { if( ev.type == ALLEGRO_EVENT_KEY_DOWN ) { if( ev.keyboard.keycode == ALLEGRO_KEY_LEFT ) { currentPiece->moveLeft( delta ); } else if( ev.keyboard.keycode == ALLEGRO_KEY_RIGHT ) { currentPiece->moveRight( delta ); } if( ev.keyboard.keycode == ALLEGRO_KEY_SPACE ) { currentPiece->moveDown( delta ); } } } simplog.writeLog( LOG_VERBOSE, "%.2f", totalTime ); if( totalTime >= 20 && currentPiece != nullptr ) { // simplog.writeLog( LOG_VERBOSE, "piece processing" ); currentPiece->update( delta ); if( checkCollision( currentPiece ) ) { delete currentPiece; currentPiece = nextPiece; nextPiece = this->getRandomPiece( blockImg); nextPiece->setX( 580 ); nextPiece->setY( 60 ); currentPiece->setX( 280 ); currentPiece->setY( -58 ); } totalTime = 0; updateCounter++; } return true; }
void SaveGame(Mapas Mapa, Heroi Heroi) { bool salvando; FadeOut(5, 150); salvando = true; ALLEGRO_EVENT evento; ALLEGRO_TIMEOUT timeout; al_init_timeout(&timeout, 0.05); FILE *save; save = fopen("SaveLoadGame/SaveLoadGame.bin", "w"); fprintf(save, "%d\n", Mapa.ID); fprintf(save, "%d\n", Heroi.x); fprintf(save, "%d\n", Heroi.y); fprintf(save, "%d\n", Heroi.LinhaAnimacao); fprintf(save, "%d\n", Heroi.ColunaAnimacao); fprintf(save, "%d\n", Heroi.Life); fprintf(save, "%d\n", Heroi.MaxLife); fprintf(save, "%d\n", Heroi.Mana); fprintf(save, "%d\n", Heroi.MaxMana); fprintf(save, "%d\n", Heroi.QtdVida); fprintf(save, "%d\n", Heroi.QtdMana); fclose(save); al_draw_text(Fonte, al_map_rgb(255, 255, 255), (cameraPosition[0] + LARGURA_TELA / 2 - al_get_font_ascent(Fonte)), (cameraPosition[1] + ALTURA_TELA / 2 - al_get_font_ascent(Fonte)), ALLEGRO_ALIGN_CENTRE, "Jogo Salvo. Preciso Enter!"); al_flip_display(); while (salvando) { int tem_eventos = al_wait_for_event_until(Fila_Eventos, &evento, &timeout); if (tem_eventos) { if (evento.keyboard.keycode == ALLEGRO_KEY_ENTER) { salvando = false; } } } }
int main(){ ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; al_init(); display = al_create_display(640,480); event_queue = al_create_event_queue(); al_register_event_source(event_queue,al_get_display_event_source(display)); al_clear_to_color(al_map_rgb(0,61,236)); al_flip_display(); while(1) { ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT timeout; al_init_timeout(&timeout, 0.06); bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); if(get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } al_clear_to_color(al_map_rgb(156,10,20)); al_flip_display(); } al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int main (int argc, char *argv[]) { ALLEGRO_BITMAP *background = NULL; ALLEGRO_BITMAP *title = NULL; ALLEGRO_BITMAP *terrain = NULL; ALLEGRO_BITMAP *play = NULL; ALLEGRO_BITMAP *ranking = NULL; ALLEGRO_BITMAP *footer = NULL; ALLEGRO_BITMAP *playstart = NULL; int pos_x; int pos_y; srand (time (NULL)); iniciar_allegro (); al_install_mouse (); background = al_load_bitmap (SPRITESHEET); title = al_load_bitmap (SPRITESHEET); terrain = al_load_bitmap (SPRITESHEET); play = al_load_bitmap (SPRITESHEET); ranking = al_load_bitmap (SPRITESHEET); footer = al_load_bitmap (SPRITESHEET); playstart = al_load_bitmap (SPRITESHEET); if (!playstart) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap del play start n***a", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } if (!background) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap del background", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } if (!title) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap del titulo", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } if (!terrain) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap del terreno", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } if (!play) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap del play", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } if (!ranking) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap del ranking", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } if (!footer) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap del footer", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } while(1){ ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT timeout; al_init_timeout (&timeout, 0.00); bool get_event = al_wait_for_event_until (event_queue, &ev, &timeout); if (get_event){ if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES){ pos_x = ev.mouse.x; pos_y = ev.mouse.y; } if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN){ if(ev.mouse.button & 1) break; } } if(redraw && al_is_event_queue_empty (event_queue)) { al_clear_to_color (al_map_rgb (0, 0, 0)); al_draw_bitmap_region(background, 0, 0, 288, 512, 0, 0, 0); al_draw_bitmap_region(title, 702, 182, 178, 48, 50, 110, 0); al_draw_bitmap_region(terrain, 584, 0 , 288, 112 , 0, 400, 0); al_draw_bitmap_region(playstart, 709, 314, 104, 56, 30, 344, 0); al_draw_bitmap_region(play, 708, 235, 104, 56, 30, 344, 0); al_draw_bitmap_region(ranking, 829, 235, 104, 56, 150, 344, 0); al_draw_bitmap_region(footer, 885, 182, 140, 20, 80, 418, 0); al_flip_display(); redraw = false; } } al_destroy_bitmap (background); al_destroy_bitmap (title); al_destroy_bitmap (terrain); al_destroy_bitmap (play); al_destroy_bitmap (playstart); al_destroy_bitmap (ranking); al_destroy_bitmap (footer); destruir_allegro (); return 0; }
int main (int argc, char **argv) { Prota mi_nave; Nave enemigo; ALLEGRO_BITMAP *xenon_spritesheet = NULL; ALLEGRO_BITMAP *enemy_spritesheet = NULL; ALLEGRO_BITMAP *bg = NULL; bool tecla[4] = { false, false, false, false }; srand (time (NULL)); iniciar_allegro (); xenon_spritesheet = al_load_bitmap (SPRITESHEET); enemy_spritesheet = al_load_bitmap (ENEMY); bg = al_load_bitmap (BG); al_convert_mask_to_alpha(xenon_spritesheet, al_map_rgb(255, 0, 255)); if (!xenon_spritesheet) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } mi_nave.set_dibujo (xenon_spritesheet); al_convert_mask_to_alpha(enemy_spritesheet, al_map_rgb(255, 0, 255)); if (!enemy_spritesheet) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } enemigo.set_dibujo (enemy_spritesheet); al_convert_mask_to_alpha(bg, al_map_rgb(255, 0, 255)); if (!bg) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } while (1) { /* Buzz Lightyear */ ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT timeout; al_init_timeout (&timeout, 0.06); bool get_event = al_wait_for_event_until (event_queue, &ev, &timeout); if (get_event) { if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (ev.type == ALLEGRO_EVENT_TIMER) redraw = true; if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: tecla[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: tecla[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: tecla[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: tecla[KEY_RIGHT] = true; break; } } if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: tecla[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: tecla[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: tecla[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: tecla[KEY_RIGHT] = false; break; } } } /* Actualizar las coordenadas de la pelota */ if (tecla[KEY_UP]) mi_nave.change_vy(-DELTA); if (tecla[KEY_DOWN]) mi_nave.change_vy(DELTA); if (tecla[KEY_LEFT]) mi_nave.change_vx(-DELTA); if (tecla[KEY_RIGHT]) mi_nave.change_vx(DELTA); mi_nave.actualizate(); if (redraw && al_is_event_queue_empty (event_queue)) { al_clear_to_color (al_map_rgb (0, 0, 0)); al_draw_bitmap_region (bg,0,0,1024,768,0,0,0); al_draw_bitmap_region (mi_nave.get_dibujo (), mi_nave.get_sprite_number() * 33, 99, 32, 27, mi_nave.get_x (), mi_nave.get_y (), 0); enemigo.move(); al_draw_bitmap_region(enemigo.get_dibujo(), enemigo.get_sprite_number() * 1, 33, 32, 32, enemigo.get_x (), enemigo.get_y (), 0); al_flip_display (); redraw = false; } } al_destroy_bitmap (xenon_spritesheet); destruir_allegro (); return 0; }
/* * Application entry point */ int main(int argc, char *argv[]) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_KEYBOARD_STATE keyboard_state; ALLEGRO_MAP *map; Avatar *tar; bool running = true; bool redraw = true; int map_x = 0, map_y = 0; int screen_width = 640; int screen_height = 480; //{{{ initialization // Initialize allegro if (!al_init()) { fprintf(stderr, "Failed to initialize allegro.\n"); return 1; } // Initialize allegro_image addon if (!al_init_image_addon()) { fprintf(stderr, "Failed to initialize image addon.\n"); return 1; } // Initialize the timer timer = al_create_timer(1.0 / FPS); if (!timer) { fprintf(stderr, "Failed to create timer.\n"); return 1; } // Install the keyboard if (!al_install_keyboard()) { fprintf(stderr, "Failed to install keyboard.\n"); return 1; } // Create the display display = al_create_display(screen_width, screen_height); if (!display) { fprintf(stderr, "Failed to create display.\n"); return 1; } else { al_set_window_title(display, "Hello Tiled!"); } // Create the event queue event_queue = al_create_event_queue(); if (!event_queue) { fprintf(stderr, "Failed to create event queue.\n"); return 1; } // Register event sources al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); //}}} // Start the timer al_start_timer(timer); // Parse the map map = al_open_map(MAP_FOLDER, "level1.tmx"); // Create avatar tar = create_avatar(0, 0, "data/avatar.png"); // Draw the map al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_map_region(map, map_x, map_y, screen_width, screen_height, 0, 0, 0); al_flip_display(); #if DEBUG // FPS counter double old_time = al_get_time(), fps = 0; int frames_done = 0; #endif // Main loop while (running) { ALLEGRO_EVENT event; ALLEGRO_TIMEOUT timeout; // Initialize the timeout (not the game's clock) al_init_timeout(&timeout, 0.06); // Fetch the event (if one exists) bool get_event = al_wait_for_event_until(event_queue, &event, &timeout); // Handle the event if (get_event) { switch (event.type) { case ALLEGRO_EVENT_TIMER: // is an arrow key being held? al_get_keyboard_state(&keyboard_state); int x_move = 0; int y_move = 0; if (al_key_down(&keyboard_state, ALLEGRO_KEY_RIGHT)) { x_move += 2; } if (al_key_down(&keyboard_state, ALLEGRO_KEY_LEFT)) { x_move -= 2; } if (al_key_down(&keyboard_state, ALLEGRO_KEY_UP)) { y_move -= 2; } if (al_key_down(&keyboard_state, ALLEGRO_KEY_DOWN)) { y_move += 2; } walk(tar, x_move, y_move); //gravity(tar, center_viewport(tar, screen_width, screen_height, map, &map_x, &map_y); redraw = true; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: running = false; break; case ALLEGRO_EVENT_KEY_DOWN: // ignore break; case ALLEGRO_EVENT_KEY_UP: // ignore break; case ALLEGRO_EVENT_KEY_CHAR: if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) { ALLEGRO_MAP_LAYER *collide_layer = al_get_map_layer(map, "Blocks 1"); ALLEGRO_MAP_TILE **tiles = get_occupied_tile_ids(tar, collide_layer, map); fprintf(stdout, "Avatar on tiles: {%s, %s, %s, %s}\n", al_get_tile_property(tiles[0], "collide", "null"), al_get_tile_property(tiles[1], "collide", "null"), al_get_tile_property(tiles[2], "collide", "null"), al_get_tile_property(tiles[3], "collide", "null")); free(tiles); tiles = NULL; } break; default: fprintf(stderr, "Unsupported event received: %d\n", event.type); break; } } if (redraw && al_is_event_queue_empty(event_queue)) { // Clear the screen al_clear_to_color(al_map_rgb(0, 0, 0)); #if DEBUG double game_time = al_get_time(); if(game_time - old_time >= 1.0) { fps = frames_done / (game_time - old_time); frames_done = 0; old_time = game_time; fprintf(stderr, "FPS:%f\n", fps); } frames_done++; #endif al_draw_map_region(map, map_x, map_y, screen_width, screen_height, 0, 0, 0); draw_avatar(tar, map, map_x, map_y); al_flip_display(); redraw = false; } } // Clean up and return free_avatar(tar); al_free_map(map); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int main (int argc, char **argv) { Pelota pelota[N]; srand (time (NULL)); iniciar_allegro (); for (int i = 0; i < N; i++) pelota[i].set_dibujo (bm); while (1) { /* Buzz Lightyear */ ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT timeout; al_init_timeout (&timeout, 0.06); bool get_event = al_wait_for_event_until (event_queue, &ev, &timeout); if (get_event) { if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (ev.type == ALLEGRO_EVENT_TIMER) redraw = true; if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: tecla[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: tecla[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: tecla[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: tecla[KEY_RIGHT] = true; break; } } if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: tecla[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: tecla[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: tecla[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: tecla[KEY_RIGHT] = false; break; case ALLEGRO_KEY_LSHIFT: sprite_number++; break; } } } /* Actualizar las coordenadas de la pelota */ if (tecla[KEY_UP]) pelota.change_vy (-DELTA); if (tecla[KEY_DOWN]) pelota.change_vy (DELTA); if (tecla[KEY_LEFT]) pelota.change_vx (-DELTA); if (tecla[KEY_RIGHT]) pelota.change_vx (DELTA); for (int i = 0; i < N; i++) pelota[i].actualizate (); if (redraw && al_is_event_queue_empty (event_queue)) { al_clear_to_color (al_map_rgb (0, 0, 0)); al_draw_bitmap (bm2, 0, 0, 0); for (int i = 0; i < N; i++) al_draw_bitmap (pelota[i].get_dibujo (), pelota[i].get_x () + 450, pelota[i].get_y () + 450, 0); al_flip_display (); redraw = false; } } destruir_allegro (); return 0; }
int game_loop(){ unsigned redraw = 0; game_rect t; rect_Zero(&t); sprite_sheet = gamespr_create("ski1.bmp"); animation_init(); init_player(); start_new_game(0); while(!mainloop){ ALLEGRO_EVENT e; ALLEGRO_TIMEOUT timeout; int i; al_init_timeout(&timeout, 0.06); bool late = al_wait_for_event_until(g_queue, &e, &timeout); if(late){ switch(e.type){ case ALLEGRO_EVENT_DISPLAY_CLOSE: mainloop = 1; break; case ALLEGRO_EVENT_KEY_UP: player_poll_kbd_up(&e); break; case ALLEGRO_EVENT_KEY_DOWN: player_poll_kbd_dn(&e); break; case ALLEGRO_EVENT_TIMER: /* main clock updates 1/60ms (60FPS LOCK) */ if(e.timer.source == g_timer){ particle_list = particles_clean(particle_list, 0); particles_update(particle_list); player_update_screen(); player_update(&ski_player, &playfield); for(i = 1; i < MAX_SPRITES; i++){ update_enemy(i); } update_enemy_behavior(gobj_list, &playfield); redraw = 1; } /* update for the chronomenter run every 100ms only) */ if( e.timer.source == timer_chrono){ HUD_UpdateChrono(); DMSG("%d", al_get_timer_count(timer_chrono)); } break; } if( redraw == 1 && al_event_queue_is_empty(g_queue)){ redraw = 0; al_clear_to_color(WHITE_COLOR); particles_draw(NULL, particle_list); player_draw(ski_player.object->position.x, ski_player.object->position.y ); for(i = 1; i < MAX_OBJECTS; i++){ int enemy_type = gobj_list[i].type; draw_enemy(enemy_type, i, 0,0, gobj_list[i].position.x, gobj_list[i].position.y, 32,32); } HUD_create_stats_box(); al_flip_display(); } } } HUD_destroy(); unload_spritesheets(); window_deinit(); particle_list = particles_clean(particle_list, PARTICLES_ALL_CLEAN); return EXIT_SUCCESS; }
int main(void) { bool GameOver = false; ALLEGRO_DISPLAY *screen = NULL; ALLEGRO_BITMAP *imagem = NULL; ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; if (!al_init()) { fprintf(stderr, "Falha ao inicializar a Allegro.\n"); return -1; } if (!al_init_image_addon()) { fprintf(stderr, "Falha ao inicializar add-on allegro_image.\n"); return -1; } screen = al_create_display(LARGURA_TELA, ALTURA_TELA); if (!screen) { fprintf(stderr, "Falha ao criar janela.\n"); return -1; } imagem = al_load_bitmap("poring.png"); if (!imagem) { fprintf(stderr, "Falha ao carregar o arquivo de imagem.\n"); al_destroy_display(screen); return -1; } fila_eventos = al_create_event_queue(); if (!fila_eventos) { fprintf(stderr, "Falha ao criar fila de eventos.\n"); al_destroy_display(screen); return -1; } al_init_primitives_addon(); al_draw_line(640,480, 0, 0, al_map_rgb(255, 255, 255), 1000); al_register_event_source(fila_eventos, al_get_display_event_source(screen)); al_flip_display(); while (!GameOver) { ALLEGRO_EVENT evento; ALLEGRO_TIMEOUT timeout; al_init_timeout(&timeout, 0.05); int tem_eventos = al_wait_for_event_until(fila_eventos, &evento, &timeout); if (tem_eventos && evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } //This is the main game loop //Create the Monstro for(int x = 1; x < 11; x++) { createmonster(x); } //Internal game loop to handle monster movement, shooting, etc. for(int walkcount = 0; walkcount < 1500; walkcount ++) { //Did the player click on 'Build a tower'? //Move the Monstro for (int x = 0; x < 11; x++) { if (Monstro[x].stillalive == true) { erasemonster(x); movemonster(x); drawmonster(x); al_flip_display(); printf("\nMonstro %d em execução\n", x); if (Monstro[x].xlocation > 529) { erasemonster(x); Monstro[x].stillalive = false; Monstro[x].xlocation = 0; Monstro[x].ylocation = 0; } } } } al_flip_display(); } al_destroy_display(screen); al_destroy_event_queue(fila_eventos); return 0; }
//Verifica se há eventos e preenche a event queue. //Retorna false se recebe um evento que provoque a saída do jogo bool EventQueue::CheckEvents() { ALLEGRO_TIMEOUT timeout; ALLEGRO_EVENT ev; if (_events.size() > MAX_QUEUE_SIZE) { fprintf(stderr, "[EventQueue] Elemento retirado da fila\n"); while (_events.size() >= MAX_QUEUE_SIZE) _events.pop(); } //10 ms de timeout para obter os eventos al_init_timeout(&timeout, 0.01); while (al_wait_for_event_until(_allegro_queue, &ev, &timeout)) { al_init_timeout(&timeout, 0.01); /* Por enquanto apenas trataremos eventos de mouse e de teclado */ if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) return false; Event e = { 0 }; if (ev.type == ALLEGRO_EVENT_KEY_DOWN || ev.type == ALLEGRO_EVENT_KEY_UP || ev.type == ALLEGRO_EVENT_KEY_CHAR ){ e.keycode = ev.keyboard.keycode; e.keyletter = (char)(ev.keyboard.unichar); switch (ev.type) { case ALLEGRO_EVENT_KEY_DOWN: case ALLEGRO_EVENT_KEY_CHAR: e.key_status = ButtonStatus::Pressed; last_key = e.keycode; break; case ALLEGRO_EVENT_KEY_UP: e.key_status = ButtonStatus::Released; last_key = 0; break; } } else { e.key_status = ButtonStatus::NoEvent; } if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN || ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP || ev.type == ALLEGRO_EVENT_MOUSE_AXES ) { mouse_lastx = ev.mouse.x; mouse_lasty = ev.mouse.y; e.mouse_button = ev.mouse.button; switch (ev.type) { case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: e.mouse_status = ButtonStatus::Pressed; break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: e.mouse_status = ButtonStatus::Released; break; default: e.mouse_status = ButtonStatus::NoEvent; break; } } else { e.mouse_button = 0; } e.mouse_x = mouse_lastx; e.mouse_y = mouse_lasty; /* fprintf(stderr, "[EventQueue] Evento #%d \n", _events.size()); fprintf(stderr, "[EventQueue]\tTecla %c (scancode %d), keystatus %d\n", e.keyletter, e.keycode, e.key_status); fprintf(stderr, "[EventQueue]\tPosição do mouse: (%d, %d), botão %d, mousestatus %d\n", e.mouse_x, e.mouse_y, e.mouse_button, e.mouse_status); */ //Insere o evento na fila _events.push(e); } return true; }
void Game::run() { bool bRedraw = true; bool bSendPlatform = true; m_pBall->setAngle(-45.0f); m_pBall->setVelocity(3.2f, 3.2f); m_bRunning = true; do { ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT t; al_init_timeout(&t, 0.005); //al_wait_for_event(m_pEventQueue, &ev); bool bGot = al_wait_for_event_until(m_pEventQueue, &ev, &t); if (bGot) { if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { m_bRunning = false; break; } else if (ev.type == ALLEGRO_EVENT_TIMER && ev.timer.source == m_pNetworkTimer) { if (bSendPlatform) { bSendPlatform = false; float fX, fY; m_pPlatform[0]->getPosition(fX, fY); m_pNetwork->updatePlatform(fY, m_pPlatform[0]->getVelocity(), m_pPlatform[0]->getPropulsionState()); } } else if (ev.type == ALLEGRO_EVENT_TIMER && ev.timer.source == m_pFrameTimer) { m_pNetwork->process(); if (m_pGraphics->process()) bRedraw = true; if (m_pKeyboard->isKeyDown(Keyboard::UP)) { //m_pPlatform[0]->decelerate(0.60f); if (m_pPlatform[0]->getPropulsionState() != Platform::UP) { m_pPlatform[0]->setPropulsionState(Platform::UP); float fX, fY; m_pPlatform[0]->getPosition(fX, fY); m_pNetwork->updatePlatform(fY, m_pPlatform[0]->getVelocity(), Platform::UP); } } else if (m_pKeyboard->isKeyDown(Keyboard::DOWN)) { //m_pPlatform[0]->accelerate(0.60f); if (m_pPlatform[0]->getPropulsionState() != Platform::DOWN) { m_pPlatform[0]->setPropulsionState(Platform::DOWN); float fX, fY; m_pPlatform[0]->getPosition(fX, fY); m_pNetwork->updatePlatform(fY, m_pPlatform[0]->getVelocity(), Platform::DOWN); } } else { if (m_pPlatform[0]->getPropulsionState() != Platform::OFF) { m_pPlatform[0]->setPropulsionState(Platform::OFF); float fX, fY; m_pPlatform[0]->getPosition(fX, fY); m_pNetwork->updatePlatform(fY, m_pPlatform[0]->getVelocity(), Platform::OFF); } } // sync the platforms //m_pPlatform[1]->setVelocity(m_pPlatform[0]->getVelocity()); float fX, fY; m_pPlatform[0]->getPosition(fX, fY); float fX2, fY2; m_pPlatform[1]->getPosition(fX2, fY2); float fBallX, fBallY; m_pBall->getPosition(fBallX, fBallY); #if 0 if (fBallX < fX + PLATFORM_W) { if (fBallY + BALL_SIZE < fY || fBallY - BALL_SIZE > fY + PLATFORM_H) { MessageBoxA(NULL, "lose tbh", "", 0); ExitProcess(0); } else { float fVelX, fVelY; m_pBall->getVelocity(fVelX, fVelY); m_pBall->setVelocity(-fVelX, fVelY); } } if (fBallX + BALL_SIZE > fX2) { if (fBallY + BALL_SIZE < fY2 || fBallY - BALL_SIZE > fY2 + PLATFORM_H) { MessageBoxA(NULL, "lose tbh", "", 0); ExitProcess(0); } else { float fVelX, fVelY; m_pBall->getVelocity(fVelX, fVelY); m_pBall->setVelocity(-fVelX, fVelY); } } #endif if (m_pPlatform[0]->process()) { bRedraw = true; bSendPlatform = true; } if (m_pPlatform[1]->process()) bRedraw = true; if (m_pBall->process()) bRedraw = true; } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { m_pKeyboard->handleKeyDown(ev.keyboard.keycode); } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { m_pKeyboard->handleKeyUp(ev.keyboard.keycode); } } if (/*bRedraw && */al_is_event_queue_empty(m_pEventQueue)) { bRedraw = false; m_pGraphics->draw(); for (int i = 0; i < 2; ++i) { m_pPlatform[i]->draw(); } m_pBall->draw(); al_flip_display(); // some platform interpolation? if (m_pNetwork->m_iAvailBuffers == 2) { float fNow = Shared::getCurrentTime(); float fFirstTime = m_pNetwork->m_fPosBufferTime[1]; float fFirstVal = m_pNetwork->m_fPosBuffer[1]; float fNewTime = m_pNetwork->m_fPosBufferTime[0]; float fNewVal = m_pNetwork->m_fPosBuffer[0]; float fDelta = fNow - fNewTime; if (fDelta <= fNewTime - fFirstTime) { float fInterpPos = fFirstVal + ((fNewVal - fFirstVal) / (fNewTime - fFirstTime)) * fDelta; /*char tmp[128]; sprintf(tmp, "%f", fDelta); m_pGraphics->showStatusMessage(tmp);*/ float fX, fY; m_pPlatform[1]->getPosition(fX, fY); m_pPlatform[1]->setPosition(fX, fInterpPos); } } } } while (m_bRunning); }
int main (int argc, char **argv) { Mario mario; ALLEGRO_BITMAP *mario_spritesheet = NULL; bool tecla[4] = { false, false, false, false }; int sprite_number = 0; srand (time (NULL)); iniciar_allegro (); mario_spritesheet = al_load_bitmap (SPRITESHEET); if (!mario_spritesheet) { al_show_native_message_box (display, "Error", "Error", "No se ha podido crear el bitmap", NULL, ALLEGRO_MESSAGEBOX_ERROR); al_destroy_timer (timer); al_destroy_display (display); exit (EXIT_FAILURE); } mario.set_dibujo (mario_spritesheet); while (1) { /* Buzz Lightyear */ ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT timeout; al_init_timeout (&timeout, 0.06); bool get_event = al_wait_for_event_until (event_queue, &ev, &timeout); if (get_event) { if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (ev.type == ALLEGRO_EVENT_TIMER) redraw = true; if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: tecla[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: tecla[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: tecla[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: tecla[KEY_RIGHT] = true; break; } } if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: tecla[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: tecla[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: tecla[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: tecla[KEY_RIGHT] = false; break; case ALLEGRO_KEY_LSHIFT: sprite_number++; break; } } } /* Actualizar las coordenadas de la pelota */ if (tecla[KEY_UP]) mario.change_vy(-DELTA); if (tecla[KEY_DOWN]) mario.change_vy(DELTA); if (tecla[KEY_LEFT]) mario.change_vx(-DELTA); if (tecla[KEY_RIGHT]) mario.change_vx(DELTA); mario.actualizate(); if (redraw && al_is_event_queue_empty (event_queue)) { al_clear_to_color (al_map_rgb (0, 0, 0)); for (int i = 0; i < N; i++) al_draw_bitmap (mario.get_dibujo (), mario.get_x () + 450, mario.get_y () + 450, 0); al_flip_display (); redraw = false; } } al_destroy_bitmap (mario_spritesheet); destruir_allegro (); return 0; }
int main(int argc, char **argv){ int colora = 0, colorb = 0, colorc = 255; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; al_init(); /* if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; }*/ system("clear"); printf("\nIntroduzca 1: "); if(getchar() == 49){//Código ascii del 1 for(int i = 0; i < 10; i++){ display = al_create_display(640, 480); /* if(!display) { fprintf(stderr, "failed to create display!\n"); return -1; }*/ event_queue = al_create_event_queue(); /* if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_display(display); return -1; }*/ al_register_event_source(event_queue, al_get_display_event_source(display)); al_clear_to_color(al_map_rgb(colora, colorb, colorc)); al_flip_display(); while(1) { ALLEGRO_EVENT ev; ALLEGRO_TIMEOUT timeout; al_init_timeout(&timeout, 0.06); bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); if(get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } al_clear_to_color(al_map_rgb(colora,colorb,colorc)); al_flip_display(); } colora +=25; colorb +=25; colorc -=25; } } al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }