Loading Screen

I’m trying to implement a loading screen in my program because it has quite a few files to load and it takes a bit of time to load it all. I want my program to display a simple loading screen while loading the data from the files in the background. I originally was going to use multithreading but when I tried, the screen wouldn’t display anything ( I think I heard somewhere that multi threading doesn’t work well with opengl), so I’m not sure how else I could implement this. Anyone have any idea?

  1. How is multi-threading related to the loading screen ? Could you please explain that to me ? ; )

  2. Obviously, if your program loads a bunch of stuff, you use functions to load those files, so the easiest solution would be to make another class called Loader or something, Give it properties of:

    • How many files do you load at all
    • Which file is being loaded
      And calculate the percentage, with the knowledge of percentage, you can draw with OpenGL a nice LoadingBar (which could another class) that has a defined height and width, and it fills the specified percentage.

I’d just initialize the Rendering context, draw something, flip buffers and start loading files. That would give a static loading Screen. If you want some Kind of Progress-indicator you’d have to draw and flip buffers from time to time - maybe from within the load-function when a file was read. Simple as that.
Things get complicated if you actually want a progress-bar as knowing in advance how much percent of the work has been done yet is something that even Windows doesn’t get right if it doesn’t count things before starting.

The reason I was thinking of multi-threading was because I was going to have the program load the files and then have a separate thread actually display the loading screen while the files were being loaded. With your suggestion, would I update the screen after every file loaded? What I mean would it be like:


for ( int i = 0; i < numFiles; i++)
{
      LoadFile(i);
      float Percent = CalculatePercentage();
      DisplayLoadingBar( Percent );
}

It can be every load or every ten loads, that depends on You. That code sure gets the job done and the message across, but I wouldn’t make such a loop, or if You were to make something like this, it should probably also have a separate class, or place it in the FileLoader.

(Ok, I was thinking how else could you do it, and probably there is a better way, I just am unable to think of anything right now ; ) Also CalculatePercentage seems pretty funny,

float Percent = numFiles / filesNumber

would be better : P)

ok, thanks. I think the way I tried to do this before posting this thread was just overcomplicating things. Also, I won’t implement it exactly like the loop I posted. I wrote it like that there mostly for concept. Once again, thanks for the help.

No problem, good to help people. It’s actually funny that I helped, cause I never look at the forum, well, You’re Welcome.

Threading works fine, but you have to remember that the current context and rendering target are per-thread state, and you can’t make a context current in multiple threads simultaneously. The underlying windowing system and/or GUI toolkit may impose its own limitations, e.g. a windows HDC shouldn’t be used from multiple threads concurrently.

You can even use OpenGL from multiple threads, provided that each thread has a separate context and any concurrency restrictions on windows, device contexts etc are observed. However, this won’t necessarily improve performance and may even make it worse.

But it’s simpler if you do all of your rendering, window management, event processing etc from the main thread and only use the additional thread(s) for manipulating data. If you need to do any kind of processing on the data (e.g. decompression), using multiple threads may improve performance.

For a single-threaded approach, I’d call an update function after each load, or even at multiple points within a load if this can take a non-trivial amount of time. The update function would check the current time, and update the loading screen if more than a given interval has elapsed since the last update. The time check avoids slowing down loading by updating the loading screen too frequently.

I also use multi-thread successfully but the main thread is the main rendering context, when I load, I start another thread that do the actual map loading. The loading thread create its own context shared with the main, you can create mostly anything VBO, IBO, texture etc, but remember VertexArray cannot, they need to be created on the main thread when your map is loaded.