dear-imgui-sdl3 0.14.1

SDL3 platform backend with optional OpenGL3 renderer for dear-imgui-rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
use super::*;

/// RAII owner for the SDL3 platform backend without an official renderer shim.
///
/// Dropping this value shuts down `imgui_impl_sdl3` while the captured Dear ImGui
/// context is still alive. If the context has already been dropped, `Drop` skips
/// the FFI call.
#[must_use = "dropping the backend owner shuts down the SDL3 platform backend"]
#[derive(Debug)]
pub struct Sdl3PlatformBackend {
    context: ContextBinding,
    shutdown_on_drop: bool,
}

impl Sdl3PlatformBackend {
    fn from_initialized_context(imgui: &Context) -> Self {
        Self {
            context: ContextBinding::capture(imgui),
            shutdown_on_drop: true,
        }
    }

    /// Initialize the SDL3 platform backend for non-OpenGL renderers.
    pub fn init_for_other(imgui: &mut Context, window: &Window) -> Result<Self, Sdl3BackendError> {
        init_for_other(imgui, window)?;
        Ok(Self::from_initialized_context(imgui))
    }

    /// Initialize the SDL3 platform backend for an OpenGL context without
    /// initializing the official OpenGL3 renderer.
    pub fn init_platform_for_opengl(
        imgui: &mut Context,
        window: &Window,
        gl_context: &GLContext,
    ) -> Result<Self, Sdl3BackendError> {
        init_platform_for_opengl(imgui, window, gl_context)?;
        Ok(Self::from_initialized_context(imgui))
    }

    /// Initialize the SDL3 platform backend for Vulkan renderers.
    pub fn init_for_vulkan(imgui: &mut Context, window: &Window) -> Result<Self, Sdl3BackendError> {
        init_for_vulkan(imgui, window)?;
        Ok(Self::from_initialized_context(imgui))
    }

    /// Initialize the SDL3 platform backend for Direct3D renderers.
    pub fn init_for_d3d(imgui: &mut Context, window: &Window) -> Result<Self, Sdl3BackendError> {
        init_for_d3d(imgui, window)?;
        Ok(Self::from_initialized_context(imgui))
    }

    /// Initialize the SDL3 platform backend for Metal renderers.
    pub fn init_for_metal(imgui: &mut Context, window: &Window) -> Result<Self, Sdl3BackendError> {
        init_for_metal(imgui, window)?;
        Ok(Self::from_initialized_context(imgui))
    }

    /// Initialize the SDL3 platform backend for SDL_Renderer-based renderers.
    ///
    /// # Safety
    ///
    /// The caller must provide a valid `SDL_Renderer` pointer associated with `window`.
    pub unsafe fn init_for_sdl_renderer(
        imgui: &mut Context,
        window: &Window,
        renderer: *mut sdl3_sys::render::SDL_Renderer,
    ) -> Result<Self, Sdl3BackendError> {
        unsafe {
            init_for_sdl_renderer(imgui, window, renderer)?;
        }
        Ok(Self::from_initialized_context(imgui))
    }

    /// Initialize the SDL3 platform backend for SDL GPU renderers.
    pub fn init_for_sdl_gpu(
        imgui: &mut Context,
        window: &Window,
    ) -> Result<Self, Sdl3BackendError> {
        init_for_sdl_gpu(imgui, window)?;
        Ok(Self::from_initialized_context(imgui))
    }

    /// Begin a new SDL3 platform frame.
    pub fn new_frame(&mut self, imgui: &mut Context) {
        self.context
            .assert_matches(imgui, "Sdl3PlatformBackend::new_frame()");
        let _guard = self.context.bind("Sdl3PlatformBackend::new_frame()");
        sdl3_new_frame_impl();
    }

    /// Process a single low-level SDL3 event with the captured ImGui context.
    pub fn process_event(&mut self, imgui: &mut Context, event: &SDL_Event) -> bool {
        self.context
            .assert_matches(imgui, "Sdl3PlatformBackend::process_event()");
        let _guard = self.context.bind("Sdl3PlatformBackend::process_event()");
        process_sys_event(event)
    }

