Loading Sprites on the Fly ??

My game has a lot of sprite sheets that I need to use all in one level and because of the memory requirements I can’t load them all at once. My thought process was maybe I could load a sprite sheet right when it was needed (on the fly). So if I have a walking sprite sheet and someone presses the jump button I could load the jump sprite sheet and unload the walking sprite sheet at that exact moment so that no new memory is used.

This idea seems to work but when I run it on an iOS 4 or an Andriod 2.2 device I can see about a 1 second delay from the sprite sheet being loaded to being displayed. Is this because of the time it takes to load the sprite sheet image from the devices file system? Is this because I am loading the sprite sheet while the game is running? Perhaps I am doing something wrong in code? Am I just S.O.L.?

Please help! Here is the code.

  
require "sprite"  
  
display.setStatusBar(display.HiddenStatusBar)  
  
local idleSheet = sprite.newSpriteSheetFromData( "Idle@2x.png", require("Idle@2x").getSpriteSheetData() )  
  
local spriteSet = sprite.newSpriteSet(idleSheet, 1, 15)  
sprite.add(spriteSet,"Idle",1,15,500,0)  
  
local spriteInstance = sprite.newSprite(spriteSet)  
spriteInstance:setReferencePoint(display.BottomCenterReferencePoint)  
spriteInstance.x = 175  
spriteInstance.y = 350  
  
spriteInstance:prepare("Idle")  
spriteInstance:play()  
  
local function onTap( event )  
 idleSheet:dispose()  
 idleSheet = nil  
  
 local walkSheet = sprite.newSpriteSheetFromData( "Walk@2x.png", require("Walk@2x").getSpriteSheetData() )  
 local spriteSet2 = sprite.newSpriteSet(walkSheet, 1, 13)  
 sprite.add(spriteSet2,"Walk",1,13,300,0)  
  
 spriteInstance = sprite.newSprite(spriteSet2)  
 spriteInstance:setReferencePoint(display.BottomCenterReferencePoint)  
 spriteInstance.x = 175  
 spriteInstance.y = 350  
  
 spriteInstance:prepare("Walk")  
 spriteInstance:play()  
end  
  
spriteInstance:addEventListener ( "tap", onTap )   
  

Thanks
ML [import]uid: 63352 topic_id: 14337 reply_id: 314337[/import]

How big are your PNG files? What memory requirements are you trying to meet?

I have an app where I load over 20 spritesheets at the same time. All of the PNG files are 15K or less so the total is only about 300,000 bytes. I’ve seen several different numbers for how much memory an app can use, but they are all in the millions of bytes.

[import]uid: 67839 topic_id: 14337 reply_id: 52949[/import]

I need to load about 30 1024x1024 RGBA8888 sprite sheets. To my knowledge each sprite sheet takes about 2.4 megs of texture memory. This is why I am trying to dynamically load them on the fly.

Thanks
ML [import]uid: 63352 topic_id: 14337 reply_id: 52954[/import]

Yeah you’re going to run into loading issues for sure with that much data. The best thing is to try to optimize your spritesheets and reuse as many of the frames as possible.
[import]uid: 27183 topic_id: 14337 reply_id: 52982[/import]

There has got to be a way. I’m close with the solution I posted above. I just need to get rid of that 1 second delay. Perhaps the delay is because of something I read in another post that states corona can’t load images during runtime without the stoppage of rendering. I don’t know if this fact but if it is the case perhaps I can do something about it with threads? Does LUA have threads? Does anyone else have games with a large amount of sprites? I can’t be the first!

Thanks
ML [import]uid: 63352 topic_id: 14337 reply_id: 53126[/import]

Is there any way you can pre-load a few sequences? So, if you have a walking sequence and maybe something else like a swimming sequence, can you just pre-load the sprite sheets related to the mode you are in? (Example: pre-load the ‘jump’ or ‘fall’ sheets when in walking mode, and pre-load the ‘dive’ sheet in swimming mode.)

I have pulled off something related to this by pre-loading the next 2 or 3 sprite sheets, and dumping/disposing unrelated sheets.

The 20MB texture limit on older iOS devices is a challenge. You might consider cutting the quality of your sprite sheets by reducing their size and then just scaling up… just for the older iOS devices. (Cut size of sheets to 50%, then scale them up by 200% in the app.) [import]uid: 10818 topic_id: 14337 reply_id: 53134[/import]

I don’t think pre-loading only a few sequences will work in my scenario. I have a lot of options a player can choose from and all of them need to be available instantly upon request.

Any idea why this one second load delay occurs? Is it the Corona rendering engine or simply loading the resource from the file system?

I’m much more concerned about the performance on android than iOS. I’ve read in another thread that because of the VM that is used android 2.2+ only has about 24 megs of texture memory. This is just like developing for the old iPhone.

Thanks
ML [import]uid: 63352 topic_id: 14337 reply_id: 53207[/import]

mathew-

I wish I knew for sure, but I’d just be taking a wild guess. My thinking goes like this…

Texture Memory is designed to be moved around really fast. I think what sets it apart is that it has more “pipelines” between itself and the CPU. Regular memory doesn’t load things in and out as fast. Thus the limit/distinction.

So I think we are talking about a limit in the hardware. You can only juggle so much fast-moving, graphical data in the Texture Memory. Moving Texture Mem between the CPU (your program) and back is fast. Moving normal Mem between the CPU and back is relatively slower. So I’m going to assume that pulling/loading in normal Mem/data/images and sticking that stuff into the Texture Mem area on the fly is what causes the lag… you are essentially trying to substitute Normal Memory for Texture Memory, right?

THIS IS MY FEEBLE UNDERSTANDING. Maybe someone with a little more hardware knowledge can verify or tell me how wrong I am. But it sounded good to my head. :wink:

That said, if it is a matter of finishing your project or abandoning it, I would look at reducing the size of the graphics/sprite sheets. You should be able to scale things up on the fly without the lag. [import]uid: 10818 topic_id: 14337 reply_id: 53274[/import]