Home Game Development sdl2 – How to setup a loading screen using SDL_Threads?

sdl2 – How to setup a loading screen using SDL_Threads?

0
sdl2 – How to setup a loading screen using SDL_Threads?

[ad_1]

I am trying to set up a basic toy example of how a loading screen would work in SDL. Here is my code below.

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_thread.h>

#define DELAY  10

std::string status = "state 0";

static int LoadThread(void* data)
{
    //Delay to simulate loading
    SDL_Delay(DELAY);
    status = "delay 0";
    SDL_Delay(DELAY*10);
    status = "delay 1";
    SDL_Delay(DELAY*100);
    status = "delay 2";
    SDL_Delay(DELAY*5);
    status = "delay 3";
    SDL_Delay(DELAY*6);
    status = "delay 4";
    SDL_Delay(DELAY*5);
    status = "delay 5";
    SDL_Delay(DELAY*2);

    status = "finished";
    return 0;
}

int main(int argc, char *argv[])
{      
    SDL_Thread *p = SDL_CreateThread(LoadThread, "LoadingAssetsThread", nullptr);
   
    std::string currentStatus = status;

    while(true)
    {        
        if (status != currentStatus)
        {
            std::cout << status << " " << tState << std::endl;
            currentStatus = status;
        }

        if (currentStatus == "finished") break;
    }        
        

    SDL_WaitThread(p, nullptr);
    
    return 0;
}

Basically I want to show a series of lines of text stating what is the current asset that is being loaded, i.e. textures, sound, etc one after as they complete loading in the separate thread.

Is this the right approach? The reason being, in my real world code, I noticed when optimization is turned on (-O3 option in g++) sometimes certain steps in “currentStatus” will be skipped over and won’t display in the main thread. I am using a std::cout to illustrate but in reality there is a function that draws the text to a SDL window.

Put another way, there is a variable that the loading function has to update as it sequentially loads one asset after another and the main game loop renders a different message to the player based on what that variable is at any given time during the loading process.

[ad_2]

Previous article Insomniac Leaks Fuels Fan Speculation About A Spider-Verse Game
Next article Grab Grand Theft Auto V On PlayStation Plus This Month
Hello there! My name is YoleeTeam, and I am thrilled to welcome you to AmazonianGames.com. As the premier destination for all things related to Amazon Games' universe, we are dedicated to providing you with the most exciting and immersive gaming experiences out there. From captivating visuals to exhilarating gameplay, our website is packed with comprehensive insights, updates, and reviews to keep you ahead of the game. Whether you're a seasoned gamer or new to the scene, I am here to guide you through this virtual frontier. If you have any questions or suggestions, feel free to reach out to me at john@yoleesolutions.com. Embark on your Amazon gaming journey today with AmazonianGames.com and let the adventure begin!