imgui_impl_a5.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 void ImGui_ImplA5_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
  18. {
  19. const float width = ImGui::GetIO().DisplaySize.x;
  20. const float height = ImGui::GetIO().DisplaySize.y;
  21. const float bw = al_get_bitmap_width(g_Surface);
  22. const float bh = al_get_bitmap_height(g_Surface);
  23. int op, src, dst;
  24. al_get_blender(&op, &src, &dst);
  25. al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
  26. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->Element))
  27. for (int n=0; n < cmd_lists_count; ++n)
  28. {
  29. const ImDrawList* cmd_list = cmd_lists[n];
  30. static ImVector<ALLEGRO_VERTEX> vertices;
  31. vertices.reserve(cmd_list->vtx_buffer.size());
  32. vertices.clear();
  33. for (int i = 0; i < cmd_list->vtx_buffer.size(); ++i)
  34. {
  35. ALLEGRO_VERTEX v;
  36. const ImDrawVert &dv = cmd_list->vtx_buffer[i];
  37. v.x = dv.pos.x;
  38. v.y = dv.pos.y;
  39. v.z = 0;
  40. v.u = dv.uv.x * bw;
  41. v.v = dv.uv.y * bh;
  42. unsigned char *c = (unsigned char*)&dv.col;
  43. v.color = al_map_rgba(c[0], c[1], c[2], c[3]);
  44. vertices.push_back(v);
  45. }
  46. int vtx_offset = 0;
  47. for (size_t cmd_i=0; cmd_i < cmd_list->commands.size(); ++cmd_i)
  48. {
  49. const ImDrawCmd *pcmd = &cmd_list->commands[cmd_i];
  50. if (pcmd->user_callback)
  51. {
  52. pcmd->user_callback(cmd_list, pcmd);
  53. }
  54. else
  55. {
  56. ALLEGRO_BITMAP *tex = (ALLEGRO_BITMAP*)pcmd->texture_id;
  57. al_set_clipping_rectangle(pcmd->clip_rect.x, pcmd->clip_rect.y, pcmd->clip_rect.z, pcmd->clip_rect.w);
  58. al_draw_prim(&vertices[0], NULL, tex, vtx_offset, vtx_offset+pcmd->vtx_count, ALLEGRO_PRIM_TRIANGLE_LIST);
  59. }
  60. vtx_offset += pcmd->vtx_count;
  61. }
  62. }
  63. #undef OFFSETOF
  64. // restore 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_Surface = 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. return true;
  105. }
  106. void ImGui_ImplA5_InvalidateDeviceObjects()
  107. {
  108. if (g_Surface)
  109. {
  110. al_destroy_bitmap(g_Surface);
  111. ImGui::GetIO().Fonts->TexID = NULL;
  112. g_Surface = NULL;
  113. }
  114. }
  115. bool ImGui_ImplA5_Init(ALLEGRO_DISPLAY* display)
  116. {
  117. g_Display = display;
  118. ImGuiIO& io = ImGui::GetIO();
  119. io.KeyMap[ImGuiKey_Tab] = ALLEGRO_KEY_TAB;
  120. io.KeyMap[ImGuiKey_LeftArrow] = ALLEGRO_KEY_LEFT;
  121. io.KeyMap[ImGuiKey_RightArrow] = ALLEGRO_KEY_RIGHT;
  122. io.KeyMap[ImGuiKey_UpArrow] = ALLEGRO_KEY_UP;
  123. io.KeyMap[ImGuiKey_DownArrow] = ALLEGRO_KEY_DOWN;
  124. io.KeyMap[ImGuiKey_Home] = ALLEGRO_KEY_HOME;
  125. io.KeyMap[ImGuiKey_End] = ALLEGRO_KEY_END;
  126. io.KeyMap[ImGuiKey_Delete] = ALLEGRO_KEY_DELETE;
  127. io.KeyMap[ImGuiKey_Backspace] = ALLEGRO_KEY_BACKSPACE;
  128. io.KeyMap[ImGuiKey_Enter] = ALLEGRO_KEY_ENTER;
  129. io.KeyMap[ImGuiKey_Escape] = ALLEGRO_KEY_ESCAPE;
  130. io.KeyMap[ImGuiKey_A] = ALLEGRO_KEY_A;
  131. io.KeyMap[ImGuiKey_C] = ALLEGRO_KEY_C;
  132. io.KeyMap[ImGuiKey_V] = ALLEGRO_KEY_V;
  133. io.KeyMap[ImGuiKey_X] = ALLEGRO_KEY_X;
  134. io.KeyMap[ImGuiKey_Y] = ALLEGRO_KEY_Y;
  135. io.KeyMap[ImGuiKey_Z] = ALLEGRO_KEY_Z;
  136. io.RenderDrawListsFn = ImGui_ImplA5_RenderDrawLists;
  137. #ifdef _WIN32
  138. io.ImeWindowHandle = al_get_win_window_handle(g_Display);
  139. #endif
  140. return true;
  141. }
  142. void ImGui_ImplA5_Shutdown()
  143. {
  144. ImGui_ImplA5_InvalidateDeviceObjects();
  145. ImGui::Shutdown();
  146. }
  147. bool ImGui_ImplA5_ProcessEvent(ALLEGRO_EVENT *ev)
  148. {
  149. ImGuiIO &io = ImGui::GetIO();
  150. switch (ev->type)
  151. {
  152. case ALLEGRO_EVENT_MOUSE_AXES:
  153. io.MouseWheel += ev->mouse.dz;
  154. return true;
  155. case ALLEGRO_EVENT_KEY_CHAR:
  156. if (ev->keyboard.display == g_Display)
  157. {
  158. io.KeysDown[ev->keyboard.keycode] = true;
  159. if (ev->keyboard.unichar > 0 && ev->keyboard.unichar < 0x10000)
  160. io.AddInputCharacter((unsigned short)ev->keyboard.unichar);
  161. }
  162. return true;
  163. case ALLEGRO_EVENT_KEY_UP:
  164. if (ev->keyboard.display == g_Display)
  165. io.KeysDown[ev->keyboard.keycode] = false;
  166. return true;
  167. }
  168. return false;
  169. }
  170. void ImGui_ImplA5_NewFrame()
  171. {
  172. if (!g_Surface)
  173. Imgui_ImplA5_CreateDeviceObjects();
  174. ImGuiIO &io = ImGui::GetIO();
  175. // Setup display size (every frame to accommodate for window resizing)
  176. int w, h;
  177. w = al_get_display_width(g_Display);
  178. h = al_get_display_height(g_Display);
  179. io.DisplaySize = ImVec2((float)w, (float)h);
  180. // Setup time step
  181. double current_time = al_get_time();
  182. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  183. g_Time = current_time;
  184. // Setup inputs
  185. ALLEGRO_KEYBOARD_STATE keys;
  186. al_get_keyboard_state(&keys);
  187. io.KeyCtrl = al_key_down(&keys, ALLEGRO_KEYMOD_CTRL);
  188. io.KeyShift = al_key_down(&keys, ALLEGRO_KEYMOD_SHIFT);
  189. io.KeyAlt = al_key_down(&keys, ALLEGRO_KEYMOD_ALT);
  190. ALLEGRO_MOUSE_STATE mouse;
  191. if (keys.display == g_Display)
  192. {
  193. al_get_mouse_state(&mouse);
  194. io.MousePos = ImVec2((float)mouse.x, (float)mouse.y);
  195. }
  196. else
  197. {
  198. io.MousePos = ImVec2(-1, -1);
  199. }
  200. al_get_mouse_state(&mouse);
  201. io.MouseDown[0] = mouse.buttons & (1 << 0);
  202. io.MouseDown[1] = mouse.buttons & (1 << 1);
  203. io.MouseDown[2] = mouse.buttons & (1 << 2);
  204. // Start the frame
  205. ImGui::NewFrame();
  206. }