Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Loading screen - help me understand loop better  (Read 3221 times)

0 Members and 1 Guest are viewing this topic.

pozioxd

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Loading screen - help me understand loop better
« 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading screen - help me understand loop better
« Reply #1 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 and the contained links.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

pozioxd

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Loading screen - help me understand loop better
« Reply #2 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading screen - help me understand loop better
« Reply #3 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

pozioxd

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Loading screen - help me understand loop better
« Reply #4 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)

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Loading screen - help me understand loop better
« Reply #5 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

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Loading screen - help me understand loop better
« Reply #6 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 ...
« Last Edit: September 23, 2014, 04:32:49 pm by wintertime »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading screen - help me understand loop better
« Reply #7 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*