Rendering thread and intense computations

Dear all,
I’m trying to implement a game with various screens (splash, main menu, level select, play a level) using a screen manager.

When it comes to initialize a level, there are some intense calculations that need to be done (this can be further optimized of course but this is not the point).

What I’m trying to achieve is to show a “Loading…” screen before starting the computation, then remove it once the computation is done.
I’m using this approach:

  • set up a screen
  • when I want to load another screen, show a “loading” image
  • remove the current screen
  • set up the new screen (that will evenutally go on top of the loading image)

If I use this code:

local function doIntenseStuff()   
  
 for i=0,30000,1 do  
 print("intense stuff"..i)  
 end  
 local level = display.newImage("level.png")  
 transition.to(level, {y=500, time=1000})  
 loading:removeSelf()  
end  
  
-- load menu and do stupid stuff  
local menu = display.newImage("menu.png")  
for i=0,200,1 do  
 print("menu"..i)  
end  
  
-- load loading screen  
loading = display.newImage("loading.png")  
  
-- change screen!  
menu:removeSelf()  
doIntenseStuff()  

The result of this is having the menu screen, then the application freezes while the doIntenseStuff() is running, and then the level screen is shown. No loading screen is seen in the meanwhile.

If I change the last line of code to

timer.performWithDelay(10,doIntenseStuff)  

then it works: the loading screen is shown, then the application freezes on the doIntenseStuff() and when it unfreezes the level menu is shown.

However, on my real code I do a loads of stuff in the “load menu and do stuff” (buttons, etc.) and I’ve noticed that the performWithDelay does work only when the delay is set to something like 100, if I use 10 it won’t work (i.e. will behave like the doIntenseStuff() is called directly).

Ok, so first of all, is this the right way of achieving this? I must admit it’s not clear to me what’s the treading model of Corona. Does a rendering thread exist in corona?
I’ve read in Corona docs that the rendering is done at 30 fps, does it mean that the rendering occurs 30 times per second if the “main” thread (whatever it means here) is not doing anything (e.g. waiting for user input)? What happen when the “main” thread is executing some computations that take a second or more?

Thanks
Regards [import]uid: 27243 topic_id: 7136 reply_id: 307136[/import]

Hi,

  1. you can set the FPS to 60 if you need that.

  2. If something takes longer than 16/33 milisecs, it will just drop the frame rate.

The question is if you want to display some image animation during the calculation.
For this I would look into LUA coroutines. You can call them and jump out of them when needed. Then go back into a coroutine at the point where you left it. You could use this to halt the calculation, let Corona update its frames and then go back to your calculation.

Cheers
Michael Hartlef

http://www.whiteskygames.com
http://www.twitter.com/mhartlef

[import]uid: 5712 topic_id: 7136 reply_id: 25114[/import]

Thanks, I’ll look into coroutines. They sound cool :slight_smile: [import]uid: 27243 topic_id: 7136 reply_id: 25121[/import]