int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_EVENT_QUEUE *events; ALLEGRO_EVENT event; double last_resize; int rs = 100; /* Initialize Allegro and create an event queue. */ if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } events = al_create_event_queue(); /* Setup a display driver and register events from it. */ al_set_new_display_flags(ALLEGRO_RESIZABLE); display = al_create_display(rs, rs); al_register_event_source(events, al_get_display_event_source(display)); /* Setup a keyboard driver and regsiter events from it. */ al_install_keyboard(); al_register_event_source(events, al_get_keyboard_event_source()); /* Display a pulsating window until a key or the closebutton is pressed. */ redraw(); last_resize = 0; while (true) { if (al_get_next_event(events, &event)) { if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { ALLEGRO_DISPLAY_EVENT *de = &event.display; al_acknowledge_resize(de->source); redraw(); } if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } if (event.type == ALLEGRO_EVENT_KEY_DOWN) { break; } } if (al_current_time() - last_resize > 0.1) { int s; last_resize = al_current_time(); rs += 10; if (rs == 300) rs = 100; s = rs; if (s > 200) s = 400 - s; al_resize_display(s, s); } } return 0; }
void GameMgr::processInput() { ALLEGRO_EVENT evento; while (!al_is_event_queue_empty(fila_eventos)) { al_clear_to_color(al_map_rgb(0, 0, 0)); // Colorindo a janela de preto al_wait_for_event(fila_eventos, &evento); if (evento.type == ALLEGRO_EVENT_KEY_DOWN) { // uma tecla foi pressionada switch (evento.keyboard.keycode) { case ALLEGRO_KEY_UP: player->trocaDirecao(0); break; case ALLEGRO_KEY_DOWN: player->trocaDirecao(2); break; case ALLEGRO_KEY_LEFT: player->trocaDirecao(3); break; case ALLEGRO_KEY_RIGHT: player->trocaDirecao(1); break; case ALLEGRO_KEY_ENTER: if (evento.keyboard.modifiers | ALLEGRO_KEYMOD_ALT) { auto mode = al_get_display_flags(display); if (mode | ALLEGRO_WINDOWED) { al_set_display_flag(display, ALLEGRO_FULLSCREEN_WINDOW, true); } else { al_set_display_flag(display, ALLEGRO_FULLSCREEN_WINDOW, false); } } break; case ALLEGRO_KEY_Q: case ALLEGRO_KEY_ESCAPE: exit(0); } } else if (evento.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { // click no mouse if (evento.mouse.x > 0 && evento.mouse.x < 20 && evento.mouse.y > 0 && evento.mouse.y < 20) // cordenadas do botao exit(0); } else if (evento.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { al_acknowledge_resize(display); screen_width = al_get_display_width(display); screen_height = al_get_display_height(display); _redraw = true; } else if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { // click no mouse exit(0); } else if (evento.type == ALLEGRO_EVENT_TIMER) { if (evento.timer.source == draw_timer) _redraw = true; else if (evento.timer.source == logic_timer) _tick = true; } redraw(); } al_destroy_display(display); // Finaliza a janela }
bool _al_win_toggle_display_flag(ALLEGRO_DISPLAY *display, int flag, bool onoff) { ALLEGRO_DISPLAY_WIN *win_display = (void*)display; double timeout; switch(flag) { case ALLEGRO_NOFRAME: _al_win_toggle_window_frame(display, win_display->window, display->w, display->h, onoff); return true; case ALLEGRO_FULLSCREEN_WINDOW: if (onoff == ((display->flags & ALLEGRO_FULLSCREEN_WINDOW) != 0)) return true; _al_win_toggle_display_flag(display, ALLEGRO_NOFRAME, !onoff); if (onoff) { ALLEGRO_MONITOR_INFO mi; int adapter = al_get_current_video_adapter(); if (adapter == -1) adapter = 0; al_get_monitor_info(adapter, &mi); display->flags |= ALLEGRO_FULLSCREEN_WINDOW; display->w = mi.x2 - mi.x1; display->h = mi.y2 - mi.y1; } else { display->flags &= ~ALLEGRO_FULLSCREEN_WINDOW; display->w = win_display->toggle_w; display->h = win_display->toggle_h; } al_resize_display(display->w, display->h); timeout = al_current_time() + 3; // 3 seconds... while (al_current_time() < timeout) { if (win_display->can_acknowledge) { al_acknowledge_resize(display); break; } } if (onoff) { al_set_window_position(display, 0, 0); // Pop it to the front // FIXME: HOW?! } /* FIXME: else center the window? */ return true; } return false; }
void loader(void) { ALLEGRO_BITMAP *bg = al_load_bitmap("background_day.png"); PROGRESSBARPTR pb1 = create_new_progress_bar("pb1", 243, 275, 300, 50, 10, 10, al_map_rgba(0, 0, 0, 127), al_map_rgba(0, 0, 255, 127), 5, true); while(pb1->bar_width < pb1->width) { pb1->counter++; al_acknowledge_resize(get_game_data()->display); scale(res_width, res_height); al_clear_to_color(data->background_color); al_draw_bitmap(bg, 0, 0, 0); render_progress_bars(); al_flip_display(); if(pb1->counter == 3) { pb1->bar_width += pb1->increment; pb1->counter = 0; } } al_destroy_bitmap(bg); remove_element_from_progress_bar_list("pb1"); }
void SceneManager::sendResizeMessage( int w, int h ) { //this is a bug in Allegro if(w == 0 && h == 0) { return; } if(!Platf::isMobile() && w >= 1900) Platf::_setHD(true); else Platf::_setHD(false); al_acknowledge_resize(m_devices->getDisplay()->getContext()); m_g.resizeBuffer(w,h); //stop transition m_transitioning = false; m_transitionOpacity = 0.0f; m_currentScene->processGuiResizeEvent(); m_currentScene->resizeEvent( w,h); Log::write("Scene Manager","Resize Event: Width:" + StringUtil::toString(w) + " Height:" + StringUtil::toString(h)); }
int main(void) { ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_MONITOR_INFO info; int w = 640, h = 480; bool done = false; bool need_redraw = true; bool background = false; if (!al_init()) { abort_example("Failed to init Allegro.\n"); return 1; } if (!al_init_image_addon()) { abort_example("Failed to init IIO addon.\n"); return 1; } al_init_font_addon(); if (!al_init_ttf_addon()) { abort_example("Failed to init TTF addon.\n"); return 1; } al_get_num_video_adapters(); al_get_monitor_info(0, &info); #ifdef ALLEGRO_IPHONE al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); #endif al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS, ALLEGRO_DISPLAY_ORIENTATION_ALL, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 8, ALLEGRO_SUGGEST); al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); example.display = al_create_display(w, h); if (!example.display) { abort_example("Error creating display.\n"); return 1; } if (!al_install_keyboard()) { abort_example("Error installing keyboard.\n"); return 1; } example.font = al_load_font("data/DejaVuSans.ttf", 40, 0); if (!example.font) { abort_example("Error loading data/DejaVuSans.ttf\n"); return 1; } example.font2 = al_load_font("data/DejaVuSans.ttf", 12, 0); if (!example.font2) { abort_example("Error loading data/DejaVuSans.ttf\n"); return 1; } example.mysha = al_load_bitmap("data/mysha.pcx"); if (!example.mysha) { abort_example("Error loading data/mysha.pcx\n"); return 1; } example.obp = al_load_bitmap("data/obp.jpg"); if (!example.obp) { abort_example("Error loading data/obp.jpg\n"); return 1; } init(); timer = al_create_timer(1.0 / FPS); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); al_register_event_source(queue, al_get_display_event_source(example.display)); al_start_timer(timer); while (!done) { ALLEGRO_EVENT event; w = al_get_display_width(example.display); h = al_get_display_height(example.display); if (!background && need_redraw && al_is_event_queue_empty(queue)) { double t = -al_get_time(); redraw(); t += al_get_time(); example.direct_speed_measure = t; al_flip_display(); need_redraw = false; } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_CHAR: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING: background = true; al_acknowledge_drawing_halt(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING: background = false; break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_TIMER: update(); need_redraw = true; break; } } return 0; }
int main(void) { ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_MONITOR_INFO info; int w = 640, h = 480; bool done = false; bool need_redraw = true; bool background = false; example.show_help = true; if (!al_init()) { abort_example("Failed to init Allegro.\n"); return 1; } if (!al_init_image_addon()) { abort_example("Failed to init IIO addon.\n"); return 1; } al_init_font_addon(); al_get_num_video_adapters(); al_get_monitor_info(0, &info); #ifdef ALLEGRO_IPHONE al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); #endif al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS, ALLEGRO_DISPLAY_ORIENTATION_ALL, ALLEGRO_SUGGEST); example.display = al_create_display(w, h); w = al_get_display_width(example.display); h = al_get_display_height(example.display); if (!example.display) { abort_example("Error creating display.\n"); return 1; } if (!al_install_keyboard()) { abort_example("Error installing keyboard.\n"); return 1; } if (!al_install_mouse()) { abort_example("Error installing mouse.\n"); return 1; } example.font = al_load_font("data/fixed_font.tga", 0, 0); if (!example.font) { abort_example("Error loading data/fixed_font.tga\n"); return 1; } example.mysha = al_load_bitmap("data/mysha256x256.png"); if (!example.mysha) { abort_example("Error loading data/mysha256x256.png\n"); return 1; } example.white = al_map_rgb_f(1, 1, 1); example.half_white = al_map_rgba_f(1, 1, 1, 0.5); example.dark = al_map_rgb(15, 15, 15); example.red = al_map_rgb_f(1, 0.2, 0.1); change_size(256); add_sprite(); add_sprite(); timer = al_create_timer(1.0 / FPS); queue = al_create_event_queue(); 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)); if (al_install_touch_input()) al_register_event_source(queue, al_get_touch_input_event_source()); al_register_event_source(queue, al_get_display_event_source(example.display)); al_start_timer(timer); while (!done) { float x, y; ALLEGRO_EVENT event; w = al_get_display_width(example.display); h = al_get_display_height(example.display); if (!background && need_redraw && al_is_event_queue_empty(queue)) { double t = -al_get_time(); add_time(); al_clear_to_color(al_map_rgb_f(0, 0, 0)); redraw(); t += al_get_time(); example.direct_speed_measure = t; al_flip_display(); need_redraw = false; } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_CHAR: /* includes repeats */ if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; else if (event.keyboard.keycode == ALLEGRO_KEY_UP) { add_sprites(1); } else if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) { remove_sprites(1); } else if (event.keyboard.keycode == ALLEGRO_KEY_LEFT) { change_size(example.bitmap_size - 1); } else if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT) { change_size(example.bitmap_size + 1); } else if (event.keyboard.keycode == ALLEGRO_KEY_F1) { example.show_help ^= 1; } else if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) { example.use_memory_bitmaps ^= 1; change_size(example.bitmap_size); } else if (event.keyboard.keycode == ALLEGRO_KEY_B) { example.blending++; if (example.blending == 4) example.blending = 0; } break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING: background = true; al_acknowledge_drawing_halt(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING: background = false; break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_TIMER: update(); need_redraw = true; break; case ALLEGRO_EVENT_TOUCH_BEGIN: x = event.touch.x; y = event.touch.y; goto click; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: x = event.mouse.x; y = event.mouse.y; goto click; click: { int fh = al_get_font_line_height(example.font); if (x < 80 && y >= h - fh * 10) { int button = (y - (h - fh * 10)) / (fh * 2); if (button == 0) { example.use_memory_bitmaps ^= 1; change_size(example.bitmap_size); } if (button == 1) { example.blending++; if (example.blending == 4) example.blending = 0; } if (button == 3) { if (x < 40) remove_sprites(example.sprite_count / 2); else add_sprites(example.sprite_count); } if (button == 2) { int s = example.bitmap_size * 2; if (x < 40) s = example.bitmap_size / 2; change_size(s); } if (button == 4) { example.show_help ^= 1; } } break; } } } al_destroy_bitmap(example.bitmap); return 0; }
int main(int, char**) { // Setup Allegro al_init(); al_install_keyboard(); al_install_mouse(); al_init_primitives_addon(); al_set_new_display_flags(ALLEGRO_RESIZABLE); ALLEGRO_DISPLAY* display = al_create_display(1280, 720); al_set_window_title(display, "ImGui Allegro 5 example"); ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 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()); // Setup ImGui binding ImGui_ImplA5_Init(display); // Load Fonts // (see extra_fonts/README.txt for more details) //ImGuiIO& io = ImGui::GetIO(); //io.Fonts->AddFontDefault(); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f); //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); // Merge glyphs from multiple fonts into one (e.g. combine default font with another with Chinese glyphs, or add icons) //ImWchar icons_ranges[] = { 0xf000, 0xf3ff, 0 }; //ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true; //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 18.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/fontawesome-webfont.ttf", 18.0f, &icons_config, icons_ranges); bool show_test_window = true; bool show_another_window = false; ImVec4 clear_color = ImColor(114, 144, 154); // Main loop bool running = true; while (running) { ALLEGRO_EVENT ev; while (al_get_next_event(queue, &ev)) { ImGui_ImplA5_ProcessEvent(&ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) running = false; if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { ImGui_ImplA5_InvalidateDeviceObjects(); al_acknowledge_resize(display); Imgui_ImplA5_CreateDeviceObjects(); } } ImGui_ImplA5_NewFrame(); // 1. Show a simple window // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" { static float f; ImGui::Text("Hello, world!"); ImGui::SliderFloat("float", &f, 0.0f, 1.0f); ImGui::ColorEdit3("clear color", (float*)&clear_color); if (ImGui::Button("Test Window")) show_test_window ^= 1; if (ImGui::Button("Another Window")) show_another_window ^= 1; ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f/ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window, this time using an explicit Begin/End pair if (show_another_window) { ImGui::SetNextWindowSize(ImVec2(200, 100), ImGuiSetCond_FirstUseEver); ImGui::Begin("Another Window", &show_another_window); ImGui::Text("Hello"); ImGui::End(); } // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow() if (show_test_window) { ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver); ImGui::ShowTestWindow(&show_test_window); } // Rendering al_clear_to_color(al_map_rgba_f(clear_color.x, clear_color.y, clear_color.z, clear_color.w)); ImGui::Render(); al_flip_display(); } // Cleanup ImGui_ImplA5_Shutdown(); al_destroy_event_queue(queue); al_destroy_display(display); return 0; }
int main(void) { ALLEGRO_DISPLAY *display[2]; ALLEGRO_EVENT event; ALLEGRO_EVENT_QUEUE *events; ALLEGRO_BITMAP *pictures[2]; ALLEGRO_BITMAP *target; int width, height; int i; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_install_keyboard(); al_install_mouse(); al_init_image_addon(); events = al_create_event_queue(); al_set_new_display_flags(ALLEGRO_WINDOWED|ALLEGRO_RESIZABLE); /* Create two windows. */ display[0] = al_create_display(W, H); pictures[0] = al_load_bitmap("data/mysha.pcx"); if (!pictures[0]) { abort_example("failed to load mysha.pcx\n"); return 1; } display[1] = al_create_display(W, H); pictures[1] = al_load_bitmap("data/allegro.pcx"); if (!pictures[1]) { abort_example("failed to load allegro.pcx\n"); return 1; } /* This is only needed since we want to receive resize events. */ al_register_event_source(events, al_get_display_event_source(display[0])); al_register_event_source(events, al_get_display_event_source(display[1])); al_register_event_source(events, al_get_keyboard_event_source()); while (1) { /* read input */ while (!al_is_event_queue_empty(events)) { al_get_next_event(events, &event); if (event.type == ALLEGRO_EVENT_KEY_DOWN) { ALLEGRO_KEYBOARD_EVENT *key = &event.keyboard; if (key->keycode == ALLEGRO_KEY_ESCAPE) { goto done; } } if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { ALLEGRO_DISPLAY_EVENT *de = &event.display; al_acknowledge_resize(de->source); } if (event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN) { printf("%p switching in\n", event.display.source); } if (event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) { printf("%p switching out\n", event.display.source); } if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { int i; for (i = 0; i < 2; i++) { if (display[i] == event.display.source) display[i] = 0; } al_destroy_display(event.display.source); for (i = 0; i < 2; i++) { if (display[i]) goto not_done; } goto done; not_done: ; } } for (i = 0; i < 2; i++) { if (!display[i]) continue; target = al_get_backbuffer(display[i]); width = al_get_bitmap_width(target); height = al_get_bitmap_height(target); al_set_target_bitmap(target); al_draw_scaled_bitmap(pictures[0], 0, 0, al_get_bitmap_width(pictures[0]), al_get_bitmap_height(pictures[0]), 0, 0, width / 2, height, 0); al_draw_scaled_bitmap(pictures[1], 0, 0, al_get_bitmap_width(pictures[1]), al_get_bitmap_height(pictures[1]), width / 2, 0, width / 2, height, 0); al_flip_display(); } al_rest(0.001); } done: al_destroy_bitmap(pictures[0]); al_destroy_bitmap(pictures[1]); return 0; }
int main(int, char**) { // Setup Allegro al_init(); al_install_keyboard(); al_install_mouse(); al_init_primitives_addon(); al_set_new_display_flags(ALLEGRO_RESIZABLE); ALLEGRO_DISPLAY* display = al_create_display(1280, 720); al_set_window_title(display, "Dear ImGui Allegro 5 example"); ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 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()); // Setup Dear ImGui binding IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls ImGui_ImplAllegro5_Init(display); // Setup style ImGui::StyleColorsDark(); //ImGui::StyleColorsClassic(); // Load Fonts // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. // - Read 'misc/fonts/README.txt' for more instructions and details. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! //io.Fonts->AddFontDefault(); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); //IM_ASSERT(font != NULL); bool show_demo_window = true; bool show_another_window = false; ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); // Main loop bool running = true; while (running) { // Poll and handle events (inputs, window resize, etc.) // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. ALLEGRO_EVENT ev; while (al_get_next_event(queue, &ev)) { ImGui_ImplAllegro5_ProcessEvent(&ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) running = false; if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { ImGui_ImplAllegro5_InvalidateDeviceObjects(); al_acknowledge_resize(display); ImGui_ImplAllegro5_CreateDeviceObjects(); } } // Start the Dear ImGui frame ImGui_ImplAllegro5_NewFrame(); ImGui::NewFrame(); // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. { static float f = 0.0f; static int counter = 0; ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state ImGui::Checkbox("Another Window", &show_another_window); ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) counter++; ImGui::SameLine(); ImGui::Text("counter = %d", counter); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); ImGui::End(); } // 3. Show another simple window. if (show_another_window) { ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) ImGui::Text("Hello from another window!"); if (ImGui::Button("Close Me")) show_another_window = false; ImGui::End(); } // Rendering ImGui::Render(); al_clear_to_color(al_map_rgba_f(clear_color.x, clear_color.y, clear_color.z, clear_color.w)); ImGui_ImplAllegro5_RenderDrawData(ImGui::GetDrawData()); al_flip_display(); } // Cleanup ImGui_ImplAllegro5_Shutdown(); ImGui::DestroyContext(); al_destroy_event_queue(queue); al_destroy_display(display); return 0; }
extern int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_FONT *font; bool done = false; bool redraw = true; use_constraints = true; (void)argc; (void)argv; if (!al_init()) { abort_example("Failed to init Allegro.\n"); } if (!al_init_primitives_addon()) { abort_example("Failed to init primitives addon.\n"); } if (!al_init_image_addon()) { abort_example("Failed to init image addon.\n"); } if (!al_init_font_addon()) { abort_example("Failed to init font addon.\n"); } if (!al_init_native_dialog_addon()) { abort_example("Failed to init native dialog addon.\n"); } al_set_new_display_flags(ALLEGRO_WINDOWED #if defined(USE_GTK) && !defined(_WIN32) | ALLEGRO_GTK_TOPLEVEL #endif | ALLEGRO_RESIZABLE | ALLEGRO_MAXIMIZED | ALLEGRO_GENERATE_EXPOSE_EVENTS); /* creating really small display */ display = al_create_display(DISPLAY_W / 3, DISPLAY_H / 3); if (!display) { abort_example("Error creating display.\n"); } /* set lower limits for constraints only */ if(!al_set_window_constraints(display, DISPLAY_W / 2, DISPLAY_H / 2, 0, 0)) abort_example("Unable to set window constraints.\n"); al_apply_window_constraints(display, use_constraints); if (!al_install_keyboard()) { abort_example("Error installing keyboard.\n"); } 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(display)); ALLEGRO_COLOR color_1 = al_map_rgb(255, 127, 0); ALLEGRO_COLOR color_2 = al_map_rgb(0, 255, 0); ALLEGRO_COLOR *color = &color_1; ALLEGRO_COLOR color_text = al_map_rgb(0, 0, 0); font = al_create_builtin_font(); while (!done) { ALLEGRO_EVENT event; if (redraw && al_is_event_queue_empty(queue)) { redraw = false; int x2 = al_get_display_width(display) - 10; int y2 = al_get_display_height(display) - 10; al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_filled_rectangle(10, 10, x2, y2, *color); draw_information(display, font, color_text); al_flip_display(); } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; break; case ALLEGRO_EVENT_KEY_UP: if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) { redraw = true; if (color == &color_1) { if (!al_set_window_constraints(display, 0, 0, DISPLAY_W, DISPLAY_H)) { abort_example("Unable to set window constraints.\n"); } color = &color_2; } else { if (!al_set_window_constraints(display, DISPLAY_W / 2, DISPLAY_H / 2, 0, 0)) { abort_example("Unable to set window constraints.\n"); } color = &color_1; } al_apply_window_constraints(display, use_constraints); } else if (event.keyboard.keycode == ALLEGRO_KEY_ENTER) { redraw = true; use_constraints = !use_constraints; al_apply_window_constraints(display, use_constraints); } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); redraw = true; break; case ALLEGRO_EVENT_DISPLAY_EXPOSE: redraw = true; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; } /* switch (event.type) { */ } al_destroy_font(font); return 0; }
int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *bitmap; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_init_image_addon(); al_install_keyboard(); al_install_mouse(); al_set_new_display_flags(ALLEGRO_RESIZABLE | ALLEGRO_GENERATE_EXPOSE_EVENTS); al_set_new_display_option(ALLEGRO_SINGLE_BUFFER, true, ALLEGRO_REQUIRE); display = al_create_display(W, H); if (!display) { abort_example("Error creating display\n"); return 1; } bitmap = al_load_bitmap("data/mysha.pcx"); if (!bitmap) { abort_example("mysha.pcx not found or failed to load\n"); return 1; } al_draw_bitmap(bitmap, 0, 0, 0); al_flip_display(); timer = al_install_timer(0.5); queue = al_create_event_queue(); 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_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (true) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { al_acknowledge_resize(event.display.source); } if (event.type == ALLEGRO_EVENT_DISPLAY_EXPOSE) { int x = event.display.x, y = event.display.y, w = event.display.width, h = event.display.height; /* Draw a red rectangle over the damaged area. */ al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO, al_map_rgba_f(1, 1, 1, 1)); al_draw_filled_rectangle(x, y, x + w, y + h, al_map_rgba_f(1, 0, 0, 1)); al_flip_display(); } if (event.type == ALLEGRO_EVENT_TIMER) { /* Slowly restore the original bitmap. */ int x, y; al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1, 1, 1, 0.1)); for (y = 0; y < al_get_display_height(); y += 200) { for (x = 0; x < al_get_display_width(); x += 320) { al_draw_bitmap(bitmap, x, y, 0); } } al_flip_display(); } } al_destroy_event_queue(queue); al_destroy_bitmap(bitmap); return 0; }
static void *thread_func(ALLEGRO_THREAD *thr, void *arg) { const int INITIAL_WIDTH = 300; const int INITIAL_HEIGHT = 300; const Background *background = (Background *) arg; ALLEGRO_DISPLAY *display; ALLEGRO_EVENT_QUEUE *queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_EVENT event; ALLEGRO_STATE state; Square squares[MAX_SQUARES]; double theta = 0.0; bool redraw = true; int i; (void)thr; al_set_new_display_flags(ALLEGRO_RESIZABLE); display = al_create_display(INITIAL_WIDTH, INITIAL_HEIGHT); if (!display) { goto Quit; } queue = al_create_event_queue(); if (!queue) { goto Quit; } timer = al_create_timer(0.1); if (!timer) { goto Quit; } 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_timer_event_source(timer)); for (i = 0; i < MAX_SQUARES; i++) { gen_square(&squares[i], INITIAL_WIDTH, INITIAL_HEIGHT); } al_start_timer(timer); while (true) { if (al_is_event_queue_empty(queue) && redraw) { double r = 0.7 + 0.3 * (sin(theta) + 1.0) / 2.0; ALLEGRO_COLOR c = al_map_rgb_f( background->rmax * r, background->gmax * r, background->bmax * r ); al_clear_to_color(c); al_store_state(&state, ALLEGRO_STATE_BLENDER | ALLEGRO_STATE_TRANSFORM); for (i = 0; i < MAX_SQUARES; i++) { draw_square(&squares[i]); } al_restore_state(&state); al_flip_display(); redraw = false; } al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_TIMER) { for (i = 0; i < MAX_SQUARES; i++) { animate_square(&squares[i]); } theta += 0.1; redraw = true; } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { al_acknowledge_resize(event.display.source); } } Quit: if (timer) { al_destroy_timer(timer); } if (queue) { al_destroy_event_queue(queue); } if (display) { al_destroy_display(display); } return NULL; }
static inline void HandleEvent(struct Game* game, ALLEGRO_EVENT* ev) { switch (ev->type) { case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING: PauseExecution(game); al_acknowledge_drawing_halt(game->display); break; case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING: al_acknowledge_drawing_resume(game->display); ReloadGamestates(game); ResumeExecution(game); break; case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT: if (game->config.autopause) { PrintConsole(game, "Focus lost, autopausing..."); PauseExecution(game); } break; case ALLEGRO_EVENT_DISPLAY_SWITCH_IN: if (game->config.autopause) { if (game->config.debug.enabled && game->config.debug.livereload) { ReloadCode(game); } ResumeExecution(game); } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: PrintConsole(game, "Resize event: %dx%d", ev->display.width, ev->display.height); #ifdef LIBSUPERDERPY_IMGUI ImGui_ImplAllegro5_InvalidateDeviceObjects(); #endif al_acknowledge_resize(game->display); #ifdef LIBSUPERDERPY_IMGUI ImGui_ImplAllegro5_CreateDeviceObjects(); #endif // SetupViewport can be expensive, so don't do it when the resize event is already outdated or doesn't change anything if (((ev->display.width != game->_priv.window_width) || (ev->display.height != game->_priv.window_height)) && (ev->display.width == al_get_display_width(game->display)) && (ev->display.height == al_get_display_height(game->display))) { SetupViewport(game); } break; case ALLEGRO_EVENT_KEY_DOWN: #ifdef ALLEGRO_ANDROID if ((ev->keyboard.keycode == ALLEGRO_KEY_MENU) || (ev->keyboard.keycode == ALLEGRO_KEY_TILDE) || (ev->keyboard.keycode == ALLEGRO_KEY_BACKQUOTE)) { #else if ((ev->keyboard.keycode == ALLEGRO_KEY_TILDE) || (ev->keyboard.keycode == ALLEGRO_KEY_BACKQUOTE)) { #endif game->_priv.showconsole = !game->_priv.showconsole; if ((ev->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) && (game->config.debug.enabled)) { game->_priv.showtimeline = game->_priv.showconsole; } } if (ev->keyboard.keycode == ALLEGRO_KEY_F12) { DrawGamestates(game); int flags = al_get_new_bitmap_flags(); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); ALLEGRO_BITMAP* bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display)); al_set_new_bitmap_flags(flags); ALLEGRO_BITMAP* target = al_get_target_bitmap(); al_set_target_bitmap(bitmap); al_draw_bitmap(al_get_backbuffer(game->display), 0, 0, 0); al_set_target_bitmap(target); PrintConsole(game, "Screenshot made! Storing..."); struct ScreenshotThreadData* data = malloc(sizeof(struct ScreenshotThreadData)); data->game = game; data->bitmap = bitmap; #ifndef LIBSUPERDERPY_SINGLE_THREAD al_run_detached_thread(ScreenshotThread, data); #else ScreenshotThread(data); #endif } break; default: break; } #ifdef MAEMO5 // on Maemo we get mouse events instead of touch ones, so we'll rewrite them by ourselves if ((ev->type == ALLEGRO_EVENT_MOUSE_AXES) || (ev->type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) || (ev->type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)) { switch (ev->type) { case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: ev->type = ALLEGRO_EVENT_TOUCH_BEGIN; break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: ev->type = ALLEGRO_EVENT_TOUCH_END; break; case ALLEGRO_EVENT_MOUSE_AXES: ev->type = ALLEGRO_EVENT_TOUCH_MOVE; break; default: break; } ALLEGRO_DISPLAY* display = ev->mouse.display; float dx = ev->mouse.dx; float dy = ev->mouse.dy; float x = ev->mouse.x; float y = ev->mouse.y; double timestamp = ev->mouse.timestamp; ev->touch.display = display; ev->touch.dx = dx; ev->touch.dy = dy; ev->touch.id = 0; ev->touch.primary = true; ev->touch.source = (ALLEGRO_TOUCH_INPUT*)al_get_touch_input_event_source(); ev->touch.timestamp = timestamp; ev->touch.x = x; ev->touch.y = y; } #endif } static inline void HandleDebugEvent(struct Game* game, ALLEGRO_EVENT* ev) { switch (ev->type) { case ALLEGRO_EVENT_KEY_DOWN: switch (ev->keyboard.keycode) { case ALLEGRO_KEY_F1: if (!game->_priv.paused) { PauseExecution(game); } else { ReloadCode(game); ResumeExecution(game); } break; case ALLEGRO_KEY_F9: game->_priv.speed = ALLEGRO_BPS_TO_SECS(60.0); game->_priv.showconsole = true; PrintConsole(game, "DEBUG: Gameplay speed: 1.00x"); break; case ALLEGRO_KEY_F10: { double speed = ALLEGRO_BPS_TO_SECS(game->_priv.speed); // inverting speed -= 10; if (speed < 10) { speed = 10; } game->_priv.speed = ALLEGRO_BPS_TO_SECS(speed); game->_priv.showconsole = true; PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed / 60.0); } break; case ALLEGRO_KEY_F11: { double speed = ALLEGRO_BPS_TO_SECS(game->_priv.speed); // inverting speed += 10; if (speed > 600) { speed = 600; } game->_priv.speed = ALLEGRO_BPS_TO_SECS(speed); game->_priv.showconsole = true; PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed / 60.0); } break; } break; default: break; } } static inline bool MainloopEvents(struct Game* game) { do { ALLEGRO_EVENT ev; if (game->_priv.paused && !IS_EMSCRIPTEN) { // there's no frame flipping when paused, so avoid pointless busylooping al_wait_for_event(game->_priv.event_queue, &ev); } else if (!al_get_next_event(game->_priv.event_queue, &ev)) { break; } #ifdef LIBSUPERDERPY_IMGUI ImGui_ImplAllegro5_ProcessEvent(&ev); switch (ev.type) { case ALLEGRO_EVENT_KEY_CHAR: case ALLEGRO_EVENT_KEY_DOWN: case ALLEGRO_EVENT_KEY_UP: if (igGetIO()->WantCaptureKeyboard) { continue; } break; case ALLEGRO_EVENT_MOUSE_AXES: case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: case ALLEGRO_EVENT_MOUSE_BUTTON_UP: case ALLEGRO_EVENT_TOUCH_BEGIN: case ALLEGRO_EVENT_TOUCH_CANCEL: case ALLEGRO_EVENT_TOUCH_END: case ALLEGRO_EVENT_TOUCH_MOVE: if (igGetIO()->WantCaptureMouse) { continue; } break; default: break; } #endif if (game->_priv.params.handlers.event) { if ((*game->_priv.params.handlers.event)(game, &ev)) { continue; } } if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { EventGamestates(game, &ev); return false; } HandleEvent(game, &ev); if (game->config.debug.enabled) { HandleDebugEvent(game, &ev); } EventGamestates(game, &ev); } while (!al_is_event_queue_empty(game->_priv.event_queue)); return true; } static inline bool MainloopTick(struct Game* game) { if (game->_priv.paused) { return true; } struct Gamestate* tmp = game->_priv.gamestates; #ifdef __EMSCRIPTEN__ emscripten_pause_main_loop(); #endif game->_priv.loading.to_load = 0; game->_priv.loading.loaded = 0; game->_priv.loading.lock = true; game->loading.progress = 0; // TODO: support gamestate dependences/ordering while (tmp) { if (tmp->pending_stop) { PrintConsole(game, "Stopping gamestate \"%s\"...", tmp->name); game->_priv.current_gamestate = tmp; (*tmp->api->stop)(game, tmp->data); tmp->started = false; tmp->pending_stop = false; PrintConsole(game, "Gamestate \"%s\" stopped successfully.", tmp->name); } if (tmp->pending_load) { game->_priv.loading.to_load++; } tmp = tmp->next; } tmp = game->_priv.gamestates; while (tmp) { if (tmp->pending_unload) { #ifdef __EMSCRIPTEN__ al_detach_voice(game->audio.v); #endif PrintConsole(game, "Unloading gamestate \"%s\"...", tmp->name); tmp->loaded = false; tmp->pending_unload = false; game->_priv.current_gamestate = tmp; (*tmp->api->unload)(game, tmp->data); PrintConsole(game, "Gamestate \"%s\" unloaded successfully.", tmp->name); #ifdef __EMSCRIPTEN__ al_attach_mixer_to_voice(game->audio.mixer, game->audio.v); #endif } if (tmp->pending_load) { #ifdef __EMSCRIPTEN__ al_detach_voice(game->audio.v); #endif if (tmp->show_loading && game->_priv.loading.gamestate->open) { (*game->_priv.loading.gamestate->api->start)(game, game->_priv.loading.gamestate->data); } if (!tmp->api) { if (!OpenGamestate(game, tmp, true) || !LinkGamestate(game, tmp)) { tmp->pending_load = false; tmp->pending_start = false; continue; } } if (tmp->api) { PrintConsole(game, "Loading gamestate \"%s\"...", tmp->name); game->_priv.loading.progress = 0; game->_priv.loading.current = tmp; game->_priv.current_gamestate = tmp; struct GamestateLoadingThreadData data = {.game = game, .gamestate = tmp, .bitmap_flags = al_get_new_bitmap_flags()}; game->_priv.loading.in_progress = true; double time = al_get_time(); game->_priv.loading.time = time; CalculateProgress(game); if (tmp->show_loading) { game->loading.shown = true; DrawGamestates(game); DrawConsole(game); al_flip_display(); #ifdef __EMSCRIPTEN__ emscripten_sleep(0); #endif } #ifndef LIBSUPERDERPY_SINGLE_THREAD al_run_detached_thread(GamestateLoadingThread, &data); while (game->_priv.loading.in_progress) { double delta = al_get_time() - game->_priv.loading.time; game->time += delta; // TODO: ability to disable passing time during loading game->_priv.loading.time += delta; if (game->loading.shown && game->_priv.loading.gamestate->open) { (*game->_priv.loading.gamestate->api->logic)(game, game->_priv.loading.gamestate->data, delta); } DrawGamestates(game); if (game->_priv.texture_sync) { al_convert_memory_bitmaps(); game->_priv.texture_sync = false; al_signal_cond(game->_priv.texture_sync_cond); game->_priv.loading.time = al_get_time(); // TODO: rethink time management during loading } DrawConsole(game); al_flip_display(); if (game->_priv.bsod_sync) { al_set_target_bitmap(NULL); game->_priv.bsod_sync = false; al_signal_cond(game->_priv.bsod_cond); } al_lock_mutex(game->_priv.bsod_mutex); while (game->_priv.in_bsod) { al_wait_cond(game->_priv.bsod_cond, game->_priv.bsod_mutex); } al_unlock_mutex(game->_priv.bsod_mutex); } #else GamestateLoadingThread(&data); DrawGamestates(game); DrawConsole(game); al_flip_display(); #ifdef __EMSCRIPTEN__ emscripten_sleep(0); #endif al_convert_memory_bitmaps(); #endif al_set_new_bitmap_flags(data.bitmap_flags); ReloadShaders(game, false); if (tmp->api->post_load) { PrintConsole(game, "[%s] Post-loading...", tmp->name); tmp->api->post_load(game, tmp->data); } game->_priv.loading.progress++; CalculateProgress(game); PrintConsole(game, "Gamestate \"%s\" loaded successfully in %f seconds.", tmp->name, al_get_time() - time); game->_priv.loading.loaded++; DrawGamestates(game); DrawConsole(game); al_flip_display(); #ifdef __EMSCRIPTEN__ emscripten_sleep(0); #endif tmp->loaded = true; tmp->pending_load = false; } if (tmp->show_loading && game->_priv.loading.gamestate->open) { (*game->_priv.loading.gamestate->api->stop)(game, game->_priv.loading.gamestate->data); } tmp->show_loading = true; game->loading.shown = false; game->_priv.timestamp = al_get_time(); #ifdef __EMSCRIPTEN__ al_attach_mixer_to_voice(game->audio.mixer, game->audio.v); #endif } tmp = tmp->next; }
// 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; }
void acknowledge_resize (void) { if (! al_acknowledge_resize (display) && ! is_fullscreen ()) error (0, 0, "%s: cannot acknowledge display resize (%p)", __func__, display); }
int main(int argc, char **argv) { al_init(); al_install_mouse(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); al_set_new_display_flags(ALLEGRO_WINDOWED|ALLEGRO_RESIZABLE); ALLEGRO_DISPLAY *display = al_create_display(640, 480); ALLEGRO_DISPLAY *tooldisplay = al_create_display(200, 480); al_set_window_position(tooldisplay, 10, 0); al_set_window_position(display, 220, 0); ALLEGRO_DISPLAY *current_display = tooldisplay; ALLEGRO_TIMER* timer = al_create_timer(1); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); al_register_event_source(event_queue, (ALLEGRO_EVENT_SOURCE *)display); al_register_event_source(event_queue, (ALLEGRO_EVENT_SOURCE *)tooldisplay); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); ALLEGRO_FONT* font = al_load_font("data/DejaVuSans.ttf", 12, 0); if(!font) font = al_load_font("examples/data/DejaVuSans.ttf", 12, 0); Layout layout; Layout_controller layout_controller; layout_controller.Set_layout(&layout); Tree* widget_tree = layout_controller.Get_skin().Clone<Tree>("tree"); widget_tree->Set_text("Desktop"); widget_tree->Select(); Widget* root_widget = layout_controller.Get_skin().Clone<Desktop>("desktop"); root_widget->Set_position(Vector2(0, 0)); root_widget->Set_size(Vector2(640, 480)); layout_controller.Set_tree(widget_tree, root_widget); layout_controller.Set_root(root_widget); layout_controller.Set_root_tree(widget_tree); layout_controller.Select_tree(widget_tree); layout.Set_skin(&layout_controller.Get_skin()); layout.Add_widget("root", root_widget, NULL); Editor_controller editor_controller; editor_controller.Set_layout_display(display); editor_controller.Set_layout_controller(layout_controller); editor_controller.Load(layout_controller.Get_skin()); //FPS Label* fps_label = layout_controller.Get_skin().Clone<Label>("label"); fps_label->Set_text("FPS: 100"); //Main vbox Vertical_box* toolvbox = layout_controller.Get_skin().Clone<Vertical_box>("vertical box"); toolvbox->Add(editor_controller.Get_root()); toolvbox->Add(fps_label); Slider_box* slider_box = layout_controller.Get_skin().Clone<Slider_box>("slider box"); slider_box->Set_child(toolvbox); Desktop* desktop = layout_controller.Get_skin().Clone<Desktop>("desktop"); desktop->Set_child(slider_box); desktop->Set_position(Vector2(0, 0)); desktop->Set_size(Vector2(200, 480)); Widget* toolroot = desktop; al_start_timer(timer); bool quit = false; while (!quit) { ALLEGRO_EVENT event; al_wait_for_event(event_queue, &event); if (ALLEGRO_EVENT_KEY_DOWN == event.type) { if (ALLEGRO_KEY_ESCAPE == event.keyboard.keycode) { quit=true; } } if (ALLEGRO_EVENT_DISPLAY_CLOSE == event.type) { quit=true; } if (ALLEGRO_EVENT_DISPLAY_RESIZE == event.type) { al_acknowledge_resize(event.display.source); if(event.display.source == display) layout_controller.Get_root()->Set_size(Vector2(event.display.width-20, event.display.height-20)); } if (ALLEGRO_EVENT_DISPLAY_SWITCH_IN == event.type) { current_display = event.display.source; } if(current_display == tooldisplay) { toolroot->Handle_event(event); } if(current_display == display) { layout_controller.Get_root()->Handle_event(event); } if(ALLEGRO_EVENT_TIMER == event.type) { double dt = al_get_timer_speed(timer); editor_controller.Update(); layout_controller.Get_skin().Update(dt); al_set_target_backbuffer(display); layout_controller.Get_root()->Render(); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); al_set_target_backbuffer(tooldisplay); toolroot->Render(); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); double fps = 1 / dt; std::stringstream ss; ss<<fps; std::string fps_string; ss>>fps_string; fps_label->Set_text((std::string("FPS: ")+fps_string).c_str()); if(dt < 1 && event.timer.count < al_get_timer_count(timer) - 4) { dt = dt * 1.1; al_set_timer_speed(timer, dt); //print("Tick too fast, setting speed to " .. timer_speed); } if(event.timer.count == al_get_timer_count(timer)) { dt = dt * 0.9; al_set_timer_speed(timer, dt); //print("Tick too fast, setting speed to " .. timer_speed); } al_rest(0.001); } }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *bmp; ALLEGRO_FONT *f; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; bool redraw; int min_w, min_h, max_w, max_h; int ret_min_w, ret_min_h, ret_max_w, ret_max_h; bool constr_min_w, constr_min_h, constr_max_w, constr_max_h; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_set_new_display_flags(ALLEGRO_RESIZABLE | ALLEGRO_GENERATE_EXPOSE_EVENTS); display = al_create_display(640, 480); if (!display) { abort_example("Unable to set any graphic mode\n"); } bmp = al_load_bitmap("data/mysha.pcx"); if (!bmp) { abort_example("Unable to load image\n"); } f = al_load_font("data/a4_font.tga", 0, 0); if (!f) { abort_example("Failed to load a4_font.tga\n"); } min_w = 640; min_h = 480; max_w = 800; max_h = 600; constr_min_w = constr_min_h = constr_max_w = constr_max_h = true; if (!al_set_window_constraints( display, constr_min_w ? min_w : 0, constr_min_h ? min_h : 0, constr_max_w ? max_w : 0, constr_max_h ? max_h : 0)) { abort_example("Unable to set window constraints.\n"); } al_apply_window_constraints(display, true); queue = al_create_event_queue(); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_keyboard_event_source()); redraw = true; while (true) { if (redraw && al_is_event_queue_empty(queue)) { al_clear_to_color(al_map_rgb(255, 0, 0)); al_draw_scaled_bitmap(bmp, 0, 0, al_get_bitmap_width(bmp), al_get_bitmap_height(bmp), 0, 0, al_get_display_width(display), al_get_display_height(display), 0); /* Display screen resolution */ al_draw_textf(f, al_map_rgb(255, 255, 255), 0, 0, 0, "Resolution: %dx%d", al_get_display_width(display), al_get_display_height(display)); if (!al_get_window_constraints(display, &ret_min_w, &ret_min_h, &ret_max_w, &ret_max_h)) { abort_example("Unable to get window constraints\n"); } al_draw_textf(f, al_map_rgb(255, 255, 255), 0, al_get_font_line_height(f), 0, "Min Width: %d, Min Height: %d, Max Width: %d, Max Height: %d", ret_min_w, ret_min_h, ret_max_w, ret_max_h); al_draw_textf(f, al_map_rgb(255, 255, 255), 0, al_get_font_line_height(f) * 2,0, "Toggle Restriction: Min Width: Z, Min Height: X, Max Width: C, Max Height: V"); al_flip_display(); redraw = false; } al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { al_acknowledge_resize(event.display.source); redraw = true; } if (event.type == ALLEGRO_EVENT_DISPLAY_EXPOSE) { redraw = true; } if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.keyboard.keycode == ALLEGRO_KEY_Z) { constr_min_w = ! constr_min_w; } else if (event.keyboard.keycode == ALLEGRO_KEY_X) { constr_min_h = ! constr_min_h; } else if (event.keyboard.keycode == ALLEGRO_KEY_C) { constr_max_w = ! constr_max_w; } else if (event.keyboard.keycode == ALLEGRO_KEY_V) { constr_max_h = ! constr_max_h; } redraw = true; if (!al_set_window_constraints(display, constr_min_w ? min_w : 0, constr_min_h ? min_h : 0, constr_max_w ? max_w : 0, constr_max_h ? max_h : 0)) { abort_example("Unable to set window constraints.\n"); } al_apply_window_constraints(display, true); } if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } } al_destroy_bitmap(bmp); al_destroy_display(display); return 0; }
int real_main() { srand48(time(NULL)); if (!al_init()) { puts("Could not initialise allegro"); return 1; } if (!al_install_keyboard()) { puts("Could not initialise allegro keyboard subsystem"); return 1; } keys = malloc(sizeof(ALLEGRO_KEYBOARD_STATE)); if (!init_font()) { puts("Could not initialise allegro font subsystem"); return 1; } al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); ALLEGRO_DISPLAY* display = al_create_display(1, 1); if (!display) { puts("Could not initialise allegro display"); return 1; } Vector size = new_vector(); size.x = al_get_display_width(display); size.y = al_get_display_height(display); Game* game = new_game(size); int i; for (i = 0; i < ASTEROIDN; i++) spawn_asteroid(game); ALLEGRO_TIMER* timer = al_create_timer(1.0/FPS); ALLEGRO_EVENT_QUEUE *timereq = al_create_event_queue(); ALLEGRO_EVENT_QUEUE *genericeq = al_create_event_queue(); if (!timereq || !genericeq) { puts("Could not create allegro event queue"); return 1; } al_register_event_source(timereq, al_get_timer_event_source(timer)); al_register_event_source(genericeq, al_get_keyboard_event_source()); al_register_event_source(genericeq, al_get_display_event_source(display)); al_start_timer(timer); // Start generating timer events ALLEGRO_EVENT *timerevent = malloc(sizeof(ALLEGRO_EVENT)); ALLEGRO_EVENT *genericevent = malloc(sizeof(ALLEGRO_EVENT)); float last_drawn, now; last_drawn = now = al_get_time(); while (game->status != Quit) { al_get_keyboard_state(keys); al_wait_for_event(timereq, NULL); al_get_next_event(timereq, timerevent); // No need to fill up the queue if we are late drawing frames al_flush_event_queue(timereq); handle_key_status(game, keys); while(al_get_next_event(genericeq, genericevent)) switch(genericevent->type) { case ALLEGRO_EVENT_KEY_DOWN: handle_key_event(game, genericevent->keyboard.keycode); break; case ALLEGRO_EVENT_DISPLAY_RESIZE: game->size.x = genericevent->display.x; game->size.y = genericevent->display.y; al_acknowledge_resize(display); break; case ALLEGRO_EVENT_DISPLAY_CLOSE: game->status = Quit; break; } now = al_get_time(); update_game(game, now - last_drawn); last_drawn = now; draw_game(game, game->status == Playing ? 1 : 0.2); switch(game->status) { case Playing: break; case Paused: draw_paused(game); break; case Won: draw_won(game); break; case Lost: draw_lost(game); break; default: break; } al_flip_display(); } free(timerevent); free(genericevent); delete_game(game); return 0; }
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; ALLEGRO_COLOR black; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_init_primitives_addon(); al_install_mouse(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); actual_buttons = al_get_mouse_num_buttons(); if (actual_buttons > NUM_BUTTONS) actual_buttons = NUM_BUTTONS; al_set_new_display_flags(ALLEGRO_RESIZABLE); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); } al_hide_mouse_cursor(display); cursor = al_load_bitmap("data/cursor.tga"); if (!cursor) { abort_example("Error loading cursor.tga\n"); } font = al_load_font("data/fixed_font.tga", 1, 0); if (!font) { abort_example("data/fixed_font.tga not found\n"); } black = al_map_rgb_f(0, 0, 0); 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_is_event_queue_empty(queue)) { al_clear_to_color(al_map_rgb(0xff, 0xff, 0xc0)); for (i = 0; i < actual_buttons; i++) { draw_mouse_button(i, buttons[i]); } al_draw_bitmap(cursor, mx, my, 0); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); al_draw_textf(font, black, 5, 5, 0, "dx %i, dy %i, dz %i, dw %i", mmx, mmy, mmz, mmw); al_draw_textf(font, black, 5, 25, 0, "x %i, y %i, z %i, w %i", mx, my, mz, mw); al_draw_textf(font, black, 5, 45, 0, "p = %g", p); al_draw_textf(font, black, 5, 65, 0, "%s", in ? "in" : "out"); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); 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; }
void InputCatcher::run(ALLEGRO_DISPLAY *display, InputContainer *held_keys, double *mouse_x, double *mouse_y) { // pressed_keys->reset(); held_keys->reset(); ALLEGRO_EVENT event; // Catch all events that have stacked up this frame. al_get_next_event() returns false when event_queue is empty, and contents of event are undefined while (al_get_next_event(event_queue, &event)) { switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: // Deliberate closing, not an error throw 0; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(display); break; case ALLEGRO_EVENT_KEY_DOWN: // //Debug: print the keycode number and name of the key we press //// printf("\n%i\t%s", event.keyboard.keycode, al_keycode_to_name(event.keyboard.keycode)); // // if (event.keyboard.keycode == config["jump"] or event.keyboard.keycode == config["jump_alt1"] or event.keyboard.keycode == config["jump_alt2"]) // { // pressed_keys->JUMP = true; // } // if (event.keyboard.keycode == config["crouch"] or event.keyboard.keycode == config["crouch_alt1"] or event.keyboard.keycode == config["crouch_alt2"]) // { // pressed_keys->CROUCH = true; // } // if (event.keyboard.keycode == config["left"] or event.keyboard.keycode == config["left_alt1"] or event.keyboard.keycode == config["left_alt2"]) // { // pressed_keys->LEFT = true; // } // if (event.keyboard.keycode == config["right"] or event.keyboard.keycode == config["right_alt1"] or event.keyboard.keycode == config["right_alt2"]) // { // pressed_keys->RIGHT = true; // } // if (event.keyboard.keycode == config["right"] or event.keyboard.keycode == config["right_alt1"] or event.keyboard.keycode == config["right_alt2"]) // { // pressed_keys->RIGHT = true; // } // if (event.keyboard.keycode == config["ability1"] or event.keyboard.keycode == config["ability1_alt1"] or event.keyboard.keycode == config["ability1_alt2"]) // { // pressed_keys->ABILITY_1 = true; // } // if (event.keyboard.keycode == config["ability2"] or event.keyboard.keycode == config["ability2_alt1"] or event.keyboard.keycode == config["ability2_alt2"]) // { // pressed_keys->ABILITY_2 = true; // } // if (event.keyboard.keycode == config["ultimate"] or event.keyboard.keycode == config["ultimate_alt1"] or event.keyboard.keycode == config["ultimate_alt2"]) // { // pressed_keys->ULTIMATE = true; // } // if (event.keyboard.keycode == config["reload"] or event.keyboard.keycode == config["reload_alt1"] or event.keyboard.keycode == config["reload_alt2"]) // { // pressed_keys->RELOAD = true; // } // switch (event.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: // Exit game throw 0; } // // case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: // switch (event.mouse.button) // { // case LEFT_MOUSE_BUTTON: // pressed_keys->PRIMARY_FIRE = true; // break; // case RIGHT_MOUSE_BUTTON: // pressed_keys->SECONDARY_FIRE = true; // break; // } } } ALLEGRO_KEYBOARD_STATE keystate; al_get_keyboard_state(&keystate); if (al_key_down(&keystate, config["jump"]) or al_key_down(&keystate, config["jump_alt1"]) or al_key_down(&keystate, config["jump_alt2"])) { held_keys->JUMP = true; } if (al_key_down(&keystate, config["crouch"]) or al_key_down(&keystate, config["crouch_alt1"]) or al_key_down(&keystate, config["crouch_alt2"])) { held_keys->CROUCH = true; } if (al_key_down(&keystate, config["left"]) or al_key_down(&keystate, config["left_alt1"]) or al_key_down(&keystate, config["left_alt2"])) { held_keys->LEFT = true; } if (al_key_down(&keystate, config["right"]) or al_key_down(&keystate, config["right_alt1"]) or al_key_down(&keystate, config["right_alt2"])) { held_keys->RIGHT = true; } if (al_key_down(&keystate, config["ability1"]) or al_key_down(&keystate, config["ability1_alt1"]) or al_key_down(&keystate, config["ability1_alt2"])) { held_keys->ABILITY_1 = true; } if (al_key_down(&keystate, config["ability2"]) or al_key_down(&keystate, config["ability2_alt1"]) or al_key_down(&keystate, config["ability2_alt2"])) { held_keys->ABILITY_2 = true; } if (al_key_down(&keystate, config["ultimate"]) or al_key_down(&keystate, config["ultimate_alt1"]) or al_key_down(&keystate, config["ultimate_alt2"])) { held_keys->ULTIMATE = true; } if (al_key_down(&keystate, config["reload"]) or al_key_down(&keystate, config["reload_alt1"]) or al_key_down(&keystate, config["reload_alt2"])) { held_keys->RELOAD = true; } ALLEGRO_MOUSE_STATE mousestate; al_get_mouse_state(&mousestate); // FIXME: I have no idea if these constants are correct, allegro docs don't mention the specifics, just that it starts with 1. if (mousestate.buttons & LEFT_MOUSE_BUTTON) { held_keys->PRIMARY_FIRE = true; } if (mousestate.buttons & RIGHT_MOUSE_BUTTON) { held_keys->SECONDARY_FIRE = true; } *mouse_x = mousestate.x; *mouse_y = mousestate.y; }