Can't do OpenGL inside of timer function

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?

Along similar lines I have noticed that the main event
loop:

while(1) { process_events(); draw_screen(); }

causes an infinite loop. Shouldn’t an event loop
block in some way? 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.
Is there a way to implement what I’m trying to do?

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?

I’m compiling under Cygwin under Windows 2000. If it
helps any the original example works fine.

Thanks in advance,

Brennan Cheung
@Brennan__________________________________________________
Do You Yahoo!?
NEW from Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: gltest.c
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20011004/6720f89b/attachment.asc

just an idea, but your timer function could be called in one other
thread…

in this case, in ordinary platform dependant OpenGL programming you
should write , for example on windows: wglMakeCurrent(…, …) to make
your OpenGL context the current for this thread.

with SDL, unfortunately, you don’t have such possibility…

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:

Ok, it seems like the best way for me to code what I’m
trying to do is to just put a delay inside of the
infinite while loop and call my update_universe (I
eventually want to add some physics in there) after
the delay statement. I guess I’ll have to avoid using
the timer function all together.
— David Olofson <david.olofson at reologica.se> wrote:> On Friday 05 October 2001 06:38, Brennan wrote:

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 -’


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Do You Yahoo!?
NEW from Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.