imgui

FORK: Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
git clone https://git.neptards.moe/neptards/imgui.git
Log | Files | Refs

imgui_impl_sdl.cpp (18704B)


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