-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
100 lines (92 loc) · 2.92 KB
/
Copy pathmain.c
File metadata and controls
100 lines (92 loc) · 2.92 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maakhan <maakhan@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/04 18:02:12 by rcreer #+# #+# */
/* Updated: 2025/03/13 17:54:28 by maakhan ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int rgb_to_int(int *color)
{
return ((color[0] << 16) | (color[1] << 8) | color[2]);
}
void cub_spawner(t_cub *cub)
{
cub->connection = NULL;
cub->img.img = NULL;
cub->img.addr = NULL;
cub->north = NULL;
cub->south = NULL;
cub->east = NULL;
cub->west = NULL;
}
void cub_init(t_cub *cub, t_parsing **parse)
{
cub_spawner(cub);
cub->parse = *parse;
cub->connection = mlx_init();
cub->win = mlx_new_window(cub->connection, SCREENWIDTH,
SCREENHEIGHT, "cub3D");
cub->img.img = mlx_new_image(cub->connection, SCREENWIDTH,
SCREENHEIGHT);
cub->img.addr = mlx_get_data_addr(cub->img.img, &cub->img.bits_per_pixel,
&cub->img.line_length, &cub->img.endian);
cub->direc = (*parse)->player;
cub->map = (*parse)->map;
init_direction(cub, cub->direc);
cub->pos_x = (*parse)->x_pos + 0.5;
cub->pos_y = (*parse)->y_pos + 0.5;
cub->floor = rgb_to_int((*parse)->rgb[0]);
cub->ceil = rgb_to_int((*parse)->rgb[1]);
init_textures(cub, (*parse));
}
int cub_rendering(t_cub *cub)
{
int x;
int y;
x = 0;
y = 0;
while (x < SCREENWIDTH)
{
perform_ray_casting(x, cub);
perform_dda(cub, cub->parse);
if (cub->side == 0)
cub->perp_wall_dist = (cub->side_dist_x - cub->delta_dist_x);
else
cub->perp_wall_dist = (cub->side_dist_y - cub->delta_dist_y);
get_drawing_coords(cub);
draw_strip(x, cub->draw_start, cub->draw_end, cub);
x++;
}
mlx_put_image_to_window(cub->connection, cub->win, cub->img.img, 0, 0);
return (1);
}
int main(int ac, char **av)
{
t_cub *cub;
t_parsing *parse;
if (ac == 2)
{
if (!valid_cub_file(av[1]))
return (EXIT_FAILURE);
if (!parse_init_save(&parse, av))
return (free_data(parse), EXIT_FAILURE);
cub = (t_cub *)malloc(sizeof(t_cub));
if (!cub)
{
free_data(parse);
exit(error_msg(ERR_MALLOC));
}
cub_init(cub, &parse);
mlx_hook(cub->win, ON_KEY_UP, MASK_KEY_UP, handle_key_event, cub);
mlx_hook(cub->win, ON_DESTROY, MASK_ON_DESTROY, handle_closing, cub);
mlx_loop_hook(cub->connection, cub_rendering, cub);
mlx_loop(cub->connection);
}
else
exit(error_msg(ERR_SYS_ARGS));
}