Jump to content
  • Advertisement
Sign in to follow this  
lodoss118

dynamic tile map?

This topic is 4075 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 created a dynamic map tile, but i am having trouble manually assigning where i want the tiles to be. here is some code?

MapWorld::MapWorld()
{
	
}

MapWorld::MapWorld(TCHAR *name, int w, int h, int images)
{
	mapName=name;
	width=w;
	height=h;
	image.resize(images);

	sprite.resize(width);
	for(int x = 0; x < width; x++ ){
		for(int y = 0; y < height; y++)
		{
			sprite[x].push_back(DXUSPRITE());
		}
	}

	level = new char *[width];

	for(int x = 0; x < width; x++)
	{
		level[x] = new char[height];
	}

	this->assignMap();
}

MapWorld::~MapWorld()
{
	for(int x = 0; x < width; x++)
	{
		delete [] level[x];
	}

	delete [] level;

	releaseMap();
}

TCHAR *MapWorld::getName()
{
	return mapName;
}

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

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

void MapWorld::tileSize(int sizeX, int sizeY)
{
	for(int x = 0; x < width; x++)
	{
		for(int y = 0; y < height; y++)
		{
			DXUSetSprite(&image[level[x][y]], &sprite[x][y], x * sizeX, y * sizeY);	
		}
	}
}

void MapWorld::assignMap()
{
	for(int x=0; x<width; x++){
		for(int y=0; y<height; y++)
		{
			level[x][y] =
				0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,
				0,1,0,1,0,1,0,0,1,0,10,1,0,1,0,1,0,0,1,0;
		}
	}
}

void MapWorld::randomMap()
{
	//generate random map 
	for(int x = 0; x < width; x++)
	{
		for(int y = 0; y < height; y++)
		{
			int random = rand()%100;
			
			if(y == 0 || y == height - 1 || x == 0 || x == width - 1)
			{
				level[x][y] = TILE_WALL;
			}
			else
			{
				if(random < 90 || (x < 3 && y < 3))
				{
					level[x][y] = TILE_EMPTY;
				}
				else
				{
					level[x][y] = TILE_WALL;
				}
			}
		}
	}
}

void MapWorld::moveMap(float sX, float sY)
{
	for(int x = 0; x < width; x++)
	{
		for(int y = 0; y < height; y++)
		{
			DXUTranslateSprite(&sprite[x][y], sX, sY);
		}
	}
}

void MapWorld::scaleMap(float sX, float sY)
{
	for(int x = 0; x < width; x++)
	{
		for(int y = 0; y < height; y++)
		{
			DXUScaleSprite(&sprite[x][y], sX, sY);
		}
	}
}

void MapWorld::rotateMap(int amount)
{
	for(int x = 0; x < width; x++)
	{
		for(int y = 0; y < height; y++)
		{
			DXURotateSprite(&sprite[x][y], amount);
		}
	}
}

void MapWorld::render()
{
	for(int x = 0; x < width; x++)
	{
		for(int y = 0; y < height; y++)
		{
			DXUDrawSprite(&sprite[x][y]);
		}
	}
}

void MapWorld::releaseMap()
{
	for(int i = 0; i < (int)image.size(); i++)
	{
		DXUReleaseImage(&image);
	}

	for(int x = 0; x < width; x++)
	{
		for(int y = 0; y < height; y++)
		{
			DXUReleaseSprite(&sprite[x][y]);
		}
	}
}

void MapWorld::setMap(char **data)
{
	level = data;
}

int MapWorld::tileWidth()
{
	return width;
}

int MapWorld::tileHeight()
{
	return height;
}


Now the random generator works, but in the assign method, where i manually assign the tiles, it doesn't show anything, help me blobby?

Share this post


Link to post
Share on other sites
Advertisement
Your code assigns the value 0 to tile (x,y) for every pair (x,y). Check the definition of the "," (comma) operator in C.

Share this post


Link to post
Share on other sites
Ultimately, you will use something like:

level[x][y] = value(x,y);

Where value(x,y) represents the value you want to give to the array element (x,y). For instance, you can create a temporary array and copy its contents into the level array, so value(x,y) would be temp[x][y].

Share this post


Link to post
Share on other sites

void MapWorld::assignMap()
{
int temp[11][7]= {2,1,2,1,2,1,2,
0,1,0,1,0,1,0,
0,1,0,1,0,1,0,
0,1,0,1,0,1,0,
0,1,0,1,0,1,0,
0,1,0,1,0,1,0,
2,1,2,1,2,1,2};

for(int x=0; x<11; x++){
for(int y=0; y<7; y++)
{
level[x][y]=temp[x][y];
}
}
}




it crashes me game whenever i do this way?

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Your data does not look like an 11x7 array, it's 7x7.


That doesn't matter, does it? He explicitly created an 11x7 array, but filled it only partially. Data will be garbage, but it shouldn't crash. Unless you were talking about the left hand array being to small?

lodoss118, are you sure the level array is of a size equal or greater than 11x7?

EDIT: I need to get way faster...

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!