Sign in to follow this  

Tile Maps Help

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi i have created a tile map class but having problems difining my map after instantiating it. For example if i declare somewhere in main CMap *map1 = new CMap("SKYLEVEL1", 1, 2, 2) //last 2 numbers //r width and height of map the problem is when i define what textures go into a map usually you would declare this like int defmap[2][2]={0,0 0,0 }; i don't know how to pass defmap to map1 so the tiles get assigned to it. the class CMap has vector of vectors i.e. vector< vector<int>> m_TileMap; how do i create a function in CMap that takes defMap as a parameter and assigns it to m_TileMap?????

Share this post


Link to post
Share on other sites
I don't really understand your question.
But what I can make out of it is that you want to pass an array as an argument to a function?


class CMap
{
public:
CMap (const char* text, int num, int width, int height, int defmap[2][2]);
};

CMap::CMap (const char* text, int num, int width, int height, int defmap[2][2])
{

}

int main (void)
{
int defmap[2][2] = {{1, 2}, {3, 4}};
CMap* map = new CMap("SKYLEVEL1", 1, 2, 2, defmap);
}







Using this method you can pass around the entire map as just an 'int*'


inline unsigned int access2D (unsigned int x, unsigned int y, unsigned int width)
{
return (y*width)+x;
}

class CMap
{
public:
CMap (const char* text, int num, unsigned int width, unsigned int height, int* defmap);

};

CMap::CMap (const char* text, int num, unsigned int width, unsigned int height, int* defmap)
{
defmap[access2D(1, 1, width)] = 42;
}

int main (void)
{
unsigned int width = 2;
unsigned int height = 2;
int* defmap = new int[width*height];

CMap* map = new CMap("SKYLEVEL1", 1, width, height, defmap);
}



Share this post


Link to post
Share on other sites
the problem is i want to define the map like int defmap[2][2] = {{1, 2}, {3, 4}}; so i can properly assign the textures of the map

how would

unsigned int width = 2;
unsigned int height = 2;
int* defmap = new int[width*height];

CMap* map = new CMap("SKYLEVEL1", 1, width, height, defmap);


know what textures i would want to load i.e with

int defmap[2][2] = {{1, 2}, {3, 4}}; corresponds to which texture sin the array to use?

Share this post


Link to post
Share on other sites
What you should be doing is allocating space for your tile indices as a single-dimensional array, but accessing it multi-dimensionally.

For example, your tile data is integers, so that's easy:

int gettile(int* map, int x, int y)
{
return map[y * map_width + x];
}

void settile(int* map, int x, int y, int new_tile)
{
map[y * map_width + x] = new_tile;
}

void examplefunc(void)
{
// Allocate the map
int map_width = 1000;
int map_height = 1000;
int* map = new int(map_width * map_height);

// get the tile at character position
int x = 100;
int y = 100;
int current_tile = map[y * map_width + x];
// the same as...
current_tile = gettile(map, x, y);

// set a map tile
int new_tile = 6;
map[y * map_width + x] = new_tile;
// the same as...
settile(map, x, y, new_tile);

// Later on, deallocate the map memory
delete [] map;
}



Don't forget to incorporate bounds-checking into your code, the above can easily produce errors if invalid values are passed to the functions.

Share this post


Link to post
Share on other sites
Ok here is my source



#include "CMap.h"

CMap::CMap(string mapName, int level, int images, int w, int h)
{
//Explicit call to the debugger.
if(SHOW_DEBUG)StartConsoleWin(80, 100);

m_MapName=mapName;
m_LevelNum=level;
m_Image.resize(images);
m_Width=w;
m_Height=h;
m_Sprite.resize(m_Width);
m_TileMap.resize(m_Width);

for(int x=0; x<m_Width; x++)
{
for(int y=0; y<m_Height; y++)
{
m_Sprite[x].resize(m_Height);
m_TileMap[x].resize(m_Height);
}
}

AssignMap();
}

CMap::~CMap()
{
ReleaseMap();
}

void CMap::CreateBackgroundTile(int index, TCHAR *path)
{
if(index>=0&&index<(int)m_Image.size())
{
DXULoadImage(&m_Image[index], path, DXUCK_NOCOLOURKEY);
}
}

void CMap::CreateBackgroundTile(int index, TCHAR *path, UCHAR red, UCHAR green, UCHAR blue)
{
if(index>=0&&index<(int)m_Image.size())
{
DXULoadImage(&m_Image[index], path, DXURGB(red, green, blue));
}
}

