1 // dear imgui: Renderer + Platform Binding 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 in imgui.cpp.
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 copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
13 // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
14 // https://github.com/ocornut/imgui, Original Allegro 5 code by @birthggd
15
16 // CHANGELOG
17 // (minor and older changes stripped away, please see git history for details)
18 // 2018-11-30: Misc: Setting up io.BackendPlatformName/io.BackendRendererName so they can be displayed in the About Window.
19 // 2018-06-13: Platform: Added clipboard support (from Allegro 5.1.12).
20 // 2018-06-13: Renderer: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
21 // 2018-06-13: Renderer: Backup/restore transform and clipping rectangle.
22 // 2018-06-11: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
23 // 2018-04-18: Misc: Renamed file from imgui_impl_a5.cpp to imgui_impl_allegro5.cpp.
24 // 2018-04-18: Misc: Added support for 32-bits vertex indices to avoid conversion at runtime. Added imconfig_allegro5.h to enforce 32-bit indices when included from imgui.h.
25 // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplAllegro5_RenderDrawData() in the .h file so you can call it yourself.
26 // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
27 // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
28
29 #include <stdint.h> // uint64_t
30 #include <cstring> // memcpy
31 #include "imgui.h"
32 #include "imgui_impl_allegro5.h"
33
34 // Allegro
35 #include <allegro5/allegro.h>
36 #include <allegro5/allegro_primitives.h>
37 #ifdef _WIN32
38 #include <allegro5/allegro_windows.h>
39 #endif
40 #define ALLEGRO_HAS_CLIPBOARD (ALLEGRO_VERSION_INT >= ((5 << 24) | (1 << 16) | (12 << 8))) // Clipboard only supported from Allegro 5.1.12
41
42 // Visual Studio warnings
43 #ifdef _MSC_VER
44 #pragma warning (disable: 4127) // condition expression is constant
45 #endif
46
47 // Data
48 static ALLEGRO_DISPLAY* g_Display = NULL;
49 static ALLEGRO_BITMAP* g_Texture = NULL;
50 static double g_Time = 0.0;
51 static ALLEGRO_MOUSE_CURSOR* g_MouseCursorInvisible = NULL;
52 static ALLEGRO_VERTEX_DECL* g_VertexDecl = NULL;
53 static char* g_ClipboardTextData = NULL;
54
55 struct ImDrawVertAllegro
56 {
57 ImVec2 pos;
58 ImVec2 uv;
59 ALLEGRO_COLOR col;
60 };
61
62 // Render function.
63 // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
ImGui_ImplAllegro5_RenderDrawData(ImDrawData * draw_data)64 void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data)
65 {
66 // Backup Allegro state that will be modified
67 ALLEGRO_TRANSFORM last_transform = *al_get_current_transform();
68 ALLEGRO_TRANSFORM last_projection_transform = *al_get_current_projection_transform();
69 int last_clip_x, last_clip_y, last_clip_w, last_clip_h;
70 al_get_clipping_rectangle(&last_clip_x, &last_clip_y, &last_clip_w, &last_clip_h);
71 int last_blender_op, last_blender_src, last_blender_dst;
72 al_get_blender(&last_blender_op, &last_blender_src, &last_blender_dst);
73
74 // Setup render state
75 al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
76
77 // Setup orthographic projection matrix
78 // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
79 {
80 float L = draw_data->DisplayPos.x;
81 float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
82 float T = draw_data->DisplayPos.y;
83 float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
84 ALLEGRO_TRANSFORM transform;
85 al_identity_transform(&transform);
86 al_use_transform(&transform);
87 al_orthographic_transform(&transform, L, T, 1.0f, R, B, -1.0f);
88 al_use_projection_transform(&transform);
89 }
90
91 for (int n = 0; n < draw_data->CmdListsCount; n++)
92 {
93 const ImDrawList* cmd_list = draw_data->CmdLists[n];
94
95 // Allegro's implementation of al_draw_indexed_prim() for DX9 is completely broken. Unindex our buffers ourselves.
96 // FIXME-OPT: Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 float as well..
97 static ImVector<ImDrawVertAllegro> vertices;
98 vertices.resize(cmd_list->IdxBuffer.Size);
99 for (int i = 0; i < cmd_list->IdxBuffer.Size; i++)
100 {
101 const ImDrawVert* src_v = &cmd_list->VtxBuffer[cmd_list->IdxBuffer[i]];
102 ImDrawVertAllegro* dst_v = &vertices[i];
103 dst_v->pos = src_v->pos;
104 dst_v->uv = src_v->uv;
105 unsigned char* c = (unsigned char*)&src_v->col;
106 dst_v->col = al_map_rgba(c[0], c[1], c[2], c[3]);
107 }
108
109 const int* indices = NULL;
110 if (sizeof(ImDrawIdx) == 2)
111 {
112 // 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.
113 // Otherwise, we convert them from 16-bit to 32-bit at runtime here, which works perfectly but is a little wasteful.
114 static ImVector<int> indices_converted;
115 indices_converted.resize(cmd_list->IdxBuffer.Size);
116 for (int i = 0; i < cmd_list->IdxBuffer.Size; ++i)
117 indices_converted[i] = (int)cmd_list->IdxBuffer.Data[i];
118 indices = indices_converted.Data;
119 }
120 else if (sizeof(ImDrawIdx) == 4)
121 {
122 indices = (const int*)cmd_list->IdxBuffer.Data;
123 }
124
125 int idx_offset = 0;
126 ImVec2 pos = draw_data->DisplayPos;
127 for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
128 {
129 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
130 if (pcmd->UserCallback)
131 {
132 pcmd->UserCallback(cmd_list, pcmd);
133 }
134 else
135 {
136 ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->TextureId;
137 al_set_clipping_rectangle(pcmd->ClipRect.x - pos.x, pcmd->ClipRect.y - pos.y, pcmd->ClipRect.z - pcmd->ClipRect.x, pcmd->ClipRect.w - pcmd->ClipRect.y);
138 al_draw_prim(&vertices[0], g_VertexDecl, texture, idx_offset, idx_offset + pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST);
139 }
140 idx_offset += pcmd->ElemCount;
141 }
142 }
143
144 // Restore modified Allegro state
145 al_set_blender(last_blender_op, last_blender_src, last_blender_dst);
146 al_set_clipping_rectangle(last_clip_x, last_clip_y, last_clip_w, last_clip_h);
147 al_use_transform(&last_transform);
148 al_use_projection_transform(&last_projection_transform);
149 }
150
ImGui_ImplAllegro5_CreateDeviceObjects()151 bool ImGui_ImplAllegro5_CreateDeviceObjects()
152 {
153 // Build texture atlas
154 ImGuiIO &io = ImGui::GetIO();
155 unsigned char *pixels;
156 int width, height;
157 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
158
159 // Create texture
160 int flags = al_get_new_bitmap_flags();
161 int fmt = al_get_new_bitmap_format();
162 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP|ALLEGRO_MIN_LINEAR|ALLEGRO_MAG_LINEAR);
163 al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE);
164 ALLEGRO_BITMAP* img = al_create_bitmap(width, height);
165 al_set_new_bitmap_flags(flags);
166 al_set_new_bitmap_format(fmt);
167 if (!img)
168 return false;
169
170 ALLEGRO_LOCKED_REGION *locked_img = al_lock_bitmap(img, al_get_bitmap_format(img), ALLEGRO_LOCK_WRITEONLY);
171 if (!locked_img)
172 {
173 al_destroy_bitmap(img);
174 return false;
175 }
176 memcpy(locked_img->data, pixels, sizeof(int)*width*height);
177 al_unlock_bitmap(img);
178
179 // Convert software texture to hardware texture.
180 ALLEGRO_BITMAP* cloned_img = al_clone_bitmap(img);
181 al_destroy_bitmap(img);
182 if (!cloned_img)
183 return false;
184
185 // Store our identifier
186 io.Fonts->TexID = (void*)cloned_img;
187 g_Texture = cloned_img;
188
189 // Create an invisible mouse cursor
190 // Because al_hide_mouse_cursor() seems to mess up with the actual inputs..
191 ALLEGRO_BITMAP* mouse_cursor = al_create_bitmap(8,8);
192 g_MouseCursorInvisible = al_create_mouse_cursor(mouse_cursor, 0, 0);
193 al_destroy_bitmap(mouse_cursor);
194
195 return true;
196 }
197
ImGui_ImplAllegro5_InvalidateDeviceObjects()198 void ImGui_ImplAllegro5_InvalidateDeviceObjects()
199 {
200 if (g_Texture)
201 {
202 al_destroy_bitmap(g_Texture);
203 ImGui::GetIO().Fonts->TexID = NULL;
204 g_Texture = NULL;
205 }
206 if (g_MouseCursorInvisible)
207 {
208 al_destroy_mouse_cursor(g_MouseCursorInvisible);
209 g_MouseCursorInvisible = NULL;
210 }
211 }
212
213 #if ALLEGRO_HAS_CLIPBOARD
ImGui_ImplAllegro5_GetClipboardText(void *)214 static const char* ImGui_ImplAllegro5_GetClipboardText(void*)
215 {
216 if (g_ClipboardTextData)
217 al_free(g_ClipboardTextData);
218 g_ClipboardTextData = al_get_clipboard_text(g_Display);
219 return g_ClipboardTextData;
220 }
221
ImGui_ImplAllegro5_SetClipboardText(void *,const char * text)222 static void ImGui_ImplAllegro5_SetClipboardText(void*, const char* text)
223 {
224 al_set_clipboard_text(g_Display, text);
225 }
226 #endif
227
ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY * display)228 bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display)
229 {
230 g_Display = display;
231
232 // Setup back-end capabilities flags
233 ImGuiIO& io = ImGui::GetIO();
234 io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
235 io.BackendPlatformName = io.BackendRendererName = "imgui_impl_allegro5";
236
237 // Create custom vertex declaration.
238 // Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 floats.
239 // 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.
240 ALLEGRO_VERTEX_ELEMENT elems[] =
241 {
242 { ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, IM_OFFSETOF(ImDrawVertAllegro, pos) },
243 { ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, IM_OFFSETOF(ImDrawVertAllegro, uv) },
244 { ALLEGRO_PRIM_COLOR_ATTR, 0, IM_OFFSETOF(ImDrawVertAllegro, col) },
245 { 0, 0, 0 }
246 };
247 g_VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro));
248
249 io.KeyMap[ImGuiKey_Tab] = ALLEGRO_KEY_TAB;
250 io.KeyMap[ImGuiKey_LeftArrow] = ALLEGRO_KEY_LEFT;
251 io.KeyMap[ImGuiKey_RightArrow] = ALLEGRO_KEY_RIGHT;
252 io.KeyMap[ImGuiKey_UpArrow] = ALLEGRO_KEY_UP;
253 io.KeyMap[ImGuiKey_DownArrow] = ALLEGRO_KEY_DOWN;
254 io.KeyMap[ImGuiKey_PageUp] = ALLEGRO_KEY_PGUP;
255 io.KeyMap[ImGuiKey_PageDown] = ALLEGRO_KEY_PGDN;
256 io.KeyMap[ImGuiKey_Home] = ALLEGRO_KEY_HOME;
257 io.KeyMap[ImGuiKey_End] = ALLEGRO_KEY_END;
258 io.KeyMap[ImGuiKey_Insert] = ALLEGRO_KEY_INSERT;
259 io.KeyMap[ImGuiKey_Delete] = ALLEGRO_KEY_DELETE;
260 io.KeyMap[ImGuiKey_Backspace] = ALLEGRO_KEY_BACKSPACE;
261 io.KeyMap[ImGuiKey_Space] = ALLEGRO_KEY_SPACE;
262 io.KeyMap[ImGuiKey_Enter] = ALLEGRO_KEY_ENTER;
263 io.KeyMap[ImGuiKey_Escape] = ALLEGRO_KEY_ESCAPE;
264 io.KeyMap[ImGuiKey_A] = ALLEGRO_KEY_A;
265 io.KeyMap[ImGuiKey_C] = ALLEGRO_KEY_C;
266 io.KeyMap[ImGuiKey_V] = ALLEGRO_KEY_V;
267 io.KeyMap[ImGuiKey_X] = ALLEGRO_KEY_X;
268 io.KeyMap[ImGuiKey_Y] = ALLEGRO_KEY_Y;
269 io.KeyMap[ImGuiKey_Z] = ALLEGRO_KEY_Z;
270
271 #if ALLEGRO_HAS_CLIPBOARD
272 io.SetClipboardTextFn = ImGui_ImplAllegro5_SetClipboardText;
273 io.GetClipboardTextFn = ImGui_ImplAllegro5_GetClipboardText;
274 io.ClipboardUserData = NULL;
275 #endif
276
277 return true;
278 }
279
ImGui_ImplAllegro5_Shutdown()280 void ImGui_ImplAllegro5_Shutdown()
281 {
282 ImGui_ImplAllegro5_InvalidateDeviceObjects();
283
284 g_Display = NULL;
285 g_Time = 0.0;
286
287 if (g_VertexDecl)
288 al_destroy_vertex_decl(g_VertexDecl);
289 g_VertexDecl = NULL;
290
291 if (g_ClipboardTextData)
292 al_free(g_ClipboardTextData);
293 g_ClipboardTextData = NULL;
294 }
295
296 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
297 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
298 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
299 // 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)300 bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT *ev)
301 {
302 ImGuiIO& io = ImGui::GetIO();
303
304 switch (ev->type)
305 {
306 case ALLEGRO_EVENT_MOUSE_AXES:
307 io.MouseWheel += ev->mouse.dz;
308 io.MouseWheelH += ev->mouse.dw;
309 return true;
310 case ALLEGRO_EVENT_KEY_CHAR:
311 if (ev->keyboard.display == g_Display)
312 if (ev->keyboard.unichar > 0 && ev->keyboard.unichar < 0x10000)
313 io.AddInputCharacter((unsigned short)ev->keyboard.unichar);
314 return true;
315 case ALLEGRO_EVENT_KEY_DOWN:
316 case ALLEGRO_EVENT_KEY_UP:
317 if (ev->keyboard.display == g_Display)
318 io.KeysDown[ev->keyboard.keycode] = (ev->type == ALLEGRO_EVENT_KEY_DOWN);
319 return true;
320 }
321 return false;
322 }
323
ImGui_ImplAllegro5_UpdateMouseCursor()324 static void ImGui_ImplAllegro5_UpdateMouseCursor()
325 {
326 ImGuiIO& io = ImGui::GetIO();
327 if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
328 return;
329
330 ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
331 if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
332 {
333 // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
334 al_set_mouse_cursor(g_Display, g_MouseCursorInvisible);
335 }
336 else
337 {
338 ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
339 switch (imgui_cursor)
340 {
341 case ImGuiMouseCursor_TextInput: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT; break;
342 case ImGuiMouseCursor_ResizeAll: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE; break;
343 case ImGuiMouseCursor_ResizeNS: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N; break;
344 case ImGuiMouseCursor_ResizeEW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E; break;
345 case ImGuiMouseCursor_ResizeNESW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE; break;
346 case ImGuiMouseCursor_ResizeNWSE: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW; break;
347 }
348 al_set_system_mouse_cursor(g_Display, cursor_id);
349 }
350 }
351
ImGui_ImplAllegro5_NewFrame()352 void ImGui_ImplAllegro5_NewFrame()
353 {
354 if (!g_Texture)
355 ImGui_ImplAllegro5_CreateDeviceObjects();
356
357 ImGuiIO &io = ImGui::GetIO();
358
359 // Setup display size (every frame to accommodate for window resizing)
360 int w, h;
361 w = al_get_display_width(g_Display);
362 h = al_get_display_height(g_Display);
363 io.DisplaySize = ImVec2((float)w, (float)h);
364
365 // Setup time step
366 double current_time = al_get_time();
367 io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
368 g_Time = current_time;
369
370 // Setup inputs
371 ALLEGRO_KEYBOARD_STATE keys;
372 al_get_keyboard_state(&keys);
373 io.KeyCtrl = al_key_down(&keys, ALLEGRO_KEY_LCTRL) || al_key_down(&keys, ALLEGRO_KEY_RCTRL);
374 io.KeyShift = al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT);
375 io.KeyAlt = al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR);
376 io.KeySuper = al_key_down(&keys, ALLEGRO_KEY_LWIN) || al_key_down(&keys, ALLEGRO_KEY_RWIN);
377
378 ALLEGRO_MOUSE_STATE mouse;
379 if (keys.display == g_Display)
380 {
381 al_get_mouse_state(&mouse);
382 io.MousePos = ImVec2((float)mouse.x, (float)mouse.y);
383 }
384 else
385 {
386 io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
387 }
388
389 al_get_mouse_state(&mouse);
390 io.MouseDown[0] = mouse.buttons & (1 << 0);
391 io.MouseDown[1] = mouse.buttons & (1 << 1);
392 io.MouseDown[2] = mouse.buttons & (1 << 2);
393
394 ImGui_ImplAllegro5_UpdateMouseCursor();
395 }
396