Sign in to follow this  

[SDL] Threads and Surfaces

This topic is 4111 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, I'm having a problem trying to draw some text to the screen. Text isn't really the hard part it's the surfaces that I'm having trouble with. Currently every time through the render loop I'm doing something like this:

while(1) // Render loop
{
  temp = TTF_RenderText_Solid(....);
  surface = SDL_DisplayFormat(temp);
  SDL_BlitSurface(surface,....);
  SDL_FreeSurface(temp);
  SDL_FreeSurface(surface);
}

It seems rendering the text to the surface every time is very slow, and CPU intensive. So I've created a thread to update the text only when the text changes. Now to the problem. When I update the surface to have the new text I need to Free the old surface. Since this is done in a thread, I'm freeing the surface and then trying to blit it. Is there any way to lock surfaces so they can't be freed until they are done being used? I know that's kinda cryptic. If you have any questions I can try to explain further. Thanks in advance! [Edited by - SigmaX on September 16, 2006 2:29:54 PM]

Share this post


Link to post
Share on other sites
Yes, but you shouldn't.

Quote:
From the documentation
In general, you must be very aware of concurrency and data integrity issues when writing multi-threaded programs. Some good guidelines include:

*
Don't call SDL video/event functions from separate threads
*
Don't use any library functions in separate threads
*
Don't perform any memory management in separate threads
*
Lock global variables which may be accessed by multiple threads
*
Never terminate threads, always set a flag and wait for them to quit
*
Think very carefully about all possible ways your code may interact


If you still want to know how, look into mutexes and conditions.

Share this post


Link to post
Share on other sites
Why would you want to use a thread anyway? Can't you just check if the text has changed and then call a function to recreate the image? There are reasons to use threads, but this does not seem to be one of them.

Share this post


Link to post
Share on other sites
Thank you for your responces. I'm working with a VLT (Video Lottery Terminal), and the text is changing depending on information pulled directly from device drivers. The thread being used because it's checking the status of multiple devices. But it's true I could probably do that in the part of the code where I display this text. Thanks I'll re-exaimine my approach.

Share this post


Link to post
Share on other sites

This topic is 4111 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this