dynamically creating a 2d array

This is a discussion on dynamically creating a 2d array within the C++ Programming forums, part of the General Programming Boards category; hello, i am trying to write a simple tile map editor for my 2d game. i want the user to ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    54

    dynamically creating a 2d array

    hello, i am trying to write a simple tile map editor for my 2d game.

    i want the user to input the number of tiles in the x and y directions for a map.

    i have a map class and a tile class

    i want my map class to contain a member that is a 2d array of objects of the tile class.

    i am having trouble dynamically assigning the size of this array..

    i thought the map class could contain a pointer to a tile object
    Code:
    cTile*m_tile;
    and then later on in the code do something like:


    Code:
    cout<<"x: ";
    cin>>x;
    cout<<"y: ";
    cin>>y;
    
    m_tile = new cTile()[x][y]
    however this gives an error,

    what is the proper way to i achive this?

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
    cTile**m_tile;
    Code:
    m_tile = new cTile[x][y];
    how about using vectors instead?
    Last edited by h_howee; 08-23-2007 at 09:22 AM.

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, my preferred way in this case would be something like that:
    Code:
    std::vector<std::vector<Tile> > tile_map(x, std::vector<Tile>(y));
    With appropriate typedefs this could be made prettier:
    Code:
    typedef std::vector<Tile> TileLine;
    typedef std::vector<TileLine> TileMap;
    
    TileMap tile_map(x, TileLine(y));
    Edit: One post short of a sin
    Last edited by anon; 08-23-2007 at 10:24 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Why does everyone insist on making tiles objects when they are not? Tiles are numbers. Tile maps are paint by the numbers. Any other way will cause you to store every tile in memory regardless if it is identical to any other tile on the map.

    Store the information once, use it multiple times.

    Instead of using a vector which is very good for arrays that can dynamically change size use a linear array like tilemap[mapwidth*mapheight] and store the ID of the tile in the array. Now if you wanted to create layers you could use a vector like so:

    Code:
    class CTileLayer
    {
      protected:
        UINT *m_pMap;
        UINT m_uHeight;
        UINT m_uWidth;
        UINT m_uSize;
    ...
    };
    
    class CTileMap
    {
      protected:
        std::vector<CTileLayer *> m_vLayers;
      public:
        CTileMap();
    
        UINT Add();
        bool Remove(UINT uLayer);
        ...
    };
    Just my two cents from my experiences with tile maps.
    Last edited by VirtualAce; 08-23-2007 at 01:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically creating an array of pointers
    By cs32 in forum C Programming
    Replies: 4
    Last Post: 02-06-2008, 08:42 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 01:01 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. 2d array problem with vc++
    By LiLgirL in forum C++ Programming
    Replies: 10
    Last Post: 03-16-2004, 07:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21