// |----------------------------------------------------------------------------| // | update() | // |----------------------------------------------------------------------------| int Input::update() { debug("Input: update() entered", 10); ALLEGRO_EVENT mouse_ev, keyboard_ev; bool mouse_error(false), keyboard_error(false); // Check for mouse related events if (!al_event_queue_is_empty(mouse_queue)) { mouse_error = !al_get_next_event(mouse_queue, &mouse_ev); if (!mouse_error) { // Process mouse location if(mouse_ev.type == ALLEGRO_EVENT_MOUSE_AXES || mouse_ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) { debug("Input: detected event - mouse movement", 9); mouse_x = mouse_ev.mouse.x; mouse_y = mouse_ev.mouse.y; } // Process clicks else if(mouse_ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { debug("Input: detected event - mouse down"); // Call appropriate functions, send the pertinent mouse button to called function if (current_screen) current_screen->onMouseDown(mouse_ev.mouse.button); } else if(mouse_ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { debug("Input: detected event - mouse up"); // Call appropriate functions, send the pertinent mouse button to called function if (current_screen) current_screen->onMouseUp(mouse_ev.mouse.button); } } } // Check for keyboard related events if (!al_event_queue_is_empty(keyboard_queue)) { keyboard_error = !al_get_next_event(keyboard_queue, &keyboard_ev); if (!keyboard_error) { // Process keypresses if(keyboard_ev.type == ALLEGRO_EVENT_KEY_DOWN) { debug("Input: detected event - key down"); // Call appropriate functions, send the pertinent keycode to called function if (current_screen) current_screen->onKeyDown(keyboard_ev.keyboard.keycode); } else if(keyboard_ev.type == ALLEGRO_EVENT_KEY_UP) { debug("Input: detected event - key up"); // Call appropriate functions, send the pertinent keycode to called function if (current_screen) current_screen->onKeyUp(keyboard_ev.keyboard.keycode); } } } debug("Input: update() exiting normally", 10); return error; }
bool AllegroEventSource::tick(State &, boost::posix_time::time_duration, boost::posix_time::time_duration) { ALLEGRO_EVENT evt; ALLEGRO_EVENT_QUEUE *q = m_impl->event_queue; while (!al_event_queue_is_empty(q) && al_get_next_event(q, &evt)) { dispatcher().queueEvent(std::make_shared<AllegroEvent>(evt)); } return true; }
void AllegroShell::run(){ // run the loop unsigned num = 0; ALLEGRO_EVENT ev; al_start_timer(timer); unsigned keyboard_count = 0; unsigned mouse_count = 0; unsigned timer_count = 0; while( run_flag ){ al_wait_for_event(queue,&ev); num++; printf("\r%d",num); if( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ run_flag = false; }else if( isKeyboardEvent(&ev)){ // handle keyboard input ++keyboard_count; al_get_keyboard_state(&(keyboard)); handle_keyboard(&ev); }else if( isMouseEvent(&ev) ){ // handle mouse input ++mouse_count; mouse->update(&ev); handle_mouse(&ev); }else if( ev.type == ALLEGRO_EVENT_TIMER){ ++timer_count; // Update the model only if we haven't drawn the scene if(draw_flag == false){ if(step_once_flag){ // always step once if the user requests it model->step(); step_once_flag = false; }else if(step_flag) { model->step(); } } draw_flag = true; }else if (ev.type == USER_VIEW_EVENT){ // handling user events al_unref_user_event(&ev.user); } if( draw_flag && al_event_queue_is_empty(queue)){ draw(); draw_flag = false; } } // end while(run_flag) Textlog::get().log("\nkeyboard_count = %u\n",keyboard_count); Textlog::get().log("mouse_count = %u\n",mouse_count); Textlog::get().log("timer_count = %u\n",timer_count); }
/* Run the FPS test. */ static void run(void) { ALLEGRO_EVENT event; int frames = 0; double start; target = al_create_bitmap(W, H); al_set_target_bitmap(target); al_clear_to_color(al_map_rgba_f(1, 1, 0, 1)); al_set_target_bitmap(al_get_backbuffer()); dx = 81; dy = 63; start = al_current_time(); while (true) { /* Check for ESC key or close button event and quit in either case. */ if (!al_event_queue_is_empty(queue)) { while (al_get_next_event(queue, &event)) { switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: quit = true; goto done; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) quit = true; goto done; if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) goto done; break; } } } draw(); print(0, 0, "FPS: %.1f", frames / (al_current_time() - start)); if (al_get_new_bitmap_flags() & ALLEGRO_FORCE_LOCKING) { print(0, al_get_font_line_height(myfont), "using forced bitmap locking"); } else { print(0, al_get_font_line_height(myfont), "drawing directly to bitmap"); } print(0, al_get_font_line_height(myfont) * 2, "Press SPACE to toggle drawing method."); al_flip_display(); frames++; } done: al_destroy_bitmap(target); }
bool CharacterSelection::run () { bool to_paint; ALLEGRO_EVENT event; while (1) { do { al_wait_for_event(global.queue, &event); switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: Utils::terminate(); break; case ALLEGRO_EVENT_TIMER: to_paint = true; n_frame ++; break; case ALLEGRO_EVENT_KEY_DOWN: case ALLEGRO_EVENT_KEY_REPEAT: switch (event.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: case ALLEGRO_KEY_X: return false; case ALLEGRO_KEY_LEFT: character = (CharacterID)((character + N_CHARACTER - 1) % N_CHARACTER); break; case ALLEGRO_KEY_RIGHT: character = (CharacterID)((character + 1) % N_CHARACTER); break; case ALLEGRO_KEY_ENTER: case ALLEGRO_KEY_Z: return true; } }; } while (!al_event_queue_is_empty(global.queue)); if (to_paint) { al_clear_to_color(global.color_black); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1.0, 1.0, 1.0, 0.3)); al_draw_bitmap(global.bitmap_char_big[(int)character], 0, 0, 0); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(0.7, 0.7, 1.0, 0.9)); al_draw_textf(global.font_menu_title, al_get_display_width() / 2, 20, ALLEGRO_ALIGN_CENTRE, "Character Selection"); al_draw_textf(global.font_menu_title, 80, 420, ALLEGRO_ALIGN_CENTRE, "[-"); al_draw_textf(global.font_menu_title, 320, 420, ALLEGRO_ALIGN_CENTRE, "%d\\%d", (int)character + 1, N_NORMAL_CHARACTER); al_draw_textf(global.font_menu_title, 560, 420, ALLEGRO_ALIGN_CENTRE, "-]"); CHARACTER_INTRO[(int)character](); al_flip_display(); } } }
// Funcao que estabelece o loop principal do programa. Aqui se fica a espera // dos eventos (de qualquer fonte de eventos) para fazer o tratamento de cada // um deles. Enquanto não houver evento, o programa fica "parado", consumindo // pouco tempo de CPU. static void main_loop(void) { ALLEGRO_EVENT event; while (true) { // Verifica se a fila de eventos esta vazia para fazer o "refresh" da tela if (al_event_queue_is_empty(event_queue)) { draw_sprite(); al_flip_display(); } // Espera ate que a fila de eventos nao esteja vazia. O primeiro evento // da fila será copiado em "event" e removido da fila. al_wait_for_event(event_queue, &event); // Verifica qual foi o evento gerado e faz o tratamento adequado. // ALLEGRO_EVENT eh uma union, e a interpretacao de seu conteudo // depende do tipo de evento, que eh definido no campo "type". // Cada evento vem de uma fonte (mouse, joystick ou teclado, // por exemplo), que pode ser acessada por "event.any.source". switch (event.type) { // Indica que o botao de fechar da janela foi pressionado. case ALLEGRO_EVENT_DISPLAY_CLOSE: return; // Encerra a execucao do programa break; // Indica que uma tecla foi pressionada. // O campo "keycode" do evento "keyboard" contem uma constante que // representa a tecla. case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) return; // Encerra a execucao do programa break; // Indica que o contador do timer foi incrementado. case ALLEGRO_EVENT_TIMER: update(); break; } } }
int main(void) { ALLEGRO_THREAD *thread[NUM_THREADS]; ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; bool need_draw; int i; for (i = 0; i < 256; i++) { sin_lut[i] = 128 + (int) (127.0 * sin(i / 8.0)); } if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_install_keyboard(); al_install_mouse(); display = al_create_display(W * IMAGES_PER_ROW, H * NUM_THREADS / IMAGES_PER_ROW); if (!display) { abort_example("Error creating display\n"); return 1; } timer = al_install_timer(1.0/3); if (!timer) { abort_example("Error creating timer\n"); return 1; } queue = al_create_event_queue(); if (!queue) { abort_example("Error creating event queue\n"); return 1; } al_register_event_source(queue, al_get_display_event_source(display)); 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_timer_event_source(timer)); /* Note: * Right now, A5 video displays can only be accessed from the thread which * created them (at lesat for OpenGL). To lift this restriction, we could * keep track of the current OpenGL context for each thread and make all * functions accessing the display check for it.. not sure it's worth the * additional complexity though. */ al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_RGB_888); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); for (i = 0; i < NUM_THREADS; i++) { thread_info[i].bitmap = al_create_bitmap(W, H); if (!thread_info[i].bitmap) { goto Error; } thread_info[i].mutex = al_create_mutex(); if (!thread_info[i].mutex) { goto Error; } thread_info[i].cond = al_create_cond(); if (!thread_info[i].cond) { goto Error; } thread_info[i].is_paused = false; thread_info[i].random_seed = i; thread[i] = al_create_thread(thread_func, &thread_info[i]); if (!thread[i]) { goto Error; } } set_target(0, -0.56062033041600878303, -0.56064322926933807256); set_target(1, -0.57798076669230014080, -0.63449861991138123418); set_target(2, 0.36676836392830602929, -0.59081385302214906030); set_target(3, -1.48319283039401317303, -0.00000000200514696273); set_target(4, -0.74052910500707636032, 0.18340899525730713915); set_target(5, 0.25437906525768350097, -0.00046678223345789554); set_target(6, -0.56062033041600878303, 0.56064322926933807256); set_target(7, -0.57798076669230014080, 0.63449861991138123418); set_target(8, 0.36676836392830602929, 0.59081385302214906030); for (i = 0; i < NUM_THREADS; i++) { al_start_thread(thread[i]); } al_start_timer(timer); need_draw = true; while (true) { if (need_draw && al_event_queue_is_empty(queue)) { show_images(); need_draw = false; } al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_TIMER) { need_draw = true; } else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { int n = (event.mouse.y / H) * IMAGES_PER_ROW + (event.mouse.x / W); if (n < NUM_THREADS) { double x = event.mouse.x - (event.mouse.x / W) * W; double y = event.mouse.y - (event.mouse.y / H) * H; /* Center to the mouse click position. */ if (thread_info[n].is_paused) { thread_info[n].target_x = x / W - 0.5; thread_info[n].target_y = y / H - 0.5; } toggle_pausedness(n); } } else if (event.type == ALLEGRO_EVENT_DISPLAY_EXPOSE) { need_draw = true; } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } need_draw = true; } } for (i = 0; i < NUM_THREADS; i++) { /* Set the flag to stop the thread. The thread might be waiting on a * condition variable, so signal the condition to force it to wake up. */ al_set_thread_should_stop(thread[i]); al_lock_mutex(thread_info[i].mutex); al_broadcast_cond(thread_info[i].cond); al_unlock_mutex(thread_info[i].mutex); /* al_destroy_thread() implicitly joins the thread, so this call is not * strictly necessary. */ al_join_thread(thread[i], NULL); al_destroy_thread(thread[i]); } al_destroy_event_queue(queue); al_uninstall_timer(timer); al_destroy_display(display); return 0; Error: return 1; }
static int allua_Event_queue_is_empty(lua_State * L) { ALLUA_event_queue event_queue = allua_check_event_queue(L, 1); lua_pushboolean(L, al_event_queue_is_empty(event_queue)); return 1; }
void engine_run(struct State *first) { int redraw = FALSE; if (engine_active) { puts("WARNING: Calling game_run() more than once"); return; } change_state(first); // Generate display events al_register_event_source(engine.event_queue, al_get_display_event_source(engine.display)); // Timer events al_register_event_source(engine.event_queue, al_get_timer_event_source(engine.timer)); // Keyboard events al_register_event_source(engine.event_queue, al_get_keyboard_event_source()); al_start_timer(engine.timer); engine_active = TRUE; // Main game loop while (engine_active) { ALLEGRO_EVENT event; al_wait_for_event(engine.event_queue, &event); // Event processing engine.states[current_state]->_events(&event); // If the close button was pressed... if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { engine_active = FALSE; break; } else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { keys[event.keyboard.keycode] = TRUE; // Escape key will end the game if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { engine_active = FALSE; break; } } else if (event.type == ALLEGRO_EVENT_KEY_UP) { keys[event.keyboard.keycode] = FALSE; } else if (event.type == ALLEGRO_EVENT_TIMER) { engine.states[current_state]->_update(); redraw = TRUE; } if (redraw && al_event_queue_is_empty(engine.event_queue)) { redraw = FALSE; al_set_target_backbuffer(engine.display); al_clear_to_color(engine.bg_color); engine.states[current_state]->_draw(); al_flip_display(); } } while (current_state >= 0) { engine.states[current_state--]->_end(); } al_destroy_display(engine.display); al_destroy_timer(engine.timer); al_destroy_event_queue(engine.event_queue); al_destroy_font(font); }
int main() { //shell vars bool render = false; //allegro vars ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; //allegro init functions printf ("Initializing allegro\n"); if (!al_init()) { al_show_native_message_box(NULL, NULL, NULL, "failed", NULL, 0); return -1; } printf("Creating display\n"); display = al_create_display(WIDTH, HEIGHT); if (!display) { al_show_native_message_box(NULL, NULL, NULL, "failed", NULL, 0); return -1; } printf("Installing addons\n"); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); al_init_image_addon(); al_install_keyboard(); al_install_mouse(); al_install_audio(); al_init_acodec_addon(); al_reserve_samples(10); //project inits srand(time(NULL)); printf("Initializing timer\n"); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); printf("Registering event sources\n"); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); printf("Init mouse and keyboard\n"); init_keyboard(); init_mouse(); printf("Loading assets\n"); load_bitmaps(); load_fonts(); load_samples(); printf ("Creating manager\n"); push_state(new TitleMenu()); printf("Beginning game\n"); while (!is_game_over()) { //declare an event ALLEGRO_EVENT event; //monitor event sources al_wait_for_event(event_queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { end_game(); } else if (event.type == ALLEGRO_EVENT_TIMER) { render = true; update_mouse(); update_keyboard(); handle_key(); update_game(); } // Render screen if (render && al_event_queue_is_empty(event_queue)) { render = false; render_game(); al_flip_display(); } } unload_assets(); al_destroy_event_queue(event_queue); al_destroy_display(display); al_destroy_timer(timer); return 0; }
// The main Allegro loop, all input handling, animation and drawing is done here _Bool main_loop(void) { // Flag for drawing _Bool needredraw = true; // Declare primitive data types float old_time = 0.0, current_time = 0, dt = 0; while(!data->exit) { if(needredraw && al_event_queue_is_empty(data->queue) && al_event_queue_is_empty(data->queue2)) { // Clear, draw, flip al_clear_to_color(data->background_color); render(); if(get_fps_status()) al_draw_textf(data->font, data->text_color, 0, res_height-30, 0, "fps: %.2f", 1/dt); al_flip_display(); needredraw = false; } // Block until an event enters the queue al_wait_for_event(data->queue, &(data->event)); while(!al_event_queue_is_empty(data->queue2)) { al_get_next_event(data->queue2, &(data->event2)); switch(data->event2.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: { // If x button is pressed on window data->exit = true; data->gamestarted = false; } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: { al_acknowledge_resize(data->event2.display.source); scale(res_width, res_height); } break; case ALLEGRO_EVENT_MOUSE_AXES: { // Stores the mouse's new position and change in position mouseaxes(&(data->event2.mouse)); } case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: { // Stores the mouse button pressed mousedown(&(data->event2.mouse)); } break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: { // Stores the mouse button released mouseup(&(data->event2.mouse)); } break; case ALLEGRO_EVENT_KEY_DOWN: { // Stores keydown keycode into keycode array for processing keydown(&(data->event2.keyboard)); } break; case ALLEGRO_EVENT_KEY_UP: { // Stores keycode into keycode array for processing keyup(&(data->event2.keyboard)); } break; default: break; } } switch (data->event.type) { case ALLEGRO_EVENT_TIMER: { // Determine the change in time between frames, in seconds current_time = al_current_time(); dt = current_time-old_time; // If the computer lags for some reason, don't penalize the player // Cap dt at 0.5 seconds if(dt > 0.25) { dt = 0.25; } // Handle Mouse and Keyboard events and Button Events buttoncheck(&buttonhandler, data); keycheck(&keyhandler, data); mousecheck(&mousehandler, data); keyupdate(); mouseupdate(); // Check if data->quit has been set before updating and drawing if(data->exit) break; // Update the game, always update(); // Skip drawing frames if computer lags if(current_time - data->event.timer.timestamp <= 1.0/al_get_display_refresh_rate(data->display)) { needredraw = true; } // Make the time at this frame the old time, for the next frame old_time = current_time; } break; default: break; } } return true; }
int main(int argc, char** argv) { time_t t; srand((unsigned) time(&t)); if (!al_init()) { std::cout << "Failed to start Allegro."; return -1; } if (!al_init_image_addon()) { std::cout << "Failed to start Allegro Imagea person who gives information to the police or to some other authority about the bad behavior or criminal activity of someone else Addon.\n"; return -1; } if (!al_init_primitives_addon()) { std::cout << "Failed to start Allegro Primitives Addon.\n"; return -1; } if (!al_init_font_addon()) { std::cout << "Failed to start Allegro Font Addon.\n"; return -1; } if (!al_init_ttf_addon()) { std::cout << "Failed to start Allegro TTF Addon.\n"; return -1; } ALLEGRO_DISPLAY* main_window = al_create_display(800, 600); if (!main_window) { std::cout << "Failed to create display."; return -1; } ALLEGRO_EVENT_QUEUE* eq = al_create_event_queue(); if (!eq) { std::cout << "Failed to create event queue."; return -1; } ALLEGRO_TIMER* fps_timer = al_create_timer(1.0 / 60); if (!fps_timer) { std::cout << "Failed to create timer."; return -1; } ALLEGRO_TIMER* count_timer = al_create_timer(1.0); if (!count_timer) { std::cout << "Failed to create count timer."; return -1; } if (!al_install_keyboard()) { std::cout << "Failed to install keyboard."; return -1; } if (!al_install_mouse() || !al_set_mouse_cursor(main_window, al_create_mouse_cursor(al_load_bitmap("resources/sprites/UI/cursor/clicker.png"), 16, 31))) { std::cout << "Failed to install mouse."; return -1; } al_register_event_source(eq, al_get_timer_event_source(fps_timer)); al_register_event_source(eq, al_get_timer_event_source(count_timer)); al_register_event_source(eq, al_get_keyboard_event_source()); al_register_event_source(eq, al_get_mouse_event_source()); al_register_event_source(eq, al_get_display_event_source(main_window)); Game base; if (!base.init()) { std::cout << "Failed to initialise game! Quitting...\n"; return -1; } bool ready_to_draw = false; bool ready_to_draw_fps = false; al_start_timer(fps_timer); al_start_timer(count_timer); int fps_count = 0; ALLEGRO_FONT* fps_font = al_load_ttf_font("resources/fonts/MontereyFLF.ttf", 11, 0); if (!fps_font) { std::cout << "Failed to load font for fps counter!"; return -1; } while (true) { while (!al_event_queue_is_empty(eq)) { ALLEGRO_EVENT ev; al_get_next_event(eq, &ev); if (ev.type == ALLEGRO_EVENT_TIMER) { if (ev.timer.source == fps_timer) { ready_to_draw = true; } else if (ev.timer.source == count_timer) { ready_to_draw_fps = true; } } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { std::cout << "Safely quitting."; al_destroy_display(main_window); al_destroy_event_queue(eq); al_destroy_timer(fps_timer); return 0; } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) //current keyboard input section { std::cout << "Safely quitting."; al_destroy_display(main_window); al_destroy_event_queue(eq); al_destroy_timer(fps_timer); return 0; } } base.runEvents(ev); } if (ready_to_draw) { fps_count++; base.runLogic(); base.runDisplay(); char buffer[25]; if (ready_to_draw_fps) { sprintf(buffer, "%d", fps_count); fps_count = 0; ready_to_draw_fps = false; } al_draw_text(fps_font, al_map_rgb(0,255,0), 5, 5, 0, buffer); al_flip_display(); ready_to_draw = false; } } }
static void go(void) { ALLEGRO_DISPLAY *d1, *d2; ALLEGRO_BITMAP *b1, *b2; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; al_set_new_display_flags(ALLEGRO_FULLSCREEN); al_set_current_video_adapter(0); d1 = al_create_display(640, 480); if (!d1) { printf("Error creating first display\n"); return; } b1 = al_load_bitmap("data/mysha.pcx"); if (!b1) { printf("Error loading mysha.pcx\n"); return; } al_set_current_video_adapter(1); d2 = al_create_display(640, 480); if (!d2) { printf("Error creating second display\n"); return; } b2 = al_load_bitmap("data/allegro.pcx"); if (!b2) { printf("Error loading allegro.pcx\n"); return; } queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); while (1) { if (!al_event_queue_is_empty(queue)) { al_get_next_event(queue, &event); if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } } } al_set_current_display(d1); al_draw_scaled_bitmap(b1, 0, 0, 320, 200, 0, 0, 640, 480, 0); al_flip_display(); al_set_current_display(d2); al_draw_scaled_bitmap(b2, 0, 0, 320, 200, 0, 0, 640, 480, 0); al_flip_display(); al_rest(0.1); } al_destroy_bitmap(b1); al_destroy_bitmap(b2); al_destroy_display(d1); al_destroy_display(d2); }
void StateControl::start_radio () { Initialize(); states.push_back(new LoginState()); states.push_back(new SignUpState()); states.push_back(new AdminAccState()); states.push_back(new RegularAccState()); states.push_back(new ManageMusicsState()); states.push_back(new AddMusicState()); states.push_back(new EditMusicState()); states.push_back(new TopTenState()); states.push_back(new MyPlaylistState()); states.push_back(new SearchState()); states.push_back(new CompetitionState()); state = -1; ChangeState(Login); cout << "Starting control cycle..." << endl; done = false; draw = true; while (!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, & ev); /* --- controlling left mouse button double press --- */ if (possible_double_press && double_press_counter < 15) { double_press_counter++; } else if (possible_double_press) { possible_double_press = false; double_press_counter = 0; } /* --- advancing through music --- */ if (ev.timer.source == timer && playing_music && music_time_counter < 130) { music_time_counter++; } else if (ev.timer.source == timer && playing_music) { playing_music = false; music_time_counter = 0; } /* --- tracking mouse --- */ if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) { mouse_x = ev.mouse.x; mouse_y = ev.mouse.y; draw = true; } if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { if (ev.mouse.button & 1) { left_mouse_button_pressed = true; left_mouse_button_released = false; draw = true; } if (ev.mouse.button & 2) { right_mouse_button_pressed = true; right_mouse_button_released = false; draw = true; } } if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { if (ev.mouse.button & 1) { if (possible_double_press) { cout << "* Left mouse button pressed twice *" << endl; left_mouse_button_pressed_twice = true; } else { cout << "* Left mouse button pressed *" << endl; } possible_double_press = true; left_mouse_button_pressed = false; left_mouse_button_released = true; draw = true; } if (ev.mouse.button & 2) { cout << "* Right mouse button pressed *" << endl; right_mouse_button_pressed = false; right_mouse_button_released = true; draw = true; } } /* --- UPDATING --- */ if (states[state]->Update(& ev) || ev.type == ALLEGRO_EVENT_TIMER || draw) { StateControl::GetInstance()->left_mouse_button_released = false; StateControl::GetInstance()->left_mouse_button_pressed_twice = false; StateControl::GetInstance()->right_mouse_button_released = false; draw = true; } /* --- now drawing --- */ if (draw && al_event_queue_is_empty(event_queue)) { states[state]->Draw(); /* player active UI */ if (state != Login && state != SignUp && state != AddMusic && state != EditMusic && state != ManageMusics && state != Competition) { al_draw_text(musics_font, DarkGray, 10, player_interface_y_coord, NULL, "Playing:"); al_draw_line(10, 420, 140, 420, DarkGray, 2.0); switch (playing_music) { case 0: { al_draw_text(tiny_font, DarkGray, 10, player_interface_y_coord + 20, NULL, "<Double-click a music>"); al_draw_circle(10, player_interface_y_coord + 40, 5, LightGray, 1.0); al_draw_filled_circle(10, player_interface_y_coord + 40, 4, White); break; } case 1: { al_draw_text(tiny_font, DarkGray, 10, player_interface_y_coord + 20, NULL, music_playing_title.c_str()); al_draw_circle(10 + music_time_counter, player_interface_y_coord + 40, 5, LightGray, 1.0); al_draw_filled_circle(10 + music_time_counter, player_interface_y_coord + 40, 4, White); break; } } } /* // mouse temp coords stringstream ss; ss << mouse_x << " " << mouse_y; al_draw_text(forms_font, Yellow, 0, 0, NULL, ss.str().c_str()); // ----------------- */ al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); draw = false; } /* if window is closed */ if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { cout << "Close button pressed..." << endl; done = true; } } Terminate(); }
int main(void) { GLuint pid; ALLEGRO_DISPLAY *d; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; int frames = 0; double start; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_set_new_display_flags(ALLEGRO_OPENGL); al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); d = al_create_display(WINDOW_W, WINDOW_H); if (!d) { abort_example("Unable to open a OpenGL display.\n"); return -1; } if (al_get_display_option(ALLEGRO_SAMPLE_BUFFERS)) { printf("With multisampling, level %i\n", al_get_display_option(ALLEGRO_SAMPLES)); } else { printf("Without multisampling.\n"); } al_install_keyboard(); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(d)); if (al_get_opengl_extension_list()->ALLEGRO_GL_ARB_multisample) { glEnable(GL_MULTISAMPLE_ARB); } if (!al_get_opengl_extension_list()->ALLEGRO_GL_ARB_vertex_program) { abort_example("This example requires a video card that supports " " the ARB_vertex_program extension.\n"); return -1; } glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDisable(GL_CULL_FACE); /* Setup projection and modelview matrices */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, WINDOW_W/(float)WINDOW_H, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* Position the camera to look at our mesh from a distance */ gluLookAt(0.0f, 20.0f, -45.0f, 0.0f, 0.0f, 0.0f, 0, 1, 0); create_mesh(); /* Define the vertex program */ glEnable(GL_VERTEX_PROGRAM_ARB); glGenProgramsARB(1, &pid); glBindProgramARB(GL_VERTEX_PROGRAM_ARB, pid); glGetError(); if (al_get_opengl_extension_list()->ALLEGRO_GL_NV_vertex_program2_option) { glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(program_nv), program_nv); } else { glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(program), program); } /* Check for errors */ if (glGetError()) { const char *pgm = al_get_opengl_extension_list()->ALLEGRO_GL_NV_vertex_program2_option ? program_nv : program; GLint error_pos; const GLubyte *error_str = glGetString(GL_PROGRAM_ERROR_STRING_ARB); glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); abort_example("Error compiling the vertex program:\n%s\n\nat " "character: %i\n%s\n", error_str, (int)error_pos, pgm + error_pos); return -1; } start = al_current_time(); while (1) { if (!al_event_queue_is_empty(queue)) { while (al_get_next_event(queue, &event)) { switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: goto done; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) goto done; break; } } } draw_mesh(); al_flip_display(); frames++; } done: printf("%.1f FPS\n", frames / (al_current_time() - start)); glDeleteProgramsARB(1, &pid); al_destroy_event_queue(queue); 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 done =false; bool redraw = true; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_TIMER *timer =NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; SpaceShip ship; Bullet bullet[num_bullets]; Commet commet[num_commets]; if(!al_init()) { fprintf(stderr,"allegro is not intialized"); return -1; } display = al_create_display(SCREEN_W,SCREEN_H); if(!display) { fprintf(stderr,"failed to intialize the display"); return -1; } timer = al_create_timer(1.0/FPS); if(!timer) { fprintf(stderr,"failed to intialize timer"); al_destroy_display(display); return -1; } event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr,"failed to create event queue"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_install_keyboard(); al_register_event_source(event_queue,al_get_timer_event_source(timer)); al_register_event_source(event_queue,al_get_keyboard_event_source()); al_register_event_source(event_queue,al_get_display_event_source(display)); init_ship(&ship); init_bullet(&bullet); init_commet(&commet); al_start_timer(timer); while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue,&ev); if(ev.type == ALLEGRO_EVENT_TIMER) { if(keys[KEY_UP]) { move_ship_up(&ship); } if(keys[KEY_DOWN]) { move_ship_down(&ship); }if(keys[KEY_LEFT]) { move_ship_left(&ship); } if(keys[KEY_RIGHT]) { move_ship_right(&ship); } if(keys[KEY_SPACEBAR]) { fire_bullet(&bullet,&ship); } if(!GameOver) { update_bullet(&bullet); update_commet(&commet); bullet_collision(&bullet,&commet); } redraw = true; } else if(ev.type==ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: { keys[KEY_UP]=true; break; } case ALLEGRO_KEY_DOWN: { keys[KEY_DOWN]=true; break; } case ALLEGRO_KEY_LEFT: { keys[KEY_LEFT]=true; break; } case ALLEGRO_KEY_RIGHT : { keys[KEY_RIGHT]=true; break; } case ALLEGRO_KEY_SPACE: { keys[KEY_SPACEBAR] = true; break; } } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: { keys[KEY_UP]=false; break; } case ALLEGRO_KEY_DOWN: { keys[KEY_DOWN]=false; break; } case ALLEGRO_KEY_LEFT: { keys[KEY_LEFT]=false; break; } case ALLEGRO_KEY_RIGHT : { keys[KEY_RIGHT]=false; break; } case ALLEGRO_KEY_SPACE: { keys[KEY_SPACEBAR]=false; break; } } } if(redraw && al_event_queue_is_empty(event_queue)) { redraw = false ; al_clear_to_color(al_map_rgb(255,100,100)); draw_ship(&ship); draw_bullet(bullet); draw_commet(commet); al_flip_display(); al_clear_to_color(al_map_rgb(255,100,100)); } } al_destroy_display(display); al_destroy_timer(timer); al_destroy_event_queue(event_queue); return 0; }
void Game::run () { bool to_paint; ALLEGRO_EVENT event; while (1) { do { al_wait_for_event(global.queue, &event); switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: Utils::terminate(); break; case ALLEGRO_EVENT_TIMER: to_paint = true; if (!paused) time_walk (); break; case ALLEGRO_EVENT_KEY_DOWN: case ALLEGRO_EVENT_KEY_REPEAT: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) paused = !paused; }; } while (!al_event_queue_is_empty(global.queue)); if (to_paint) { al_clear_to_color(global.color_black); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1.0, 1.0, 1.0, 0.3)); al_draw_bitmap(global.bitmap_bga[0], GAME_X1, n_frame - al_get_bitmap_height(global.bitmap_bgd[0]) - al_get_bitmap_height(global.bitmap_bgc[0]) - al_get_bitmap_height(global.bitmap_bgb[0]) - al_get_bitmap_height(global.bitmap_bga[0]) + GAME_H, 0); al_draw_bitmap(global.bitmap_bgb[0], GAME_X1, n_frame - al_get_bitmap_height(global.bitmap_bgd[0]) - al_get_bitmap_height(global.bitmap_bgc[0]) - al_get_bitmap_height(global.bitmap_bgb[0]) + GAME_H, 0); al_draw_bitmap(global.bitmap_bgc[0], GAME_X1, n_frame - al_get_bitmap_height(global.bitmap_bgd[0]) - al_get_bitmap_height(global.bitmap_bgc[0]) + GAME_H, 0); al_draw_bitmap(global.bitmap_bgd[0], GAME_X1, n_frame - al_get_bitmap_height(global.bitmap_bgd[0]) + GAME_H, 0); if (gamedata.fl_invincible && n_frame % 6 < 3) { al_set_blender(ALLEGRO_ALPHA, ALLEGRO_ONE, al_map_rgba_f(0.0, 0.0, 1.0, 1.0)); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); } else { /* if (gamedata.character == CHAR_SILK) { al_set_blender(ALLEGRO_ALPHA, ALLEGRO_ONE, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); } else */ { al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); } } al_set_blender(ALLEGRO_ALPHA, ALLEGRO_ONE, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_crd[gamedata.character]), 0); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); for (int i = 0; i < gamedata.n_pbullet; i ++) gamedata.pbullet[i] -> draw (); for (int i = 0; i < gamedata.n_ebullet; i ++) gamedata.ebullet[i] -> draw (); for (int i = 0; i < gamedata.n_enemy; i ++) gamedata.enemy[i] -> draw (); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); al_draw_bitmap(global.bitmap_game, 1, 1, 0); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(0.7, 0.7, 1.0, 0.9)); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 50, ALLEGRO_ALIGN_LEFT, "HISCORE"); al_draw_textf(global.font_game_score, SCREEN_W - 20, GAME_Y1 + 50, ALLEGRO_ALIGN_RIGHT, "0"); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 75, ALLEGRO_ALIGN_LEFT, "SCORE"); al_draw_textf(global.font_game_score, SCREEN_W - 20, GAME_Y1 + 75, ALLEGRO_ALIGN_RIGHT, "0"); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 110, ALLEGRO_ALIGN_LEFT, "Life"); { char buf[128]; int tmp = 0, bp = 0; while (tmp + HP_PER_LIFE <= gamedata.hp) { buf[bp ++] = '@'; tmp += HP_PER_LIFE; } while (tmp + 1 <= gamedata.hp) { buf[bp ++] = '.'; tmp += HP_PER_LIFE; } buf[bp] = 0; al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 110, ALLEGRO_ALIGN_LEFT, buf); } al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 135, ALLEGRO_ALIGN_LEFT, "Power"); al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 135, ALLEGRO_ALIGN_LEFT, "%d.%02d \\ 5.00", gamedata.power / 100, gamedata.power % 100); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 160, ALLEGRO_ALIGN_LEFT, "Skill"); al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 160, ALLEGRO_ALIGN_LEFT, "17234 \\ 1500"); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 195, ALLEGRO_ALIGN_LEFT, "Point"); al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 195, ALLEGRO_ALIGN_LEFT, "20000"); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 220, ALLEGRO_ALIGN_LEFT, "Graze"); al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 220, ALLEGRO_ALIGN_LEFT, "%d", gamedata.graze); { char buf[128]; sprintf(buf, "%2.2lf", global.calculated_fps); al_draw_textf(global.font_game_score, SCREEN_W, SCREEN_H - 20, ALLEGRO_ALIGN_RIGHT, buf); } { double cur_time = al_current_time (); global.calculated_fps = FPS / (cur_time - global.time_previous[d_frame % FPS]); global.time_previous[d_frame % FPS] = cur_time; d_frame ++; } al_flip_display(); to_paint = false; } } }
int main() { ALLEGRO_DISPLAY* display = NULL; ALLEGRO_DISPLAY_MODE disp; ALLEGRO_EVENT_QUEUE* eq = NULL; ALLEGRO_TIMER* timer = NULL; ALLEGRO_TIMER* ball_timer = NULL; bool run = true; bool draw = false; if (!al_init()) { return -1; } al_get_display_mode(al_get_num_display_modes() - 1, &disp); std::cout << disp.height << " " << height << std::endl; std::cout << disp.width << " " << width << std::endl; height = disp.height / 3 * 2; width = height / 9 * 16; std::cout << disp.height << " " << height << std::endl; std::cout << disp.width << " " << width << std::endl; al_set_new_display_flags(ALLEGRO_FULLSCREEN); display = al_create_display(width, height); al_set_window_title(display, "PONG V2"); if (display == NULL) { return -1; } init(&player1, &player2, &ball); eq = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); ball_timer = al_create_timer(15.0); al_install_keyboard(); al_init_primitives_addon(); al_init_font_addon(); al_init_ttf_addon(); font = al_load_ttf_font("./arial.ttf", 18, 0); scorefont = al_load_ttf_font("./arial.ttf", (height * width) / 36000, 0); al_register_event_source(eq, al_get_keyboard_event_source()); al_register_event_source(eq, al_get_display_event_source(display)); al_register_event_source(eq, al_get_timer_event_source(timer)); al_hide_mouse_cursor(display); al_start_timer(timer); while (run) { ALLEGRO_EVENT ev; al_wait_for_event(eq, &ev); if (ev.type == ALLEGRO_EVENT_TIMER) { draw = true; } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { run = false; } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_S: keys[P1][DOWN] = true; break; case ALLEGRO_KEY_W: keys[P1][UP] = true; break; case ALLEGRO_KEY_DOWN: keys[P2][DOWN] = true; break; case ALLEGRO_KEY_UP: keys[P2][UP] = true; break; case ALLEGRO_KEY_SPACE: start = true; firstGame = false; break; case ALLEGRO_KEY_ESCAPE: run = false; break; case ALLEGRO_KEY_P: if (pause == true) pause = false; else if (pause == false) pause = true; break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_S: keys[P1][DOWN] = false; break; case ALLEGRO_KEY_W: keys[P1][UP] = false; break; case ALLEGRO_KEY_DOWN: keys[P2][DOWN] = false; break; case ALLEGRO_KEY_UP: keys[P2][UP] = false; break; } } if (draw && al_event_queue_is_empty(eq)) { if (!pause) { if (keys[P1][UP])update(&player1, -1); if (keys[P1][DOWN])update(&player1, 1); if (keys[P2][UP])update(&player2, -1); if (keys[P2][DOWN])update(&player2, 1); update(&ball); updateGame(&player1, &player2, &ball); render(); draw = false; } } } al_destroy_display(display); al_destroy_event_queue(eq); return 0; }
// |----------------------------------------------------------------------------| // | isEmpty() | // |----------------------------------------------------------------------------| bool Input::isEmpty() { return al_event_queue_is_empty(keyboard_queue) && al_event_queue_is_empty(mouse_queue); }
int main (int argc, char *argv[]) { Ship* ship = NULL; ALLEGRO_TIMER *main_timer = NULL; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_COLOR cyan = al_map_rgb(0,255,255); ALLEGRO_BITMAP *space_background = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; bool redraw = true; ALLEGRO_EVENT ev; if(!al_init()) { fprintf(stderr, "Error initializing Allegro"); exit(EXIT_FAILURE); } if(!al_init_image_addon()) { fprintf(stderr, "Error initializing Allegro"); exit(EXIT_FAILURE); } if(!al_install_keyboard()) { fprintf(stderr, "Error initializing keyboard\n"); exit(EXIT_FAILURE); } display=al_create_display(W, H); if(display == NULL) { fprintf(stderr, "Error creating main display"); exit(EXIT_FAILURE); } space_background = al_load_bitmap(BACKGROUND_FILE); if(space_background == NULL) { fprintf(stderr, "Error loading background\n"); exit(EXIT_FAILURE); } ship=create_new_ship("../data/spaceship.png"); ship->current_dir = DOWN; al_set_target_backbuffer(display); al_draw_bitmap(space_background, 0, 0, 0); print_ship_on_screen(ship); al_flip_display(); main_timer=al_create_timer(1.0/FPS); if(main_timer == NULL) { fprintf(stderr, "Error creating main timer\n"); exit(EXIT_FAILURE); } event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_timer_event_source(main_timer)); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_start_timer(main_timer); while(1) { al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else { switch (ev.type) { case ALLEGRO_EVENT_TIMER: redraw = true; break; case ALLEGRO_EVENT_KEY_DOWN: switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: ship->is_moving = 1; ship->current_dir = UP; break; case ALLEGRO_KEY_DOWN: ship->is_moving = 1; ship->current_dir = DOWN; break; case ALLEGRO_KEY_LEFT: ship->is_moving = 1; ship->current_dir = LEFT; break; case ALLEGRO_KEY_RIGHT: ship->is_moving = 1; ship->current_dir = RIGHT; break; default: break; } break; case ALLEGRO_EVENT_KEY_UP: ship->is_moving = 0; break; default: break; } } if(redraw && al_event_queue_is_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(space_background, 0, 0, 0); move_ship(ship); animate_ship(ship); print_ship_on_screen(ship); al_flip_display(); } } al_destroy_timer(main_timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
/* Run our test. */ static void run(void) { ALLEGRO_EVENT event; float x, y; bool need_draw = true; while (1) { /* Perform frame skipping so we don't fall behind the timer events. */ if (need_draw && al_event_queue_is_empty(ex.queue)) { tick(); need_draw = false; } al_wait_for_event(ex.queue, &event); switch (event.type) { /* Was the X button on the window pressed? */ case ALLEGRO_EVENT_DISPLAY_CLOSE: return; /* Was a key pressed? */ case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) return; break; /* Is it time for the next timer tick? */ case ALLEGRO_EVENT_TIMER: need_draw = true; break; /* Mouse click? */ case ALLEGRO_EVENT_MOUSE_BUTTON_UP: x = event.mouse.x; y = event.mouse.y; if (x >= ex.BUTTONS_X) { int button = y / 20; if (button == 2) ex.image = 0; if (button == 3) ex.image = 1; if (button == 4) ex.image = 2; if (button == 5) ex.image = 3; if (button == 6) ex.image = 4; if (button == 7) ex.image = 5; if (button == 10) ex.mode = 0; if (button == 11) ex.mode = 1; if (button == 12) ex.mode = 2; if (button == 13) ex.mode = 3; if (button == 14) ex.mode = 4; if (button == 15) ex.mode = 5; if (button == 16) ex.mode = 6; if (button == 17) ex.mode = 7; if (button == 18) ex.mode = 8; if (button == 19) ex.mode = 9; if (button == 20) ex.mode = 10; } break; } } }
int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *cursor; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; ALLEGRO_FONT *font; int mx = 0; int my = 0; int mz = 0; int mw = 0; int mmx = 0; int mmy = 0; int mmz = 0; int mmw = 0; bool in = true; bool buttons[NUM_BUTTONS] = {false}; int i; float p = 0.0; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_install_mouse(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_set_new_display_flags(ALLEGRO_RESIZABLE); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); return 1; } al_hide_mouse_cursor(); cursor = al_load_bitmap("data/cursor.tga"); if (!cursor) { abort_example("Error loading cursor.tga\n"); return 1; } font = al_load_font("data/fixed_font.tga", 1, 0); if (!font) { abort_example("data/fixed_font.tga not found\n"); return 1; } queue = al_create_event_queue(); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); while (1) { if (al_event_queue_is_empty(queue)) { al_clear_to_color(al_map_rgb(0xff, 0xff, 0xc0)); for (i = 0; i < NUM_BUTTONS; i++) { draw_mouse_button(i, buttons[i]); } al_draw_bitmap(cursor, mx, my, 0); al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgb_f(0, 0, 0)); al_draw_textf(font, 5, 5, 0, "dx %i, dy %i, dz %i, dw %i", mmx, mmy, mmz, mmw); al_draw_textf(font, 5, 15, 0, "x %i, y %i, z %i, w %i", mx, my, mz, mw); al_draw_textf(font, 5, 25, 0, "p = %g", p); al_draw_textf(font, 5, 35, 0, "%s", in ? "in" : "out"); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA, al_map_rgb_f(1, 1, 1)); mmx = mmy = mmz = 0; al_flip_display(); } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_MOUSE_AXES: mx = event.mouse.x; my = event.mouse.y; mz = event.mouse.z; mw = event.mouse.w; mmx = event.mouse.dx; mmy = event.mouse.dy; mmz = event.mouse.dz; mmw = event.mouse.dw; p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: if (event.mouse.button-1 < NUM_BUTTONS) { buttons[event.mouse.button-1] = true; } p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: if (event.mouse.button-1 < NUM_BUTTONS) { buttons[event.mouse.button-1] = false; } p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY: in = true; break; case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY: in = false; break; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { goto done; } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_CLOSE: goto done; } } done: al_destroy_event_queue(queue); return 0; }