Tutorial: A better way to embed an SDL window into Gtk (or GNOME, etc)

Hello,

This is just a quick tutorial on a better way to embed an SDL window
into Gtk (or GNOME, etc). I should probably write a demo for it, but
I really don’t have the time (right now). So, I’ll just explain
how to do it, on the mailing list.

When I first starting learning SDL, I wanted to use it with Gtk
(and GNOME). I found the Gtk Demo on the SDL website. This
demo used the “SDL_WINDOWID” environment variable hack to
accomplish this. The only problem with this is that you loose
the ability to switch to full screen.

There is another way to embed the SDL window into GTK (and GNOME, etc)
that will keep you ability to switch to full screen. It is illustrated
with the code:

#include <gtk/gtk.h>

#include <gdk/gdkx.h> /* For:
                       *     Macros:
                       *         GTK_WINDOW_XWINDOW
                       */


#include <SDL.h>

#include <SDL_syswm.h> /* For:
                        *     Functions:
                        *         SDL_GetWMInfo
                        *     Types:
                        *         SDL_SysWMinfo
                        */

#include <SDL_version.h> /* For:
                          *     Macros:
                          *         SDL_version
                          */



// ...




SDL_SysWMinfo info;
SDL_VERSION(&(info.version));
if ( -1 == SDL_GetWMInfo(&info) ) {
    printf("Error getting WM info: %s\n", SDL_GetError());
    exit(1);
}


// ...


GtkWidget * my_widget; // This could be referencing any Gtk or
                       // Gnome widget.  When I first tried it,
                       // I put it on the GnomeCanvas.


// Assign something to "my_widget".


// ...



// Embed the SDL window in Gtk or Gnome.
{
    Window parent;
    int x = 5;  // Set this to where ever you want it.
    int y = 20; // Set this to where ever you want it.

    // This just seems to make it work better.
    XSync(display, False);

    parent = GDK_WINDOW_XWINDOW(GTK_WIDGET(my_widget)->window);
    XReparentWindow(info.info.x11.display,
                    info.info.x11.wmwindow,
                    parent,
                    x, y);
}

(There is one problem with this though. The SDL window first appears
outside your Gtk or Gnome application window… but will quickly
go inside of it. It’s a little annoying… but you can still go
full screen with this method.)

Someone might find that useful.

See ya

 Charles Iliya Krempeaux
 tnt @ linux.ca
 ckrempea @ alumni.sfu.ca