    /// Configure how the SDL3 backend handles gamepads for the captured context.
    pub fn set_gamepad_mode(&mut self, imgui: &mut Context, mode: GamepadMode) {
        self.context
            .assert_matches(imgui, "Sdl3PlatformBackend::set_gamepad_mode()");
        let _guard = self.context.bind("Sdl3PlatformBackend::set_gamepad_mode()");
        set_gamepad_mode(mode);
    }

    /// Configure SDL3 backend to use manual gamepad selection for the captured context.
    ///
    /// # Safety
    ///
    /// - The caller must ensure every pointer in `gamepads` is a valid, opened `SDL_Gamepad`.
    /// - The caller is responsible for keeping those gamepads alive for the duration of ImGui usage.
    /// - The slice itself is only read during this call; the backend copies the pointers.
    pub unsafe fn set_gamepad_mode_manual(
        &mut self,
        imgui: &mut Context,
        gamepads: &[*mut sdl3_sys::gamepad::SDL_Gamepad],
    ) {
        self.context
            .assert_matches(imgui, "Sdl3PlatformBackend::set_gamepad_mode_manual()");
        let _guard = self
            .context
            .bind("Sdl3PlatformBackend::set_gamepad_mode_manual()");
        unsafe {
            set_gamepad_mode_manual(gamepads);
        }
    }

    /// Shut down the SDL3 platform backend before dropping the owner.
    pub fn shutdown(mut self, imgui: &mut Context) {
        self.context
            .assert_matches(imgui, "Sdl3PlatformBackend::shutdown()");
        {
            let _guard = self.context.bind("Sdl3PlatformBackend::shutdown()");
            shutdown_platform_impl();
        }
        self.shutdown_on_drop = false;
    }
}

impl Drop for Sdl3PlatformBackend {
    fn drop(&mut self) {
        if self.shutdown_on_drop {
            if let Some(_guard) = self.context.bind_for_drop() {
                shutdown_platform_impl();
            }
        }
    }
}

/// RAII owner for SDL3 platform + official OpenGL3 renderer backends.
#[cfg(feature = "opengl3-renderer")]
#[must_use = "dropping the backend owner shuts down the SDL3 + OpenGL3 backends"]
#[derive(Debug)]
pub struct Sdl3OpenGl3Backend {
    context: ContextBinding,
    shutdown_on_drop: bool,
}

#[cfg(feature = "opengl3-renderer")]
impl Sdl3OpenGl3Backend {
    fn from_initialized_context(imgui: &Context) -> Self {
        Self {
            context: ContextBinding::capture(imgui),
            shutdown_on_drop: true,
        }
    }

    /// Initialize the SDL3 platform backend and the official OpenGL3 renderer.
    pub fn init(
        imgui: &mut Context,
        window: &Window,
        gl_context: &GLContext,
        glsl_version: &str,
    ) -> Result<Self, Sdl3BackendError> {
        init_for_opengl(imgui, window, gl_context, glsl_version)?;
        Ok(Self::from_initialized_context(imgui))
    }

    /// Initialize the SDL3 + OpenGL3 backends with the upstream default GLSL version.
    pub fn init_default(
        imgui: &mut Context,
        window: &Window,
        gl_context: &GLContext,
    ) -> Result<Self, Sdl3BackendError> {
        init_for_opengl_default(imgui, window, gl_context)?;
        Ok(Self::from_initialized_context(imgui))
    }

    /// Begin a new SDL3 + OpenGL3 frame.
    pub fn new_frame(&mut self, imgui: &mut Context) {
        self.context
            .assert_matches(imgui, "Sdl3OpenGl3Backend::new_frame()");
        let _guard = self.context.bind("Sdl3OpenGl3Backend::new_frame()");
        new_frame_opengl3_impl();
    }

    /// Process a single low-level SDL3 event with the captured ImGui context.
    pub fn process_event(&mut self, imgui: &mut Context, event: &SDL_Event) -> bool {
        self.context
            .assert_matches(imgui, "Sdl3OpenGl3Backend::process_event()");
        let _guard = self.context.bind("Sdl3OpenGl3Backend::process_event()");
        process_sys_event(event)
    }

