• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // dear imgui: Renderer + Platform Backend for Allegro 5
2 // (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.)
3 
4 // Implemented features:
5 //  [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as ImTextureID. Read the FAQ about ImTextureID!
6 //  [X] Platform: Clipboard support (from Allegro 5.1.12)
7 //  [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
8 // Issues:
9 //  [ ] Renderer: The renderer is suboptimal as we need to unindex our buffers and convert vertices manually.
10 //  [ ] Platform: Missing gamepad support.
11 
12 // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
13 // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
14 // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
15 // Read online: https://github.com/ocornut/imgui/tree/master/docs
16 
17 // CHANGELOG
18 // (minor and older changes stripped away, please see git history for details)
19 //  2021-08-17: Calling io.AddFocusEvent() on ALLEGRO_EVENT_DISPLAY_SWITCH_OUT/ALLEGRO_EVENT_DISPLAY_SWITCH_IN events.
20 //  2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
21 //  2021-05-19: Renderer: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
22 //  2021-02-18: Change blending equation to preserve alpha in output buffer.
23 //  2020-08-10: Inputs: Fixed horizontal mouse wheel direction.
24 //  2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
25 //  2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
26 //  2019-05-11: Inputs: Don't filter character value from ALLEGRO_EVENT_KEY_CHAR before calling AddInputCharacter().
27 //  2019-04-30: Renderer: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
28 //  2018-11-30: Platform: Added touchscreen support.
29 //  2018-11-30: Misc: Setting up io.BackendPlatformName/io.BackendRendererName so they can be displayed in the About Window.
30 //  2018-06-13: Platform: Added clipboard support (from Allegro 5.1.12).
31 //  2018-06-13: Renderer: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
32 //  2018-06-13: Renderer: Backup/restore transform and clipping rectangle.
33 //  2018-06-11: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
34 //  2018-04-18: Misc: Renamed file from imgui_impl_a5.cpp to imgui_impl_allegro5.cpp.
35 //  2018-04-18: Misc: Added support for 32-bit vertex indices to avoid conversion at runtime. Added imconfig_allegro5.h to enforce 32-bit indices when included from imgui.h.
36 //  2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplAllegro5_RenderDrawData() in the .h file so you can call it yourself.
37 //  2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
38 //  2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
39 
40 #include <stdint.h>     // uint64_t
41 #include <cstring>      // memcpy
42 #include "imgui.h"
43 #include "imgui_impl_allegro5.h"
44 
45 // Allegro
46 #include <allegro5/allegro.h>
47 #include <allegro5/allegro_primitives.h>
48 #ifdef _WIN32
49 #include <allegro5/allegro_windows.h>
50 #endif
51 #define ALLEGRO_HAS_CLIPBOARD   (ALLEGRO_VERSION_INT >= ((5 << 24) | (1 << 16) | (12 << 8)))    // Clipboard only supported from Allegro 5.1.12
52 
53 // Visual Studio warnings
54 #ifdef _MSC_VER
55 #pragma warning (disable: 4127) // condition expression is constant
56 #endif
57 
58 // Allegro Data
59 struct ImGui_ImplAllegro5_Data
60 {
61     ALLEGRO_DISPLAY*            Display;
62     ALLEGRO_BITMAP*             Texture;
63     double                      Time;
64     ALLEGRO_MOUSE_CURSOR*       MouseCursorInvisible;
65     ALLEGRO_VERTEX_DECL*        VertexDecl;
66     char*                       ClipboardTextData;
67 
ImGui_ImplAllegro5_DataImGui_ImplAllegro5_Data68     ImGui_ImplAllegro5_Data()   { memset(this, 0, sizeof(*this)); }
69 };
70 
71 // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
72 // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
73 // FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
ImGui_ImplAllegro5_GetBackendData()74 static ImGui_ImplAllegro5_Data* ImGui_ImplAllegro5_GetBackendData()     { return ImGui::GetCurrentContext() ? (ImGui_ImplAllegro5_Data*)ImGui::GetIO().BackendPlatformUserData : NULL; }
75 
76 struct ImDrawVertAllegro
77 {
78     ImVec2 pos;
79     ImVec2 uv;
80     ALLEGRO_COLOR col;
81 };
82 
ImGui_ImplAllegro5_SetupRenderState(ImDrawData * draw_data)83 static void ImGui_ImplAllegro5_SetupRenderState(ImDrawData* draw_data)
84 {
85     // Setup blending
86     al_set_separate_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
87 
88     // Setup orthographic projection matrix
89     // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
90     {
91         float L = draw_data->DisplayPos.x;
92         float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
93         float T = draw_data->DisplayPos.y;
94         float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
95         ALLEGRO_TRANSFORM transform;
96         al_identity_transform(&transform);
97         al_use_transform(&transform);
98         al_orthographic_transform(&transform, L, T, 1.0f, R, B, -1.0f);
99         al_use_projection_transform(&transform);
100     }
101 }
102 
103 // Render function.
ImGui_ImplAllegro5_RenderDrawData(ImDrawData * draw_data)104 void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data)
105 {
106     // Avoid rendering when minimized
107     if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
108         return;
109 
110     // Backup Allegro state that will be modified
111     ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
112     ALLEGRO_TRANSFORM last_transform = *al_get_current_transform();
113     ALLEGRO_TRANSFORM last_projection_transform = *al_get_current_projection_transform();
114     int last_clip_x, last_clip_y, last_clip_w, last_clip_h;
115     al_get_clipping_rectangle(&last_clip_x, &last_clip_y, &last_clip_w, &last_clip_h);
116     int last_blender_op, last_blender_src, last_blender_dst;
117     al_get_blender(&last_blender_op, &last_blender_src, &last_blender_dst);
118 
119     // Setup desired render state
120     ImGui_ImplAllegro5_SetupRenderState(draw_data);
121 
122     // Render command lists
123     for (int n = 0; n < draw_data->CmdListsCount; n++)
124     {
125         const ImDrawList* cmd_list = draw_data->CmdLists[n];
126 
127         // Allegro's implementation of al_draw_indexed_prim() for DX9 is completely broken. Unindex our buffers ourselves.
128         // FIXME-OPT: Unfortunately Allegro doesn't support 32-bit packed colors so we have to convert them to 4 float as well..
129         static ImVector<ImDrawVertAllegro> vertices;
130         vertices.resize(cmd_list->IdxBuffer.Size);
131         for (int i = 0; i < cmd_list->IdxBuffer.Size; i++)
132         {
133             const ImDrawVert* src_v = &cmd_list->VtxBuffer[cmd_list->IdxBuffer[i]];
134             ImDrawVertAllegro* dst_v = &vertices[i];
135             dst_v->pos = src_v->pos;
136             dst_v->uv = src_v->uv;
137             unsigned char* c = (unsigned char*)&src_v->col;
138             dst_v->col = al_map_rgba(c[0], c[1], c[2], c[3]);
139         }
140 
141         const int* indices = NULL;
142         if (sizeof(ImDrawIdx) == 2)
143         {
144             // FIXME-OPT: Unfortunately Allegro doesn't support 16-bit indices.. You can '#define ImDrawIdx int' in imconfig.h to request Dear ImGui to output 32-bit indices.
145             // Otherwise, we convert them from 16-bit to 32-bit at runtime here, which works perfectly but is a little wasteful.
146             static ImVector<int> indices_converted;
147             indices_converted.resize(cmd_list->IdxBuffer.Size);
148             for (int i = 0; i < cmd_list->IdxBuffer.Size; ++i)
149                 indices_converted[i] = (int)cmd_list->IdxBuffer.Data[i];
150             indices = indices_converted.Data;
151         }
152         else if (sizeof(ImDrawIdx) == 4)
153         {
154             indices = (const int*)cmd_list->IdxBuffer.Data;
155         }
156 
157         // Render command lists
158         int idx_offset = 0;
159         ImVec2 clip_off = draw_data->DisplayPos;
160         for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
161         {
162             const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
163             if (pcmd->UserCallback)
164             {
165                 // User callback, registered via ImDrawList::AddCallback()
166                 // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
167                 if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
168                     ImGui_ImplAllegro5_SetupRenderState(draw_data);
169                 else
170                     pcmd->UserCallback(cmd_list, pcmd);
171             }
172             else
173             {
174                 // Project scissor/clipping rectangles into framebuffer space
175                 ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y);
176                 ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y);
177                 if (clip_max.x < clip_min.x || clip_max.y < clip_min.y)
178                     continue;
179 
180                 // Apply scissor/clipping rectangle, Draw
181                 ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->GetTexID();
182                 al_set_clipping_rectangle(clip_min.x, clip_min.y, clip_max.x - clip_min.x, clip_max.y - clip_min.y);
183                 al_draw_prim(&vertices[0], bd->VertexDecl, texture, idx_offset, idx_offset + pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST);
184             }
185             idx_offset += pcmd->ElemCount;
186         }
187     }
188 
189     // Restore modified Allegro state
190     al_set_blender(last_blender_op, last_blender_src, last_blender_dst);
191     al_set_clipping_rectangle(last_clip_x, last_clip_y, last_clip_w, last_clip_h);
192     al_use_transform(&last_transform);
193     al_use_projection_transform(&last_projection_transform);
194 }
195 
ImGui_ImplAllegro5_CreateDeviceObjects()196 bool ImGui_ImplAllegro5_CreateDeviceObjects()
197 {
198     // Build texture atlas
199     ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
200     ImGuiIO& io = ImGui::GetIO();
201     unsigned char* pixels;
202     int width, height;
203     io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
204 
205     // Create texture
206     int flags = al_get_new_bitmap_flags();
207     int fmt = al_get_new_bitmap_format();
208     al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP | ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
209     al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE);
210     ALLEGRO_BITMAP* img = al_create_bitmap(width, height);
211     al_set_new_bitmap_flags(flags);
212     al_set_new_bitmap_format(fmt);
213     if (!img)
214         return false;
215 
216     ALLEGRO_LOCKED_REGION* locked_img = al_lock_bitmap(img, al_get_bitmap_format(img), ALLEGRO_LOCK_WRITEONLY);
217     if (!locked_img)
218     {
219         al_destroy_bitmap(img);
220         return false;
221     }
222     memcpy(locked_img->data, pixels, sizeof(int) * width * height);
223     al_unlock_bitmap(img);
224 
225     // Convert software texture to hardware texture.
226     ALLEGRO_BITMAP* cloned_img = al_clone_bitmap(img);
227     al_destroy_bitmap(img);
228     if (!cloned_img)
229         return false;
230 
231     // Store our identifier
232     io.Fonts->SetTexID((void*)cloned_img);
233     bd->Texture = cloned_img;
234 
235     // Create an invisible mouse cursor
236     // Because al_hide_mouse_cursor() seems to mess up with the actual inputs..
237     ALLEGRO_BITMAP* mouse_cursor = al_create_bitmap(8, 8);
238     bd->MouseCursorInvisible = al_create_mouse_cursor(mouse_cursor, 0, 0);
239     al_destroy_bitmap(mouse_cursor);
240 
241     return true;
242 }
243 
ImGui_ImplAllegro5_InvalidateDeviceObjects()244 void ImGui_ImplAllegro5_InvalidateDeviceObjects()
245 {
246     ImGuiIO& io = ImGui::GetIO();
247     ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
248     if (bd->Texture)
249     {
250         io.Fonts->SetTexID(NULL);
251         al_destroy_bitmap(bd->Texture);
252         bd->Texture = NULL;
253     }
254     if (bd->MouseCursorInvisible)
255     {
256         al_destroy_mouse_cursor(bd->MouseCursorInvisible);
257         bd->MouseCursorInvisible = NULL;
258     }
259 }
260 
261 #if ALLEGRO_HAS_CLIPBOARD
ImGui_ImplAllegro5_GetClipboardText(void *)262 static const char* ImGui_ImplAllegro5_GetClipboardText(void*)
263 {
264     ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
265     if (bd->ClipboardTextData)
266         al_free(bd->ClipboardTextData);
267     bd->ClipboardTextData = al_get_clipboard_text(bd->Display);
268     return bd->ClipboardTextData;
269 }
270 
ImGui_ImplAllegro5_SetClipboardText(void *,const char * text)271 static void ImGui_ImplAllegro5_SetClipboardText(void*, const char* text)
272 {
273     ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
274     al_set_clipboard_text(bd->Display, text);
275 }
276 #endif
277 
ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY * display)278 bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display)
279 {
280     ImGuiIO& io = ImGui::GetIO();
281     IM_ASSERT(io.BackendPlatformUserData == NULL && "Already initialized a platform backend!");
282 
283     // Setup backend capabilities flags
284     ImGui_ImplAllegro5_Data* bd = IM_NEW(ImGui_ImplAllegro5_Data)();
285     io.BackendPlatformUserData = (void*)bd;
286     io.BackendPlatformName = io.BackendRendererName = "imgui_impl_allegro5";
287     io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;       // We can honor GetMouseCursor() values (optional)
288 
289     bd->Display = display;
290 
291     // Create custom vertex declaration.
292     // Unfortunately Allegro doesn't support 32-bit packed colors so we have to convert them to 4 floats.
293     // We still use a custom declaration to use 'ALLEGRO_PRIM_TEX_COORD' instead of 'ALLEGRO_PRIM_TEX_COORD_PIXEL' else we can't do a reliable conversion.
294     ALLEGRO_VERTEX_ELEMENT elems[] =
295     {
296         { ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, IM_OFFSETOF(ImDrawVertAllegro, pos) },
297         { ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, IM_OFFSETOF(ImDrawVertAllegro, uv) },
298         { ALLEGRO_PRIM_COLOR_ATTR, 0, IM_OFFSETOF(ImDrawVertAllegro, col) },
299         { 0, 0, 0 }
300     };
301     bd->VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro));
302 
303     io.KeyMap[ImGuiKey_Tab] = ALLEGRO_KEY_TAB;
304     io.KeyMap[ImGuiKey_LeftArrow] = ALLEGRO_KEY_LEFT;
305     io.KeyMap[ImGuiKey_RightArrow] = ALLEGRO_KEY_RIGHT;
306     io.KeyMap[ImGuiKey_UpArrow] = ALLEGRO_KEY_UP;
307     io.KeyMap[ImGuiKey_DownArrow] = ALLEGRO_KEY_DOWN;
308     io.KeyMap[ImGuiKey_PageUp] = ALLEGRO_KEY_PGUP;
309     io.KeyMap[ImGuiKey_PageDown] = ALLEGRO_KEY_PGDN;
310     io.KeyMap[ImGuiKey_Home] = ALLEGRO_KEY_HOME;
311     io.KeyMap[ImGuiKey_End] = ALLEGRO_KEY_END;
312     io.KeyMap[ImGuiKey_Insert] = ALLEGRO_KEY_INSERT;
313     io.KeyMap[ImGuiKey_Delete] = ALLEGRO_KEY_DELETE;
314     io.KeyMap[ImGuiKey_Backspace] = ALLEGRO_KEY_BACKSPACE;
315     io.KeyMap[ImGuiKey_Space] = ALLEGRO_KEY_SPACE;
316     io.KeyMap[ImGuiKey_Enter] = ALLEGRO_KEY_ENTER;
317     io.KeyMap[ImGuiKey_Escape] = ALLEGRO_KEY_ESCAPE;
318     io.KeyMap[ImGuiKey_KeyPadEnter] = ALLEGRO_KEY_PAD_ENTER;
319     io.KeyMap[ImGuiKey_A] = ALLEGRO_KEY_A;
320     io.KeyMap[ImGuiKey_C] = ALLEGRO_KEY_C;
321     io.KeyMap[ImGuiKey_V] = ALLEGRO_KEY_V;
322     io.KeyMap[ImGuiKey_X] = ALLEGRO_KEY_X;
323     io.KeyMap[ImGuiKey_Y] = ALLEGRO_KEY_Y;
324     io.KeyMap[ImGuiKey_Z] = ALLEGRO_KEY_Z;
325     io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
326 
327 #if ALLEGRO_HAS_CLIPBOARD
328     io.SetClipboardTextFn = ImGui_ImplAllegro5_SetClipboardText;
329     io.GetClipboardTextFn = ImGui_ImplAllegro5_GetClipboardText;
330     io.ClipboardUserData = NULL;
331 #endif
332 
333     return true;
334 }
335 
ImGui_ImplAllegro5_Shutdown()336 void ImGui_ImplAllegro5_Shutdown()
337 {
338     ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
339     IM_ASSERT(bd != NULL && "No platform backend to shutdown, or already shutdown?");
340     ImGuiIO& io = ImGui::GetIO();
341 
342     ImGui_ImplAllegro5_InvalidateDeviceObjects();
343     if (bd->VertexDecl)
344         al_destroy_vertex_decl(bd->VertexDecl);
345     if (bd->ClipboardTextData)
346         al_free(bd->ClipboardTextData);
347 
348     io.BackendPlatformUserData = NULL;
349     io.BackendPlatformName = io.BackendRendererName = NULL;
350     IM_DELETE(bd);
351 }
352 
353 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
354 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
355 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
356 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT * ev)357 bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* ev)
358 {
359     ImGuiIO& io = ImGui::GetIO();
360     ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
361 
362     switch (ev->type)
363     {
364     case ALLEGRO_EVENT_MOUSE_AXES:
365         if (ev->mouse.display == bd->Display)
366         {
367             io.MouseWheel += ev->mouse.dz;
368             io.MouseWheelH -= ev->mouse.dw;
369             io.MousePos = ImVec2(ev->mouse.x, ev->mouse.y);
370         }
371         return true;
372     case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
373     case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
374         if (ev->mouse.display == bd->Display && ev->mouse.button <= 5)
375             io.MouseDown[ev->mouse.button - 1] = (ev->type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN);
376         return true;
377     case ALLEGRO_EVENT_TOUCH_MOVE:
378         if (ev->touch.display == bd->Display)
379             io.MousePos = ImVec2(ev->touch.x, ev->touch.y);
380         return true;
381     case ALLEGRO_EVENT_TOUCH_BEGIN:
382     case ALLEGRO_EVENT_TOUCH_END:
383     case ALLEGRO_EVENT_TOUCH_CANCEL:
384         if (ev->touch.display == bd->Display && ev->touch.primary)
385             io.MouseDown[0] = (ev->type == ALLEGRO_EVENT_TOUCH_BEGIN);
386         return true;
387     case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
388         if (ev->mouse.display == bd->Display)
389             io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
390         return true;
391     case ALLEGRO_EVENT_KEY_CHAR:
392         if (ev->keyboard.display == bd->Display)
393             if (ev->keyboard.unichar != 0)
394                 io.AddInputCharacter((unsigned int)ev->keyboard.unichar);
395         return true;
396     case ALLEGRO_EVENT_KEY_DOWN:
397     case ALLEGRO_EVENT_KEY_UP:
398         if (ev->keyboard.display == bd->Display)
399             io.KeysDown[ev->keyboard.keycode] = (ev->type == ALLEGRO_EVENT_KEY_DOWN);
400         return true;
401     case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
402         if (ev->display.source == bd->Display)
403             io.AddFocusEvent(false);
404         return true;
405     case ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
406         if (ev->display.source == bd->Display)
407         {
408             io.AddFocusEvent(true);
409 #if defined(ALLEGRO_UNSTABLE)
410             al_clear_keyboard_state(bd->Display);
411 #endif
412         }
413         return true;
414     }
415     return false;
416 }
417 
ImGui_ImplAllegro5_UpdateMouseCursor()418 static void ImGui_ImplAllegro5_UpdateMouseCursor()
419 {
420     ImGuiIO& io = ImGui::GetIO();
421     if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
422         return;
423 
424     ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
425     ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
426     if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
427     {
428         // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
429         al_set_mouse_cursor(bd->Display, bd->MouseCursorInvisible);
430     }
431     else
432     {
433         ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
434         switch (imgui_cursor)
435         {
436         case ImGuiMouseCursor_TextInput:    cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT; break;
437         case ImGuiMouseCursor_ResizeAll:    cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE; break;
438         case ImGuiMouseCursor_ResizeNS:     cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N; break;
439         case ImGuiMouseCursor_ResizeEW:     cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E; break;
440         case ImGuiMouseCursor_ResizeNESW:   cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE; break;
441         case ImGuiMouseCursor_ResizeNWSE:   cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW; break;
442         case ImGuiMouseCursor_NotAllowed:   cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_UNAVAILABLE; break;
443         }
444         al_set_system_mouse_cursor(bd->Display, cursor_id);
445     }
446 }
447 
ImGui_ImplAllegro5_NewFrame()448 void ImGui_ImplAllegro5_NewFrame()
449 {
450     ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
451     IM_ASSERT(bd != NULL && "Did you call ImGui_ImplAllegro5_Init()?");
452 
453     if (!bd->Texture)
454         ImGui_ImplAllegro5_CreateDeviceObjects();
455 
456     ImGuiIO& io = ImGui::GetIO();
457 
458     // Setup display size (every frame to accommodate for window resizing)
459     int w, h;
460     w = al_get_display_width(bd->Display);
461     h = al_get_display_height(bd->Display);
462     io.DisplaySize = ImVec2((float)w, (float)h);
463 
464     // Setup time step
465     double current_time = al_get_time();
466     io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f);
467     bd->Time = current_time;
468 
469     // Setup inputs
470     ALLEGRO_KEYBOARD_STATE keys;
471     al_get_keyboard_state(&keys);
472     io.KeyCtrl = al_key_down(&keys, ALLEGRO_KEY_LCTRL) || al_key_down(&keys, ALLEGRO_KEY_RCTRL);
473     io.KeyShift = al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT);
474     io.KeyAlt = al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR);
475     io.KeySuper = al_key_down(&keys, ALLEGRO_KEY_LWIN) || al_key_down(&keys, ALLEGRO_KEY_RWIN);
476 
477     ImGui_ImplAllegro5_UpdateMouseCursor();
478 }
479