imgui_impl_sdl.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // dear imgui: Platform Backend for SDL2
  2. // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
  3. // (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
  4. // (Requires: SDL 2.0. Prefer SDL 2.0.4+ for full feature support.)
  5. // Implemented features:
  6. // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
  7. // [X] Platform: Clipboard support.
  8. // [X] Platform: Keyboard arrays indexed using SDL_SCANCODE_* codes, e.g. ImGui::IsKeyPressed(SDL_SCANCODE_SPACE).
  9. // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  10. // Missing features:
  11. // [ ] Platform: SDL2 handling of IME under Windows appears to be broken and it explicitly disable the regular Windows IME. You can restore Windows IME by compiling SDL with SDL_DISABLE_WINDOWS_IME.
  12. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  13. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
  14. // https://github.com/ocornut/imgui
  15. // CHANGELOG
  16. // (minor and older changes stripped away, please see git history for details)
  17. // 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends.
  18. // 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2).
  19. // 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state).
  20. // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
  21. // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
  22. // 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
  23. // 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized.
  24. // 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls.
  25. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  26. // 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'.
  27. // 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
  28. // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
  29. // 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples.
  30. // 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter.
  31. // 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText).
  32. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  33. // 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
  34. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  35. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  36. // 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
  37. // 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
  38. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  39. // 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS.
  40. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  41. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  42. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  43. #include "imgui.h"
  44. #include "imgui_impl_sdl.h"
  45. // SDL
  46. #include <SDL.h>
  47. #include <SDL_syswm.h>
  48. #if defined(__APPLE__)
  49. #include "TargetConditionals.h"
  50. #endif
  51. #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE SDL_VERSION_ATLEAST(2,0,4)
  52. #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
  53. // Data
  54. static SDL_Window* g_Window = NULL;
  55. static Uint64 g_Time = 0;
  56. static bool g_MousePressed[3] = { false, false, false };
  57. static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {};
  58. static char* g_ClipboardTextData = NULL;
  59. static bool g_MouseCanUseGlobalState = true;
  60. static const char* ImGui_ImplSDL2_GetClipboardText(void*)
  61. {
  62. if (g_ClipboardTextData)
  63. SDL_free(g_ClipboardTextData);
  64. g_ClipboardTextData = SDL_GetClipboardText();
  65. return g_ClipboardTextData;
  66. }
  67. static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text)
  68. {
  69. SDL_SetClipboardText(text);
  70. }
  71. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  72. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  73. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  74. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  75. // If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field.
  76. bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
  77. {
  78. ImGuiIO& io = ImGui::GetIO();
  79. switch (event->type)
  80. {
  81. case SDL_MOUSEWHEEL:
  82. {
  83. if (event->wheel.x > 0) io.MouseWheelH += 1;
  84. if (event->wheel.x < 0) io.MouseWheelH -= 1;
  85. if (event->wheel.y > 0) io.MouseWheel += 1;
  86. if (event->wheel.y < 0) io.MouseWheel -= 1;
  87. return true;
  88. }
  89. case SDL_MOUSEBUTTONDOWN:
  90. {
  91. if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
  92. if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
  93. if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
  94. return true;
  95. }
  96. case SDL_TEXTINPUT:
  97. {
  98. io.AddInputCharactersUTF8(event->text.text);
  99. return true;
  100. }
  101. case SDL_KEYDOWN:
  102. case SDL_KEYUP:
  103. {
  104. int key = event->key.keysym.scancode;
  105. IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
  106. io.KeysDown[key] = (event->type == SDL_KEYDOWN);
  107. io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
  108. io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
  109. io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
  110. #ifdef _WIN32
  111. io.KeySuper = false;
  112. #else
  113. io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
  114. #endif
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. static bool ImGui_ImplSDL2_Init(SDL_Window* window)
  121. {
  122. g_Window = window;
  123. // Setup backend capabilities flags
  124. ImGuiIO& io = ImGui::GetIO();
  125. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  126. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  127. io.BackendPlatformName = "imgui_impl_sdl";
  128. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
  129. io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
  130. io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
  131. io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
  132. io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
  133. io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
  134. io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
  135. io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
  136. io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
  137. io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
  138. io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
  139. io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
  140. io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
  141. io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
  142. io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
  143. io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
  144. io.KeyMap[ImGuiKey_KeyPadEnter] = SDL_SCANCODE_KP_ENTER;
  145. io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
  146. io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
  147. io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
  148. io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
  149. io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
  150. io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
  151. io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
  152. io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
  153. io.ClipboardUserData = NULL;
  154. // Load mouse cursors
  155. g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
  156. g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
  157. g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
  158. g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
  159. g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
  160. g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
  161. g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
  162. g_MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
  163. g_MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO);
  164. // Check and store if we are on Wayland
  165. g_MouseCanUseGlobalState = strncmp(SDL_GetCurrentVideoDriver(), "wayland", 7) != 0;
  166. #ifdef _WIN32
  167. SDL_SysWMinfo wmInfo;
  168. SDL_VERSION(&wmInfo.version);
  169. SDL_GetWindowWMInfo(window, &wmInfo);
  170. io.ImeWindowHandle = wmInfo.info.win.window;
  171. #else
  172. (void)window;
  173. #endif
  174. return true;
  175. }
  176. bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
  177. {
  178. (void)sdl_gl_context; // Viewport branch will need this.
  179. return ImGui_ImplSDL2_Init(window);
  180. }
  181. bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window)
  182. {
  183. #if !SDL_HAS_VULKAN
  184. IM_ASSERT(0 && "Unsupported");
  185. #endif
  186. return ImGui_ImplSDL2_Init(window);
  187. }
  188. bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window)
  189. {
  190. #if !defined(_WIN32)
  191. IM_ASSERT(0 && "Unsupported");
  192. #endif
  193. return ImGui_ImplSDL2_Init(window);
  194. }
  195. bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window)
  196. {
  197. return ImGui_ImplSDL2_Init(window);
  198. }
  199. void ImGui_ImplSDL2_Shutdown()
  200. {
  201. g_Window = NULL;
  202. // Destroy last known clipboard data
  203. if (g_ClipboardTextData)
  204. SDL_free(g_ClipboardTextData);
  205. g_ClipboardTextData = NULL;
  206. // Destroy SDL mouse cursors
  207. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  208. SDL_FreeCursor(g_MouseCursors[cursor_n]);
  209. memset(g_MouseCursors, 0, sizeof(g_MouseCursors));
  210. }
  211. static void ImGui_ImplSDL2_UpdateMousePosAndButtons()
  212. {
  213. ImGuiIO& io = ImGui::GetIO();
  214. // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  215. if (io.WantSetMousePos)
  216. SDL_WarpMouseInWindow(g_Window, (int)io.MousePos.x, (int)io.MousePos.y);
  217. else
  218. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  219. int mx, my;
  220. Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
  221. io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & 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.
  222. io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
  223. io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
  224. g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
  225. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS)
  226. SDL_Window* focused_window = SDL_GetKeyboardFocus();
  227. if (g_Window == focused_window)
  228. {
  229. if (g_MouseCanUseGlobalState)
  230. {
  231. // SDL_GetMouseState() gives mouse position seemingly based on the last window entered/focused(?)
  232. // The creation of a new windows at runtime and SDL_CaptureMouse both seems to severely mess up with that, so we retrieve that position globally.
  233. // Won't use this workaround when on Wayland, as there is no global mouse position.
  234. int wx, wy;
  235. SDL_GetWindowPosition(focused_window, &wx, &wy);
  236. SDL_GetGlobalMouseState(&mx, &my);
  237. mx -= wx;
  238. my -= wy;
  239. }
  240. io.MousePos = ImVec2((float)mx, (float)my);
  241. }
  242. // SDL_CaptureMouse() let the OS know e.g. that our imgui drag outside the SDL window boundaries shouldn't e.g. trigger the OS window resize cursor.
  243. // The function is only supported from SDL 2.0.4 (released Jan 2016)
  244. bool any_mouse_button_down = ImGui::IsAnyMouseDown();
  245. SDL_CaptureMouse(any_mouse_button_down ? SDL_TRUE : SDL_FALSE);
  246. #else
  247. if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_INPUT_FOCUS)
  248. io.MousePos = ImVec2((float)mx, (float)my);
  249. #endif
  250. }
  251. static void ImGui_ImplSDL2_UpdateMouseCursor()
  252. {
  253. ImGuiIO& io = ImGui::GetIO();
  254. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  255. return;
  256. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  257. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  258. {
  259. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  260. SDL_ShowCursor(SDL_FALSE);
  261. }
  262. else
  263. {
  264. // Show OS mouse cursor
  265. SDL_SetCursor(g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
  266. SDL_ShowCursor(SDL_TRUE);
  267. }
  268. }
  269. static void ImGui_ImplSDL2_UpdateGamepads()
  270. {
  271. ImGuiIO& io = ImGui::GetIO();
  272. memset(io.NavInputs, 0, sizeof(io.NavInputs));
  273. if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
  274. return;
  275. // Get gamepad
  276. SDL_GameController* game_controller = SDL_GameControllerOpen(0);
  277. if (!game_controller)
  278. {
  279. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  280. return;
  281. }
  282. // Update gamepad inputs
  283. #define MAP_BUTTON(NAV_NO, BUTTON_NO) { io.NavInputs[NAV_NO] = (SDL_GameControllerGetButton(game_controller, BUTTON_NO) != 0) ? 1.0f : 0.0f; }
  284. #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float vn = (float)(SDL_GameControllerGetAxis(game_controller, AXIS_NO) - V0) / (float)(V1 - V0); if (vn > 1.0f) vn = 1.0f; if (vn > 0.0f && io.NavInputs[NAV_NO] < vn) io.NavInputs[NAV_NO] = vn; }
  285. const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value.
  286. MAP_BUTTON(ImGuiNavInput_Activate, SDL_CONTROLLER_BUTTON_A); // Cross / A
  287. MAP_BUTTON(ImGuiNavInput_Cancel, SDL_CONTROLLER_BUTTON_B); // Circle / B
  288. MAP_BUTTON(ImGuiNavInput_Menu, SDL_CONTROLLER_BUTTON_X); // Square / X
  289. MAP_BUTTON(ImGuiNavInput_Input, SDL_CONTROLLER_BUTTON_Y); // Triangle / Y
  290. MAP_BUTTON(ImGuiNavInput_DpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT); // D-Pad Left
  291. MAP_BUTTON(ImGuiNavInput_DpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); // D-Pad Right
  292. MAP_BUTTON(ImGuiNavInput_DpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP); // D-Pad Up
  293. MAP_BUTTON(ImGuiNavInput_DpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN); // D-Pad Down
  294. MAP_BUTTON(ImGuiNavInput_FocusPrev, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); // L1 / LB
  295. MAP_BUTTON(ImGuiNavInput_FocusNext, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); // R1 / RB
  296. MAP_BUTTON(ImGuiNavInput_TweakSlow, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); // L1 / LB
  297. MAP_BUTTON(ImGuiNavInput_TweakFast, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); // R1 / RB
  298. MAP_ANALOG(ImGuiNavInput_LStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768);
  299. MAP_ANALOG(ImGuiNavInput_LStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767);
  300. MAP_ANALOG(ImGuiNavInput_LStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32767);
  301. MAP_ANALOG(ImGuiNavInput_LStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767);
  302. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  303. #undef MAP_BUTTON
  304. #undef MAP_ANALOG
  305. }
  306. void ImGui_ImplSDL2_NewFrame(SDL_Window* window)
  307. {
  308. ImGuiIO& io = ImGui::GetIO();
  309. IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer backend. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
  310. // Setup display size (every frame to accommodate for window resizing)
  311. int w, h;
  312. int display_w, display_h;
  313. SDL_GetWindowSize(window, &w, &h);
  314. if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
  315. w = h = 0;
  316. SDL_GL_GetDrawableSize(window, &display_w, &display_h);
  317. io.DisplaySize = ImVec2((float)w, (float)h);
  318. if (w > 0 && h > 0)
  319. io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
  320. // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
  321. static Uint64 frequency = SDL_GetPerformanceFrequency();
  322. Uint64 current_time = SDL_GetPerformanceCounter();
  323. io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f);
  324. g_Time = current_time;
  325. ImGui_ImplSDL2_UpdateMousePosAndButtons();
  326. ImGui_ImplSDL2_UpdateMouseCursor();
  327. // Update game controllers (if enabled and available)
  328. ImGui_ImplSDL2_UpdateGamepads();
  329. }