    /// Render Dear ImGui draw data using the official OpenGL3 renderer.
    pub fn render(&mut self, draw_data: &mut DrawData) {
        let _guard = self.context.bind("Sdl3OpenGl3Backend::render()");
        self.context
            .assert_current_draw_data(draw_data, "Sdl3OpenGl3Backend::render()");
        render_opengl3_impl(draw_data);
    }

    /// Update a single ImGui texture using the official OpenGL3 renderer.
    pub fn update_texture(&mut self, imgui: &mut Context, tex: &mut TextureData) {
        self.context
            .assert_matches(imgui, "Sdl3OpenGl3Backend::update_texture()");
        let _guard = self.context.bind("Sdl3OpenGl3Backend::update_texture()");
        update_texture(tex);
    }

    /// Configure how the SDL3 backend handles gamepads for the captured context.
    pub fn set_gamepad_mode(&mut self, imgui: &mut Context, mode: GamepadMode) {
        self.context
            .assert_matches(imgui, "Sdl3OpenGl3Backend::set_gamepad_mode()");
        let _guard = self.context.bind("Sdl3OpenGl3Backend::set_gamepad_mode()");
        set_gamepad_mode(mode);
    }

    /// Configure SDL3 backend to use manual gamepad selection for the captured context.
    ///
    /// # Safety
    ///
    /// - The caller must ensure every pointer in `gamepads` is a valid, opened `SDL_Gamepad`.
    /// - The caller is responsible for keeping those gamepads alive for the duration of ImGui usage.
    /// - The slice itself is only read during this call; the backend copies the pointers.
    pub unsafe fn set_gamepad_mode_manual(
        &mut self,
        imgui: &mut Context,
        gamepads: &[*mut sdl3_sys::gamepad::SDL_Gamepad],
    ) {
        self.context
            .assert_matches(imgui, "Sdl3OpenGl3Backend::set_gamepad_mode_manual()");
        let _guard = self
            .context
            .bind("Sdl3OpenGl3Backend::set_gamepad_mode_manual()");
        unsafe {
            set_gamepad_mode_manual(gamepads);
        }
    }

    /// Create OpenGL3 renderer device objects.
    pub fn create_device_objects(&mut self, imgui: &mut Context) -> bool {
        self.context
            .assert_matches(imgui, "Sdl3OpenGl3Backend::create_device_objects()");
        let _guard = self
            .context
            .bind("Sdl3OpenGl3Backend::create_device_objects()");
        create_device_objects()
    }

    /// Destroy OpenGL3 renderer device objects.
    pub fn destroy_device_objects(&mut self, imgui: &mut Context) {
        self.context
            .assert_matches(imgui, "Sdl3OpenGl3Backend::destroy_device_objects()");
        let _guard = self
            .context
            .bind("Sdl3OpenGl3Backend::destroy_device_objects()");
        destroy_device_objects();
    }

    /// Shut down the official OpenGL3 renderer and SDL3 platform backend.
    pub fn shutdown(mut self, imgui: &mut Context) {
        self.context
            .assert_matches(imgui, "Sdl3OpenGl3Backend::shutdown()");
        {
            let _guard = self.context.bind("Sdl3OpenGl3Backend::shutdown()");
            shutdown_opengl3_impl();
        }
        self.shutdown_on_drop = false;
    }
}

#[cfg(feature = "opengl3-renderer")]
impl Drop for Sdl3OpenGl3Backend {
    fn drop(&mut self) {
        if self.shutdown_on_drop {
            if let Some(_guard) = self.context.bind_for_drop() {
                shutdown_opengl3_impl();
            }
        }
    }
}

/// RAII owner for SDL3 platform + official SDLRenderer3 renderer backends.
#[cfg(feature = "sdlrenderer3-renderer")]
#[must_use = "dropping the backend owner shuts down the SDL3 + SDLRenderer3 backends"]
#[derive(Debug)]
pub struct Sdl3RendererBackend {
    context: ContextBinding,
    shutdown_on_drop: bool,
}

#[cfg(feature = "sdlrenderer3-renderer")]
impl Sdl3RendererBackend {
    fn from_initialized_context(imgui: &Context) -> Self {
        Self {
            context: ContextBinding::capture(imgui),
            shutdown_on_drop: true,
        }
    }

