Delays due to image/spritesheet loads

Hi - I have a rhythm/music-timing based app, where the appearance / disappearance of sprites needs to be timed to a music backing track.

I use separate functions with images with transitions (using time=xxxx) to give me predictable timed moves between images / cut scenes etc.

Initial Problem: when I load all my spritesheets in advance, everything plays in time (with timings working fine on iPad/Phone/Android). However, I get texture memory problems, and so am trying to allocate (newSpriteSheetFromData) and deallocate ([sheet]:dispose() images as I go between functions etc. to keep overall texture memory down.

The new problem is that the spritesheet ‘load’ introduces a minor delay, which bumps out the timing (especially as the process is repeated).

Is there anyway of having a “transition.to” and “newSpriteSheetFromData” in the same function, but both working at the same time (as opposed to load first, then transition?).

Thanks!
Dave. [import]uid: 82595 topic_id: 21267 reply_id: 321267[/import]

I think what you’re going to need to do is asynchronous image loading in a separate thread. Lua does have coroutines which might fit the bill (I’m pretty sure you can use them in Corona) but I haven’t dug deep enough yet into them to know for sure. [import]uid: 115159 topic_id: 21267 reply_id: 84245[/import]

Thanks moopf - I wouldn’t have thought to use this.

I have now dusted off my 'Programming in Lua (2nd Ed.)" manual, and am reading through Chapter 9: Coroutines.

I’ve also tried the following:

print (“Coroutine stuff”)
co = coroutine.create(function() print(“hi”) end)
print (co)
print (coroutine.status(co))
coroutine.resume(co)

… which gives…

Coroutine stuff
thread: 0x5c59a0
suspended
hi

So I’m hoping I can create all the coroutines upfront, and then ‘resume’ them in the necessary functions. I’ll update here once I’ve tried this.
[import]uid: 82595 topic_id: 21267 reply_id: 84250[/import]

No problem, do update this thread with how you get on. I know coroutines aren’t really threading but I’ll be really interested to know if it works well for this kind of background asset loading. [import]uid: 115159 topic_id: 21267 reply_id: 84252[/import]

It’s pretty much working now. The sync is now spot on with iPad / iPhone. I’m having some lag issues still with Android, but I’m looking into these.

Basically, I have created the coroutines upfront - as they don’t take any resource, and it’s easy for admin to group them together:

co_sheet1 = nil
co_sheet1 = coroutine.create(function() spritesheet1 = sprite.newSpriteSheetFromData(“texture_sheet.png”, sheet_data); end)

Basically, “co_sheet1” is the co-routine ‘wrapper’ for whatever your spritesheet or display.newImage command is. In my example, I have spritesheetfromdata, as my spritesheet has irregular sprites in it.

Then within the main body of the code…
(note: please be aware that I am relying heavily on transition timings for jumping between functions for my time/rhythm-based game (you may approach this differently):

function verse1()

transition.to([time_placeholder, {time=10000, onComplete=verse2}) --this controls the time between each ‘verse’

transition([image], {time=100, onComplete=coroutine.resume(co_sheet1)}) --this loads up the spritesheet in the background, partway through the overall transition.

end

function verse2()

verse2_image=sprite.newSprite(sprite.newSpriteSet (sheet1,1,1)) --display a new image from the pre-allocated spritesheet, with no delay

end


I have embedded the coroutine.resume statements within a ‘parallel’ transition, as I found this the least disruptive. When I included them as ‘stand-alone’ statements, there would be ‘lag’ betwen the different transition statements (or so it seemed). Or, it would interfere with the smoothness / execution of other transitions in the function.

Anyway - hope this has helped. It has certainly helped me. I’ve got almost 4 minutes worth of sync’d functions using time=nnnn within transitions, and it’s now all (pretty much) in sync to within 1/10ths of a second!!! [import]uid: 82595 topic_id: 21267 reply_id: 84813[/import]

moopf - just thought I would update you… my app went live today (iOS) and runs absolutely perfectly. All the coroutines ensure that I’m loading / disposing spritesheets behind the scenes without causing any slow-down.

On Android, it’s still a bit jumpy (but still in time), so I’m still looking at that, and have deferred our Android launch for now.

Dave. [import]uid: 82595 topic_id: 21267 reply_id: 92173[/import]