Skip to content

Commit c16629e

Browse files
authored
Merge pull request #8 from mjakeman/sdl2-backend
2 parents 5fb1c3b + fa7de22 commit c16629e

File tree

7 files changed

+955
-0
lines changed

7 files changed

+955
-0
lines changed

build.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub const Backend = enum {
77
glfw_dx12,
88
win32_dx12,
99
glfw,
10+
sdl2_opengl3,
1011
};
1112

1213
pub fn build(b: *std.Build) void {
@@ -338,6 +339,18 @@ pub fn build(b: *std.Build) void {
338339
.flags = cflags,
339340
});
340341
},
342+
.sdl2_opengl3 => {
343+
if (b.lazyDependency("zsdl", .{})) |zsdl| {
344+
imgui.addIncludePath(zsdl.path("libs/sdl2/include"));
345+
}
346+
imgui.addCSourceFiles(.{
347+
.files = &.{
348+
"libs/imgui/backends/imgui_impl_sdl2.cpp",
349+
"libs/imgui/backends/imgui_impl_opengl3.cpp",
350+
},
351+
.flags = &(cflags.* ++ .{"-DIMGUI_IMPL_OPENGL_LOADER_CUSTOM"}),
352+
});
353+
},
341354
.no_backend => {},
342355
}
343356

build.zig.zon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
.hash = "1220777b1f964f666f5e990412f34c7dd36b6c399ff15920754f842df297425dffa5",
2626
.lazy = true,
2727
},
28+
.zsdl = .{
29+
.url = "https://github.com/zig-gamedev/zsdl/archive/360a5033010c71445ba42d1de3973abd50c6b128.tar.gz",
30+
.hash = "1220b661c47496008c7b2717c06b7bc1f79b1cfc430c885222d5d49c91d3fc57cbde",
31+
.lazy = true,
32+
},
2833
.freetype = .{
2934
.url = "https://github.com/hexops/freetype/archive/972cd37bccecae2cc9f54cf0b562263a13209d02.tar.gz",
3035
.hash = "12204cba3a237cd2c4ab983f5a28d9b54e7a9912d8c7c6e38e23140b0171d6e1ebf8",

libs/imgui/backends/imgui_impl_sdl2.cpp

Lines changed: 808 additions & 0 deletions
Large diffs are not rendered by default.

libs/imgui/backends/imgui_impl_sdl2.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
5+
// Implemented features:
6+
// [X] Platform: Clipboard support.
7+
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
8+
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5]
9+
// [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
10+
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
11+
// [X] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!.
12+
13+
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
14+
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
15+
// Learn about Dear ImGui:
16+
// - FAQ https://dearimgui.com/faq
17+
// - Getting Started https://dearimgui.com/getting-started
18+
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
19+
// - Introduction, links and more at the top of imgui.cpp
20+
21+
#pragma once
22+
#include "imgui.h" // IMGUI_IMPL_API
23+
#ifndef IMGUI_DISABLE
24+
25+
struct SDL_Window;
26+
struct SDL_Renderer;
27+
struct _SDL_GameController;
28+
typedef union SDL_Event SDL_Event;
29+
30+
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
31+
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context);
32+
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window);
33+
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window);
34+
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window);
35+
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer);
36+
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForOther(SDL_Window* window);
37+
IMGUI_IMPL_API void ImGui_ImplSDL2_Shutdown();
38+
IMGUI_IMPL_API void ImGui_ImplSDL2_NewFrame();
39+
IMGUI_IMPL_API bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event);
40+
41+
// Gamepad selection automatically starts in AutoFirst mode, picking first available SDL_Gamepad. You may override this.
42+
// When using manual mode, caller is responsible for opening/closing gamepad.
43+
enum ImGui_ImplSDL2_GamepadMode { ImGui_ImplSDL2_GamepadMode_AutoFirst, ImGui_ImplSDL2_GamepadMode_AutoAll, ImGui_ImplSDL2_GamepadMode_Manual };
44+
IMGUI_IMPL_API void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array = nullptr, int manual_gamepads_count = -1);
45+
46+
#endif // #ifndef IMGUI_DISABLE