    /// Initialize the SDL3 platform backend and the official SDLRenderer3 renderer.
    pub fn init(
        imgui: &mut Context,
        window: &Window,
        canvas: &WindowCanvas,
    ) -> Result<Self, Sdl3BackendError> {
        init_for_canvas(imgui, window, canvas)?;
        Ok(Self::from_initialized_context(imgui))
    }

    /// Begin a new SDL3 + SDLRenderer3 frame.
    pub fn new_frame(&mut self, imgui: &mut Context) {
        self.context
            .assert_matches(imgui, "Sdl3RendererBackend::new_frame()");
        let _guard = self.context.bind("Sdl3RendererBackend::new_frame()");
        new_frame_sdlrenderer3_impl();
    }

    /// Process a single low-level SDL3 event with the captured ImGui context.
    pub fn process_event(&mut self, imgui: &mut Context, event: &SDL_Event) -> bool {
        self.context
            .assert_matches(imgui, "Sdl3RendererBackend::process_event()");
        let _guard = self.context.bind("Sdl3RendererBackend::process_event()");
        process_sys_event(event)
    }

    /// Render Dear ImGui draw data using the official SDLRenderer3 renderer.
    pub fn render(&mut self, draw_data: &mut DrawData, canvas: &WindowCanvas) {
        let _guard = self.context.bind("Sdl3RendererBackend::render()");
        self.context
            .assert_current_draw_data(draw_data, "Sdl3RendererBackend::render()");
        render_sdlrenderer3_impl(draw_data, canvas);
    }

    /// Update a single ImGui texture using the official SDLRenderer3 renderer.
    pub fn update_texture(&mut self, imgui: &mut Context, tex: &mut TextureData) {
        self.context
            .assert_matches(imgui, "Sdl3RendererBackend::update_texture()");
        let _guard = self.context.bind("Sdl3RendererBackend::update_texture()");
        canvas_update_texture(tex);
    }

    /// Configure how the SDL3 backend handles gamepads for the captured context.
    pub fn set_gamepad_mode(&mut self, imgui: &mut Context, mode: GamepadMode) {
        self.context
            .assert_matches(imgui, "Sdl3RendererBackend::set_gamepad_mode()");
        let _guard = self.context.bind("Sdl3RendererBackend::set_gamepad_mode()");
        set_gamepad_mode(mode);
    }

    /// Configure SDL3 backend to use manual gamepad selection for the captured context.
    ///
    /// # Safety
    ///
    /// - The caller must ensure every pointer in `gamepads` is a valid, opened `SDL_Gamepad`.
    /// - The caller is responsible for keeping those gamepads alive for the duration of ImGui usage.
    /// - The slice itself is only read during this call; the backend copies the pointers.
    pub unsafe fn set_gamepad_mode_manual(
        &mut self,
        imgui: &mut Context,
        gamepads: &[*mut sdl3_sys::gamepad::SDL_Gamepad],
    ) {
        self.context
            .assert_matches(imgui, "Sdl3RendererBackend::set_gamepad_mode_manual()");
        let _guard = self
            .context
            .bind("Sdl3RendererBackend::set_gamepad_mode_manual()");
        unsafe {
            set_gamepad_mode_manual(gamepads);
        }
    }

    /// Create SDLRenderer3 renderer device objects.
    pub fn create_device_objects(&mut self, imgui: &mut Context) {
        self.context
            .assert_matches(imgui, "Sdl3RendererBackend::create_device_objects()");
        let _guard = self
            .context
            .bind("Sdl3RendererBackend::create_device_objects()");
        canvas_create_device_objects();
    }

    /// Destroy SDLRenderer3 renderer device objects.
    pub fn destroy_device_objects(&mut self, imgui: &mut Context) {
        self.context
            .assert_matches(imgui, "Sdl3RendererBackend::destroy_device_objects()");
        let _guard = self
            .context
            .bind("Sdl3RendererBackend::destroy_device_objects()");
        canvas_destroy_device_objects();
    }

    /// Shut down the official SDLRenderer3 renderer and SDL3 platform backend.
    pub fn shutdown(mut self, imgui: &mut Context) {
        self.context
            .assert_matches(imgui, "Sdl3RendererBackend::shutdown()");
        {
            let _guard = self.context.bind("Sdl3RendererBackend::shutdown()");
            shutdown_sdlrenderer3_impl();
        }
        self.shutdown_on_drop = false;
    }
}

