Reputation:
For a 3D-modeling project I am using SDL and including in a GTK Widget (thanks to [GTKSDL], http://gtksdl.sourceforge.net "Direct Link to GTKSDL").
The widget is simply the loading of a .obj file with GTK and modeling it with OpenGL and then displaying it in the SDL Widget. So far, everything is working.
The issue comes when I try to move the object in the widget using SDL events. Before the integration of the SDL window in GTK, the events were working without any troubles. Also, once the 3D model is displayed, it is impossible for me to interact with GTK since apparently the event loop of SDL is waiting for something and whatever I do, it's not getting it.
I thought of forking the two event loop but it seems that GTK and SDL are trying to access the X server at the same time and it creates multiple conflicts. I tried to remove the endless loop in SDL but it doesn't work too.
I am on Devian, I searched the internet for an implementation of "GTKSdl" but it seems outdated, any ideas how to patch it ?
UPDATE:
I'm already using SDLPollEvent and g_idle add. So right after I choosed a file (with GTK), I fill the struct "t_stage" and use g_idle_add :
g_idle_add((GSourceFunc) &mainloop, stage);
gboolean mainloop(t_stage *stage)
{
SDL_Event evt;
while (SDL_PollEvent(&evt) != 0)
{
if (evt.type == SDL_MOUSEBUTTONDOWN)
on_mouse_down(stage, &(evt.button));
else if (evt.type == SDL_MOUSEBUTTONUP)
on_mouse_up(stage, &(evt.button));
else if (evt.type == SDL_MOUSEMOTION)
on_mouse_move(stage, &(evt.motion));
else if (evt.type == SDL_KEYDOWN)
handle_key(stage, evt.key.keysym.sym, 1);
else if (evt.type == SDL_KEYUP)
handle_key(stage, evt.key.keysym.sym, 0);
}
apply_keys(stage);
draw(stage, 0);
return (1);
}
But the events are still not received. Any ideas ?
Upvotes: 2
Views: 1005
Reputation: 11454
GTKSDL seems to be an alpha-quality widget created back in 2001 and never maintained (nor widely used, I think). GTK has changed a lot since then, so I'm not sure it's the best solution for you.
However, one of the problems you're facing is that you can't use an endless loop inside an event handler in GTK (and the same is true for every event-driven toolkit using a message pump I know). If you do that, you just prevent GTK from getting a chance to process the pending events. For more information on how to solve this, give a look to Embedding SDL into GTK+
UPDATE:
g_idle_add
allows to specify a callback and an argument to pass to that callback (the gpointer data
argument). A gpointer is merely a pointer to void. You can pass it the address of a structure to read/write if you need your callback to be aware of several parameters.
In your callback registered with g_idle_add
, you may read SDL events. Don't use SDL_WaitEvent
for that because it will block until an SDL event occurred, use SDL_PollEvent
(retrieves a pending event if any) or SDL_PeepEvents
(retrieves all pending events if any). These functions are non-blocking, they return if no event is pending, whereas SDL_WaitEvent would block until an event is received.
You may then process the events, making sure it doesn't take too long until you exit the idele handler, otherwise the GTK UI will freeze. You may also prefer to just translate these events into GTK events and just dispatch them to GTK for it to process them.
You also have an old example of SDL integration with GTK 1.2. I think most of it would work with GTK 2, the basic idea is there, just need to update the code to replace symbols that were since then deprecated in GTK.
Upvotes: 2