imgui_impl_a5.cpp 8.9 KB

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