SDL threads and opengl

Started by
4 comments, last by Promit 19 years, 6 months ago
hi, hardcore coders,
I think this is more a SDL question rather than a OpenGL one.

I'm aware SDL doc says "Don't use any library functions in separate threads".
But I don't see any other way to do this :


bool loading;

void loading_message ( void* data) {
float x=0;
while (loading) {
// do some drawing with opengl
// for example : draw a message moving from left to right
glBindTexture ( texture_WeAreLoading_PleaseWait );
glBegin ();
glVertex3f ( x, ... )
...
glEnd ();
x += 0.1;
if (x>100) x=0;
}
}


main
{
loading = true;

SDL_CreateThread ( loading_message , NULL );

// meanwhile I do the loading

bla bla bla
// but in order to create some textures I need to use opengl functions like
glGenTextures(1, &a_new_texture);
glBindTexture(GL_TEXTURE_2D, a_new_texture);
glTexImage2D(GL_TEXTURE_2D, 0, pixelSize, width, height, 0, format, GL_UNSIGNED_BYTE, pixel);

// tell the thread to exit because it's done
loading = false;
}

What's the solution ?
I saw some PlayStation load part of a game whie displaying animation, but i can't remember a PC game doing this.
That's why I'm wondering is there even a solution ?
Advertisement
I don't know the answer, but I can direct you to some SDL + threading resources:

http://gameprogrammer.com/programming.html

Hope that helps!


Ryan
--Visit the Game Programming Wiki!
I think it says that so you don't accidentally use non-threadsafe functions. In any case you shouldn't be using OpenGL functions in more than one thread. In fact, OpenGL functions can only be used in the same thread that called SDL_Init - so it's best to keep them all in main().

A better way of structuring it would be to get your new thread to do the loading and for it to fire off SDL_Events to main() (or change a variable using mutexes or something) saying how far it's progressed. main() would contain the code for retrieving this information (e.g. by pumping the message queue) and drawing the status dialog box in a process that loops until the thread's done.

[EDIT] Having said that, I'm fairly sure it's safe to the the glBind... functions in a separate thread. If not, then they'll certainly be quick enough to do without a status box.
[teamonkey] [blog] [tinyminions]
Ryan Clark :

No it didn't help much but thanks for taking time to answer.

Plus, I played bounce !



teamonkey :

I'm going deeper with mutex ( never used this before )



I forgot to say that I tried the two opposite ways ( display in the main and load in the thread, and load in the main and display in the thread ). Each time some textures seemed to be freed ( they were all white ) and/or I had a strange message I had never seen before involving the name of my video card ( "bla bla bla radeon bla bla bla" ).

This message made me put an end to my random guessing with threads :

Don't mess with the driver, ask for help instead !

Quote:Original post by der
I forgot to say that I tried the two opposite ways ( display in the main and load in the thread, and load in the main and display in the thread ). Each time some textures seemed to be freed ( they were all white ) and/or I had a strange message I had never seen before involving the name of my video card ( "bla bla bla radeon bla bla bla" ).

This message made me put an end to my random guessing with threads :

Don't mess with the driver, ask for help instead !


In that case, OpenGL is definitely not thread safe. You can load in a separate thread, but you can't put it into a texture or anything like that.
[teamonkey] [blog] [tinyminions]
Yeah, realize that you have to keep all your OGL calls in the same thread. So usually, the best thing is to slide a disk load off into a seperate thread (or better yet, use async I/O). Then when your primary thread gets a chance, call the relevant OGL functions.

Needless to say, this complicates life immensely.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement