aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_drawmap.c
blob: 97ed6b047e66e33cfabda71f36ff3d82e13e51fc (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* ************************************************************************** */
/*                                                          LE - /            */
/*                                                              /             */
/*   ft_drawmap.c                                     .::    .:/ .      .::   */
/*                                                 +:+:+   +:    +:  +:+:+    */
/*   By: rbousset <marvin@le-101.fr>                +:+   +:    +:    +:+     */
/*                                                 #+#   #+    #+    #+#      */
/*   Created: 2020/02/02 17:19:18 by rbousset     #+#   ##    ##    #+#       */
/*   Updated: 2020/02/02 17:19:19 by rbousset    ###    #+. /#+    ###.fr     */
/*                                                         /                  */
/*                                                        /                   */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <mlx.h>
#include <stdint.h>

static void
	ft_draw_core_map(char **map, t_cub *clist)
{
	const uint8_t	scale = clist->scale;
	size_t			x;
	size_t			y;

	x = 0;
	y = 0;
	while (map[y])
	{
		while (map[y][x])
		{
			if (map[y][x] == '1')
				ft_drawsquare(scale + (x * (scale + 1)),
								scale + (y * (scale + 1)), 0x00aa99aa, clist);
			else if (map[y][x] == '2')
				ft_drawsquare(scale + (x * (scale + 1)),
								scale + (y * (scale + 1)), 0x0033ccff, clist);
			else
				ft_drawsquare(scale + (x * (scale + 1)),
					scale + (y * (scale + 1)),
					ft_rgb_to_hex(clist->f_rgb), clist);
			x++;
		}
		x = 0;
		y++;
	}
}

void	ft_drawline(t_cub *clist, int startX, int startY)
{
	int a;
	int b;
	int p;
	int x;
	int y;

	x = startX;
	y = startY;
	while (x > 25 * clist->scale)
	{
		a = 2 * (y - 1 * clist->scale);
		b = a - (2 * (x - 25 * clist->scale));
		p = a - (x - 25 * clist->scale);
		if (p < 0)
		{
			x--;
			*(int*)(clist->img.ptr + (x * 4 + (y * clist->img.sizeline))) = 0x00ff0000;
			p += a;
		}
		else if (p >= 0)
		{
			y--;
			*(int*)(clist->img.ptr + (x * 4 + (y * clist->img.sizeline))) = 0x00ff0000;
			p += b;
		}
		x--;
	}
}
static void
	ft_draw_player(t_player *plist, t_cub *clist)
{
	const float		x = plist->pos_x;
	const float		y = plist->pos_y;
	const uint16_t	scale = clist->scale;

	ft_drawsquare(scale + (x * (scale + 1)),
					scale + (y * (scale + 1)),
					0x009843fa, clist);
	ft_drawline(clist, x * (scale + 1), y * (scale + 1));
}

void
	ft_drawmap(t_cub *clist)
{
	mlx_clear_window(clist->wlist->wlx, clist->wlist->winptr);
	clist->img.img = mlx_new_image(clist->wlist->wlx,
						clist->wlist->x_size, clist->wlist->y_size);
	clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp,
						&clist->img.sizeline, &clist->img.endian);
	ft_draw_core_map(clist->map, clist);
	ft_draw_player(clist->plist, clist);
	mlx_put_image_to_window(clist->wlist->wlx,
						clist->wlist->winptr, clist->img.img, 0, 0);
	mlx_destroy_image(clist->wlist->wlx, clist->img.img);
}