imgui_impl_sdl2.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // ImGui Platform Binding 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. // Implemented features:
  5. // [X] User texture binding. Cast 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
  6. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  7. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  8. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  9. // https://github.com/ocornut/imgui
  10. // CHANGELOG
  11. // (minor and older changes stripped away, please see git history for details)
  12. // 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformInterface
  13. // 2018-XX-XX: Misc: ImGui_ImplSDL2_Init() now takes a SDL_GLContext parameter.
  14. // 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
  15. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  16. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  17. // 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
  18. // 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
  19. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  20. // 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.
  21. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  22. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  23. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  24. #include "imgui.h"
  25. #include "imgui_impl_sdl2.h"
  26. #include "imgui_internal.h" // FIXME-PLATFORM
  27. // SDL
  28. #include <SDL.h>
  29. #include <SDL_syswm.h>
  30. #define SDL_HAS_CAPTURE_MOUSE SDL_VERSION_ATLEAST(2,0,4)
  31. #define SDL_HAS_WINDOW_OPACITY SDL_VERSION_ATLEAST(2,0,5)
  32. #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
  33. #if !SDL_HAS_VULKAN
  34. static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
  35. #endif
  36. // Data
  37. static SDL_Window* g_Window = NULL;
  38. static Uint64 g_Time = 0;
  39. static bool g_MousePressed[3] = { false, false, false };
  40. static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_Count_] = { 0 };
  41. // Forward Declarations
  42. static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context);
  43. static void ImGui_ImplSDL2_ShutdownPlatformInterface();
  44. static const char* ImGui_ImplSDL2_GetClipboardText(void*)
  45. {
  46. return SDL_GetClipboardText();
  47. }
  48. static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text)
  49. {
  50. SDL_SetClipboardText(text);
  51. }
  52. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  53. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  54. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  55. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  56. bool ImGui_ImplSDL2_ProcessEvent(SDL_Event* event)
  57. {
  58. ImGuiIO& io = ImGui::GetIO();
  59. switch (event->type)
  60. {
  61. case SDL_MOUSEWHEEL:
  62. {
  63. if (event->wheel.x > 0) io.MouseWheelH += 1;
  64. if (event->wheel.x < 0) io.MouseWheelH -= 1;
  65. if (event->wheel.y > 0) io.MouseWheel += 1;
  66. if (event->wheel.y < 0) io.MouseWheel -= 1;
  67. return true;
  68. }
  69. case SDL_MOUSEBUTTONDOWN:
  70. {
  71. if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
  72. if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
  73. if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
  74. return true;
  75. }
  76. case SDL_TEXTINPUT:
  77. {
  78. io.AddInputCharactersUTF8(event->text.text);
  79. return true;
  80. }
  81. case SDL_KEYDOWN:
  82. case SDL_KEYUP:
  83. {
  84. int key = event->key.keysym.scancode;
  85. IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
  86. io.KeysDown[key] = (event->type == SDL_KEYDOWN);
  87. io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
  88. io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
  89. io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
  90. io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. bool ImGui_ImplSDL2_Init(SDL_Window* window, void* sdl_gl_context)
  97. {
  98. g_Window = window;
  99. // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  100. ImGuiIO& io = ImGui::GetIO();
  101. io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
  102. io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
  103. io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
  104. io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
  105. io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
  106. io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
  107. io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
  108. io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
  109. io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
  110. io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
  111. io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
  112. io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
  113. io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
  114. io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
  115. io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
  116. io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
  117. io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
  118. io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
  119. io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
  120. io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
  121. io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
  122. io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
  123. io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
  124. io.ClipboardUserData = NULL;
  125. g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
  126. g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
  127. g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
  128. g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
  129. g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
  130. g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
  131. g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
  132. #ifdef _WIN32
  133. SDL_SysWMinfo wmInfo;
  134. SDL_VERSION(&wmInfo.version);
  135. SDL_GetWindowWMInfo(window, &wmInfo);
  136. io.ImeWindowHandle = wmInfo.info.win.window;
  137. #endif
  138. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  139. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  140. main_viewport->PlatformHandle = (void*)window;
  141. // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports.
  142. // We left the call to ImGui_ImplSDL2_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings.
  143. #if SDL_HAS_CAPTURE_MOUSE
  144. io.ConfigFlags |= ImGuiConfigFlags_PlatformHasViewports;
  145. #endif
  146. if ((io.ConfigFlags & ImGuiConfigFlags_EnableViewports) && (io.ConfigFlags & ImGuiConfigFlags_PlatformHasViewports))
  147. ImGui_ImplSDL2_InitPlatformInterface(window, sdl_gl_context);
  148. return true;
  149. }
  150. void ImGui_ImplSDL2_Shutdown()
  151. {
  152. ImGui_ImplSDL2_ShutdownPlatformInterface();
  153. g_Window = NULL;
  154. // Destroy SDL mouse cursors
  155. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_Count_; cursor_n++)
  156. SDL_FreeCursor(g_MouseCursors[cursor_n]);
  157. memset(g_MouseCursors, 0, sizeof(g_MouseCursors));
  158. }
  159. static void ImGui_ImplSDL2_UpdateMouse()
  160. {
  161. ImGuiIO& io = ImGui::GetIO();
  162. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  163. io.MousePosViewport = 0;
  164. io.MouseHoveredViewport = 0;
  165. int mx, my;
  166. Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
  167. 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.
  168. io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
  169. io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
  170. g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
  171. #if SDL_HAS_CAPTURE_MOUSE
  172. SDL_Window* focused_window = SDL_GetKeyboardFocus();
  173. if (focused_window)
  174. {
  175. // SDL_GetMouseState() gives me mouse position seemingly based on the last window entered/focused(?)
  176. // The creation of new window and SDL_CaptureMouse both seems to severely mess up with that, so we retrieve that position globally.
  177. int wx, wy;
  178. SDL_GetWindowPosition(focused_window, &wx, &wy);
  179. SDL_GetGlobalMouseState(&mx, &my);
  180. mx -= wx;
  181. my -= wy;
  182. }
  183. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)focused_window))
  184. {
  185. io.MousePos = ImVec2(viewport->Pos.x + (float)mx, viewport->Pos.y + (float)my);
  186. io.MousePosViewport = viewport->ID;
  187. }
  188. // We already retrieve global mouse position, SDL_CaptureMouse() also let the OS know our drag outside boundaries shouldn't trigger, e.g.: OS window resize cursor
  189. // The function is only supported from SDL 2.0.4 (released Jan 2016)
  190. bool any_mouse_button_down = ImGui::IsAnyMouseDown();
  191. SDL_CaptureMouse(any_mouse_button_down ? SDL_TRUE : SDL_FALSE);
  192. #else
  193. if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_INPUT_FOCUS)
  194. io.MousePos = ImVec2((float)mx, (float)my);
  195. #endif
  196. // Update OS/hardware mouse cursor if imgui isn't drawing a software cursor
  197. ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
  198. if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
  199. {
  200. SDL_ShowCursor(SDL_FALSE);
  201. }
  202. else
  203. {
  204. SDL_SetCursor(g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
  205. SDL_ShowCursor(SDL_TRUE);
  206. }
  207. }
  208. void ImGui_ImplSDL2_NewFrame(SDL_Window* window)
  209. {
  210. ImGuiIO& io = ImGui::GetIO();
  211. IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame()
  212. // Setup display size (every frame to accommodate for window resizing)
  213. int w, h;
  214. int display_w, display_h;
  215. SDL_GetWindowSize(window, &w, &h);
  216. SDL_GL_GetDrawableSize(window, &display_w, &display_h);
  217. io.DisplaySize = ImVec2((float)w, (float)h);
  218. io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
  219. // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
  220. static Uint64 frequency = SDL_GetPerformanceFrequency();
  221. Uint64 current_time = SDL_GetPerformanceCounter();
  222. io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f);
  223. g_Time = current_time;
  224. ImGui_ImplSDL2_UpdateMouse();
  225. // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
  226. ImGui::NewFrame();
  227. }
  228. // --------------------------------------------------------------------------------------------------------
  229. // Platform Windows
  230. // --------------------------------------------------------------------------------------------------------
  231. struct ImGuiPlatformDataSDL2
  232. {
  233. SDL_Window* Window;
  234. Uint32 WindowID;
  235. SDL_GLContext GLContext;
  236. ImGuiPlatformDataSDL2() { Window = NULL; WindowID = 0; GLContext = NULL; }
  237. ~ImGuiPlatformDataSDL2() { IM_ASSERT(Window == NULL && GLContext == NULL); }
  238. };
  239. static void ImGui_ImplSDL2_CreateViewport(ImGuiViewport* viewport)
  240. {
  241. ImGuiPlatformDataSDL2* data = IM_NEW(ImGuiPlatformDataSDL2)();
  242. viewport->PlatformUserData = data;
  243. // Share GL resources with main context
  244. // FIXME-PLATFORM
  245. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  246. ImGuiPlatformDataSDL2* main_viewport_data = (ImGuiPlatformDataSDL2*)main_viewport->PlatformUserData;
  247. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  248. SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext);
  249. // We don't enable SDL_WINDOW_RESIZABLE because it enforce windows decorations
  250. Uint32 sdl_flags = 0;
  251. sdl_flags |= main_viewport_data->GLContext ? SDL_WINDOW_OPENGL : SDL_WINDOW_VULKAN;
  252. sdl_flags |= SDL_WINDOW_HIDDEN;
  253. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0;
  254. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE;
  255. data->Window = SDL_CreateWindow("No Title Yet", 0, 0, (int)viewport->Size.x, (int)viewport->Size.y, sdl_flags);
  256. if (main_viewport_data->GLContext)
  257. data->GLContext = SDL_GL_CreateContext(data->Window);
  258. viewport->PlatformHandle = (void*)data->Window;
  259. }
  260. static void ImGui_ImplSDL2_DestroyViewport(ImGuiViewport* viewport)
  261. {
  262. if (ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData)
  263. {
  264. if (data->GLContext)
  265. SDL_GL_DeleteContext(data->GLContext);
  266. data->GLContext = NULL;
  267. if (data->Window)
  268. SDL_DestroyWindow(data->Window);
  269. data->Window = NULL;
  270. IM_DELETE(data);
  271. }
  272. viewport->PlatformUserData = viewport->PlatformHandle = NULL;
  273. }
  274. static void ImGui_ImplSDL2_ShowWindow(ImGuiViewport* viewport)
  275. {
  276. ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData;
  277. #if defined(_WIN32)
  278. SDL_SysWMinfo info;
  279. SDL_VERSION(&info.version);
  280. if (SDL_GetWindowWMInfo(data->Window, &info))
  281. {
  282. HWND hwnd = info.info.win.window;
  283. // SDL hack: Hide icon from task bar
  284. // Note: SDL 2.0.6+ has a SDL_WINDOW_SKIP_TASKBAR flag which is supported under Windows but the way it create the window breaks our seamless transition.
  285. ImGuiIO& io = ImGui::GetIO();
  286. if (io.ConfigFlags & ImGuiConfigFlags_PlatformNoTaskBar)
  287. {
  288. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  289. ex_style &= ~WS_EX_APPWINDOW;
  290. ex_style |= WS_EX_TOOLWINDOW;
  291. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  292. }
  293. // SDL hack: SDL always activate/focus windows :/
  294. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  295. {
  296. ::ShowWindow(hwnd, SW_SHOWNA);
  297. return;
  298. }
  299. }
  300. #endif
  301. SDL_ShowWindow(data->Window);
  302. }
  303. static ImVec2 ImGui_ImplSDL2_GetWindowPos(ImGuiViewport* viewport)
  304. {
  305. ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData;
  306. int x = 0, y = 0;
  307. SDL_GetWindowPosition(data->Window, &x, &y);
  308. return ImVec2((float)x, (float)y);
  309. }
  310. static void ImGui_ImplSDL2_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  311. {
  312. ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData;
  313. SDL_SetWindowPosition(data->Window, (int)pos.x, (int)pos.y);
  314. }
  315. static ImVec2 ImGui_ImplSDL2_GetWindowSize(ImGuiViewport* viewport)
  316. {
  317. ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData;
  318. int w = 0, h = 0;
  319. SDL_GetWindowSize(data->Window, &w, &h);
  320. return ImVec2((float)w, (float)h);
  321. }
  322. static void ImGui_ImplSDL2_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  323. {
  324. ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData;
  325. SDL_SetWindowSize(data->Window, (int)size.x, (int)size.y);
  326. }
  327. static void ImGui_ImplSDL2_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  328. {
  329. ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData;
  330. SDL_SetWindowTitle(data->Window, title);
  331. }
  332. static void ImGui_ImplSDL2_RenderViewport(ImGuiViewport* viewport)
  333. {
  334. ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData;
  335. if (data->GLContext)
  336. SDL_GL_MakeCurrent(data->Window, data->GLContext);
  337. }
  338. static void ImGui_ImplSDL2_SwapBuffers(ImGuiViewport* viewport)
  339. {
  340. ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData;
  341. if (data->GLContext)
  342. {
  343. SDL_GL_MakeCurrent(data->Window, data->GLContext); // FIXME-PLATFORM2
  344. SDL_GL_SwapWindow(data->Window);
  345. }
  346. }
  347. // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
  348. // SDL is graceful enough to _not_ need <vulkan/vulkan.h> so we can safely include this.
  349. #if SDL_HAS_VULKAN
  350. #include <SDL_vulkan.h>
  351. static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
  352. {
  353. ImGuiPlatformDataSDL2* data = (ImGuiPlatformDataSDL2*)viewport->PlatformUserData;
  354. (void)vk_allocator;
  355. SDL_bool ret = SDL_Vulkan_CreateSurface(data->Window, (VkInstance)vk_instance, (VkSurfaceKHR*)out_vk_surface);
  356. return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY
  357. }
  358. #endif // SDL_HAS_VULKAN
  359. static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context)
  360. {
  361. // Register platform interface (will be coupled with a renderer interface)
  362. ImGuiIO& io = ImGui::GetIO();
  363. io.PlatformInterface.CreateViewport = ImGui_ImplSDL2_CreateViewport;
  364. io.PlatformInterface.DestroyViewport = ImGui_ImplSDL2_DestroyViewport;
  365. io.PlatformInterface.ShowWindow = ImGui_ImplSDL2_ShowWindow;
  366. io.PlatformInterface.SetWindowPos = ImGui_ImplSDL2_SetWindowPos;
  367. io.PlatformInterface.GetWindowPos = ImGui_ImplSDL2_GetWindowPos;
  368. io.PlatformInterface.SetWindowSize = ImGui_ImplSDL2_SetWindowSize;
  369. io.PlatformInterface.GetWindowSize = ImGui_ImplSDL2_GetWindowSize;
  370. io.PlatformInterface.SetWindowTitle = ImGui_ImplSDL2_SetWindowTitle;
  371. io.PlatformInterface.RenderViewport = ImGui_ImplSDL2_RenderViewport;
  372. io.PlatformInterface.SwapBuffers = ImGui_ImplSDL2_SwapBuffers;
  373. #if SDL_HAS_VULKAN
  374. io.PlatformInterface.CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface;
  375. #endif
  376. io.ConfigFlags |= SDL_HAS_WINDOW_OPACITY ? ImGuiConfigFlags_PlatformHasWindowAlpha : 0;
  377. // Register main window handle
  378. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  379. ImGuiPlatformDataSDL2* data = IM_NEW(ImGuiPlatformDataSDL2)();
  380. data->Window = window;
  381. data->WindowID = SDL_GetWindowID(window);
  382. data->GLContext = sdl_gl_context;
  383. main_viewport->PlatformUserData = data;
  384. main_viewport->PlatformHandle = data->Window;
  385. }
  386. static void ImGui_ImplSDL2_ShutdownPlatformInterface()
  387. {
  388. ImGuiIO& io = ImGui::GetIO();
  389. memset(&io.PlatformInterface, 0, sizeof(io.PlatformInterface));
  390. }