imgui_impl_sdl.cpp 9.1 KB

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