The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_CreateRGBSurface in second thread?
mediata


Joined: 10 Mar 2014
Posts: 12
Hello,
I have a question: Is it possible to call SDL_CreateRGBSurface and SDL_CreateRGBSurfaceFrom in second thread? They only load buffer and set R,G,B mask.
Also asking about IMG_Load() From SDL_image library. They use libpng or libjpeg for example to create buffer from image file and after that they Create Surface.

My task is to create Texture form png. To that moment i load buffer using libpng in second thread and after that in main thread i use opengl to create texture. And now i want to migrate to SDL and is it possible to
IMG_Load to SDL_Surface in second thread and after that in main thread to CreateTexture using SDL_CreateTextureFromSurface?
mediata


Joined: 10 Mar 2014
Posts: 12
I have progress about this question i create simple program to test this case
here is thread function:
Code:

static void
load_texture()
{
    SDL_Surface *surface = nullptr;
   
    std::cout << "THREAD STARTED!\n";
    while (true)
    {
        surface = IMG_Load("images/wheel/background.png");
        if (surface == nullptr)
        {
            std::cout << "ERROR LOADNING SURFACE!!!\n";
           
        }
        SDL_FreeSurface(surface);
        usleep(5000);
    }
    std::cout << "THREAD END!\n";
}


And i have no problem with drawing and loading for past 10 minutes
Naith


Joined: 03 Jul 2014
Posts: 158
I asked the same thing about IMG_Load a while back and I got the answer that it's safe to call IMG_Load in another thread and then call SDL-specific functions (SDL_CreateTextureFromSurface for example) in the main to create the final texture.
I am using this exact solution when I create a texture in my framework. Smile

Short answer: it's safe to execute IMG_Load in another thread and SDL-specific functions should be executed in the thread that created the SDL-window, which is usually the main thread.