• 13
• 15
• 27
• 9
• 9

# XY Map class

This topic is 2564 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In the past I have been using a 2 dimensional array to store my map structure like so:

struct MapInfo
{
int tile;
int blah;
}
MapInfo MyMap[x][y];

I want to try to move this into a class so I can easily pass around a pointer to a specific location to various classes that would need it, I started with this, but I cannot figure out how to insert and access the locations properly, I think it may just be too early for me to wrap my mind around what im doing.

Below is sort the route that I was trying to go, but as you see the way X and Y map locations are added, (and accessed) I can't figure out a way to add them in the correct order to my vector, and then be able to access them correctly by just specifying a x and y location.

I know my logic as below is completely flawed but I figured posting what I was trying to do would make more sense than explaining it, thanks!!

Map.h
 #ifndef MAPCLASS #define MAPCLASS /*------------------------------------------------------------------------------------------------- */ /* this class Deals with our map data */ /* */ /*-------------------------------------------------------------------------------------------------*/ #include <vector> #include <iostream> using namespace std; class MapTile { public: int x; int y; int iCurrentTileGraphic; //Tile in this location int iOriginalTileGraphic; //this was the tile we started with (for animation) int Lighting; //Lighting value associated with this tile int Alpha; //Transparency value bool Blocked; //If this tile is blocked or not }; class MapInfo { private: vector <MapTile> TileLoc; //this stores all of our map info public: MapTile* GetMapLocPtr(int X, int Y); void CreateMap(int Xsize, int Ysize); //Creates a map with X*Y size }; #endif 

Map.cpp
 #include "Map.h" MapTile* MapInfo::GetMapLocPtr(int X, int Y) { int TileNum = X * Y; //get our tile location (problem here) if(TileNum < (int) TileLoc.size() || TileNum < 0) //make sure its in bounds { return &TileLoc.at(TileNum); } else //else this is out of range { cout << "Tried to access a out of range map location: " << X << " - " << Y << " = " << TileNum << endl; } return NULL; } void MapInfo::CreateMap(int Xsize, int Ysize) //Creates a map with X*Y size { int MapSize = Xsize * Ysize; //get our tile location TileLoc.reserve(MapSize +1); //reserve enough memory space for(int x=0;x<Xsize+1;x++) //(problem here) { for(int y=0;y<Ysize+1;y++) { MapTile NewTile; NewTile.Alpha = 0; NewTile.Blocked = false; NewTile.iCurrentTileGraphic = 0; NewTile.iOriginalTileGraphic = 0; NewTile.Lighting = 0; NewTile.x = x; NewTile.y = y; TileLoc.push_back(NewTile); //add to struct } } cout << "Created map with: " << MapSize << " Members" << endl; cout << "TileLoc contains: " << TileLoc.size() << endl; } 

##### Share on other sites

In the past I have been using a 2 dimensional array to store my map structure like so:

struct MapInfo
{
int tile;
int blah;
}
MapInfo MyMap[x][y];

I want to try to move this into a class so I can easily pass around a pointer to a specific location to various classes that would need it, I started with this, but I cannot figure out how to insert and access the locations properly, I think it may just be too early for me to wrap my mind around what im doing.

Below is sort the route that I was trying to go, but as you see the way X and Y map locations are added, (and accessed) I can't figure out a way to add them in the correct order to my vector, and then be able to access them correctly by just specifying a x and y location.

I know my logic as below is completely flawed but I figured posting what I was trying to do would make more sense than explaining it, thanks!!

Map.h
 #ifndef MAPCLASS #define MAPCLASS /*------------------------------------------------------------------------------------------------- */ /* this class Deals with our map data */ /* */ /*-------------------------------------------------------------------------------------------------*/ #include <vector> #include <iostream> using namespace std; class MapTile { public: int x; int y; int iCurrentTileGraphic; //Tile in this location int iOriginalTileGraphic; //this was the tile we started with (for animation) int Lighting; //Lighting value associated with this tile int Alpha; //Transparency value bool Blocked; //If this tile is blocked or not }; class MapInfo { private: vector <MapTile> TileLoc; //this stores all of our map info public: MapTile* GetMapLocPtr(int X, int Y); void CreateMap(int Xsize, int Ysize); //Creates a map with X*Y size }; #endif 

Map.cpp
 #include "Map.h" MapTile* MapInfo::GetMapLocPtr(int X, int Y) { int TileNum = X * Y; //get our tile location (problem here) if(TileNum < (int) TileLoc.size() || TileNum < 0) //make sure its in bounds { return &TileLoc.at(TileNum); } else //else this is out of range { cout << "Tried to access a out of range map location: " << X << " - " << Y << " = " << TileNum << endl; } return NULL; } void MapInfo::CreateMap(int Xsize, int Ysize) //Creates a map with X*Y size { int MapSize = Xsize * Ysize; //get our tile location TileLoc.reserve(MapSize +1); //reserve enough memory space for(int x=0;x<Xsize+1;x++) //(problem here) { for(int y=0;y<Ysize+1;y++) { MapTile NewTile; NewTile.Alpha = 0; NewTile.Blocked = false; NewTile.iCurrentTileGraphic = 0; NewTile.iOriginalTileGraphic = 0; NewTile.Lighting = 0; NewTile.x = x; NewTile.y = y; TileLoc.push_back(NewTile); //add to struct } } cout << "Created map with: " << MapSize << " Members" << endl; cout << "TileLoc contains: " << TileLoc.size() << endl; } 

Part of your problem (i didn't read the entire code) is that your TileNum should be something like X + Width * Y;

Also, when you reserve your array you can then simply index it so when adding new tiles you'd use the same X + Width(Xsize) * Y to access elements of your vector.

Hope this helps

##### Share on other sites

Part of your problem (i didn't read the entire code) is that your TileNum should be something like X + Width * Y;

Also, when you reserve your array you can then simply index it so when adding new tiles you'd use the same X + Width(Xsize) * Y to access elements of your vector.

Hope this helps

Awesome, yeah I think that will defiantly make it work, I just couldn't put it all together, thanks!
I will give this a try here hopefully soon.