Tilemap using spritesheet very slow on device. Any help?

Hi!
I’m doing some tests with spritesheet to make tilemap in a platformer.
The thing is that it looks good on the simulator with scrolling and everything but runs very very slow on device with or without scrolling.

This is how i create the spritesheet :
[lua]local mySheet = sprite.newSpriteSheet( “spritesheet.png”, 32, 32 )
local tileSet = sprite.newSpriteSet( mySheet, 1, 18 )
sprite.add( tileSet, “tile0”, 1, 1, 1, 1 )
sprite.add( tileSet, “tile1”, 2, 1, 1, 1 )
sprite.add( tileSet, “tile2”, 3, 1, 1, 1 )
sprite.add( tileSet, “tile3”, 4, 1, 1, 1 )
sprite.add( tileSet, “tile4”, 5, 1, 1, 1 )
sprite.add( tileSet, “tile5”, 6, 1, 1, 1 )
sprite.add( tileSet, “tile6”, 7, 1, 1, 1 )
sprite.add( tileSet, “tile7”, 8, 1, 1, 1 )
sprite.add( tileSet, “tile8”, 9, 1, 1, 1 )
sprite.add( tileSet, “tile9”, 10, 1, 1, 1 )
sprite.add( tileSet, “tile10”, 11, 1, 1, 1 )
sprite.add( tileSet, “tile11”, 12, 1, 1, 1 )
sprite.add( tileSet, “tile12”, 13, 1, 1, 1 )
sprite.add( tileSet, “tile13”, 14, 1, 1, 1 )
sprite.add( tileSet, “tile14”, 15, 1, 1, 1 )
sprite.add( tileSet, “tile15”, 16, 1, 1, 1 )
sprite.add( tileSet, “tile16”, 17, 1, 1, 1 )
sprite.add( tileSet, “tile17”, 18, 1, 1, 1 )[/lua]

This is how I create the tilemap (using a 2D array):
[lua]local function buildMap()
local xTile = 0
local yTile = 0
for mapy=1, mapHeight do
for mapx=1, mapWidth do
local tile = sprite.newSprite( tileSet )
tile:prepare( “tile” …map[mapy][mapx] )
tile.xReference = -tile.width / 2
tile.yReference = -tile.height / 2
tile.x = xTile
tile.y = yTile
world:insert( tile )
xTile = xTile + tileStep
end
xTile = 0
yTile = yTile + tileStep
end
end[/lua]

Any idea how to optimize or this is a known issue?

Thanks [import]uid: 25327 topic_id: 8214 reply_id: 308214[/import]

refer to this thread
http://developer.anscamobile.com/forum/2011/01/29/object-culling-render-process-when-not-content-area [import]uid: 12108 topic_id: 8214 reply_id: 29305[/import]

yes p120ph37’s method there works well. it’s not plotting the whole map, just enough sprites to show it on screen and changing the sprite frame according to position [import]uid: 6645 topic_id: 8214 reply_id: 29316[/import]

Since you are making a platformer you probably have elements with physics properties. I would suggest instead of applying those properties to the tiles directly, have a second layer over the tiles with physics bodies. That way you use his optimization for the visual tiles.

For example, the ground doesn’t need to be a whole bunch of little physics bodies tiled together, it can just be one long invisible rectangle stretched across the level. I don’t know off the top of my head if that is faster, but I’m guessing it is so it’s worth testing. [import]uid: 12108 topic_id: 8214 reply_id: 29318[/import]

Yeah that’s what I’m doing. I’m trying to make the less physics objects possible then I apply the tilemap.
I’ve started to play around with the “chunk” method. Looks promising but very difficult to master with my poor programming skills… [import]uid: 25327 topic_id: 8214 reply_id: 29319[/import]

Could you try to explain me the quad and chunk methods with simple exemples?
The one in the thread you are refering is a bit much for me to swallow as i’ve started to code 2 month ago…
Thanks in advance :slight_smile: [import]uid: 25327 topic_id: 8214 reply_id: 29320[/import]

Honestly p120ph37’s method is probably easier to wrap your head around than mine (and since it also pushed more tiles that makes it pretty much superior in every way.) There’s a more direct mapping between the position of tiles in the array and their position on screen.

If you can’t work out how to use his code then I’m afraid you may be left hoping that Graham is implementing that technique in Lime. Despite being keen on that issue he never commented, so I don’t know if he noticed. [import]uid: 12108 topic_id: 8214 reply_id: 29325[/import]

I tried p120ph37’s method on device… Impressive! Now, I know I won’t leave my computer until I understand his code.
Thanks for your help :slight_smile: [import]uid: 25327 topic_id: 8214 reply_id: 29416[/import]

Hi again!

I have question about this part of the code :

[lua]–randomly populate tile ids (normally this data would be loaded from a file)
for y = 0, map.yTiles - 1 do
for x = 0, map.xTiles - 1 do
map.tiles[#map.tiles + 1] = math.random(171)
end
end[/lua]

What kind of file should be loaded?
I tried to populate from a 2D array but I get weird results :

[lua]for y=1, map.yTiles - 1 do
for x=1, map.xTiles - 1 do
map.tiles[#map.tiles +1] = map.mapping[y][x]
end
end[/lua]

thanks :slight_smile: [import]uid: 25327 topic_id: 8214 reply_id: 29449[/import]

What kind of file should be loaded?

Any kind of file.* Sorry for the cheeky answer, but that part of his code really is totally open-ended. His code makes no assumptions about whether the data comes from a local XML file, local JSON, either streamed over the internet, etc. I haven’t used Tiled but I’m sure that outputs the map into a text file you can parse.

I tried to populate from a 2D array

If you already have the map data in an array then use that array. You’ll have to modify his code a little to use your array instead of his, but looking at the snippet you posted (I hadn’t examined his code closely so I missed that part) I’m actually surprised he didn’t already put the tiles into a 2D array. I guess it’s a little more efficient to look everything up by offset in a 1D array than to first lookup the value in one table and then hit a second table. hm, I wonder if Lua provides functions to join a 2D array into a long 1D array; I know Python has functions for that.

*Well any text file. I don’t think Corona supports loading binary data, and even if it does you don’t want to go there. [import]uid: 12108 topic_id: 8214 reply_id: 29468[/import]

i’m sure it does support loading binary

[lua]flle = io.open(filename, “rb”)
data = file:read("*a")[/lua]

[import]uid: 6645 topic_id: 8214 reply_id: 29501[/import]

oh well that’s good to know. [import]uid: 12108 topic_id: 8214 reply_id: 29508[/import]