SFML community forums

Help => General => Topic started by: pozioxd on September 22, 2014, 09:39:10 pm

Title: Loading screen - help me understand loop better
Post by: pozioxd on September 22, 2014, 09:39:10 pm
Hello everyone.

Let's say i have this code:
    // Loading
        while(stateLoading)
        {
            Window.clear(sf::Color::Black);
            //Here draw a picture saying "Loading";
            Window.display();
           
            //Load file X
            //Load file Y
            //Load file Z
            //Load file ...

        stateLoading=false;
        stateMainMenu=true;
        }
 

My question: will all the files first be loaded, and then it will jump to the next state, or some files may not load but it will still go to the next section.

I just wonder, if this is enough to make loading screen, or I need to do something more complex. I know that while in SFML loops over and over again, but does .loadFromFile() method make it something like "wait! we must load this file, then u can go further!"?

I hope you understand my question, if my request wasn't clear I'm sorry, and I'll try to make it more clear.
Have a nice day, good people.
Title: Re: Loading screen - help me understand loop better
Post by: Nexus on September 22, 2014, 09:47:59 pm
Unless explicitly noted, function calls are always synchronous, meaning that control only returns to the caller when the execution of the function has ended. So the loading of the files will block your loop.

To introduce asynchronous execution, you need multithreading. The C++11 threading API provides all you need, std::async() can simplify things.

Keep in mind that multithreading is a very complex topic. You should spend at least several days delving into it and experiment with small examples; otherwise you'll be constantly frustrated because of very tricky bugs you don't understand. See this post (http://en.sfml-dev.org/forums/index.php?topic=15759.msg112366#msg112366) and the contained links.
Title: Re: Loading screen - help me understand loop better
Post by: pozioxd on September 22, 2014, 09:51:39 pm
Thank you for very fast answer. So, this will work? First it will load all the items that I put here, then it will go to the next state?
Title: Re: Loading screen - help me understand loop better
Post by: Nexus on September 22, 2014, 09:53:18 pm
If you want to keep the window responding while loading files, your current approach will not work.

Loading files in a separate thread will work.
Title: Re: Loading screen - help me understand loop better
Post by: pozioxd on September 22, 2014, 10:10:11 pm
I don't. I just want them to load, with a fancy image saying "Loading" or something. Thank you for your response 8)
Title: Re: Loading screen - help me understand loop better
Post by: AlexAUT on September 22, 2014, 10:18:26 pm
But keep in mind when you have a fullscreen application the user won't be able to switch out of the game, and depending on the loading duration(~15sec+) windows(OS) could complain about a frozen application.


AlexAUT
Title: Re: Loading screen - help me understand loop better
Post by: wintertime on September 23, 2014, 04:30:38 pm
You can improve that code a bit without multithreading, if you dont load all files at once. Just load one file (or even just a bit of a file), check for events, redraw, next ...
Title: Re: Loading screen - help me understand loop better
Post by: Hapax on September 23, 2014, 10:24:55 pm
I think he's asking if one of the loads fails, will it loop. Short answer, no.

This while will never actually loop as you're changing the condition that it requires to loop before it ends. Here, the while would work identically to an if.

Starting from your current code, you could move the stateLoading = false; to the beginning of the loop and then, if anything fails, change it to true.
You'll have to be aware that it'll probably just keep failing though.

This state will "block" until it's finished though so unless it's very short and quick, you should probably look at giving it responsiveness. Some people have already provided ways to investigate how to do that.