I want to animate the loading bar while loading assets.
The basic idea:
create the loading bar
load asset 1
interpolate loading bar to 25%
load asset 2
interpolate loading bar to 50%
-repeat
By default each of these steps has to be in a seperate game cycle I think (because otherwise the loading bar will not visually progress).
Also normally these steps have to be in the create method?. - so there will be no rendering at all
Your scene will freezes, there is no way to do so.
You could do just a trick, after each object created update some % value of progress, but no animation can run at that moment.
And how you deal with OpenGL context? @captainflyaway confirm that you asking about say creating 1k Sprites in another thread while animation running? There is no way in cocos2d-x to do so, because it’s not thread safe.
I use an std::vector that contains std::threads that are detached. One of them operates on Sprite Cache to load and unload assets I need to use or are done using. I tend to use whole spritesheets when possible but in some of my games I didn’t. You can’t do what you want in the main Cocos thread without locking the rest of your game.
// 4. We need to run an action every XX seconds that periodically saves the games data
vTimers.push_back(std::thread([]() {
TimerForSavingGameData _timerForSavingGameData;
_timerForSavingGameData.detach();
}));
// 5. We need to run an action every XX seconds that provides notifications to the user
vTimers.push_back(std::thread([]() {
TimerForNotifications _timerForNotifications;
_timerForNotifications.detach();
}));
I have IntroScene and GameScene. In my GameScene in init I have some code like this: for 1 to 10000 Sprite sprite = createSprite("name.png");
And creating this will freeze any animation. So @slackmoehrle howto load GameScene in another thread and got no problems with OpenGL context? Thats is actual question from me and captainflyaway.
OpenGL context is not thread-safe, you can’t call retain(), release(), or create/ addChild() new Node type(which uses GLContext).
So, my question is how you do that? I want to see that full code sample to learn. Ideally a sample project, because I always wanted to to load a whole scene in another thread while animation running. Same for the creating Sprites in background thread.
What does loading resources have anything to do with scene creation related code like init() or addChild()? You shouldn’t be calling them in any thread besides the main thread. There’s a difference between loading resources and actually using those resources to create a scene.
Have you tried calling addSpriteFramesWithFile() in a separate thread in order to cache the graphic resources? Once that thread finishes loading the resources, you can start using them in the main thread (calling init()/addChild() etc to create your scene).
Here’s the order of how it should be done:
1 - Load initial animation (splash screen or anything else you want) in the main thread as you would normally.
2 - Play the spash screen animation.
3 - Spawn a new thread to load the rest of the game resources that you require for that scene (for instance, with addSpriteFramesWithFile(…)). Do not attempt to use the resources yet in creating sprites/nodes in this thread, because it does not belong in the resource loading code.
4 - Once the loading thread has finished, you can now call the scene creation code in the main thread as normal.