Sign in to follow this  
yewbie

XY Map class

Recommended Posts

yewbie    677
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
[code]
#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
[/code]

Map.cpp
[code]
#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;
}
[/code]

Share this post


Link to post
Share on other sites
xEricx    572
[quote name='yewbie' timestamp='1299776429' post='4784050']
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
[code]
#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
[/code]

Map.cpp
[code]
#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;
}
[/code]
[/quote]

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 this post


Link to post
Share on other sites
yewbie    677
[quote name='xEricx' timestamp='1299777117' post='4784054']
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
[/quote]

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this