src/backend_sdl2.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const gui = @import("gui.zig");
2+
3+
pub fn initOpenGL(
4+
window: *const anyopaque, // SDL_Window
5+
context: *const anyopaque, // SDL_GL_Context
6+
) void {
7+
if (!ImGui_ImplSDL2_InitForOpenGL(window, context)) {
8+
unreachable;
9+
}
10+
}
11+
12+
pub fn processEvent(
13+
event: *const anyopaque, // SDL_Event
14+
) bool {
15+
return ImGui_ImplSDL2_ProcessEvent(event);
16+
}
17+
18+
pub fn deinit() void {
19+
ImGui_ImplSDL2_Shutdown();
20+
}
21+
22+
pub fn newFrame() void {
23+
ImGui_ImplSDL2_NewFrame();
24+
}
25+
26+
// These functions are defined in `imgui_impl_sdl2.cpp`
27+
extern fn ImGui_ImplSDL2_InitForOpenGL(window: *const anyopaque, sdl_gl_context: *const anyopaque) bool;
28+
extern fn ImGui_ImplSDL2_ProcessEvent(event: *const anyopaque) bool;
29+
extern fn ImGui_ImplSDL2_NewFrame() void;
30+
extern fn ImGui_ImplSDL2_Shutdown() void;

src/backend_sdl2_opengl.zig

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const gui = @import("gui.zig");
2+
const backend_sdl2 = @import("backend_sdl2.zig");
3+
4+
pub fn initWithGlSlVersion(
5+
window: *const anyopaque, // SDL_Window
6+
context: *const anyopaque, // SDL_GL_Context
7+
glsl_version: ?[:0]const u8, // e.g. "#version 130"
8+
) void {
9+
backend_sdl2.initOpenGL(window, context);
10+
11+
ImGui_ImplOpenGL3_Init(@ptrCast(glsl_version));
12+
}
13+
14+
pub fn init(
15+
window: *const anyopaque, // SDL_Window
16+
context: *const anyopaque, // SDL_GL_Context
17+
) void {
18+
initWithGlSlVersion(window, context, null);
19+
}
20+
21+
pub fn processEvent(
22+
event: *const anyopaque, // SDL_Event
23+
) bool {
24+
return backend_sdl2.processEvent(event);
25+
}
26+
27+
pub fn deinit() void {
28+
ImGui_ImplOpenGL3_Shutdown();
29+
backend_sdl2.deinit();
30+
}
31+
32+
pub fn newFrame(fb_width: u32, fb_height: u32) void {
33+
backend_sdl2.newFrame();
34+
ImGui_ImplOpenGL3_NewFrame();
35+
36+
gui.io.setDisplaySize(@as(f32, @floatFromInt(fb_width)), @as(f32, @floatFromInt(fb_height)));
37+
gui.io.setDisplayFramebufferScale(1.0, 1.0);
38+
39+
gui.newFrame();
40+
}
41+
42+
pub fn draw() void {
43+
gui.render();
44+
ImGui_ImplOpenGL3_RenderDrawData(gui.getDrawData());
45+
}
46+
47+
// These functions are defined in 'imgui_impl_opengl3.cpp`
48+
// (they include few custom changes).
49+
extern fn ImGui_ImplOpenGL3_Init(glsl_version: [*c]const u8) void;
50+
extern fn ImGui_ImplOpenGL3_Shutdown() void;
51+
extern fn ImGui_ImplOpenGL3_NewFrame() void;
52+
extern fn ImGui_ImplOpenGL3_RenderDrawData(data: *const anyopaque) void;

src/gui.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub const backend = switch (@import("zgui_options").backend) {
1515
.glfw_dx12 => @import("backend_glfw_dx12.zig"),
1616
.glfw => @import("backend_glfw.zig"),
1717
.win32_dx12 => @import("backend_win32_dx12.zig"),
18+
.sdl2_opengl3 => @import("backend_sdl2_opengl.zig"),
1819
.no_backend => .{},
1920
};
2021
const te_enabled = @import("zgui_options").with_te;

0 commit comments

Comments
 (0)