Skip to content

Read_map

Zome edited this page Jul 11, 2020 · 2 revisions

This function registers the bitmap and checks that it is valid.

void		read_map(char *line, int fd, int len, t_map *map)
{
	short	player;
	char	*line_2;
	int	y;
	int	num_sprites;

num_sprites = get_map_dimensions(line, fd, len, map);
charge_map(map);
if ((fd = open(map→file_name, O_RDONLY)) == – 1)
ft_exit_fail(“Error opening file (read_map.c)”);
player = 0;
line_2 = 0;
while (map→map_line— > 0)
{
get_next_line(fd, &line_2);
if (map→map_line > 0)
free(line_2);
}
y = -1;
while (++y <= map→y)
{
fill_map(line_2, y, map, &player);
get_next_line(fd, &line_2);
}
free(line_2);
map→num_sprites = num_sprites;
valid_map(map, (int)map→camera→pos_y, (int)map→camera→pos_x);
}


The get_map_dimensions function counts the rows and columns of the bitmap, checks that it has no invalid elements and registers some of them. See in detail on the parse_map page.

num_sprites = get_map_dimensions(line, fd, len, map);

The charge_map function allocates the bitmap in a int** array and the sprites in a t_sprite* array.

num_sprites = get_map_dimensions(line, fd, len, map);

static void	charge_map(t_map *map)
{
	int	y;

if (!(map→sprite = (t_sprite**)malloc(map→num_sprites *
sizeof(t_sprite*))))
ft_exit_fail(“Couldn’t allocate sprites array”);
if (!(map→map = (int**)malloc((map→y + 1) * sizeof(int*))))
ft_exit_fail(“Couldn’t allocate bitmap rows”);
y = 0;
while (y <= map→y)
{
if (!(map→map[y] = (int*)malloc(map→x * sizeof(int))))
{
free(map→map);
ft_exit_fail(“Couldn’t register bitmap col”);
}
y++;
}
}


The program opens the .cub file once again and calls the get_next_line function in a loop until the bitmap is encountered.

if ((fd = open(map->file_name, O_RDONLY)) == -1)
		ft_exit_fail("Error opening file (read_map.c)");
	line_2 = 0;
	while (map->map_line-- > 0)
	{
		get_next_line(fd, &line_2);
		if (map->map_line > 0)
			free(line_2);
	}

The fill_map function loads each of the bitmap lines to the int** array that had been created previously.

y = -1;
	while (++y <= map->y)
	{
		fill_map(line_2, y, map, &player);
		get_next_line(fd, &line_2);
	}
	free(line_2);

static void	fill_map(char *line, int y, t_map *map, short *player)
{
	int x;
	int it;

it = 0;
x = -1;
while (++x < map→x)
{
if (‘0’ <= line[it] && line[it] <= ‘9’)
map_objects(line[it], x, y, map);
else
{
map→map[y][x] = 0;
if (ft_isalpha(line[it]))
{
if (player == 1)
ft_exit_fail(“Many players”);
map→camera = check_coord(line[it], x, y);
*player = 1;
}
}
it += (line[it] != ‘\0’) ? 1 : 0;
}
free(line);
}


The map_objects function registers the numbers on the bitmap (not the spaces nor the player, which is a letter).
if ('0' <= line[it] && line[it] <= '9')
			map_objects(line[it], x, y, map);

static void	map_objects(char number, int x, int y, t_map *map)
{
	map->map[y][x] = number - '0';
	if (number == '2')
		charge_sprite((double)x, (double)y, --map->num_sprites, 
				map->sprite);
}

The charge_sprites function registers the sprites.
static void	charge_sprite(double x, double y, int num_sprites, 
		t_sprite *sprite)
{
if (!(sprite[num_sprites] = (t_sprite*)malloc(sizeof(t_sprite))))
ft_exit_fail(“Didn’t allocate sprite[] (read_map.c)”);
sprite[num_sprites]→pos_x = x + 0.5;
sprite[num_sprites]→pos_y = y + 0.5;

Back to the fill_map function, the check_coord function registers the initial position of the player and the camera. See in more detail on the Parse_map page.
if (ft_isalpha(line[it]))
			{
				if (*player == 1)
					ft_exit_fail("Many players");
				map->camera = check_coord(line[it], x, y);
				*player = 1;
			}

The player variable controls that this function is only called once, that’s to say, that there is only one player in the bitmap.

The number of sprites is copied to a temporary variable because the map_objects function we saw before uses this variable as a counter and changes its value. Here we restore it.

map->num_sprites = num_sprites;

The valid_map function checks that the map is valid. See in detail on the Parse_map page.

valid_map(map, (int)map->camera->pos_y, (int)map->camera->pos_x);

Clone this wiki locally