Yes – you can “park” the SDL 2 renderer inside a regular GTK widget and keep the rest of your application in “pure GTK”.
Below is everything you need to know: the basic idea, the gotchas, and a minimal working example (Linux/X11). Feel free to scroll straight to the code if you just want to copy-paste!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-
HOW IT WORKS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-
Create a normal GTK window with (for example) a GtkDrawingArea that will act as the canvas for SDL.
-
Wait until that widget has a real, native window (→ “realize” signal).
-
Grab the platform-dependent native handle (X11 Window, Win32 HWND, Wayland surface, …).
-
Call SDL_CreateWindowFrom(void *native_handle) → SDL thinks that widget is “its” window.
-
Create an SDL renderer or GL context using the returned SDL_Window *.
-
Pump / render from inside the GTK main loop (idle/tick callback).
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2. EVENT LOOPS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
GTK owns the main loop; you never enter SDL_WaitEvent.
Inside a g_idle_add() or g_timeout_add() callback you:
- call
SDL_PollEvent(&e) a couple times (to keep SDL happy: joystick, game-controller, etc.),
- update your game/editor logic,
- call your
SDL_Render* commands,
SDL_RenderPresent(renderer).
GTK <--> SDL event duplication?
• Keyboard / mouse events will reach you twice (once via GDK, once via SDL).
Pick one system and ignore the other to avoid conflicts.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
3. LIMITATIONS / GOTCHAS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Cross-platform status
– Linux/X11: works well.
– Linux/Wayland: use gdk_wayland_window_get_wl_surface(); SDL2≥2.0.22 needed.
– Windows: use gdk_win32_window_get_handle().
– macOS: officially not supported – Cocoa back-end differences; you can hack it, but expect pain.
• HiDPI / scaling: SDL and GTK disagree on pixels vs. points in some setups; query gtk_widget_get_scale_factor() or SDL_GetDisplayUsableBounds.
• Lifetime: NEVER destroy the widget before you’ve destroyed the SDL renderer/window; otherwise SDL may try to touch freed memory.
• Transparency: SDL’s renderer clears the entire widget; if you want only a sub-area, clip in SDL.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
4. MINIMAL WORKING EXAMPLE (X11, C, GTK 3 + SDL 2)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#include <gtk/gtk.h>
#include <SDL.h>
#ifdef GDK_WINDOWING_X11 /* only compiled on X11 */
#include <gdk/gdkx.h>
#endif
/* Globals for demo purpose */
static SDL_Window *sdl_win = NULL;
static SDL_Renderer *sdl_ren = NULL;
/* Draw callback (called by GTK whenever it thinks the widget needs paint) */
static gboolean on_draw(GtkWidget *widget, cairo_t *cr, gpointer userdata)
{
if (!sdl_ren) return FALSE; /* Not ready yet */
/* === SDL rendering === */
SDL_SetRenderDrawColor(sdl_ren, 25, 25, 25, 255);
SDL_RenderClear(sdl_ren);
SDL_SetRenderDrawColor(sdl_ren, 200, 50, 50, 255);
SDL_Rect r = { 50, 50, 200, 120 };
SDL_RenderFillRect(sdl_ren, &r);
SDL_RenderPresent(sdl_ren);
/* Let GTK continue – we didn’t actually use cairo */
return FALSE;
}
/* Realize callback: widget now has a native window we can steal */
static void on_realize(GtkWidget *widget, gpointer userdata)
{
#ifdef GDK_WINDOWING_X11
GdkWindow *gdk_win = gtk_widget_get_window(widget);
Window xid = GDK_WINDOW_XID(gdk_win);
char xid_str[32];
/* SDL hint for X11 backend (not strictly required but avoids warnings) */
snprintf(xid_str, sizeof xid_str, "%lu", (unsigned long)xid);
SDL_SetHint(SDL_HINT_VIDEO_X11_WINDOWID, xid_str);
/* Turn that XID into an SDL window */
sdl_win = SDL_CreateWindowFrom((void *)(uintptr_t)xid);
if (!sdl_win) g_error("SDL_CreateWindowFrom failed: %s", SDL_GetError());
/* Create renderer */
sdl_ren = SDL_CreateRenderer(sdl_win, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
#else
g_error("This example is X11-only. See source for Wayland/Win32 variants.");
#endif
}
/* Idle callback: pump SDL events */
static gboolean pump_sdl(gpointer data)
{
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT) gtk_main_quit();
/* … handle extra SDL events if you need them … */
}
/* Ask GTK to queue a redraw of the drawing area */
gtk_widget_queue_draw(GTK_WIDGET(data));
return TRUE; /* keep this idle callback alive */
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
SDL_Init(SDL_INIT_VIDEO); /* No SDL main loop */
/* GTK UI --------------------------------------------------- */
GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *area = gtk_drawing_area_new();
gtk_window_set_default_size(GTK_WINDOW(win), 800, 600);
gtk_container_add(GTK_CONTAINER(win), area);
g_signal_connect(win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(area, "realize", G_CALLBACK(on_realize), NULL);
g_signal_connect(area, "draw", G_CALLBACK(on_draw), NULL);
gtk_widget_show_all(win);
/* Start idle pump to keep SDL events flowing */
g_idle_add(pump_sdl, area);
gtk_main();
/* Clean-up */
if (sdl_ren) SDL_DestroyRenderer(sdl_ren);
if (sdl_win) SDL_DestroyWindow(sdl_win);
SDL_Quit();
return 0;
}
Compile:
gcc `pkg-config --cflags gtk+-3.0 sdl2` -o gtk_sdl_embed main.c \
`pkg-config --libs gtk+-3.0 sdl2`
Run ➜ one GTK window, SDL drawing inside the DrawingArea.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
5. PORTING NOTES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Wayland (GTK 3.22+ / 4):
#include <gdk/gdkwayland.h>
struct wl_surface *surf = gdk_wayland_window_get_wl_surface(gdk_win);
sdl_win = SDL_CreateWindowFrom(surf);
Windows (MSYS/MinGW):
#include <gdk/gdkwin32.h>
HWND hwnd = gdk_win32_window_get_handle(gdk_win);
sdl_win = SDL_CreateWindowFrom(hwnd);
Once you wrap the platform if-defs in helper functions, the rest of your code can stay 100 % platform-agnostic.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
6. SHOULD I DO THIS?
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Pros
✓ SDL’s lightning-fast renderer / GL layer inside a mature GUI.
✓ Use GTK’s file pickers, TreeViews, toolbars, etc. for your level editor.
✓ Keep your existing SDL rendering codebase.
Cons
✗ Not officially first-class in SDL – future breakage possible.
✗ Two toolkits means two sets of build dependencies.
✗ Complexity with event handling / focus / IME / HiDPI.
If you only need a viewport plus traditional tools & menus, this approach is great.
If your entire UI is game-like, consider staying inside SDL and adding e.g. Dear ImGui instead of GTK.
Happy hacking and good luck with your editor! 🎮🛠️