Using threads with SDL

Hi,

this is my very first message to the list.
I’m planning to build a videogame with SDL
and OpenGL. I want to split it in several threads:
one thread handle the moving of the map, another one
moves the player and another one updates the lives,
energy ecc on the screen. I know that with SDL
I have to call graphics function from the main thread.
So how can I manage several thread to perform
graphic operation ? Is there a trick or something else ?

Thank you,–
Giuseppe Torelli

You can access graphics from different threads as long as you are only
doing so from one thread at a time. Use a mutex.

Chris Saton

torelli at szn.it wrote:> Hi,

this is my very first message to the list.
I’m planning to build a videogame with SDL
and OpenGL. I want to split it in several threads:
one thread handle the moving of the map, another one
moves the player and another one updates the lives,
energy ecc on the screen. I know that with SDL
I have to call graphics function from the main thread.
So how can I manage several thread to perform
graphic operation ? Is there a trick or something else ?

Thank you,

You can access graphics from different threads as long as you are only
doing so from one thread at a time. Use a mutex.

No, in general you can only make graphics calls from the main thread.
You can access software surfaces however you want, but hardware surfaces
and update calls should only be made from the main thread.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

You can create one “screen layer” for each thread. When the main thread
wants to update the real screen, it will merge all layers. It’s really
simple to implement.

If you create layers as large as screen, you will have no work and
threads will got a lot of space to play, but this is REALLY slow. I
recommend to create limited layers. Ex: the live update thread will have
a layer that only occupies the screen area where the live indicator appears.

Best regards,
Eduardo Costa

PS: I think this mt solution will have some overhead on a uniprocessor
machine.

torelli at szn.it wrote:>this is my very first message to the list.

I’m planning to build a videogame with SDL
and OpenGL. I want to split it in several threads:
one thread handle the moving of the map, another one
moves the player and another one updates the lives,
energy ecc on the screen. I know that with SDL
I have to call graphics function from the main thread.
So how can I manage several thread to perform
graphic operation ? Is there a trick or something else ?