SDL2, multithreading and loading screen

Hi there!

I would like to create my textures and other objects in another thread (i.e, a thread that is not the same thread as I’m creating the window in).
For example, I have a state machine containing a menu state and a game state. Whenever I switch from the menu state to the game state I would like to create a thread which creates the textures and other objects for the game and while these objects is being created, I would like to have a loading screen which is rendering a texture or some text saying ‘Loading…’ (not important how the loading screen looks right now). As soon as the game is created (i.e, all the game objects has been successfully created), the loading screen stop rendering and the game state is rendered.

How can I achieve this with SDL2?

I’ve heard that CreateTexture and such shouldn’t be executed in a thread that is not the main thread. Is this true?

As far as I know your right about screen access routines needing to be in the same thread.

Of course you could just code a test using two threads and see what happens …



Why don’t you add one intermediate “loading” state and switch to it from the menu to render progress before loader thread start. Then the loader thread will switch state to the “game” on loading success. It could update progress variable using some atomic functions.