-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
164 lines (153 loc) · 4.81 KB
/
main.c
File metadata and controls
164 lines (153 loc) · 4.81 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dasalaza <dasalaza@student.42barcelona.c> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 18:48:54 by dasalaza #+# #+# */
/* Updated: 2025/09/12 16:41:15 by anamedin ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3D.h"
#include "mlx/mlx.h"
#include "srcs/parser/parser_file_loader.h"
#include "srcs/render/render_hud.h"
#include "srcs/render/render_floor.h"
#include "srcs/parser/parser_map_validator.h"
#include "srcs/init_game/error_handler.h"
#include "srcs/input/input_handler.h"
#include "srcs/init_game/init_game.h"
#include "srcs/game/game_player.h"
#include "srcs/file_loader/check_file_map.h"
/**
* Updates the game world state:
*
* Processes player input (keybinds).
* Manages timing and delays if bonus mode is enabled.
* Updates player motion and sprite rendering.
* Refreshes pathfinding logic for enemies/objects.
*/
void update_world(t_game *game)
{
update_keybinds(game);
if (game->bonus)
{
get_delay(true, FPS_BONUS, !LINUX);
while (game->delay > FPS_BONUS)
{
update_motion(&(game->player), game);
draw_sprites(game, true);
game->time++;
game->delay -= FPS_BONUS;
}
}
update_motion(&(game->player), game);
}
/**
* Renders one frame of the game:
* Otherwise, updates the game, draws floor, skybox, walls (raycasting),
* sprites, HUD, cursor, and hand.
* Handles saving mode (--save) and frame timing for bonus mode.
* Syncs with the display before returning.
*/
int render_next_frame(t_game *game)
{
if (game->player.health <= 0)
draw_death(game);
else
{
update_world(game);
draw_floor(game);
if (!game->bmp)
draw_skybox(game);
ray(game, &(game->img));
draw_sprites(game, false);
if (!game->bmp)
{
mlx_put_image_to_window(game->mlx, game->win, game->img.img, 0, 0);
draw_cursor(game);
draw_hud(game);
render_hand(game);
}
if (game->bonus)
game->delay += get_delay(false, FPS_BONUS, !LINUX);
game->time++;
}
mlx_do_sync(game->mlx);
return (0);
}
/**
* Prepares rendering resources:
* Sets the field of view and camera width.
* Creates the main rendering image.
* Configures render distance and lighting based on bonus mode and assets.
* Allocates the depth buffer.
* Initializes the blur effect for rendering.
* @param game
*/
void setup_render(t_game *game)
{
t_shape shape;
game->fov = (double)game->resy / (double)game->resx;
game->cam_width = (1 / (cos(atan2(-0.5, game->fov))));
game->img = make_image(game->mlx, game->resx, game->resy);
if (!game->bonus || ((!game->floor.img && game->bottom_color != 0) \
|| (!game->skybox.img && game->top_color != 0)))
{
game->should_dim = false;
game->render_distance = 50;
}
game->depth = malloc(sizeof(double) * game->resx);
if (!game->depth)
handle_error(game, "Failed to allocate depth buffer.", NULL);
shape.width = 210;
shape.height = 150;
shape.x = 20;
shape.y = game->resy - shape.height - 45;
game->blur = make_blur_struct(game->mlx, game->img, shape);
}
/**
* Loads program arguments and configuration:
* Validates command-line arguments (expects .cub file and optional --save).
* Enables saving mode if --save is specified.
* Verifies file extension and loads map configuration.
* Raises errors for invalid input.
*/
void load_args(int argc, char **argv, t_game *game)
{
if (argc == 2)
{
if (!check_iscub(argv[1]))
handle_error(game, ERROR_FILE_TYPE, argv[1]);
load_f(argv[1], game);
}
else
handle_error(game, ERROR_USAGE_CUB, NULL);
}
/**
* Loads arguments and validates configuration.
* Prepares rendering and checks save mode.
* Creates the game window, starts ambient sound if available.
*/
int main(int argc, char **argv)
{
t_game game;
init_default(&game);
set_bonus(&(game.bonus));
game.mlx = mlx_init();
if (!game.mlx)
handle_error(&game, FAIL_MLX, NULL);
load_args(argc, argv, &game);
check_define(&game);
setup_render(&game);
game.win = mlx_new_window(game.mlx, game.resx, game.resy, TITLE_WINDOWS);
if (game.sounds.ambient)
play_sound_alt(game.sounds.ambient, true, true);
mlx_hook(game.win, 2, 1L << 0, key_press, &game);
mlx_hook(game.win, 3, 1L << 1, key_lift, &game);
mlx_hook(game.win, 33, 1L << 17, clean_and_exit_z, &game);
mlx_loop_hook(game.mlx, render_next_frame, &game);
mlx_loop(game.mlx);
return (0);
}