[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]