cub3d/src/main.c

64 lines
2.3 KiB
C
Raw Normal View History

2025-05-05 17:09:11 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
2025-05-05 17:09:11 +02:00
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
2025-07-31 14:07:56 +02:00
/* Updated: 2025/07/31 13:57:08 by tchampio ### ########.fr */
2025-05-05 17:09:11 +02:00
/* */
/* ************************************************************************** */
#include "../libft/includes/libft.h"
2025-06-06 13:57:31 +02:00
#include "../mlx/mlx.h"
#include "consts.h"
2025-07-28 13:04:32 +02:00
#include "cub3d_data.h"
#include "player/move.h"
#include "draw/draw_map.h"
2025-07-30 16:25:12 +02:00
#include "raycast/raycaster.h"
#include "renderer/render.h"
#include "raycast/ray.h"
#include "utils/hooks.h"
#include "utils/inits.h"
2025-07-31 14:07:56 +02:00
#include <bits/types/struct_timeval.h>
2025-05-06 13:24:32 +02:00
#include <stdbool.h>
2025-07-09 16:45:13 +02:00
#include <X11/keysym.h>
#include <X11/X.h>
2025-05-06 13:24:32 +02:00
#include <unistd.h>
#include <fcntl.h>
2025-07-31 14:07:56 +02:00
#include "utils/time.h"
2025-07-09 17:30:16 +02:00
int game_loop(t_cub3d_data *data)
{
2025-07-30 16:25:12 +02:00
t_ray ray;
2025-07-31 14:07:56 +02:00
data->delta = (get_milliseconds() - data->last_tick);
mlx_destroy_image(data->mlx, data->img_data->img);
data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
2025-07-30 16:25:12 +02:00
reset_matrix(data);
raycaster(data, &ray);
move_player(data);
2025-07-30 16:25:12 +02:00
matrix_to_image(data);
2025-07-30 16:35:39 +02:00
draw_map(data->map, &data->player, data->img_data);
2025-07-15 10:33:25 +02:00
mlx_put_image_to_window(data->mlx, data->mlx_win,
data->img_data->img, 0, 0);
2025-07-15 10:33:25 +02:00
mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT);
2025-07-31 14:07:56 +02:00
data->last_tick = get_milliseconds();
2025-07-09 17:30:16 +02:00
return (0);
}
2025-05-05 17:09:11 +02:00
int main(int argc, char **argv)
{
2025-07-15 10:33:25 +02:00
t_cub3d_data data;
2025-05-06 12:25:17 +02:00
2025-05-05 17:09:11 +02:00
if (argc < 2)
2025-06-06 18:38:21 +02:00
return (ft_printf("Error: Missing cub3d file\n"), 1);
init_cub3d_data(&data, argv);
2025-07-09 16:45:13 +02:00
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data);
2025-07-15 10:33:25 +02:00
mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask,
keyrelease_handler, &data);
2025-07-09 17:30:16 +02:00
mlx_loop_hook(data.mlx, game_loop, &data);
mlx_loop(data.mlx);
2025-05-05 17:09:11 +02:00
}