#[cfg(feature = "sdlrenderer3-renderer")]
impl Drop for Sdl3RendererBackend {
    fn drop(&mut self) {
        if self.shutdown_on_drop {
            if let Some(_guard) = self.context.bind_for_drop() {
                shutdown_sdlrenderer3_impl();
            }
        }
    }
}

/// Render Dear ImGui draw data using the OpenGL3 backend.
///
/// This assumes an OpenGL context is current.
#[cfg(feature = "opengl3-renderer")]
pub fn render(draw_data: &mut DrawData) {
    render_opengl3_impl(draw_data);
}

#[cfg(feature = "opengl3-renderer")]
fn render_opengl3_impl(draw_data: &mut DrawData) {
    unsafe {
        let raw = draw_data as *mut DrawData as *mut sys::ImDrawData;
        opengl3_backend::dear_imgui_backend_opengl3_render_draw_data(raw);
    }
}

/// Render Dear ImGui draw data using the SDLRenderer3 backend.
#[cfg(feature = "sdlrenderer3-renderer")]
pub fn canvas_render(draw_data: &mut DrawData, canvas: &WindowCanvas) {
    render_sdlrenderer3_impl(draw_data, canvas);
}

#[cfg(feature = "sdlrenderer3-renderer")]
fn render_sdlrenderer3_impl(draw_data: &mut DrawData, canvas: &WindowCanvas) {
    let sdl_renderer = canvas.raw();
    unsafe {
        let raw = draw_data as *mut DrawData as *mut sys::ImDrawData;
        sdlrenderer3_backend::dear_imgui_backend_sdlrenderer3_render_draw_data(
            raw,
            sdl_renderer as *mut std::ffi::c_void,
        );
    }
}

/// Update a single ImGui texture using the OpenGL3 backend.
///
/// This is an advanced helper that delegates to `ImGui_ImplOpenGL3_UpdateTexture`.
#[cfg(feature = "opengl3-renderer")]
pub fn update_texture(tex: &mut TextureData) {
    unsafe {
        opengl3_backend::dear_imgui_backend_opengl3_update_texture(tex.as_raw_mut());
    }
}

/// Update a single ImGui texture using the SDLRenderer3 backend.
///
/// This is an advanced helper that delegates to `ImGui_ImplSDLRenderer3_UpdateTexture`.
#[cfg(feature = "sdlrenderer3-renderer")]
pub fn canvas_update_texture(tex: &mut TextureData) {
    unsafe {
        sdlrenderer3_backend::dear_imgui_backend_sdlrenderer3_update_texture(tex.as_raw_mut());
    }
}

/// Create OpenGL3 renderer device objects.
///
/// This is an optional advanced helper mirroring `ImGui_ImplOpenGL3_CreateDeviceObjects`.
#[cfg(feature = "opengl3-renderer")]
pub fn create_device_objects() -> bool {
    unsafe { opengl3_backend::dear_imgui_backend_opengl3_create_device_objects() }
}

/// Destroy OpenGL3 renderer device objects.
///
/// This is an optional advanced helper mirroring `ImGui_ImplOpenGL3_DestroyDeviceObjects`.
#[cfg(feature = "opengl3-renderer")]
pub fn destroy_device_objects() {
    unsafe {
        opengl3_backend::dear_imgui_backend_opengl3_destroy_device_objects();
    }
}

/// Create SDLRenderer3 renderer device objects.
///
/// This is an optional advanced helper mirroring `ImGui_ImplSDLRenderer3_CreateDeviceObjects`.
#[cfg(feature = "sdlrenderer3-renderer")]
pub fn canvas_create_device_objects() {
    unsafe { sdlrenderer3_backend::dear_imgui_backend_sdlrenderer3_create_device_objects() }
}

/// Destroy SDLRenderer3 renderer device objects.
///
/// This is an optional advanced helper mirroring `ImGui_ImplSDLRenderer3_DestroyDeviceObjects`.
#[cfg(feature = "sdlrenderer3-renderer")]
pub fn canvas_destroy_device_objects() {
    unsafe {
        sdlrenderer3_backend::dear_imgui_backend_sdlrenderer3_destroy_device_objects();
    }
}