Skip to main content
gamedev.net
🔒 Locked

C++ Class with Multi-Dimension Array [Solved]

Started by Chu 6 replies 3.2k views
Original Post
Chu
Chu
I'm working on a sort of Map system. I have a basic enumerator, called STATE which is just the available states of the map tiles (PASS as in you can pass it, BLOCK it's a wall, and SPECIAL meaning it's a door, item, or something 'special'). I then have a basic Tile object, which contains a STATE and X/Y co-ordinates. I want to use a 2 dimensional array of Tile objects to represent the map (As seen in the Map class below), I don't want the size of the map to be known until compile time, so the user (or programmer) could create a map that is 16 x 24 Tiles, or 8 x 4 Tiles, here begins my problem.. How can you define a 2 dimensional 'dynamic' array?
enum STATE
{
	PASS, BLOCK, SPECIAL
};

class Tile
{
    private:
        int mX;
        int mY;
        STATE mCurrentState;
    public:
        STATE getState();
        void setState(STATE state);
        void setX(int x);
        void setY(int y);
        int getX();
        int getY();
};

class Map
{
    private:
        int mWidth;
        int mHeight;
        Tile mMap[][];
    public:
        Map(int width, int height);
};

Basically, I just want to make sure I'm going about this the correct way (I haven't actually put pen to paper yet, I'm designing and making sure it will all work), because I don't wanna start now, get half way and realise I screwed up. (Which I do all too often with projects), and an understanding of 'Dynamic' multi-dimensional arrays would be cool. --- Edit: Thanks guys, this works perfect now. [Edited by - Chu on August 12, 2006 4:33:12 AM]
Ex777
Ex777
or std::list>;

But, list is generaly for things that are constantly changing in size (on a per-frame basis on some occasions), for something that will only change in size, every, once in a while, the std::vector, is a better canidate.
jpetrie
jpetrie
A vector-of-vectors won't neccessarily remain rectangular unless you manually enforce it. In the long run its probably easier to use a vector, but keep it one-dimensional treat it like a 2D array using the usual indexing "trick":

// in the class definition:std::vector< Tile >  mapData;// in the constructor:mapData.resize(mWidth * mHeight);// Now, to access map element at (x,y) use:// mapData[x + y * mWidth]

MaulingMonkey
MaulingMonkey
boost::multi_array< Tile , 2 > mapData;mapData.resize( boost::extents[ mWidth ][ mHeight ] );//to access://   mapData[x][y]//size://   width = mapData.shape()[0]//   height = mapData.shape()[1]
Chu
Chu
Quote:
Original post by jpetrie
A vector-of-vectors won't neccessarily remain rectangular unless you manually enforce it. In the long run its probably easier to use a vector, but keep it one-dimensional treat it like a 2D array using the usual indexing "trick":

// in the class definition:std::vector< Tile >  mapData;// in the constructor:mapData.resize(mWidth * mHeight);// Now, to access map element at (x,y) use:// mapData[x + y * mWidth]

Hey thanks for this! It works, perfectly :)

Now that this works though, I need to create an actual 'Map' for my game to use (At least for the meantime while I'm still developing it). So I have started a sort of Map Editor Tool (In C#, although the rest of the project is in Cpp).

I have this code so far:
[SOURCE]public class TileArray : System.Collections.CollectionBase{    private readonly System.Windows.Forms.Form HostForm;    public System.Windows.Forms.PictureBox AddNewPictureBox()    {        System.Windows.Forms.PictureBox aTile = new System.Windows.Forms.PictureBox();        this.List.Add(aTile);        HostForm.Controls.Add(aTile);        aTile.Tag = this.Count;        aTile.Text = "Tile " + this.Count.ToString();        aTile.Click += new System.EventHandler(ClickHandler);        return aTile;    }    public TileArray(System.Windows.Forms.Form host)    {        HostForm = host;        this.AddNewPictureBox();    }    public System.Windows.Forms.PictureBox this [int Index]    {        get        {            return (System.Windows.Forms.PictureBox) this.List[Index];        }    }    public void Remove()    {        if (this.Count > 0)        {            HostForm.Controls.Remove(this[this.Count -1]);	    this.List.RemoveAt(this.Count -1);        }    }    public void ClickHandler(Object sender, System.EventArgs e)    {        System.Windows.Forms.MessageBox.Show("You have clicked Tile " + ((System.Windows.Forms.PictureBox) sender).Tag.ToString());    }}private TileArray MapTiles;public void BuildMap(int x, int y){    const int TILE_SIZE = 16;    const int START_LOCATION = 8;    int counter = 0;    int locationX = START_LOCATION;    int locationY = START_LOCATION;    int distX = x;    int distY = y;    for (int j = 0; j <= (distY - 1);j++)    {        for (int i = counter; i <= (distX - 1); i++)        {            MapTiles.AddNewPictureBox();            MapTiles.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;            MapTiles.Location = new System.Drawing.Point(locationX, locationY);            MapTiles.Size = new System.Drawing.Size(TILE_SIZE, TILE_SIZE);            MapTiles.Visible = true;            locationX += 15;            counter++;        }    distX = (x * (j + 1));    locationY += 15;    locationX = START_LOCATION;			    }}[/SOURCE]
[/source][/source]

If someone could please tell me what I'm doing wrong!!

It 'works' to a point.. It prints the 'Map' out, like so (Using a map that is 5 x 5 Tiles big):
xxxxxoooooxxxxxxxxxxxxxxx

Where the x's are visible tiles, and the o is apparantly one invisible tile.

If you need any more explanation I will gladly give it, but I honestly need help.
I've been staring at my screen for the past few hours to just ripping my hair out... (I have a lot of hair :p)

---
Edit: This now works too, thanks again.



[Edited by - Chu on August 12, 2006 4:04:45 AM]
rgirard413
rgirard413
doesn't this work if you don't want to use vector...?

type **map;map = new type[number_x];for(int i = 0; i < 10; i++)    map = new type[number_y];


yes its ugly and why wouldn't you want to use vector...
http://www.thedizzle.com
Zahlman
Zahlman
0) Boost::multi_array, like MaulingMonkey said.

1) Read this.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.