Jump to content
  • Advertisement
Sign in to follow this  
der

SDL threads and opengl

This topic is 5402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 !

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!