imgui_impl_sdl.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // ImGui SDL2 binding with OpenGL
  2. // https://github.com/ocornut/imgui
  3. #include <SDL.h>
  4. #include <SDL_syswm.h>
  5. #include <SDL_opengl.h>
  6. #include <imgui.h>
  7. #include "imgui_impl_sdl.h"
  8. // Data
  9. static double g_Time = 0.0f;
  10. static bool g_MousePressed[3] = { false, false, false };
  11. static float g_MouseWheel = 0.0f;
  12. static GLuint g_FontTexture = 0;
  13. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  14. // If text or lines are blurry when integrating ImGui in your engine:
  15. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  16. static void ImGui_ImplSdl_RenderDrawLists(ImDrawData* draw_data)
  17. {
  18. // We are using the OpenGL fixed pipeline to make the example code simpler to read!
  19. // A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer.
  20. // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
  21. glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
  22. glEnable(GL_BLEND);
  23. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  24. glDisable(GL_CULL_FACE);
  25. glDisable(GL_DEPTH_TEST);
  26. glEnable(GL_SCISSOR_TEST);
  27. glEnableClientState(GL_VERTEX_ARRAY);
  28. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  29. glEnableClientState(GL_COLOR_ARRAY);
  30. glEnable(GL_TEXTURE_2D);
  31. //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context
  32. // Setup orthographic projection matrix
  33. const float width = ImGui::GetIO().DisplaySize.x;
  34. const float height = ImGui::GetIO().DisplaySize.y;
  35. glMatrixMode(GL_PROJECTION);
  36. glPushMatrix();
  37. glLoadIdentity();
  38. glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f);
  39. glMatrixMode(GL_MODELVIEW);
  40. glPushMatrix();
  41. glLoadIdentity();
  42. // Render command lists
  43. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
  44. for (int n = 0; n < draw_data->CmdListsCount; n++)
  45. {
  46. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  47. const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front();
  48. const ImDrawIdx* idx_buffer = &cmd_list->IdxBuffer.front();
  49. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
  50. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
  51. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
  52. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
  53. {
  54. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  55. if (pcmd->UserCallback)
  56. {
  57. pcmd->UserCallback(cmd_list, pcmd);
  58. }
  59. else
  60. {
  61. glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
  62. glScissor((int)pcmd->ClipRect.x, (int)(height - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
  63. glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, GL_UNSIGNED_SHORT, idx_buffer);
  64. }
  65. idx_buffer += pcmd->ElemCount;
  66. }
  67. }
  68. #undef OFFSETOF
  69. // Restore modified state
  70. glDisableClientState(GL_COLOR_ARRAY);
  71. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  72. glDisableClientState(GL_VERTEX_ARRAY);
  73. glBindTexture(GL_TEXTURE_2D, 0);
  74. glMatrixMode(GL_MODELVIEW);
  75. glPopMatrix();
  76. glMatrixMode(GL_PROJECTION);
  77. glPopMatrix();
  78. glPopAttrib();
  79. }
  80. static const char* ImGui_ImplSdl_GetClipboardText()
  81. {
  82. return SDL_GetClipboardText();
  83. }
  84. static void ImGui_ImplSdl_SetClipboardText(const char* text)
  85. {
  86. SDL_SetClipboardText(text);
  87. }
  88. bool ImGui_ImplSdl_ProcessEvent(SDL_Event* event)
  89. {
  90. ImGuiIO& io = ImGui::GetIO();
  91. switch (event->type)
  92. {
  93. case SDL_MOUSEWHEEL:
  94. {
  95. if (event->wheel.y > 0)
  96. g_MouseWheel = 1;
  97. if (event->wheel.y < 0)
  98. g_MouseWheel = -1;
  99. return true;
  100. }
  101. case SDL_MOUSEBUTTONDOWN:
  102. {
  103. if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
  104. if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
  105. if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
  106. return true;
  107. }
  108. case SDL_TEXTINPUT:
  109. {
  110. ImGuiIO& io = ImGui::GetIO();
  111. io.AddInputCharactersUTF8(event->text.text);
  112. return true;
  113. }
  114. case SDL_KEYDOWN:
  115. case SDL_KEYUP:
  116. {
  117. int key = event->key.keysym.sym & ~SDLK_SCANCODE_MASK;
  118. io.KeysDown[key] = (event->type == SDL_KEYDOWN);
  119. io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
  120. io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
  121. io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. bool ImGui_ImplSdl_CreateDeviceObjects()
  128. {
  129. ImGuiIO& io = ImGui::GetIO();
  130. // Build texture
  131. unsigned char* pixels;
  132. int width, height;
  133. io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
  134. // Create texture
  135. glGenTextures(1, &g_FontTexture);
  136. glBindTexture(GL_TEXTURE_2D, g_FontTexture);
  137. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  138. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  139. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
  140. // Store our identifier
  141. io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
  142. // Cleanup (don't clear the input data if you want to append new fonts later)
  143. io.Fonts->ClearInputData();
  144. io.Fonts->ClearTexData();
  145. return true;
  146. }
  147. void ImGui_ImplSdl_InvalidateDeviceObjects()
  148. {
  149. if (g_FontTexture)
  150. {
  151. glDeleteTextures(1, &g_FontTexture);
  152. ImGui::GetIO().Fonts->TexID = 0;
  153. g_FontTexture = 0;
  154. }
  155. }
  156. bool ImGui_ImplSdl_Init(SDL_Window *window)
  157. {
  158. ImGuiIO& io = ImGui::GetIO();
  159. io.KeyMap[ImGuiKey_Tab] = SDLK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  160. io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
  161. io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
  162. io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
  163. io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
  164. io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
  165. io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
  166. io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
  167. io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
  168. io.KeyMap[ImGuiKey_Delete] = SDLK_DELETE;
  169. io.KeyMap[ImGuiKey_Backspace] = SDLK_BACKSPACE;
  170. io.KeyMap[ImGuiKey_Enter] = SDLK_RETURN;
  171. io.KeyMap[ImGuiKey_Escape] = SDLK_ESCAPE;
  172. io.KeyMap[ImGuiKey_A] = SDLK_a;
  173. io.KeyMap[ImGuiKey_C] = SDLK_c;
  174. io.KeyMap[ImGuiKey_V] = SDLK_v;
  175. io.KeyMap[ImGuiKey_X] = SDLK_x;
  176. io.KeyMap[ImGuiKey_Y] = SDLK_y;
  177. io.KeyMap[ImGuiKey_Z] = SDLK_z;
  178. io.RenderDrawListsFn = ImGui_ImplSdl_RenderDrawLists;
  179. io.SetClipboardTextFn = ImGui_ImplSdl_SetClipboardText;
  180. io.GetClipboardTextFn = ImGui_ImplSdl_GetClipboardText;
  181. #ifdef _WIN32
  182. SDL_SysWMinfo wmInfo;
  183. SDL_VERSION(&wmInfo.version);
  184. SDL_GetWindowWMInfo(window, &wmInfo);
  185. io.ImeWindowHandle = wmInfo.info.win.window;
  186. #endif
  187. return true;
  188. }
  189. void ImGui_ImplSdl_Shutdown()
  190. {
  191. ImGui_ImplSdl_InvalidateDeviceObjects();
  192. ImGui::Shutdown();
  193. }
  194. void ImGui_ImplSdl_NewFrame(SDL_Window *window)
  195. {
  196. if (!g_FontTexture)
  197. ImGui_ImplSdl_CreateDeviceObjects();
  198. ImGuiIO& io = ImGui::GetIO();
  199. // Setup display size (every frame to accommodate for window resizing)
  200. int w, h;
  201. SDL_GetWindowSize(window, &w, &h);
  202. io.DisplaySize = ImVec2((float)w, (float)h);
  203. // Setup time step
  204. Uint32 time = SDL_GetTicks();
  205. double current_time = time / 1000.0;
  206. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  207. g_Time = current_time;
  208. // Setup inputs
  209. // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
  210. int mx, my;
  211. Uint32 mouseMask = SDL_GetMouseState(&mx, &my);
  212. if (SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_FOCUS)
  213. io.MousePos = ImVec2((float)mx, (float)my); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
  214. else
  215. io.MousePos = ImVec2(-1,-1);
  216. io.MouseDown[0] = g_MousePressed[0] || (mouseMask & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
  217. io.MouseDown[1] = g_MousePressed[1] || (mouseMask & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
  218. io.MouseDown[2] = g_MousePressed[2] || (mouseMask & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
  219. g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
  220. io.MouseWheel = g_MouseWheel;
  221. g_MouseWheel = 0.0f;
  222. // Hide OS mouse cursor if ImGui is drawing it
  223. SDL_ShowCursor(io.MouseDrawCursor ? 0 : 1);
  224. // Start the frame
  225. ImGui::NewFrame();
  226. }