cub3d/src/main.c

88 lines
3.1 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 #+# #+# */
/* Updated: 2025/08/11 11:03:22 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"
2025-08-06 14:06:15 +02:00
#include "sprites/sprite_caster.h"
#include "utils/inits.h"
2025-08-05 14:17:29 +02:00
#include "sprites/sprite.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-08-11 10:50:59 +02:00
int fps;
char *fps_string;
2025-07-30 16:25:12 +02:00
data->last_tick = get_milliseconds();
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-08-06 14:06:15 +02:00
sprite_caster(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);
data->delta = (get_milliseconds() - data->last_tick);
2025-08-11 10:50:59 +02:00
fps = 1000000.0 / data->delta;
fps_string = ft_itoa(fps);
mlx_string_put(data->mlx, data->mlx_win, WIDTH - 20, 15, 0xFF0000,
fps_string);
2025-08-11 10:50:59 +02:00
free(fps_string);
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);
// FIXME: Delete this whole piece of code when we will init sprites from
// map
data.sprite_list = ft_calloc(sizeof(t_sprite *), 3);
data.sprite_list[0] = ft_calloc(sizeof(t_sprite), 1);
data.sprite_list[1] = ft_calloc(sizeof(t_sprite), 1);
data.sprite_list[0]->x = data.map->startx + 1;
data.sprite_list[0]->y = data.map->starty;
data.sprite_list[0]->image = load_single_texture(&data,
"ressources/box.xpm");
data.sprite_list[1]->x = data.map->startx;
data.sprite_list[1]->y = data.map->starty - 2;
data.sprite_list[1]->image = load_single_texture(&data,
"ressources/test_holed.xpm");
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
}