Gonzalo, I will apologize up front for the length of my reply. I’ve
been pretty consumed with this issue lately.
Conceptually there seems to be 2 approaches to this: 1) GTK window devoted
to hosting an SDL frame buffer surface where something is happening all
the time. The gtk-demo at libsdl.org is a good example of this approach.
2) SDL hosted as a widget within a larger GTK widget framework. There are
not too many good examples of this. While I am sure that there are good
reasons behind the first approach, I am not sure what they are exactly.
If you want a window with a lot of drawing going on all the time, why not
just use SDL without the overhead of GTK…? But if you want to take
advantage of GTK’s widget set to build tools or an application that could
also leverage SDL’s media integration- well, it sounds like you are in the
market for an SDL widget.
Anyway, so assuming that you want to take the widget approach, here are a
couple of things to keep in mind:
The most popular options for getting SDL into a GTK widget are: using a
widget like GtkDrawingArea as a container; building an SDL widget as
either a derived or scratch built widget. I believe that the container
and derived widget approaches have problems with the expose event, and I
prefer the scratch built approach. For references see:
The SF gtksdl project is 100% unencumbered by documentation and has not
been updated for some time. However, it is useful as an example of a
scratch built SDL widget and demonstrates the window hack. It does not
handle the expose event very well.
SDL Window hack: You can (and must) explicitly tell SDL which window to
draw in by setting the SDL_WINDOWID environment variable. See:
Expose Event: See Sam’s post here:
http://www.libsdl.org/pipermail/sdl/2003-January/051813.html What you’ll
see happen is: your widget (or other SDL implementation code) will handle
the expose event and flip the surface, update the rectangles, etc; and
then GTK will apparently repaint the exposed area with the default widget
color. This happens because GTK tells GDK to clear the exposed area to
the default widget color before the expose event is handled. The problem
is that SDL will finish its updates in the expose event handler before the
GDK clear area finishes. Unfortunately calling XSync did not work in my
case. A suggested solution: do not set GDK_EXPOSURE_MASK in the SDL
widget window’s attributes.event_mask member. This means that your window
will not receive expose events at all. You will need to put your SDL
widget in a container (a frame, for example) and wrap the frame’s expose
event with a function that calls your SDL widget’s expose event. It would
be nice if you could tell GTK to ignore certain portions of your widget
window, but not setting the GDK_EXPOSURE_MASK attribute is pretty much
doing just that. Owen Taylor talks about an anti-expose here:
http://www.gtk.org/~otaylor/gtk/2.0/gdk-drawing.html See `Expose
Queueing and Invalid regions’. This could be a cool solution.
Finally, I am sorry to be long in explanation and short on code. Its like
a regular undergrad CS class around here! All of the discussion that I
could find on the GTK SDL issue seems to stop at
Here's how you give SDL a window ID'' and ''I think you just put it in a GtkDrawingArea'' andMy
expose handler doesn’t work’’. Hopefully my notes are helpful. Please
consider them (and me) open to correction. My code is a mess, but if you
want to look at it, I’ll happily tar it up and send it to you.
shawnOn Thu Dec 11, 2003 at 03:08:58PM +0100, Gonzalo Aguilar Delgado wrote:
Anyone knows how to do SDL to work with GTK2?