Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog
Main Page About Help FAQ Special pages Log in

Large Matrix of Animated Tiles

From ShiVa Wiki

Contents

Introduction

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.

Principle

Creation

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 )

Animation

QuadBatch.png

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

    mesh.setSubsetVertexTexCoord

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 )
 

Source STE

Pick apart the STE to use in your own megatile creations.

Demo

Click on the image to play the demo!

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



Retrieved from "http://www.shivaengine.com/developer/wiki/index.php?title=Large_Matrix_of_Animated_Tiles"

This page has been accessed 5,820 times. This page was last modified on 29 October 2013, at 18:39.