PDA

View Full Version : Loading Screen



Niven07
08-11-2013, 04:02 PM
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?

Decesive
08-11-2013, 05:07 PM
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.

Niven07
08-11-2013, 05:18 PM
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 );
}

Decesive
08-11-2013, 05:35 PM
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)

Niven07
08-11-2013, 05:41 PM
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.

Decesive
08-11-2013, 05:46 PM
No problem, good to help people. It's actually funny that I helped, cause I never look at the forum, well, You're Welcome.

GClements
08-11-2013, 06:32 PM
I think I heard somewhere that multi threading doesn't work well with opengl
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.

Sylvain Rochette
08-12-2013, 06:28 PM
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.