Skip to content

Commit f884b45

Browse files
committed
SDL3
1 parent a91a3ec commit f884b45

4 files changed

Lines changed: 128 additions & 128 deletions

File tree

CMakeLists.txt

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -257,28 +257,8 @@ set(LIBS ${LIBS} lua)
257257
##
258258
#################
259259

260-
find_package(SDL2 REQUIRED)
261-
if(MSVC OR APPLE)
262-
set(SDL_LIBS SDL2::SDL2)
263-
elseif(MSYS)
264-
set(SDL_LIBS mingw32 SDL2)
265-
else()
266-
# maybe one of these will work?
267-
if(TARGET SDL2::SDL2)
268-
set(SDL_LIBS SDL2::SDL2)
269-
# SDL2main is not needed on linux (and broken on fedora)
270-
else()
271-
if(SDL2_LIBRARIES)
272-
string(STRIP ${SDL2_LIBRARIES} SDL2_LIBRARIES)
273-
endif()
274-
set(SDL_LIBS SDL2 ${SDL2_LIBRARY} ${SDL2_LIBRARIES})
275-
include_directories(SYSTEM ${SDL2_INCLUDE_DIR})
276-
include_directories(SYSTEM ${SDL2_INCLUDE_DIRS})
277-
include_directories(SYSTEM /usr/include/SDL2)
278-
endif()
279-
endif()
280-
set(LIBS ${LIBS} ${SDL_LIBS})
281-
260+
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
261+
set(LIBS ${LIBS} SDL3::SDL3)
282262

283263
#################
284264
##
@@ -452,9 +432,9 @@ endif()
452432

453433
target_link_libraries(vpv PRIVATE ${LIBS})
454434
if(MSVC OR APPLE)
455-
target_link_libraries(vpv PRIVATE SDL2::SDL2main)
435+
target_link_libraries(vpv PRIVATE SDL3::SDL3main)
456436
elseif(MSYS)
457-
target_link_libraries(vpv PRIVATE SDL2main)
437+
target_link_libraries(vpv PRIVATE SDL3main)
458438
endif()
459439