void CMap::SetTileSize(int sizeX, int sizeY)
{
for(int x=0; x<m_Width; x++)
{
for(int y=0; y<m_Height; y++)
{
DXUSetSprite(&m_Image[(int)m_TileMap[x][y]], &m_Sprite[x][y], x*sizeX, y*sizeY);
}
}
}

void CMap::AssignMap()
{
unsigned int temp[20][30]=
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};

for(int x=0; x<m_Width; x++)
{
for(int y=0; y<m_Height; y++)
{
m_TileMap[x][y]=temp[x][y];
}
}
}

void CMap::RandomMap()
{
//Generate a random map.
for(int x=0; x<m_Width; x++)
{
for(int y=0; y<m_Height; y++)
{
int random=rand()%100;

if(y==0||y==m_Height-1||x==0||x==m_Width-1)
{
m_TileMap[x][y]=TILE_WALL;
}
else
{
if(random<90||(x<3&&y<3))
{
m_TileMap[x][y]=TILE_EMPTY;
}
else
{
m_TileMap[x][y]=TILE_WALL;
}
}
}
}
}

void CMap::MoveMap(float sX, float sY)
{
for(int x=0; x<m_Width; x++)
{
for(int y=0; y<m_Height; y++)
{
DXUTranslateSprite(&m_Sprite[x][y], sX, sY);
}
}
}

void CMap::ScaleMap(float sX, float sY)
{
for(int x=0; x<m_Width; x++)
{
for(int y=0; y<m_Height; y++)
{
DXUScaleSprite(&m_Sprite[x][y], sX, sY);
}
}
}

void CMap::RotateMap(float amount)
{
for(int x=0; x<m_Width; x++)
{
for(int y=0; y<m_Height; y++)
{
DXURotateSprite(&m_Sprite[x][y], (float)((double)(amount*D3DX_PI)));
}
}
}

void CMap::DrawMap()
{
for(int x=0; x<m_Width; x++)
{
for(int y=0; y<m_Height; y++)
{
DXUDrawSprite(&m_Sprite[x][y]);
}
}
}

void CMap::ReleaseMap()
{
for(int i=0; i<(int)m_Image.size(); i++)
{
DXUReleaseImage(&m_Image[i]);
}

for(int x=0; x<m_Width; x++)
{
for(int y=0; y<m_Height; y++)
{
DXUReleaseSprite(&m_Sprite[x][y]);
}
}

m_Image.clear();
m_Sprite.clear();
m_TileMap.clear();
}

void CMap::SetMap(vector< vector<int> > tileMap)
{
m_TileMap=tileMap;
}

int CMap::GetTileWidth()
{
return m_Width;
}

int CMap::GetTileHeight()
{
return m_Height;
}

string CMap::GetMapName()
{
return m_MapName;
}

int CMap::IsLevel()
{
return m_LevelNum;
}






Ok now what i i need is a method in that class that will take this a 2d dimensional array and assign it to the vectors in that class so

i want something like this

int MAP[2][2] = {0,0
0,0};

and then MAP can be passed to map1 which will build the map but how can i do this???

CMap *map1=new CMap("SKYLEVEL1", 1, 2, 2, MAP);

Share this post


Link to post
Share on other sites
Is this what you're looking for?


inline unsigned int access2D (unsigned int x, unsigned int y, unsigned int width)
{
return (y*width)+x;
}

CMap::CMap(string mapName, int level, int images, unsigned int w, unsigned int h, int* defmap)
{
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
m_TileMap[x][y] = defmap[access2D(x, y, w)];
}
}
}

Share this post


Link to post
Share on other sites
Ahh so with that method i could do this

int MAP[5][5]={0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0};

CMap *map1 = new CMap("SKYLEVEl1", 1, 5, 5, &MAP);


will this work with the above code mentioned.

Share this post


Link to post
Share on other sites
Well, no. You'll have to do this:


int tilemap[5*5];

for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
tilemap[access2D(x, y, 5)] = 0;
}
}

CMap* map = new CMap("SKYLEVEL1", 1, 5, 5, tilemap);


Share this post


Link to post
Share on other sites
wouldn't that be harder to allocate what texures i want in my map

i.e using


int MAP1[2][2] = {0,2,
1,0};



much easier this way, is there anyway to do it this way??

Share this post


Link to post
Share on other sites
Yes you can do that if you want, but you still need to treat the multidimensional array as an 'int*' when passing it to functions.


// Use this function when accessing the multidimensional array represented as an 'int*'
inline unsigned int std2D (unsigned int x, unsigned int y, unsigned int width)
{
return (x*width)+y;
}

int tilemap[2][2] = {{1, 2}, {3, 4}};

CMap* map = new CMap("SKYLEVEL1", 1, 2, 2, &tilemap[0][0]);


Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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