main.cpp (6511B)
1 // Dear ImGui: standalone example application for Allegro 5 2 // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 3 // Read online: https://github.com/ocornut/imgui/tree/master/docs 4 5 #include <stdint.h> 6 #include <allegro5/allegro.h> 7 #include <allegro5/allegro_primitives.h> 8 #include "imgui.h" 9 #include "imgui_impl_allegro5.h" 10 11 int main(int, char**) 12 { 13 // Setup Allegro 14 al_init(); 15 al_install_keyboard(); 16 al_install_mouse(); 17 al_init_primitives_addon(); 18 al_set_new_display_flags(ALLEGRO_RESIZABLE); 19 ALLEGRO_DISPLAY* display = al_create_display(1280, 720); 20 al_set_window_title(display, "Dear ImGui Allegro 5 example"); 21 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 22 al_register_event_source(queue, al_get_display_event_source(display)); 23 al_register_event_source(queue, al_get_keyboard_event_source()); 24 al_register_event_source(queue, al_get_mouse_event_source()); 25 26 // Setup Dear ImGui context 27 IMGUI_CHECKVERSION(); 28 ImGui::CreateContext(); 29 ImGuiIO& io = ImGui::GetIO(); (void)io; 30 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls 31 32 // Setup Dear ImGui style 33 ImGui::StyleColorsDark(); 34 //ImGui::StyleColorsClassic(); 35 36 // Setup Platform/Renderer backends 37 ImGui_ImplAllegro5_Init(display); 38 39 // Load Fonts 40 // - 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. 41 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. 42 // - 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). 43 // - 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. 44 // - Read 'docs/FONTS.md' for more instructions and details. 45 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! 46 //io.Fonts->AddFontDefault(); 47 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); 48 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); 49 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); 50 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); 51 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); 52 //IM_ASSERT(font != NULL); 53 54 bool show_demo_window = true; 55 bool show_another_window = false; 56 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); 57 58 // Main loop 59 bool running = true; 60 while (running) 61 { 62 // Poll and handle events (inputs, window resize, etc.) 63 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. 64 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. 65 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. 66 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. 67 ALLEGRO_EVENT ev; 68 while (al_get_next_event(queue, &ev)) 69 { 70 ImGui_ImplAllegro5_ProcessEvent(&ev); 71 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 72 running = false; 73 if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) 74 { 75 ImGui_ImplAllegro5_InvalidateDeviceObjects(); 76 al_acknowledge_resize(display); 77 ImGui_ImplAllegro5_CreateDeviceObjects(); 78 } 79 } 80 81 // Start the Dear ImGui frame 82 ImGui_ImplAllegro5_NewFrame(); 83 ImGui::NewFrame(); 84 85 // 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!). 86 if (show_demo_window) 87 ImGui::ShowDemoWindow(&show_demo_window); 88 89 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. 90 { 91 static float f = 0.0f; 92 static int counter = 0; 93 94 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. 95 96 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) 97 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state 98 ImGui::Checkbox("Another Window", &show_another_window); 99 100 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f 101 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color 102 103 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) 104 counter++; 105 ImGui::SameLine(); 106 ImGui::Text("counter = %d", counter); 107 108 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); 109 ImGui::End(); 110 } 111 112 // 3. Show another simple window. 113 if (show_another_window) 114 { 115 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) 116 ImGui::Text("Hello from another window!"); 117 if (ImGui::Button("Close Me")) 118 show_another_window = false; 119 ImGui::End(); 120 } 121 122 // Rendering 123 ImGui::Render(); 124 al_clear_to_color(al_map_rgba_f(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w)); 125 ImGui_ImplAllegro5_RenderDrawData(ImGui::GetDrawData()); 126 al_flip_display(); 127 } 128 129 // Cleanup 130 ImGui_ImplAllegro5_Shutdown(); 131 ImGui::DestroyContext(); 132 al_destroy_event_queue(queue); 133 al_destroy_display(display); 134 135 return 0; 136 }