-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_2d.c
executable file
·93 lines (84 loc) · 2.33 KB
/
render_2d.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render_2d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgrissen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/14 18:05:07 by hgrissen #+# #+# */
/* Updated: 2021/02/19 14:50:50 by hgrissen ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void my_mlx_pixel_put(t_img *data, int x, int y, int color)
{
char *dst;
if (x < g_prm.w && x >= 0 && y < g_prm.h && y >= 0)
{
dst = data->addr + (y * data->ll + x * (data->bpp / 8));
*(unsigned int*)dst = color;
}
}
void render_map(void)
{
int i;
int j;
int mini_w;
int mini_h;
mini_h = (TILE_SIZE) / MINI_MAP;
mini_w = (TILE_SIZE) / MINI_MAP;
i = 0;
while (i < g_prm.nwlcnt + 2)
{
j = 0;
while (j < g_prm.lnglin + 2)
{
if (g_prm.map[i][j] == '2')
draw_rect((j * mini_w), (i * mini_h), C_SPRITE);
else if (g_prm.map[i][j] == '1')
draw_rect((j * mini_w), (i * mini_h), C_WALL);
else if (g_prm.map[i][j] == '0' || is_player(g_prm.map[i][j]))
draw_rect((j * mini_w), (i * mini_h), C_EMPTY);
j++;
}
i++;
}
}
void draw_player(int x, int y, int clr)
{
float xc;
float yc;
xc = ((x) / TILE_SIZE);
yc = ((y) / TILE_SIZE);
xc *= (TILE_SIZE) / MINI_MAP;
yc *= (TILE_SIZE) / MINI_MAP;
draw_rect(xc, yc, C_PLAYER);
clr = 0;
}
void draw_ray(void)
{
int j;
int i;
g_xc = g_p.x;
g_yc = g_p.y;
j = 0;
g_inc = d2r(60.0f / g_prm.w);
g_str_ang = g_p.rotang - d2r(30);
while (j < g_prm.w)
{
i = 1;
g_str_ang += g_inc;
while (i < g_prm.w * g_prm.h)
{
g_h = i * cos(g_str_ang);
g_v = i * sin(g_str_ang);
if (side_hit(g_xc + g_h, g_yc + g_v) ||
is_corner(g_xc + g_h, g_yc + g_v))
break ;
my_mlx_pixel_put(&g_img, (g_xc + g_h)
/ MINI_MAP, (g_yc + g_v) / MINI_MAP, C_RAY);
i++;
}
j++;
}
}