I am learning both SDL and OpenGL and am having some
difficulty. Can someone please help me out?
I copied the example from
http://sdldoc.csn.ul.ie/guidevideoopengl.php and
modified it so that I had a timer function and so it
was running in a window. (see attached)
Why is it that I can not make any OpenGL calls or
SDL_PollEvent from inside the timer callback?
Because neither SDL nor OpenGL are thread safe by design - and if you
have an OpenGL implementation that is thread safe (ie is aware of
preemptive context switches), SDL doesn’t support it. You’d need to use
special (wgl, glx or whatever) calls to “bind” threads to OpenGL contexts
for that to work.
Along similar lines I have noticed that the main event
loop:
while(1) { process_events(); draw_screen(); }
causes an infinite loop.
Of course it does - SDL_PollEvent() does exactly what the name implies.
Use SDL_WaitEvent() instead.
Shouldn’t an event loop block in some way?
No, at least not on events, in your average single threaded game. (In
fact, you can’t use a blocking event API in a single threaded game,
unless it’s ok that the game freezes when there’s no user input.)
Incoming events should not have to
be polled. They should be handled through callbacks.
And the draw_screen should be done in a timer
callback.
I don’t know about should here. It’s a matter of taste and needs.
Besides, rendering should block only of SDL_Flip(), which should perform
a retrace synchronized hardware flip to the next buffer in a chain of
three. Unfortunately, retrace sync as well as triple buffering is only
possible on a few platforms, and triple buffering is not supported at all
by SDL.
Is there a way to implement what I’m trying to do?
Yes, but you have to shift things around a bit. Do the rendering from
within your main thread, and move the event loop into a thread of it’s
own. Make sure that you never perform OpenGL calls, or SDL calls other
than the event related ones, from the event thread context.
Is there a reason why I can’t do OpenGL or SDL inside
of the timer callback? Is it that SDL can’t do that
or something is wrong on my end?
(See above.)
The specific reason why it doesn’t work at all on Win2k is probably that
Win2k has some ideas about threading… You need some special wgl calls
to tell OpenGL that you’ll be calling from another thread than the main
application thread.
I’m compiling under Cygwin under Windows 2000.
Compiler shouldn’t make a difference, but the code would probably work on
some other platforms…
//David Olofson — Programmer, Reologica Instruments AB
.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |
-------------------------------------> http://olofson.net -'On Friday 05 October 2001 06:38, Brennan wrote: