Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

2d tile map

This topic is 2131 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

well I am working on an adventure game using c++. I am want to draw text based map such as

XXXXXXXXX

XXXXXXXXX

XXXXXXXXX

XXXXXXXXX

XXXXCXXXX

XXXXXXXXX

XXXXXXXXX

XXXXXXXXX

XXXXXXXXX

 

I am using the code to draw a map without the "C" character.

 

 

for (int i=1; i<=9; i++)

{

for (int j=1; j<=9; j++)

{

cout << "X";

}

cout << endl;

}

 

I am not trying to troll and I know this is a very simple question. I will work on this problem more.

Share this post


Link to post
Share on other sites
Advertisement

Lucky you, I recently wrote exactly what you describe, though for me the letters have a different meaning.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>

#include "map.h"

int TileNum(FILE *fp)
{
	int c = 0;
	char ch = '\0';
	
	while(ch != EOF) 
	{
		ch=fgetc(fp);
		if(ch != '\n' && ch != EOF) // don't increment on EOF and newline
			c++;
	}
	
	rewind(fp);
	
	return c;
}

int GetTileIdentifier(int c)
{
	switch(c)
	{
		case 65:
			return 1;
		break;
		case 66:
			return 2;
		break;
		case 88:
			return 3;
		break;
		case 90:
			return 4;
		break;
		case 58:
			return 3;
		break;
		default:
			return 3;
		break;
		
	}
}

int parse_map()
{
	FILE *fp = fopen("map.txt", "r");
	if(!fp)
		return -1;

	Map = malloc(sizeof(struct _Map));
	if(!Map)
		return -1;
		
	Map->tile_num = TileNum(fp);
	assert(Map->tile_num > 0);
	
	Map->tile = calloc(Map->tile_num, sizeof(struct Tile));
	if(!Map->tile)
		return -1;
	/*	
	for(int j = 0; j < Map->tile_num; j++)
	{
		Map->tile[j] = NULL;
		Map->tile[j] = malloc(sizeof(struct Tile));
		if(!Map->tile[j])
			return -1;
	}
	*/
	
	
	char ch;
	int i = 0;
	int row = 0, col = 0, prev_col = 0, tiles_per_row = 1;
	Map->tp_rows = malloc(1*sizeof(int));
	Map->rows = 1;

	while((ch = fgetc(fp)) != EOF)
	{
		if(ch == '\n')
		{
			LEVEL_WIDTH = TILE_WIDTH * col;
			LEVEL_HEIGHT += TILE_HEIGHT * row;
			row++;
			Map->tp_rows = realloc(Map->tp_rows, ((row+1)*sizeof(int)));
			(Map->rows)++;
			col = 0;
			tiles_per_row = 1;
			continue;
		}
		Map->tp_rows[row] = tiles_per_row;
		Map->tile[i].type = GetTileIdentifier(ch);
		Map->tile[i].x = (col * TILE_WIDTH);
		Map->tile[i].y = (row * TILE_HEIGHT);	
		Map->tile[i].w = TILE_WIDTH;
		Map->tile[i].h = TILE_HEIGHT;
		
		/* 
		 * Since 'A' in the map represents the
		 * player it's also treated as a tile and thus
		 * placed on the map as such but we don't
		 * want that and we manually replace with 'X'
		 * as a non-solid tile. Not exactly the best solution
		 * I guess.
		 */
		switch(Map->tile[i].type)
		{
			case ENUM_PLAYER:
				player.pos.x = (col * TILE_WIDTH);
				player.pos.w = TILE_WIDTH;
				player.pos.h = TILE_HEIGHT;
				player.pos.y = (row * TILE_HEIGHT);
				Map->tile[i].type = ENUM_TILE_AIR;
				Map->tile[i].sY = 0;
				Map->tile[i].sX = 32;		
			break;
			case ENUM_TILE_AIR:
				Map->tile[i].sY = 0;
				Map->tile[i].sX = 32;
			break;
			case ENUM_TILE_DIRT:
				Map->tile[i].sY = 0;
				Map->tile[i].sX = 0;			
			default:
				Map->tile[i].sY = 0;
				Map->tile[i].sX = 0;
			break;
		}
		
		i++;
		tiles_per_row++;
		col++;
	}

	fclose(fp);
	
	return 0;
}

inline int get_tile_index(int row, int col, int tiles_per_row)
{
	return (row * tiles_per_row) + col;
}


Modify as needed.

Edited by farmdve

Share this post


Link to post
Share on other sites

Well, I've tailored my code specifically for me, it doesn't use a 2D array so it's definitely different, but you and anyone else are free to borrow it and use it as you see fit.

 

However, this does not mean my code is bug free, perhaps it may even be bad design, it's even possible that an experienced programmer tells me it's crappy. My code will probably have to be refactored once more objects get added to the map.

Edited by farmdve

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!