imgui_impl_sdl.cpp 8.7 KB

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