summaryrefslogtreecommitdiff
path: root/map_draw.c
blob: 3c43203ce2dd7c24e20255a1f4b008b50328cbdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   map_draw.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: kdx    <kdx   @student.42angouleme.fr      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/10/10 19:43:45 by kdx               #+#    #+#             */
/*   Updated: 2022/10/12 07:09:49 by kdx              ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "map.h"
#include "sily.h"
#include "game.h"

static char	map_get_grid(t_map *map, int x, int y)
{
	return (map_get(map, x * TSIZE, y * TSIZE));
}

static char	wall_spr(t_map *map, int x, int y)
{
	char	spr;

	spr = 0;
	spr |= (x <= 0 || map_get_grid(map, x - 1, y) == TILE_WALL)
		<< 0;
	spr |= (y <= 0 || map_get_grid(map, x, y - 1) == TILE_WALL)
		<< 1;
	spr |= (x + 1 >= (int)map->width || map_get_grid(map, x + 1, y)
			== TILE_WALL)
		<< 2;
	spr |= (y + 1 >= (int)map->height || map_get_grid(map, x, y + 1)
			== TILE_WALL)
		<< 3;
	return (spr + 'a');
}

void	map_draw_tile(t_sily *sily, t_map *map, int x, int y)
{
	unsigned char	tile;

	x /= TSIZE;
	y /= TSIZE;
	tile = map_get_grid(map, x, y);
	if (tile == TILE_WALL)
		tile = wall_spr(map, x, y);
	sprite_draw(sily, sily->game->s_tiles[tile], x * TSIZE, y * TSIZE);
}

void	map_draw(t_sily *sily, t_map *map)
{
	size_t			x;
	size_t			y;
	unsigned char	tile;

	y = -1;
	while (++y < map->height)
	{
		x = -1;
		while (++x < map->width)
		{
			tile = map_get_grid(map, x, y);
			if (tile == TILE_WALL)
				tile = wall_spr(map, x, y);
			sprite_draw(sily, sily->game->s_tiles[tile], x * TSIZE, y * TSIZE);
		}
	}
}