460440
target_compile_options(vpv PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:$<$<CONFIG:DEBUG>:${VPV_CXX_FLAGS}>>")

external/imgui/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
2323
// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
2424
// 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
25-
// 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
25+
// 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
2626
// 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
2727
// 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.
2828
// 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
@@ -43,8 +43,7 @@
4343
#include "imgui_impl_sdl_gl3.h"
4444

4545
// SDL,GL3W
46-
#include <SDL.h>
47-
#include <SDL_syswm.h>
46+
#include <SDL3/SDL.h>
4847
#include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you.
4948
#include "../../src/Shader.hpp"
5049

@@ -89,7 +88,7 @@ static unsigned int g_VboHandle = 0,g_ElementsHandle = 0;
8988
Shader::Program * g_shader;
9089

9190
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
92-
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
91+
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
9392
// If text or lines are blurry when integrating ImGui in your engine: in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
9493
void ImGui_ImplSdlGL3_RenderDrawData(ImDrawData* draw_data)
9594
{
@@ -149,7 +148,7 @@ void ImGui_ImplSdlGL3_RenderDrawData(ImDrawData* draw_data)
149148
glBindSampler(0, 0); // Rely on combined texture/sampler state.
150149
GLDEBUG();
151150

152-
// Recreate the VAO every time
151+
// Recreate the VAO every time
153152
// (This is to easily allow multiple GL contexts. VAO are not shared among GL contexts, and we don't track creation/deletion of windows so we don't have an obvious key to use to cache them.)
154153
GLuint vao_handle = 0;
155154
glGenVertexArrays(1, &vao_handle);
@@ -245,45 +244,45 @@ bool ImGui_ImplSdlGL3_ProcessEvent(SDL_Event* event)
245244
ImGuiIO& io = ImGui::GetIO();
246245
switch (event->type)
247246
{
248-
case SDL_MOUSEWHEEL:
247+
case SDL_EVENT_MOUSE_WHEEL:
249248
{
250249
if (event->wheel.x > 0) io.MouseWheelH += 1;
251250
if (event->wheel.x < 0) io.MouseWheelH -= 1;
252251
if (event->wheel.y > 0) io.MouseWheel += 1;
253252
if (event->wheel.y < 0) io.MouseWheel -= 1;
254253
return true;
255254
}
256-
case SDL_MOUSEBUTTONDOWN:
255+
case SDL_EVENT_MOUSE_BUTTON_DOWN:
257256
{
258257
if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
259258
if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
260259
if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
261260
return true;
262261
}
263-
case SDL_TEXTINPUT:
262+
case SDL_EVENT_TEXT_INPUT:
264263
{
265264
io.AddInputCharactersUTF8(event->text.text);
266265
return true;
267266
}
268-
case SDL_KEYDOWN:
269-
case SDL_KEYUP:
267+
case SDL_EVENT_KEY_DOWN:
268+
case SDL_EVENT_KEY_UP:
270269
{
271-
int key = event->key.keysym.scancode;
270+
SDL_Keycode key = event->key.key & ~SDLK_SCANCODE_MASK;
272271
IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
273-
io.KeysDown[key] = (event->type == SDL_KEYDOWN);
274-
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
275-
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
276-
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
277-
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
272+
io.KeysDown[key] = (event->type == SDL_EVENT_KEY_DOWN);
273+
io.KeyShift = ((SDL_GetModState() & SDL_KMOD_SHIFT) != 0);
274+
io.KeyCtrl = ((SDL_GetModState() & SDL_KMOD_CTRL) != 0);
275+
io.KeyAlt = ((SDL_GetModState() & SDL_KMOD_ALT) != 0);
276+
io.KeySuper = ((SDL_GetModState() & SDL_KMOD_GUI) != 0);
278277
return true;
279278
}
280-
case SDL_WINDOWEVENT:
281-
{
279+
default:
280+
if (event->type >= SDL_EVENT_WINDOW_FIRST and event->type <= SDL_EVENT_WINDOW_LAST) {
282281
SDL_Window* window = SDL_GetWindowFromID(event->window.windowID);
283282
int w, h;
284283
int display_w, display_h;
285284
SDL_GetWindowSize(window, &w, &h);
286-
SDL_GL_GetDrawableSize(window, &display_w, &display_h);
285+
SDL_GetWindowSizeInPixels(window, &display_w, &display_h);
287286
io.DisplaySize = ImVec2((float)w, (float)h);
288287
io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
289288
}
@@ -409,39 +408,41 @@ bool ImGui_ImplSdlGL3_Init(SDL_Window* window, const char* glsl_version)
409408
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
410409

411410
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
412-
io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
413-
io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
414-
io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
415-
io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
416-
io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
417-
io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
418-
io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
419-
io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
420-
io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
421-
io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
422-
io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
423-
io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
424-
io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
425-
io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
426-
io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
427-
io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
428-
io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
429-
io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
430-
io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
431-
io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
432-
io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
411+
#define M(k) ((k) & ~SDLK_SCANCODE_MASK)
412+
io.KeyMap[ImGuiKey_Tab] = M(SDLK_TAB);
413+
io.KeyMap[ImGuiKey_LeftArrow] = M(SDLK_LEFT);
414+
io.KeyMap[ImGuiKey_RightArrow] = M(SDLK_RIGHT);
415+
io.KeyMap[ImGuiKey_UpArrow] = M(SDLK_UP);
416+
io.KeyMap[ImGuiKey_DownArrow] = M(SDLK_DOWN);
417+
io.KeyMap[ImGuiKey_PageUp] = M(SDLK_PAGEUP);
418+
io.KeyMap[ImGuiKey_PageDown] = M(SDLK_PAGEDOWN);
419+
io.KeyMap[ImGuiKey_Home] = M(SDLK_HOME);
420+
io.KeyMap[ImGuiKey_End] = M(SDLK_END);
421+
io.KeyMap[ImGuiKey_Insert] = M(SDLK_INSERT);
422+
io.KeyMap[ImGuiKey_Delete] = M(SDLK_DELETE);
423+
io.KeyMap[ImGuiKey_Backspace] = M(SDLK_BACKSPACE);
424+
io.KeyMap[ImGuiKey_Space] = M(SDLK_SPACE);
425+
io.KeyMap[ImGuiKey_Enter] = M(SDLK_RETURN);
426+
io.KeyMap[ImGuiKey_Escape] = M(SDLK_ESCAPE);
427+
io.KeyMap[ImGuiKey_A] = SDLK_A;
428+
io.KeyMap[ImGuiKey_C] = SDLK_C;
429+
io.KeyMap[ImGuiKey_V] = SDLK_V;
430+
io.KeyMap[ImGuiKey_X] = SDLK_X;
431+
io.KeyMap[ImGuiKey_Y] = SDLK_Y;
432+
io.KeyMap[ImGuiKey_Z] = SDLK_Z;
433+
#undef M
433434

434435
io.SetClipboardTextFn = ImGui_ImplSdlGL3_SetClipboardText;
435436
io.GetClipboardTextFn = ImGui_ImplSdlGL3_GetClipboardText;
436437
io.ClipboardUserData = NULL;
437438

438-
g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
439-
g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
440-
g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
441-
g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
442-
g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
443-
g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
444-
g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
439+
g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
440+
g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT);
441+
g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_MOVE);
442+
g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NS_RESIZE);
443+
g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_EW_RESIZE);
444+
g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NESW_RESIZE);
445+
g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NWSE_RESIZE);
445446

