Debian Codegraph @

subprojects/imgui-1.91.6/backends/imgui_impl_allegro5.cpp

raw
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: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy ALLEGRO_KEY_* values are obsolete since 1.87 and not supported since 1.91.5]
7 // [X] Platform: Clipboard support (from Allegro 5.1.12).
8 // [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
9 // Missing features or Issues:
10 // [ ] Renderer: The renderer is suboptimal as we need to convert vertices manually.
11 // [ ] Platform: Missing gamepad support.
12
13 // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
14 // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
15 // Learn about Dear ImGui:
16 // - FAQ https://dearimgui.com/faq
17 // - Getting Started https://dearimgui.com/getting-started
18 // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
19 // - Introduction, links and more at the top of imgui.cpp
20
21 // CHANGELOG
22 // (minor and older changes stripped away, please see git history for details)
23 // 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
24 // - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
25 // - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
26 // 2022-11-30: Renderer: Restoring using al_draw_indexed_prim() when Allegro version is >= 5.2.5.
27 // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
28 // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
29 // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
30 // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
31 // 2022-01-17: Inputs: always calling io.AddKeyModsEvent() next and before key event (not in NewFrame) to fix input queue with very low framerates.
32 // 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
33 // 2021-12-08: Renderer: Fixed mishandling of the ImDrawCmd::IdxOffset field! This is an old bug but it never had an effect until some internal rendering changes in 1.86.
34 // 2021-08-17: Calling io.AddFocusEvent() on ALLEGRO_EVENT_DISPLAY_SWITCH_OUT/ALLEGRO_EVENT_DISPLAY_SWITCH_IN events.
35 // 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).
36 // 2021-05-19: Renderer: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
37 // 2021-02-18: Change blending equation to preserve alpha in output buffer.
38 // 2020-08-10: Inputs: Fixed horizontal mouse wheel direction.
39 // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
40 // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
41 // 2019-05-11: Inputs: Don't filter character value from ALLEGRO_EVENT_KEY_CHAR before calling AddInputCharacter().
42 // 2019-04-30: Renderer: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
43 // 2018-11-30: Platform: Added touchscreen support.
44 // 2018-11-30: Misc: Setting up io.BackendPlatformName/io.BackendRendererName so they can be displayed in the About Window.
45 // 2018-06-13: Platform: Added clipboard support (from Allegro 5.1.12).
46 // 2018-06-13: Renderer: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
47 // 2018-06-13: Renderer: Stopped using al_draw_indexed_prim() as it is buggy in Allegro's DX9 backend.
48 // 2018-06-13: Renderer: Backup/restore transform and clipping rectangle.
49 // 2018-06-11: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
50 // 2018-04-18: Misc: Renamed file from imgui_impl_a5.cpp to imgui_impl_allegro5.cpp.
51 // 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.
52 // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplAllegro5_RenderDrawData() in the .h file so you can call it yourself.
53 // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
54 // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
55
56 #include "imgui.h"
57 #ifndef IMGUI_DISABLE
58 #include "imgui_impl_allegro5.h"
59 #include <stdint.h> // uint64_t
60 #include <cstring> // memcpy
61
62 // Allegro
63 #include <allegro5/allegro.h>
64 #include <allegro5/allegro_primitives.h>
65 #ifdef _WIN32
66 #include <allegro5/allegro_windows.h>
67 #endif
68 #define ALLEGRO_HAS_CLIPBOARD ((ALLEGRO_VERSION_INT & ~ALLEGRO_UNSTABLE_BIT) >= ((5 << 24) | (1 << 16) | (12 << 8))) // Clipboard only supported from Allegro 5.1.12
69 #define ALLEGRO_HAS_DRAW_INDEXED_PRIM ((ALLEGRO_VERSION_INT & ~ALLEGRO_UNSTABLE_BIT) >= ((5 << 24) | (2 << 16) | ( 5 << 8))) // DX9 implementation of al_draw_indexed_prim() got fixed in Allegro 5.2.5
70
71 // Visual Studio warnings
72 #ifdef _MSC_VER
73 #pragma warning (disable: 4127) // condition expression is constant
74 #endif
75
76 struct ImDrawVertAllegro
77 {
78 ImVec2 pos;
79 ImVec2 uv;
80 ALLEGRO_COLOR col;
81 };
82
83 // FIXME-OPT: Unfortunately Allegro doesn't support 32-bit packed colors so we have to convert them to 4 float as well..
84 // FIXME-OPT: Consider inlining al_map_rgba()?
85 // see https://github.com/liballeg/allegro5/blob/master/src/pixels.c#L554
86 // and https://github.com/liballeg/allegro5/blob/master/include/allegro5/internal/aintern_pixels.h
87 #define DRAW_VERT_IMGUI_TO_ALLEGRO(DST, SRC) { (DST)->pos = (SRC)->pos; (DST)->uv = (SRC)->uv; unsigned char* c = (unsigned char*)&(SRC)->col; (DST)->col = al_map_rgba(c[0], c[1], c[2], c[3]); }
88
89 // Allegro Data
90 struct ImGui_ImplAllegro5_Data
91 {
92 ALLEGRO_DISPLAY* Display;
93 ALLEGRO_BITMAP* Texture;
94 double Time;
95 ALLEGRO_MOUSE_CURSOR* MouseCursorInvisible;
96 ALLEGRO_VERTEX_DECL* VertexDecl;
97 char* ClipboardTextData;
98
99 ImVector<ImDrawVertAllegro> BufVertices;
100 ImVector<int> BufIndices;
101
102 ImGui_ImplAllegro5_Data() { memset((void*)this, 0, sizeof(*this)); }
103 };
104
105 // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
106 // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
107 // FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
108 static ImGui_ImplAllegro5_Data* ImGui_ImplAllegro5_GetBackendData() { return ImGui::GetCurrentContext() ? (ImGui_ImplAllegro5_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; }
109
110 static void ImGui_ImplAllegro5_SetupRenderState(ImDrawData* draw_data)
111 {
112 // Setup blending
113 al_set_separate_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
114
115 // Setup orthographic projection matrix
116 // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
117 {
118 float L = draw_data->DisplayPos.x;
119 float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
120 float T = draw_data->DisplayPos.y;
121 float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
122 ALLEGRO_TRANSFORM transform;
123 al_identity_transform(&transform);
124 al_use_transform(&transform);
125 al_orthographic_transform(&transform, L, T, 1.0f, R, B, -1.0f);
126 al_use_projection_transform(&transform);
127 }
128 }
129
130 // Render function.
131 void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data)
132 {
133 // Avoid rendering when minimized
134 if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
135 return;
136
137 // Backup Allegro state that will be modified
138 ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
139 ALLEGRO_TRANSFORM last_transform = *al_get_current_transform();
140 ALLEGRO_TRANSFORM last_projection_transform = *al_get_current_projection_transform();
141 int last_clip_x, last_clip_y, last_clip_w, last_clip_h;
142 al_get_clipping_rectangle(&last_clip_x, &last_clip_y, &last_clip_w, &last_clip_h);
143 int last_blender_op, last_blender_src, last_blender_dst;
144 al_get_blender(&last_blender_op, &last_blender_src, &last_blender_dst);
145
146 // Setup desired render state
147 ImGui_ImplAllegro5_SetupRenderState(draw_data);
148
149 // Render command lists
150 for (int n = 0; n < draw_data->CmdListsCount; n++)
151 {
152 const ImDrawList* draw_list = draw_data->CmdLists[n];
153
154 ImVector<ImDrawVertAllegro>& vertices = bd->BufVertices;
155 #if ALLEGRO_HAS_DRAW_INDEXED_PRIM
156 vertices.resize(draw_list->VtxBuffer.Size);
157 for (int i = 0; i < draw_list->VtxBuffer.Size; i++)
158 {
159 const ImDrawVert* src_v = &draw_list->VtxBuffer[i];
160 ImDrawVertAllegro* dst_v = &vertices[i];
161 DRAW_VERT_IMGUI_TO_ALLEGRO(dst_v, src_v);
162 }
163 const int* indices = nullptr;
164 if (sizeof(ImDrawIdx) == 2)
165 {
166 // FIXME-OPT: Allegro doesn't support 16-bit indices.
167 // You can '#define ImDrawIdx int' in imconfig.h to request Dear ImGui to output 32-bit indices.
168 // Otherwise, we convert them from 16-bit to 32-bit at runtime here, which works perfectly but is a little wasteful.
169 bd->BufIndices.resize(draw_list->IdxBuffer.Size);
170 for (int i = 0; i < draw_list->IdxBuffer.Size; ++i)
171 bd->BufIndices[i] = (int)draw_list->IdxBuffer.Data[i];
172 indices = bd->BufIndices.Data;
173 }
174 else if (sizeof(ImDrawIdx) == 4)
175 {
176 indices = (const int*)draw_list->IdxBuffer.Data;
177 }
178 #else
179 // Allegro's implementation of al_draw_indexed_prim() for DX9 was broken until 5.2.5. Unindex buffers ourselves while converting vertex format.
180 vertices.resize(draw_list->IdxBuffer.Size);
181 for (int i = 0; i < draw_list->IdxBuffer.Size; i++)
182 {
183 const ImDrawVert* src_v = &draw_list->VtxBuffer[draw_list->IdxBuffer[i]];
184 ImDrawVertAllegro* dst_v = &vertices[i];
185 DRAW_VERT_IMGUI_TO_ALLEGRO(dst_v, src_v);
186 }
187 #endif
188
189 // Render command lists
190 ImVec2 clip_off = draw_data->DisplayPos;
191 for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
192 {
193 const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
194 if (pcmd->UserCallback)
195 {
196 // User callback, registered via ImDrawList::AddCallback()
197 // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
198 if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
199 ImGui_ImplAllegro5_SetupRenderState(draw_data);
200 else
201 pcmd->UserCallback(draw_list, pcmd);
202 }
203 else
204 {
205 // Project scissor/clipping rectangles into framebuffer space
206 ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y);
207 ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y);
208 if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
209 continue;
210
211 // Apply scissor/clipping rectangle, Draw
212 ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->GetTexID();
213 al_set_clipping_rectangle(clip_min.x, clip_min.y, clip_max.x - clip_min.x, clip_max.y - clip_min.y);
214 #if ALLEGRO_HAS_DRAW_INDEXED_PRIM
215 al_draw_indexed_prim(&vertices[0], bd->VertexDecl, texture, &indices[pcmd->IdxOffset], pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST);
216 #else
217 al_draw_prim(&vertices[0], bd->VertexDecl, texture, pcmd->IdxOffset, pcmd->IdxOffset + pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST);
218 #endif
219 }
220 }
221 }
222
223 // Restore modified Allegro state
224 al_set_blender(last_blender_op, last_blender_src, last_blender_dst);
225 al_set_clipping_rectangle(last_clip_x, last_clip_y, last_clip_w, last_clip_h);
226 al_use_transform(&last_transform);
227 al_use_projection_transform(&last_projection_transform);
228 }
229
230 bool ImGui_ImplAllegro5_CreateDeviceObjects()
231 {
232 // Build texture atlas
233 ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
234 ImGuiIO& io = ImGui::GetIO();
235 unsigned char* pixels;
236 int width, height;
237 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
238
239 // Create texture
240 // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
241 int flags = al_get_new_bitmap_flags();
242 int fmt = al_get_new_bitmap_format();
243 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP | ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
244 al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE);
245 ALLEGRO_BITMAP* img = al_create_bitmap(width, height);
246 al_set_new_bitmap_flags(flags);
247 al_set_new_bitmap_format(fmt);
248 if (!img)
249 return false;
250
251 ALLEGRO_LOCKED_REGION* locked_img = al_lock_bitmap(img, al_get_bitmap_format(img), ALLEGRO_LOCK_WRITEONLY);
252 if (!locked_img)
253 {
254 al_destroy_bitmap(img);
255 return false;
256 }
257 memcpy(locked_img->data, pixels, sizeof(int) * width * height);
258 al_unlock_bitmap(img);
259
260 // Convert software texture to hardware texture.
261 ALLEGRO_BITMAP* cloned_img = al_clone_bitmap(img);
262 al_destroy_bitmap(img);
263 if (!cloned_img)
264 return false;
265
266 // Store our identifier
267 io.Fonts->SetTexID((ImTextureID)(intptr_t)cloned_img);
268 bd->Texture = cloned_img;
269
270 // Create an invisible mouse cursor
271 // Because al_hide_mouse_cursor() seems to mess up with the actual inputs..
272 ALLEGRO_BITMAP* mouse_cursor = al_create_bitmap(8, 8);
273 bd->MouseCursorInvisible = al_create_mouse_cursor(mouse_cursor, 0, 0);
274 al_destroy_bitmap(mouse_cursor);
275
276 return true;
277 }
278
279 void ImGui_ImplAllegro5_InvalidateDeviceObjects()
280 {
281 ImGuiIO& io = ImGui::GetIO();
282 ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
283 if (bd->Texture)
284 {
285 io.Fonts->SetTexID(0);
286 al_destroy_bitmap(bd->Texture);
287 bd->Texture = nullptr;
288 }
289 if (bd->MouseCursorInvisible)
290 {
291 al_destroy_mouse_cursor(bd->MouseCursorInvisible);
292 bd->MouseCursorInvisible = nullptr;
293 }
294 }
295
296 #if ALLEGRO_HAS_CLIPBOARD
297 static const char* ImGui_ImplAllegro5_GetClipboardText(ImGuiContext*)
298 {
299 ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
300 if (bd->ClipboardTextData)
301 al_free(bd->ClipboardTextData);
302 bd->ClipboardTextData = al_get_clipboard_text(bd->Display);
303 return bd->ClipboardTextData;
304 }
305
306 static void ImGui_ImplAllegro5_SetClipboardText(ImGuiContext*, const char* text)
307 {
308 ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
309 al_set_clipboard_text(bd->Display, text);
310 }
311 #endif
312
313 // Not static to allow third-party code to use that if they want to (but undocumented)
314 ImGuiKey ImGui_ImplAllegro5_KeyCodeToImGuiKey(int key_code);
315 ImGuiKey ImGui_ImplAllegro5_KeyCodeToImGuiKey(int key_code)
316 {
317 switch (key_code)
318 {
319 case ALLEGRO_KEY_TAB: return ImGuiKey_Tab;
320 case ALLEGRO_KEY_LEFT: return ImGuiKey_LeftArrow;
321 case ALLEGRO_KEY_RIGHT: return ImGuiKey_RightArrow;
322 case ALLEGRO_KEY_UP: return ImGuiKey_UpArrow;
323 case ALLEGRO_KEY_DOWN: return ImGuiKey_DownArrow;
324 case ALLEGRO_KEY_PGUP: return ImGuiKey_PageUp;
325 case ALLEGRO_KEY_PGDN: return ImGuiKey_PageDown;
326 case ALLEGRO_KEY_HOME: return ImGuiKey_Home;
327 case ALLEGRO_KEY_END: return ImGuiKey_End;
328 case ALLEGRO_KEY_INSERT: return ImGuiKey_Insert;
329 case ALLEGRO_KEY_DELETE: return ImGuiKey_Delete;
330 case ALLEGRO_KEY_BACKSPACE: return ImGuiKey_Backspace;
331 case ALLEGRO_KEY_SPACE: return ImGuiKey_Space;
332 case ALLEGRO_KEY_ENTER: return ImGuiKey_Enter;
333 case ALLEGRO_KEY_ESCAPE: return ImGuiKey_Escape;
334 case ALLEGRO_KEY_QUOTE: return ImGuiKey_Apostrophe;
335 case ALLEGRO_KEY_COMMA: return ImGuiKey_Comma;
336 case ALLEGRO_KEY_MINUS: return ImGuiKey_Minus;
337 case ALLEGRO_KEY_FULLSTOP: return ImGuiKey_Period;
338 case ALLEGRO_KEY_SLASH: return ImGuiKey_Slash;
339 case ALLEGRO_KEY_SEMICOLON: return ImGuiKey_Semicolon;
340 case ALLEGRO_KEY_EQUALS: return ImGuiKey_Equal;
341 case ALLEGRO_KEY_OPENBRACE: return ImGuiKey_LeftBracket;
342 case ALLEGRO_KEY_BACKSLASH: return ImGuiKey_Backslash;
343 case ALLEGRO_KEY_CLOSEBRACE: return ImGuiKey_RightBracket;
344 case ALLEGRO_KEY_TILDE: return ImGuiKey_GraveAccent;
345 case ALLEGRO_KEY_CAPSLOCK: return ImGuiKey_CapsLock;
346 case ALLEGRO_KEY_SCROLLLOCK: return ImGuiKey_ScrollLock;
347 case ALLEGRO_KEY_NUMLOCK: return ImGuiKey_NumLock;
348 case ALLEGRO_KEY_PRINTSCREEN: return ImGuiKey_PrintScreen;
349 case ALLEGRO_KEY_PAUSE: return ImGuiKey_Pause;
350 case ALLEGRO_KEY_PAD_0: return ImGuiKey_Keypad0;
351 case ALLEGRO_KEY_PAD_1: return ImGuiKey_Keypad1;
352 case ALLEGRO_KEY_PAD_2: return ImGuiKey_Keypad2;
353 case ALLEGRO_KEY_PAD_3: return ImGuiKey_Keypad3;
354 case ALLEGRO_KEY_PAD_4: return ImGuiKey_Keypad4;
355 case ALLEGRO_KEY_PAD_5: return ImGuiKey_Keypad5;
356 case ALLEGRO_KEY_PAD_6: return ImGuiKey_Keypad6;
357 case ALLEGRO_KEY_PAD_7: return ImGuiKey_Keypad7;
358 case ALLEGRO_KEY_PAD_8: return ImGuiKey_Keypad8;
359 case ALLEGRO_KEY_PAD_9: return ImGuiKey_Keypad9;
360 case ALLEGRO_KEY_PAD_DELETE: return ImGuiKey_KeypadDecimal;
361 case ALLEGRO_KEY_PAD_SLASH: return ImGuiKey_KeypadDivide;
362 case ALLEGRO_KEY_PAD_ASTERISK: return ImGuiKey_KeypadMultiply;
363 case ALLEGRO_KEY_PAD_MINUS: return ImGuiKey_KeypadSubtract;
364 case ALLEGRO_KEY_PAD_PLUS: return ImGuiKey_KeypadAdd;
365 case ALLEGRO_KEY_PAD_ENTER: return ImGuiKey_KeypadEnter;
366 case ALLEGRO_KEY_PAD_EQUALS: return ImGuiKey_KeypadEqual;
367 case ALLEGRO_KEY_LCTRL: return ImGuiKey_LeftCtrl;
368 case ALLEGRO_KEY_LSHIFT: return ImGuiKey_LeftShift;
369 case ALLEGRO_KEY_ALT: return ImGuiKey_LeftAlt;
370 case ALLEGRO_KEY_LWIN: return ImGuiKey_LeftSuper;
371 case ALLEGRO_KEY_RCTRL: return ImGuiKey_RightCtrl;
372 case ALLEGRO_KEY_RSHIFT: return ImGuiKey_RightShift;
373 case ALLEGRO_KEY_ALTGR: return ImGuiKey_RightAlt;
374 case ALLEGRO_KEY_RWIN: return ImGuiKey_RightSuper;
375 case ALLEGRO_KEY_MENU: return ImGuiKey_Menu;
376 case ALLEGRO_KEY_0: return ImGuiKey_0;
377 case ALLEGRO_KEY_1: return ImGuiKey_1;
378 case ALLEGRO_KEY_2: return ImGuiKey_2;
379 case ALLEGRO_KEY_3: return ImGuiKey_3;
380 case ALLEGRO_KEY_4: return ImGuiKey_4;
381 case ALLEGRO_KEY_5: return ImGuiKey_5;
382 case ALLEGRO_KEY_6: return ImGuiKey_6;
383 case ALLEGRO_KEY_7: return ImGuiKey_7;
384 case ALLEGRO_KEY_8: return ImGuiKey_8;
385 case ALLEGRO_KEY_9: return ImGuiKey_9;
386 case ALLEGRO_KEY_A: return ImGuiKey_A;
387 case ALLEGRO_KEY_B: return ImGuiKey_B;
388 case ALLEGRO_KEY_C: return ImGuiKey_C;
389 case ALLEGRO_KEY_D: return ImGuiKey_D;
390 case ALLEGRO_KEY_E: return ImGuiKey_E;
391 case ALLEGRO_KEY_F: return ImGuiKey_F;
392 case ALLEGRO_KEY_G: return ImGuiKey_G;
393 case ALLEGRO_KEY_H: return ImGuiKey_H;
394 case ALLEGRO_KEY_I: return ImGuiKey_I;
395 case ALLEGRO_KEY_J: return ImGuiKey_J;
396 case ALLEGRO_KEY_K: return ImGuiKey_K;
397 case ALLEGRO_KEY_L: return ImGuiKey_L;
398 case ALLEGRO_KEY_M: return ImGuiKey_M;
399 case ALLEGRO_KEY_N: return ImGuiKey_N;
400 case ALLEGRO_KEY_O: return ImGuiKey_O;
401 case ALLEGRO_KEY_P: return ImGuiKey_P;
402 case ALLEGRO_KEY_Q: return ImGuiKey_Q;
403 case ALLEGRO_KEY_R: return ImGuiKey_R;
404 case ALLEGRO_KEY_S: return ImGuiKey_S;
405 case ALLEGRO_KEY_T: return ImGuiKey_T;
406 case ALLEGRO_KEY_U: return ImGuiKey_U;
407 case ALLEGRO_KEY_V: return ImGuiKey_V;
408 case ALLEGRO_KEY_W: return ImGuiKey_W;
409 case ALLEGRO_KEY_X: return ImGuiKey_X;
410 case ALLEGRO_KEY_Y: return ImGuiKey_Y;
411 case ALLEGRO_KEY_Z: return ImGuiKey_Z;
412 case ALLEGRO_KEY_F1: return ImGuiKey_F1;
413 case ALLEGRO_KEY_F2: return ImGuiKey_F2;
414 case ALLEGRO_KEY_F3: return ImGuiKey_F3;
415 case ALLEGRO_KEY_F4: return ImGuiKey_F4;
416 case ALLEGRO_KEY_F5: return ImGuiKey_F5;
417 case ALLEGRO_KEY_F6: return ImGuiKey_F6;
418 case ALLEGRO_KEY_F7: return ImGuiKey_F7;
419 case ALLEGRO_KEY_F8: return ImGuiKey_F8;
420 case ALLEGRO_KEY_F9: return ImGuiKey_F9;
421 case ALLEGRO_KEY_F10: return ImGuiKey_F10;
422 case ALLEGRO_KEY_F11: return ImGuiKey_F11;
423 case ALLEGRO_KEY_F12: return ImGuiKey_F12;
424 default: return ImGuiKey_None;
425 }
426 }
427
428 bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display)
429 {
430 ImGuiIO& io = ImGui::GetIO();
431 IMGUI_CHECKVERSION();
432 IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
433
434 // Setup backend capabilities flags
435 ImGui_ImplAllegro5_Data* bd = IM_NEW(ImGui_ImplAllegro5_Data)();
436 io.BackendPlatformUserData = (void*)bd;
437 io.BackendPlatformName = io.BackendRendererName = "imgui_impl_allegro5";
438 io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
439
440 bd->Display = display;
441
442 // Create custom vertex declaration.
443 // Unfortunately Allegro doesn't support 32-bit packed colors so we have to convert them to 4 floats.
444 // 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.
445 ALLEGRO_VERTEX_ELEMENT elems[] =
446 {
447 { ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, pos) },
448 { ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, uv) },
449 { ALLEGRO_PRIM_COLOR_ATTR, 0, offsetof(ImDrawVertAllegro, col) },
450 { 0, 0, 0 }
451 };
452 bd->VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro));
453
454 #if ALLEGRO_HAS_CLIPBOARD
455 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
456 platform_io.Platform_SetClipboardTextFn = ImGui_ImplAllegro5_SetClipboardText;
457 platform_io.Platform_GetClipboardTextFn = ImGui_ImplAllegro5_GetClipboardText;
458 #endif
459
460 return true;
461 }
462
463 void ImGui_ImplAllegro5_Shutdown()
464 {
465 ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
466 IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
467 ImGuiIO& io = ImGui::GetIO();
468
469 ImGui_ImplAllegro5_InvalidateDeviceObjects();
470 if (bd->VertexDecl)
471 al_destroy_vertex_decl(bd->VertexDecl);
472 if (bd->ClipboardTextData)
473 al_free(bd->ClipboardTextData);
474
475 io.BackendPlatformName = io.BackendRendererName = nullptr;
476 io.BackendPlatformUserData = nullptr;
477 io.BackendFlags &= ~ImGuiBackendFlags_HasMouseCursors;
478 IM_DELETE(bd);
479 }
480
481 // ev->keyboard.modifiers seems always zero so using that...
482 static void ImGui_ImplAllegro5_UpdateKeyModifiers()
483 {
484 ImGuiIO& io = ImGui::GetIO();
485 ALLEGRO_KEYBOARD_STATE keys;
486 al_get_keyboard_state(&keys);
487 io.AddKeyEvent(ImGuiMod_Ctrl, al_key_down(&keys, ALLEGRO_KEY_LCTRL) || al_key_down(&keys, ALLEGRO_KEY_RCTRL));
488 io.AddKeyEvent(ImGuiMod_Shift, al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT));
489 io.AddKeyEvent(ImGuiMod_Alt, al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR));
490 io.AddKeyEvent(ImGuiMod_Super, al_key_down(&keys, ALLEGRO_KEY_LWIN) || al_key_down(&keys, ALLEGRO_KEY_RWIN));
491 }
492
493 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
494 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
495 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
496 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
497 bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* ev)
498 {
499 ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
500 IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplAllegro5_Init()?");
501 ImGuiIO& io = ImGui::GetIO();
502
503 switch (ev->type)
504 {
505 case ALLEGRO_EVENT_MOUSE_AXES:
506 if (ev->mouse.display == bd->Display)
507 {
508 io.AddMousePosEvent(ev->mouse.x, ev->mouse.y);
509 io.AddMouseWheelEvent(-ev->mouse.dw, ev->mouse.dz);
510 }
511 return true;
512 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
513 case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
514 if (ev->mouse.display == bd->Display && ev->mouse.button > 0 && ev->mouse.button <= 5)
515 io.AddMouseButtonEvent(ev->mouse.button - 1, ev->type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN);
516 return true;
517 case ALLEGRO_EVENT_TOUCH_MOVE:
518 if (ev->touch.display == bd->Display)
519 io.AddMousePosEvent(ev->touch.x, ev->touch.y);
520 return true;
521 case ALLEGRO_EVENT_TOUCH_BEGIN:
522 case ALLEGRO_EVENT_TOUCH_END:
523 case ALLEGRO_EVENT_TOUCH_CANCEL:
524 if (ev->touch.display == bd->Display && ev->touch.primary)
525 io.AddMouseButtonEvent(0, ev->type == ALLEGRO_EVENT_TOUCH_BEGIN);
526 return true;
527 case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
528 if (ev->mouse.display == bd->Display)
529 io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
530 return true;
531 case ALLEGRO_EVENT_KEY_CHAR:
532 if (ev->keyboard.display == bd->Display)
533 if (ev->keyboard.unichar != 0)
534 io.AddInputCharacter((unsigned int)ev->keyboard.unichar);
535 return true;
536 case ALLEGRO_EVENT_KEY_DOWN:
537 case ALLEGRO_EVENT_KEY_UP:
538 if (ev->keyboard.display == bd->Display)
539 {
540 ImGui_ImplAllegro5_UpdateKeyModifiers();
541 ImGuiKey key = ImGui_ImplAllegro5_KeyCodeToImGuiKey(ev->keyboard.keycode);
542 io.AddKeyEvent(key, (ev->type == ALLEGRO_EVENT_KEY_DOWN));
543 io.SetKeyEventNativeData(key, ev->keyboard.keycode, -1); // To support legacy indexing (<1.87 user code)
544 }
545 return true;
546 case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
547 if (ev->display.source == bd->Display)
548 io.AddFocusEvent(false);
549 return true;
550 case ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
551 if (ev->display.source == bd->Display)
552 {
553 io.AddFocusEvent(true);
554 #if defined(ALLEGRO_UNSTABLE)
555 al_clear_keyboard_state(bd->Display);
556 #endif
557 }
558 return true;
559 }
560 return false;
561 }
562
563 static void ImGui_ImplAllegro5_UpdateMouseCursor()
564 {
565 ImGuiIO& io = ImGui::GetIO();
566 if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
567 return;
568
569 ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
570 ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
571 if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
572 {
573 // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
574 al_set_mouse_cursor(bd->Display, bd->MouseCursorInvisible);
575 }
576 else
577 {
578 ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
579 switch (imgui_cursor)
580 {
581 case ImGuiMouseCursor_TextInput: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT; break;
582 case ImGuiMouseCursor_ResizeAll: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE; break;
583 case ImGuiMouseCursor_ResizeNS: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N; break;
584 case ImGuiMouseCursor_ResizeEW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E; break;
585 case ImGuiMouseCursor_ResizeNESW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE; break;
586 case ImGuiMouseCursor_ResizeNWSE: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW; break;
587 case ImGuiMouseCursor_NotAllowed: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_UNAVAILABLE; break;
588 }
589 al_set_system_mouse_cursor(bd->Display, cursor_id);
590 }
591 }
592
593 void ImGui_ImplAllegro5_NewFrame()
594 {
595 ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
596 IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplAllegro5_Init()?");
597
598 if (!bd->Texture)
599 ImGui_ImplAllegro5_CreateDeviceObjects();
600
601 ImGuiIO& io = ImGui::GetIO();
602
603 // Setup display size (every frame to accommodate for window resizing)
604 int w, h;
605 w = al_get_display_width(bd->Display);
606 h = al_get_display_height(bd->Display);
607 io.DisplaySize = ImVec2((float)w, (float)h);
608
609 // Setup time step
610 double current_time = al_get_time();
611 io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f);
612 bd->Time = current_time;
613
614 // Setup mouse cursor shape
615 ImGui_ImplAllegro5_UpdateMouseCursor();
616 }
617
618 //-----------------------------------------------------------------------------
619
620 #endif // #ifndef IMGUI_DISABLE