imgui_impl_a5.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // ImGui Allegro 5 bindings
  2. // https://github.com/ocornut/imgui
  3. // by @birthggd
  4. #include <stdint.h> // uint64_t
  5. #include <cstring> // memcpy
  6. #include <imgui.h>
  7. #include "imgui_impl_a5.h"
  8. #include <allegro5/allegro.h>
  9. #include <allegro5/allegro_primitives.h>
  10. #ifdef _WIN32
  11. #include <allegro5/allegro_windows.h>
  12. #endif
  13. // Data
  14. static ALLEGRO_DISPLAY* g_Display = NULL;
  15. static ALLEGRO_BITMAP* g_Texture = NULL;
  16. static double g_Time = 0.0;
  17. static ALLEGRO_MOUSE_CURSOR* g_MouseCursorInvisible = NULL;
  18. static ALLEGRO_VERTEX_DECL* g_VertexDecl = NULL;
  19. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
  20. struct ImDrawVertAllegro
  21. {
  22. ImVec2 pos;
  23. ImVec2 uv;
  24. ALLEGRO_COLOR col;
  25. };
  26. static void ImGui_ImplA5_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
  27. {
  28. int op, src, dst;
  29. al_get_blender(&op, &src, &dst);
  30. al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
  31. for (int n = 0; n < cmd_lists_count; n++)
  32. {
  33. const ImDrawList* cmd_list = cmd_lists[n];
  34. // FIXME-OPT: Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 floats
  35. static ImVector<ImDrawVertAllegro> vertices;
  36. vertices.resize(cmd_list->vtx_buffer.size());
  37. for (int i = 0; i < cmd_list->vtx_buffer.size(); ++i)
  38. {
  39. const ImDrawVert &dv = cmd_list->vtx_buffer[i];
  40. ImDrawVertAllegro v;
  41. v.pos = dv.pos;
  42. v.uv = dv.uv;
  43. unsigned char *c = (unsigned char*)&dv.col;
  44. v.col = al_map_rgba(c[0], c[1], c[2], c[3]);
  45. vertices[i] = v;
  46. }
  47. int vtx_offset = 0;
  48. for (int cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
  49. {
  50. const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
  51. if (pcmd->user_callback)
  52. {
  53. pcmd->user_callback(cmd_list, pcmd);
  54. }
  55. else
  56. {
  57. ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->texture_id;
  58. al_set_clipping_rectangle(pcmd->clip_rect.x, pcmd->clip_rect.y, pcmd->clip_rect.z-pcmd->clip_rect.x, pcmd->clip_rect.w-pcmd->clip_rect.y);
  59. al_draw_prim(&vertices[0], g_VertexDecl, texture, vtx_offset, vtx_offset+pcmd->vtx_count, ALLEGRO_PRIM_TRIANGLE_LIST);
  60. }
  61. vtx_offset += pcmd->vtx_count;
  62. }
  63. }
  64. // Restore modified state
  65. al_set_blender(op, src, dst);
  66. al_set_clipping_rectangle(0, 0, al_get_display_width(g_Display), al_get_display_height(g_Display));
  67. }
  68. bool Imgui_ImplA5_CreateDeviceObjects()
  69. {
  70. ImGuiIO &io = ImGui::GetIO();
  71. // Build texture
  72. unsigned char *pixels;
  73. int width, height;
  74. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  75. // Create texture
  76. int flags = al_get_new_bitmap_flags();
  77. int fmt = al_get_new_bitmap_format();
  78. al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP|ALLEGRO_MIN_LINEAR|ALLEGRO_MAG_LINEAR);
  79. al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE);
  80. ALLEGRO_BITMAP* img = al_create_bitmap(width, height);
  81. al_set_new_bitmap_flags(flags);
  82. al_set_new_bitmap_format(fmt);
  83. if (!img)
  84. return false;
  85. ALLEGRO_LOCKED_REGION *locked_img = al_lock_bitmap(img, al_get_bitmap_format(img), ALLEGRO_LOCK_WRITEONLY);
  86. if (!locked_img)
  87. {
  88. al_destroy_bitmap(img);
  89. return false;
  90. }
  91. memcpy(locked_img->data, pixels, sizeof(int)*width*height);
  92. al_unlock_bitmap(img);
  93. // Convert software texture to hardware texture.
  94. ALLEGRO_BITMAP* cloned_img = al_clone_bitmap(img);
  95. al_destroy_bitmap(img);
  96. if (!cloned_img)
  97. return false;
  98. // Store our identifier
  99. io.Fonts->TexID = (void*)cloned_img;
  100. g_Texture = cloned_img;
  101. // Cleanup (don't clear the input data if you want to append new fonts later)
  102. io.Fonts->ClearInputData();
  103. io.Fonts->ClearTexData();
  104. // Create an invisible mouse cursor
  105. // Because al_hide_mouse_cursor() seems to mess up with the actual inputs..
  106. ALLEGRO_BITMAP* mouse_cursor = al_create_bitmap(8,8);
  107. g_MouseCursorInvisible = al_create_mouse_cursor(mouse_cursor, 0, 0);
  108. al_destroy_bitmap(mouse_cursor);
  109. return true;
  110. }
  111. void ImGui_ImplA5_InvalidateDeviceObjects()
  112. {
  113. if (g_Texture)
  114. {
  115. al_destroy_bitmap(g_Texture);
  116. ImGui::GetIO().Fonts->TexID = NULL;
  117. g_Texture = NULL;
  118. }
  119. if (g_MouseCursorInvisible)
  120. {
  121. al_destroy_mouse_cursor(g_MouseCursorInvisible);
  122. g_MouseCursorInvisible = NULL;
  123. }
  124. }
  125. bool ImGui_ImplA5_Init(ALLEGRO_DISPLAY* display)
  126. {
  127. g_Display = display;
  128. // Create custom vertex declaration.
  129. // Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 floats.
  130. // 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.
  131. ALLEGRO_VERTEX_ELEMENT elems[] =
  132. {
  133. { ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, OFFSETOF(ImDrawVertAllegro, pos) },
  134. { ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, OFFSETOF(ImDrawVertAllegro, uv) },
  135. { ALLEGRO_PRIM_COLOR_ATTR, 0, OFFSETOF(ImDrawVertAllegro, col) },
  136. { 0, 0, 0 }
  137. };
  138. g_VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro));
  139. ImGuiIO& io = ImGui::GetIO();
  140. io.KeyMap[ImGuiKey_Tab] = ALLEGRO_KEY_TAB;
  141. io.KeyMap[ImGuiKey_LeftArrow] = ALLEGRO_KEY_LEFT;
  142. io.KeyMap[ImGuiKey_RightArrow] = ALLEGRO_KEY_RIGHT;
  143. io.KeyMap[ImGuiKey_UpArrow] = ALLEGRO_KEY_UP;
  144. io.KeyMap[ImGuiKey_DownArrow] = ALLEGRO_KEY_DOWN;
  145. io.KeyMap[ImGuiKey_PageUp] = ALLEGRO_KEY_PGUP;
  146. io.KeyMap[ImGuiKey_PageDown] = ALLEGRO_KEY_PGDN;
  147. io.KeyMap[ImGuiKey_Home] = ALLEGRO_KEY_HOME;
  148. io.KeyMap[ImGuiKey_End] = ALLEGRO_KEY_END;
  149. io.KeyMap[ImGuiKey_Delete] = ALLEGRO_KEY_DELETE;
  150. io.KeyMap[ImGuiKey_Backspace] = ALLEGRO_KEY_BACKSPACE;
  151. io.KeyMap[ImGuiKey_Enter] = ALLEGRO_KEY_ENTER;
  152. io.KeyMap[ImGuiKey_Escape] = ALLEGRO_KEY_ESCAPE;
  153. io.KeyMap[ImGuiKey_A] = ALLEGRO_KEY_A;
  154. io.KeyMap[ImGuiKey_C] = ALLEGRO_KEY_C;
  155. io.KeyMap[ImGuiKey_V] = ALLEGRO_KEY_V;
  156. io.KeyMap[ImGuiKey_X] = ALLEGRO_KEY_X;
  157. io.KeyMap[ImGuiKey_Y] = ALLEGRO_KEY_Y;
  158. io.KeyMap[ImGuiKey_Z] = ALLEGRO_KEY_Z;
  159. io.RenderDrawListsFn = ImGui_ImplA5_RenderDrawLists;
  160. #ifdef _WIN32
  161. io.ImeWindowHandle = al_get_win_window_handle(g_Display);
  162. #endif
  163. return true;
  164. }
  165. void ImGui_ImplA5_Shutdown()
  166. {
  167. ImGui_ImplA5_InvalidateDeviceObjects();
  168. ImGui::Shutdown();
  169. }
  170. bool ImGui_ImplA5_ProcessEvent(ALLEGRO_EVENT *ev)
  171. {
  172. ImGuiIO &io = ImGui::GetIO();
  173. switch (ev->type)
  174. {
  175. case ALLEGRO_EVENT_MOUSE_AXES:
  176. io.MouseWheel += ev->mouse.dz;
  177. return true;
  178. case ALLEGRO_EVENT_KEY_CHAR:
  179. if (ev->keyboard.display == g_Display)
  180. if (ev->keyboard.unichar > 0 && ev->keyboard.unichar < 0x10000)
  181. io.AddInputCharacter((unsigned short)ev->keyboard.unichar);
  182. return true;
  183. case ALLEGRO_EVENT_KEY_DOWN:
  184. case ALLEGRO_EVENT_KEY_UP:
  185. if (ev->keyboard.display == g_Display)
  186. io.KeysDown[ev->keyboard.keycode] = (ev->type == ALLEGRO_EVENT_KEY_DOWN);
  187. return true;
  188. }
  189. return false;
  190. }
  191. void ImGui_ImplA5_NewFrame()
  192. {
  193. if (!g_Texture)
  194. Imgui_ImplA5_CreateDeviceObjects();
  195. ImGuiIO &io = ImGui::GetIO();
  196. // Setup display size (every frame to accommodate for window resizing)
  197. int w, h;
  198. w = al_get_display_width(g_Display);
  199. h = al_get_display_height(g_Display);
  200. io.DisplaySize = ImVec2((float)w, (float)h);
  201. // Setup time step
  202. double current_time = al_get_time();
  203. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  204. g_Time = current_time;
  205. // Setup inputs
  206. ALLEGRO_KEYBOARD_STATE keys;
  207. al_get_keyboard_state(&keys);
  208. io.KeyCtrl = al_key_down(&keys, ALLEGRO_KEY_LCTRL) || al_key_down(&keys, ALLEGRO_KEY_RCTRL);
  209. io.KeyShift = al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT);
  210. io.KeyAlt = al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR);
  211. ALLEGRO_MOUSE_STATE mouse;
  212. if (keys.display == g_Display)
  213. {
  214. al_get_mouse_state(&mouse);
  215. io.MousePos = ImVec2((float)mouse.x, (float)mouse.y);
  216. }
  217. else
  218. {
  219. io.MousePos = ImVec2(-1, -1);
  220. }
  221. al_get_mouse_state(&mouse);
  222. io.MouseDown[0] = mouse.buttons & (1 << 0);
  223. io.MouseDown[1] = mouse.buttons & (1 << 1);
  224. io.MouseDown[2] = mouse.buttons & (1 << 2);
  225. // Hide OS mouse cursor if ImGui is drawing it
  226. if (io.MouseDrawCursor)
  227. {
  228. al_set_mouse_cursor(g_Display, g_MouseCursorInvisible);
  229. }
  230. else
  231. {
  232. ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
  233. switch (ImGui::GetMouseCursor())
  234. {
  235. case ImGuiMouseCursor_TextInput: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT; break;
  236. case ImGuiMouseCursor_Move: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE; break;
  237. case ImGuiMouseCursor_ResizeNS: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N; break;
  238. case ImGuiMouseCursor_ResizeEW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E; break;
  239. case ImGuiMouseCursor_ResizeNESW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE; break;
  240. case ImGuiMouseCursor_ResizeNWSE: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW; break;
  241. }
  242. al_set_system_mouse_cursor(g_Display, cursor_id);
  243. }
  244. // Start the frame
  245. ImGui::NewFrame();
  246. }