Large Matrix of Animated Tiles
From ShiVa Wiki
Anybody wanting to write large tile games (minesweeper, tetris, bejewelled etc) no longer needs to worry about low frame rates on low end devices. Here is a solution that allows a large matrix of tiles to be animated with just 2 drawcalls!
Every tile can have its texture changed very quickly at runtime.
Textured Quads are created at runtime
local hQuad = scene.createRuntimeObject ( hScene, "QUAD" )
on a grid measuring
this.nWidth ( nW ) this.nHeight ( nH )
and then being parented to a shared root object:
object.setParent ( hQuad, hGroup, true )
The group is then being combined to just one object and effectively being transformed into a new object, so that the old group can be removed:
local hCombined = scene.combineRuntimeObjectsGroup ( hScene, hGroup ) scene.destroyRuntimeObject ( hScene, hGroup )
Quads are textured with an atlas that combines all textures into one big image. Animation is achieved by changing the image shown on a quad - since we are dealing with an atlas though, we do not switch images, we merely change the part of the image that is shown to the user by adjusting
Depending on the size of the atlas, we then pick one random image for all quads as described in AI_Main.RandomCells ( ) or for the quads successively as in AI_Main.SetQuadUV.
In order to write to the mesh subset vertex buffer, you have to lock and unlock it every time:
mesh.lockSubsetVertexBuffer ( hMesh, 0, mesh.kLockModeWrite ) --some code mesh.unlockSubsetVertexBuffer ( hMesh, 0 )
Pick apart the STE to use in your own megatile creations.
- The ShiVaTech-hosted package can be found here: shivaengine.com > ColorTiles.ste
- The latest user-hosted STE is available here: silicondroid.com > ColorTiles.ste
Click on the image to play the demo!
The demo starts in serial tile-change mode, one tile has its texture changed every frame starting from bottom left. Gain focus and press space to toggle into parallel switch mode, which re-textures all 2500 tiles at a rate of 20Hz.
Credits and Further Reading
Big Thanks to Forum User Fraser!
Link To The Original Forum Thread