TMXTileMap drop in FPS when loading. Will loading next tilemap with Multithreading work?

I’ve created a platformer where the player is running through a randomly generated level.
The level chunks are created in Tiled and loaded from .tmx files.

All of the level chunks use the same tileset and image for that tileset.

the tiles are 32x32

The level chunks are very small, e.i. 25x25

When the player gets near the end of the run, a new random level chunk is added to the end. Old level chunks are deleted when the player passes them.

The problem is that I get a huge drop in FPS every time a new level chunk is created and added to the end of the run.

I think it may be due to the fact that a TMXTiledMap creates a new texture atlas everytime the a new chunk loads? Shouldnt the texture atlas be reused since it is the exact same tile set?

this is what is written everytime a new tileset is created:

Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [526] to [702].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [702] to [937].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [937] to [1250].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [1250] to [1668].

So my console window looks something like this as I run through the level and chunks are added to the end:

Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [526] to [702].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [702] to [937].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [937] to [1250].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [1250] to [1668].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [526] to [702].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [702] to [937].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [937] to [1250].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [1250] to [1668].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [526] to [702].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [702] to [937].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [937] to [1250].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [1250] to [1668].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [526] to [702].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [702] to [937].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [937] to [1250].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [1250] to [1668].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [526] to [702].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [702] to [937].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [937] to [1250].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [1250] to [1668].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [526] to [702].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [702] to [937].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [937] to [1250].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [1250] to [1668].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [526] to [702].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [702] to [937].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [937] to [1250].
Cocos2d: cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from [1250] to [1668].

e.t.c....

Any ideas on how to optimize this would be extremely appreciated.

I think i’m going to load all the TMX files in advance and reuse them as needed. I just have one question that hopefully someone will be able to answer…

All my TMX files use the same tile set. Will that image be loaded into memory for every object, or will the image only be loaded once and all the tile maps will reference the same image?

Preloading the TMX files proved to be too heavy. There are around 300 tilemaps to load. I’ll have to find another solution. Maybe multithreading to load the next chunk ahead of time.

I know that cocos2d-x is not 100% compatible with multithreading.

Does anyone think that i’d be able to load the next .tmx tilemap via another thread while the game is running. Then once the tilemap is loaded, place it at the end of the run and join the thread?
Are there restrictions with this? Would it be compatible with iphone and android?

Thanks.

found the article that says that you cannot call retain() release() or autoreleas() from a thread. http://www.cocos2d-x.org/projects/cocos2d-x/wiki/How_to_use_pthread

I guess this blows the idea of multithreading tilemaps out of the water.

I’m sure i’m not the only person who loads his tilemaps on the fly. Anyone have other ideas?

I have the same problem. Not found a good solution yet.

We found a way to solve the drop in FPS when loading the map file on the fly. I find the solution is cleaner that multithreading.

We ended up fixing the drop in FPS by binarizing the loaded CCTMXMapInfo to file.
Then we dump the memory directly from binary file instead of parsing a .tmx file.

Now huge tilemap files can be loaded on the fly without noticing any glitch or stutter whatsoever and is it’s cleaner and less bug prone than multithreading would have been.

Now I’m facing with the loading large map TMX trouble and couldn’t find any solution.
@Marquisd Can you share your sample with me, please!