I’m implementing a tile engine for games using C++. Currently the game is divided into maps, each map has a 2D grid of sprites where each represents a tile.
I am coding a system where if several maps are adjacents you can walk from one to the other.
At startup of the game, all the maps are instancied but are “unloaded” ie the sprites objects are not in memory. When I’m close enough of an adjacent map, the maps sprites are “loaded” in memory by basically doing:
for(int i=0; i < sizeX; i++) {
for(int j=0; j < sizeY; j++) {
Tile *tile_ptr = new Tile(tileset, tilesId[i][j], i + offsetX, j + offsetY);
tilesMap[i][j] = tile_ptr;
}
}
And they are unloaded by being destroyed the same way when I am too far away from the map.
For a 50×50 map of sprites of 32×32 pixels, it takes me roughly 0.3 secs to load or unload which is done during 1 frame. My question is: what is a more efficient way to load/unload maps dynamically, even using a totally different mechanism? thanks
PS : I’m using SFML as a graphic library but I’m not sure this changes anything
A different possibility to improve latency, but will increase overall number of ops needed:
Instead of waiting when you are ‘too close’ or ‘too far’ from a map, store in memory the maps for a bigger square around the player [i.e. if the map is 50×50, store 150×150], but show only the 50×50. now, every step – calculate the new 150×150 map, it will require 150 destroy ops, and 150 build ops in each step.
By doing so, you will actually need to calculate and build/destroy elements more times! But, latency will improve, since you don’t need to wait 0.3 secs for building 2,500 elements, since you always need a small portion: 150*2 = 300 elements.