Animated Splash Screen while Loading Assets

My resources already loaded in AppDelegate. Its quick. I need to load Scene in another thread. There are many Sprite objects created there. It’s very time consuming and mostly there are just creating of new Nodes.

What you just stated has nothing to do with loading resources. Loading resources has nothing to do with creating instances of cocos2d objects (like sprites/layers/nodes etc).

Have you tried to analyze (and timed) the scene creation method to figure out what exactly is slowing it down? Does that scene creation load resources as well? There shouldn’t be any reason for it to be so slow if all you’re doing is creating objects (assuming you’re not loading new resources). Can you paste code here so we can get a better understanding of how you’re creating your scene? Without code it’s really hard to understand why you’re having such issues.

Thats a huge game with a lot of code and initialization of game objects.

I just want to have a general option - load Scene in another thread.
Example: during gameplay when player and a lot of animations running I want to preload some scene in some moment. Just calling createScene(); will cause freezing for some short time.

I understand what you are trying to do, but what is going on inside the createScene() method? Are you trying to load new resources at all in there?

As you know already, OpenGL is not thread-safe, so you cannot create a new scene in a separate thread (assuming the code uses OpenGL calls). It’s not a limitation of Cocos2d-x, but OpenGL itself. So, with this in mind, it’s a matter of designing around this limitation, which is what needs to be done for any application that uses OpenGL (regardless of which game engine it uses).

Also, do you absolutely have to create the entire scene in the init() method? Is it possible for to create game objects when they’re actually required (which seems like the most reasonable way to handle things in any game engine)?

Well, typical initializations of some game objects, basically Sprite::createWithSpriteFrameName("…");

All res. loaded in AppDelegate.

Yep. I just answered about this but get another opinion about this problem. But I really want to load scene in bg, so I’m waiting for answer from @slackmoehrle as he said just use std:thread Just read topic from the beginning.
I’m not asking this question actually :smiley: I know that it’s can’t be done.

Do you have a rough estimate of how many objects you create in that scene that freezes the game? I’m curious to know.

Yes, 1820 png files, and that not all game, overall + what in other scenes should be about 2000.
However, also some other code in init will add processing time and overall result will be freezing.

Do you need all that files at begging of the game?

That certainly is a lot of objects, and a lot of code that needs to run.

Is there anything in the init() method that is unrelated to creating cocos2d objects? If there is, then that would be a good candidate to move to a separate thread. Still though, are you sure you need to create them all in the init() method? Can you not just create them later?

I’m just making assumptions here because I don’t know what those objects are for or when you require them, so please just ignore this if it’s stuff you have thought of already.

It’s a huge game so some code for sure is unrelated there.

Well, that like workaround and for some cases its possible and for some not. Anyway creating later can cause freezing too.

Finally, the main thing - I’ve already tried a lot and even dealing with OpenGL context, however most stable is just not using any threads :smiley: and again, when I saw answer about just std:thread, I wonder maybe there is an option.

A long time ago at a company I previously worked for, code was introduced where threads were used that had OpenGL calls in them. The product passed weeks of internal testing, external laboratory testing and approval, and when it was finally out in the field, it started failing… that’s how random thread issues can be.

The fact that it didn’t crash during the extensive testing was the bit that confused the developers who implemented that code, and not realizing OpenGL was not thread-safe, they weren’t quite sure what was happening. Took a while to track down the offending bit of code if I recall correctly.

Anyhow, unfortunately std::thread is only an option for code that doesn’t use OpenGL calls, and for your case, if you’ve already tried to refactor the code to break it up into more time efficient sections, then there may be nothing else you can do. :frowning:

Hi, this is a very cool topic.

In cocos2d I would suggest to postpone “real” init. Lets say you have scene1 and scene2. Switch to scene2, but show only “loading” screen. Then schedule update and initialize “real” scene. All the time loading screen in scene2 is visible to the user. When real loading is done remove “loading” screen. You can divide real initialization so that you could update loading image/progress bar or something.

If you want to have fully animated splash while loading scene I think the only (safe!) solution is to use platform specific code. For example in android you can show some animated graphic easily. It should work because it will run in different thread and won’t block cocos thread.

Unfortunately I don’t see any other solutions. And as @R101 mentioned it must be safe.