on 28-06-2012 10:03 PM
Hello. I've recently been struggling to draw a scrolling tilemap that takes a specific tile from a tilesheet and scrolls smoothly. Not necessarily a massive map, just functional and simple enough for me to understand how it works (I like to think this is possible). So far I've gotten close to what I want, but there is a slight slowdown when the tiles are scrolled and the code is not the best.
Currently my method is this;
I populate a spritelist with tiles that are created in a pair of for loops (enought to fill the screen) - getting the reference for their source from another list based on a camera's position. This is called only once, not in a regular update. The spritelist is then added to the scene and its position is updated, along with the camera's, when directions are pressed. A simple check for when a row or column of tiles has moved off screen calls the 'createmap' function again, which refreshes the map. This includes emptying the list of its children, setting the new camera position for the tile references, creating sprites, and populating the spritelist with the new tiles. Finally, I jump the spritelist's position by the distance of one tile the opposite way it is scrolling. So the scrolling works kind of like a typewriter
I'm pretty sure this is not the right way to go about things, but don't know how else to do it. I am quite new to programming so any advice or tips on how to improve on this, or rather what I should be doing instead, is very much appreciated.
Solved! Go to Solution.
10-07-2012 11:07 AM - edited 10-07-2012 11:12 AM
You can use a VertexBuffer and fill it with the tiles positions. Then, you can draw it in one draw call. This is very fast, even with lots of tiles (I have tested). Of course, you will need to adjust the map position on screen with matrices & co.
Or you can implement the use of a index array. So you have the VertexBuffer containing all the tiles positions and you have an index buffer (big enougth to contain the maximum visible tiles on screen and a bit more..). Then you have to check if the scroll (when you move) is greater than a tile size, if it is the case, you recalculate the entire index array with the new tiles index (you need to store some data like the x and y position of the first visible tile, the number of tiles who fit on the screen, etc..). you can draw it with one draw call. And only the few tiles visibles will be draw.
These methods are fast. But it is for static tiles, i mean not animated tiles.
on 11-07-2012 06:41 PM
I see. Thank you very much! I think I will try the second option and implement an index array too. Looking at the documentation for VertexBuffers is a bit confusing but I should be able to figure it out.
Since posting last time I've also shifted to rawSpriteTiles/ rawSpriteTileLists which helped a bit. Plus, for anyone else having the same difficulty as me, some tutorials here http://xnagpa.net/rpgtutorials.html have been quite useful.
Thanks again fr_romain1985.
Website ©2013 Sony Computer Entertainment Europe
All content, game titles, trade names and/or trade dress, trademarks, artwork and associated imagery are trademarks and/or copyright material of their respective owners. All rights reserved. [more info]