9
« on: June 25, 2011, 03:42:20 pm »
How big is each of your tile objects? 1000000 tiles should only be a few MB at most.
Does each of your map objects contain 1000000 tiles, or are there 1000000 tiles in the world in total? 1 GB of memory is a lot for just a million tiles. I don't know how you managed to get 1KB per tile. Does each tile have its own sf::Image by any chance? It's far better to have an array of sf::Images that can be accessed by the rendering code, and give each tile object an index into that array.
I'd split the world/maps up into smaller chunks - for example, in my game, I have a (practically) infinite world, which is split into 32x32 chunks. Only the chunks near the player's location need to be loaded, so the rest of them can be saved to a file and removed from RAM.
You could probably use bigger chunks than that (the reason mine are that size is that each chunk is rendered at once, and my graphics card can't cope with chunks much larger). Each tile is 4 bytes, so a chunk is 4 KB. This means I can load enough chunks around the player to make the world seamless without using much memory. Loading chunks from files is fast, too, as there isn't much data that needs to be loaded.
Then, separate from the chunk objects, I have a fixed number of chunk renderer objects. All visible chunks are attached to one of those; when the player moves far enough that the visible chunks change, I detach the renderers and reattach them to the new visible chunks. That way, only the chunks right next to the player need to have any sort of rendering data; further-away chunks only need to store basic tile data (i.e. only take 4 KB per chunk).
I'm using straight OpenGL for rendering, but I'd imagine a good strategy to use in SFML would be to render each chunk to a single sf::Image (or if you're using layers, one sf::Image per layer). Then, you only need one sf::Sprite per chunk, saving rendering time (though it would be more memory-expensive, as you'll need a large sf::Image per chunk). When a chunk is no longer visible, you can delete its image. I know that's how RPG Maker works; each map is rendered to an image rather than being drawn tile-by-tile each frame.