446447
#ifdef _WIN32
447448
SDL_SysWMinfo wmInfo;
@@ -455,7 +456,7 @@ bool ImGui_ImplSdlGL3_Init(SDL_Window* window, const char* glsl_version)
455456
int w, h;
456457
int display_w, display_h;
457458
SDL_GetWindowSize(window, &w, &h);
458-
SDL_GL_GetDrawableSize(window, &display_w, &display_h);
459+
SDL_GetWindowSizeInPixels(window, &display_w, &display_h);
459460
io.DisplaySize = ImVec2((float)w, (float)h);
460461
io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
461462
return true;
@@ -465,7 +466,7 @@ void ImGui_ImplSdlGL3_Shutdown()
465466
{
466467
// Destroy SDL mouse cursors
467468
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
468-
SDL_FreeCursor(g_MouseCursors[cursor_n]);
469+
SDL_DestroyCursor(g_MouseCursors[cursor_n]);
469470
memset(g_MouseCursors, 0, sizeof(g_MouseCursors));
470471

471472
// Destroy OpenGL objects
@@ -483,7 +484,7 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window* window)
483484
int w, h;
484485
int display_w, display_h;
485486
SDL_GetWindowSize(window, &w, &h);
486-
SDL_GL_GetDrawableSize(window, &display_w, &display_h);
487+
SDL_GetWindowSizeInPixels(window, &display_w, &display_h);
487488
io.DisplaySize = ImVec2((float)w, (float)h);
488489
io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
489490

@@ -494,16 +495,16 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window* window)
494495
g_Time = current_time;
495496

496497
// Setup mouse inputs (we already got mouse wheel, keyboard keys & characters from our event handler)
497-
int mx, my;
498+
float mx, my;
498499
Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
499500
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
500-
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.
501-
io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
502-
io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
501+
io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & SDL_BUTTON_MASK(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.
502+
io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON_MASK(SDL_BUTTON_RIGHT)) != 0;
503+
io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON_MASK(SDL_BUTTON_MIDDLE)) != 0;
503504
g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
504505

