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