-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
86 lines (79 loc) · 2.34 KB
/
main.c
File metadata and controls
86 lines (79 loc) · 2.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aylaaouf <aylaaouf@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/21 05:46:51 by aylaaouf #+# #+# */
/* Updated: 2025/10/30 00:47:38 by aylaaouf ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int init_window_and_image(t_game *game)
{
game->win = mlx_new_window(game->mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3D");
if (!game->win)
{
ft_putendl_fd("Window creation failed", 2);
return (0);
}
game->img = mlx_new_image(game->mlx, SCREEN_WIDTH, SCREEN_HEIGHT);
if (!game->img)
{
ft_putendl_fd("Image buffer creation failed", 2);
return (0);
}
game->img_data = mlx_get_data_addr(game->img, &game->bpp, &game->size_line,
&game->endian);
return (1);
}
int init_graphics(t_game *game)
{
game->mlx = mlx_init();
if (!game->mlx)
{
ft_putendl_fd("MLX init failed", 2);
return (0);
}
if (!init_window_and_image(game))
return (0);
return (1);
}
int init_all_textures(t_game *game)
{
if (!init_textures(game))
{
ft_putendl_fd("Failed to initialize textures", 2);
return (0);
}
return (1);
}
void setup_hooks_and_loop(t_game *game)
{
mlx_loop_hook(game->mlx, render_frame, game);
mlx_hook(game->win, 2, 1L << 0, handle_key_press, game);
mlx_hook(game->win, 3, 1L << 1, handle_key_release, game);
mlx_hook(game->win, 6, 1L << 6, mouse_move, game);
mlx_hook(game->win, 17, 1L << 17, close_game, game);
mlx_loop(game->mlx);
}
int main(int ac, char **av)
{
t_game *game;
if (init_game(ac, av, &game))
return (1);
if (!init_graphics(game))
{
free_game_resources(game);
return (1);
}
if (!init_all_textures(game))
{
free_game_resources(game);
return (1);
}
setup_hooks_and_loop(game);
free_game_resources(game);
return (0);
}