505506
// We need to use SDL_CaptureMouse() to easily retrieve mouse coordinates outside of the client area. This is only supported from SDL 2.0.4 (released Jan 2016)
506-
#if (SDL_MAJOR_VERSION >= 2) && (SDL_MINOR_VERSION >= 0) && (SDL_PATCHLEVEL >= 4)
507+
#if (SDL_MAJOR_VERSION >= 2) && (SDL_MINOR_VERSION >= 0) && (SDL_PATCHLEVEL >= 4)
507508
if ((SDL_GetWindowFlags(window) & (SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_MOUSE_CAPTURE)) != 0)
508509
io.MousePos = ImVec2((float)mx, (float)my);
509510
bool any_mouse_button_down = false;
@@ -524,15 +525,21 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window* window)
524525
ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
525526
if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
526527
{
527-
SDL_ShowCursor(0);
528+
SDL_HideCursor();
528529
}
529530
else
530531
{
531532
SDL_SetCursor(g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
532-
SDL_ShowCursor(1);
533+
SDL_ShowCursor();
533534
}
534535
}
535536

536537
// 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.
537538
ImGui::NewFrame();
539+
540+
if (io.WantCaptureKeyboard) {
541+
SDL_StartTextInput(window);
542+
} else {
543+
SDL_StopTextInput(window);
544+
}
538545
}

src/events.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,53 @@
22

33
#include <imgui.h>
44

5-
#include <SDL.h>
5+
#include <SDL3/SDL.h>
66

77
#include "events.hpp"
88

99
int getCode(const char* name)
1010
{
11-
#define specials(n, sdl, sfml) \
12-
if (std::string(name) == #n) { \
13-
return SDL_SCANCODE_##sdl; \
11+
#define specials(n, sdl) \
12+
if (std::string(name) == #n) { \
13+
return SDLK_##sdl & ~SDLK_SCANCODE_MASK; \
1414
}
1515

16-
specials(left, LEFT, Left);
17-
specials(right, RIGHT, Right);
18-
specials(up, UP, Up);
19-
specials(down, DOWN, Down);
20-
specials(F1, F1, F1);
21-
specials(F2, F2, F2);
22-
specials(F3, F3, F3);
23-
specials(F4, F4, F4);
24-
specials(F5, F5, F5);
25-
specials(F6, F6, F6);
26-
specials(F7, F7, F7);
27-
specials(F8, F8, F8);
28-
specials(F9, F9, F9);
29-
specials(F10, F10, F10);
30-
specials(F11, F11, F11);
31-
specials(F12, F12, F12);
16+
specials(left, LEFT);
17+
specials(right, RIGHT);
18+
specials(up, UP);
19+
specials(down, DOWN);
20+
specials(F1, F1);
21+
specials(F2, F2);
22+
specials(F3, F3);
23+
specials(F4, F4);
24+
specials(F5, F5);
25+
specials(F6, F6);
26+
specials(F7, F7);
27+
specials(F8, F8);
28+
specials(F9, F9);
29+
specials(F10, F10);
30+
specials(F11, F11);
31+
specials(F12, F12);
32+
specials(1, 1);
33+
specials(2, 2);
34+
specials(3, 3);
35+
specials(4, 4);
36+
specials(5, 5);
37+
specials(6, 6);
38+
specials(7, 7);
39+
specials(8, 8);
40+
specials(9, 9);
3241

3342
#undef specials
3443

3544
switch (*name) {
3645
default: {
3746
SDL_Keycode key = SDL_GetKeyFromName(name);
38-
if (key != SDLK_UNKNOWN)
39-
return SDL_GetScancodeFromKey(key);
47+
if (key != SDLK_UNKNOWN) {
48+
key &= ~SDLK_SCANCODE_MASK;
49+
if (key < 512)
50+
return key;
51+
}
4052
}
4153
}
4254
printf("unknown key '%s'\n", name);

0 commit comments

Comments
 (0)