|
16 | 16 | #include "imgui.h" |
17 | 17 | #include "imgui_impl_allegro5.h" |
18 | 18 |
|
| 19 | +// Forward declarations of helper functions |
| 20 | +bool MainLoopStep(ALLEGRO_DISPLAY* display, ALLEGRO_EVENT_QUEUE* queue); |
| 21 | + |
| 22 | +// Main code |
19 | 23 | int main(int, char**) |
20 | 24 | { |
21 | 25 | // Setup Allegro |
22 | 26 | al_init(); |
23 | 27 | al_install_keyboard(); |
24 | 28 | al_install_mouse(); |
25 | 29 | al_init_primitives_addon(); |
| 30 | + |
| 31 | + // Create window with graphics context and event queue |
26 | 32 | al_set_new_display_flags(ALLEGRO_RESIZABLE); |
27 | 33 | ALLEGRO_DISPLAY* display = al_create_display(1280, 720); |
28 | 34 | al_set_window_title(display, "Dear ImGui Allegro 5 example"); |
@@ -60,86 +66,98 @@ int main(int, char**) |
60 | 66 | //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); |
61 | 67 | //IM_ASSERT(font != NULL); |
62 | 68 |
|
63 | | - bool show_demo_window = true; |
64 | | - bool show_another_window = false; |
65 | | - ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
66 | | - |
67 | 69 | // Main loop |
68 | | - bool running = true; |
69 | | - while (running) |
| 70 | + while (true) |
| 71 | + { |
| 72 | + if (!MainLoopStep(display, queue)) |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + // Cleanup |
| 77 | + ImGui_ImplAllegro5_Shutdown(); |
| 78 | + ImGui::DestroyContext(); |
| 79 | + al_destroy_event_queue(queue); |
| 80 | + al_destroy_display(display); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +bool MainLoopStep(ALLEGRO_DISPLAY* display, ALLEGRO_EVENT_QUEUE* queue) |
| 86 | +{ |
| 87 | + // Poll and handle events (inputs, window resize, etc.) |
| 88 | + // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. |
| 89 | + // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
| 90 | + // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
| 91 | + // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. |
| 92 | + bool done = false; |
| 93 | + ALLEGRO_EVENT ev; |
| 94 | + while (al_get_next_event(queue, &ev)) |
70 | 95 | { |
71 | | - // Poll and handle events (inputs, window resize, etc.) |
72 | | - // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. |
73 | | - // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
74 | | - // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
75 | | - // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. |
76 | | - ALLEGRO_EVENT ev; |
77 | | - while (al_get_next_event(queue, &ev)) |
| 96 | + ImGui_ImplAllegro5_ProcessEvent(&ev); |
| 97 | + if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) |
| 98 | + done = true; |
| 99 | + if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) |
78 | 100 | { |
79 | | - ImGui_ImplAllegro5_ProcessEvent(&ev); |
80 | | - if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) |
81 | | - running = false; |
82 | | - if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) |
83 | | - { |
84 | | - ImGui_ImplAllegro5_InvalidateDeviceObjects(); |
85 | | - al_acknowledge_resize(display); |
86 | | - ImGui_ImplAllegro5_CreateDeviceObjects(); |
87 | | - } |
| 101 | + ImGui_ImplAllegro5_InvalidateDeviceObjects(); |
| 102 | + al_acknowledge_resize(display); |
| 103 | + ImGui_ImplAllegro5_CreateDeviceObjects(); |
88 | 104 | } |
| 105 | + } |
| 106 | + if (done) |
| 107 | + return false; |
89 | 108 |
|
90 | | - // Start the Dear ImGui frame |
91 | | - ImGui_ImplAllegro5_NewFrame(); |
92 | | - ImGui::NewFrame(); |
| 109 | + // Start the Dear ImGui frame |
| 110 | + ImGui_ImplAllegro5_NewFrame(); |
| 111 | + ImGui::NewFrame(); |
93 | 112 |
|
94 | | - // 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!). |
95 | | - if (show_demo_window) |
96 | | - ImGui::ShowDemoWindow(&show_demo_window); |
| 113 | + // Our state |
| 114 | + // (we use static, which essentially makes the variable globals, as a convenience to keep the example code easy to follow) |
| 115 | + static bool show_demo_window = true; |
| 116 | + static bool show_another_window = false; |
| 117 | + static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
97 | 118 |
|
98 | | - // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. |
99 | | - { |
100 | | - static float f = 0.0f; |
101 | | - static int counter = 0; |
| 119 | + // 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!). |
| 120 | + if (show_demo_window) |
| 121 | + ImGui::ShowDemoWindow(&show_demo_window); |
102 | 122 |
|
103 | | - ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. |
| 123 | + // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. |
| 124 | + { |
| 125 | + static float f = 0.0f; |
| 126 | + static int counter = 0; |
104 | 127 |
|
105 | | - ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) |
106 | | - ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state |
107 | | - ImGui::Checkbox("Another Window", &show_another_window); |
| 128 | + ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. |
108 | 129 |
|
109 | | - ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f |
110 | | - ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color |
| 130 | + ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) |
| 131 | + ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state |
| 132 | + ImGui::Checkbox("Another Window", &show_another_window); |
111 | 133 |
|
112 | | - if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) |
113 | | - counter++; |
114 | | - ImGui::SameLine(); |
115 | | - ImGui::Text("counter = %d", counter); |
| 134 | + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f |
| 135 | + ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color |
116 | 136 |
|
117 | | - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); |
118 | | - ImGui::End(); |
119 | | - } |
| 137 | + if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) |
| 138 | + counter++; |
| 139 | + ImGui::SameLine(); |
| 140 | + ImGui::Text("counter = %d", counter); |
120 | 141 |
|
121 | | - // 3. Show another simple window. |
122 | | - if (show_another_window) |
123 | | - { |
124 | | - 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) |
125 | | - ImGui::Text("Hello from another window!"); |
126 | | - if (ImGui::Button("Close Me")) |
127 | | - show_another_window = false; |
128 | | - ImGui::End(); |
129 | | - } |
| 142 | + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); |
| 143 | + ImGui::End(); |
| 144 | + } |
130 | 145 |
|
131 | | - // Rendering |
132 | | - ImGui::Render(); |
133 | | - 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)); |
134 | | - ImGui_ImplAllegro5_RenderDrawData(ImGui::GetDrawData()); |
135 | | - al_flip_display(); |
| 146 | + // 3. Show another simple window. |
| 147 | + if (show_another_window) |
| 148 | + { |
| 149 | + 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) |
| 150 | + ImGui::Text("Hello from another window!"); |
| 151 | + if (ImGui::Button("Close Me")) |
| 152 | + show_another_window = false; |
| 153 | + ImGui::End(); |
136 | 154 | } |
137 | 155 |
|
138 | | - // Cleanup |
139 | | - ImGui_ImplAllegro5_Shutdown(); |
140 | | - ImGui::DestroyContext(); |
141 | | - al_destroy_event_queue(queue); |
142 | | - al_destroy_display(display); |
| 156 | + // Rendering |
| 157 | + ImGui::Render(); |
| 158 | + 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)); |
| 159 | + ImGui_ImplAllegro5_RenderDrawData(ImGui::GetDrawData()); |
| 160 | + al_flip_display(); |
143 | 161 |
|
144 | | - return 0; |
| 162 | + return true; |
145 | 163 | } |
0 commit comments