Jump to content
  • Advertisement
Sign in to follow this  
lodoss118

Tile Maps Help

This topic is 3702 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
Advertisement
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,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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,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);
}

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!