0

I'm learning Cocos2dx and I'm using tiled map. So, let consider a following piece of code:

 auto map = TMXTiledMap::create("map.tmx");
 auto layer = map->getLayer("Tile Layer 1");   
 auto gid = layer->tileGIDAt(Point(X, Y));

And last line matters to me. I'm confused because I saw implementation of tileGIDAt(Point):

uint32_t TMXLayer::getTileGIDAt(const Vec2& pos, TMXTileFlags* flags/* = nullptr*/)
{
    CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position");
    CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released");

    ssize_t idx = static_cast<int>((pos.x + pos.y * _layerSize.width));
    // Bits on the far end of the 32-bit global tile ID are used for tile flags
    uint32_t tile = _tiles[idx];

    // issue1264, flipped tiles can be changed dynamically
    if (flags) 
    {
        *flags = (TMXTileFlags)(tile & kTMXFlipedAll);
    }

    return (tile & kTMXFlippedMask);
}

So, I am confused because the map seems to be represented as array in memory.

Why is it possible? It matters to me because as you know tiled map may be very large. And what's about map of size: 1000000 tiles x 1000000 tiles. In a result we should take a 1000000^2 elements of Tile, so we need a very long continuous chunk of memory. But it is impossible to get such big chunk of memory ( is it possible?).

Please explain me.

Nishanthi Grashia
  • 9,645
  • 5
  • 41
  • 55
J. Doe
  • 91
  • 6

2 Answers2

2

Don't put all of the map into memory at once. The player is hardly going to be able to see all of it anyway. Instead have just the tile the player is on, plus a certain amount of tiles in all directions around the player.

Then as the player moves, load new tile information from whatever map you have on disk, or generate if you do procedural tiles, replacing the tiles in memory with the new one you load/generate.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
1

According to the MSDN:

The virtual address space for a process is the set of virtual memory addresses that it can use. The address space for each process is private and cannot be accessed by other processes unless it is shared. A virtual address does not represent the actual physical location of an object in memory; instead, the system maintains a page table for each process, which is an internal data structure used to translate virtual addresses into their corresponding physical addresses. Each time a thread references an address, the system translates the virtual address to a physical address.

So there is no problem with allocation of huge arrays, but there still is chance to get into hip memory fragmentation

Community
  • 1
  • 1
VinSmile
  • 706
  • 5
  • 10