2025-05-05 17:09:11 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* main.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
2025-07-17 14:11:30 +02:00
|
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
2025-05-05 17:09:11 +02:00
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2025-07-17 14:11:30 +02:00
|
|
|
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
|
2025-07-21 11:50:30 +02:00
|
|
|
/* Updated: 2025/07/21 11:50:30 by kcolin ### ########.fr */
|
2025-05-05 17:09:11 +02:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2025-07-17 14:11:30 +02:00
|
|
|
#include "../libft/includes/libft.h"
|
2025-06-06 13:57:31 +02:00
|
|
|
#include "../mlx/mlx.h"
|
2025-07-17 14:55:04 +02:00
|
|
|
#include "consts.h"
|
2025-07-17 14:33:55 +02:00
|
|
|
#include "map/map_checker.h"
|
2025-07-17 14:19:34 +02:00
|
|
|
#include "draw/draw_map.h"
|
|
|
|
|
#include "utils/hooks.h"
|
2025-07-21 11:29:36 +02:00
|
|
|
#include "utils/frees.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-05-06 09:30:32 +02:00
|
|
|
|
2025-07-07 14:40:37 +02:00
|
|
|
void init_player(t_mapdata *mapdata, t_player *player)
|
|
|
|
|
{
|
|
|
|
|
player->health = 100;
|
|
|
|
|
player->x = mapdata->startx;
|
|
|
|
|
player->y = mapdata->starty;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 17:30:16 +02:00
|
|
|
int game_loop(t_cub3d_data *data)
|
|
|
|
|
{
|
|
|
|
|
data->player.x += data->player.movement.x;
|
|
|
|
|
data->player.y += data->player.movement.y;
|
2025-07-17 14:57:29 +02:00
|
|
|
mlx_destroy_image(data->mlx, data->img_data->img);
|
|
|
|
|
data->img_data->img = mlx_new_image(data->mlx, 800, 600);
|
|
|
|
|
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,
|
2025-07-17 14:57:29 +02:00
|
|
|
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-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);
|
2025-06-30 16:16:45 +02:00
|
|
|
data.map = ft_calloc(sizeof(t_mapdata), 1);
|
2025-06-24 11:51:49 +02:00
|
|
|
if (!check_cubfile(argv[1], data.map))
|
2025-07-15 10:33:25 +02:00
|
|
|
return (ft_printf("Error: Wrong map file. Reason: %s\n",
|
2025-07-21 11:50:30 +02:00
|
|
|
data.map->error), free_map(data.map), 1);
|
2025-06-24 11:51:49 +02:00
|
|
|
data.mlx = mlx_init();
|
2025-07-21 11:29:36 +02:00
|
|
|
if (data.mlx == NULL)
|
|
|
|
|
return (ft_printf("Error: Failed to initalize mlx\n"),
|
|
|
|
|
free_map(data.map), 1);
|
2025-06-24 11:51:49 +02:00
|
|
|
data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d");
|
2025-07-17 14:57:29 +02:00
|
|
|
data.img_data = ft_calloc(sizeof(t_img_data), 1);
|
|
|
|
|
data.img_data->img = mlx_new_image(data.mlx, 800, 600);
|
|
|
|
|
data.img_data->addr = mlx_get_data_addr(data.img_data->img,
|
|
|
|
|
&data.img_data->bits_per_pixel, &data.img_data->line_length,
|
|
|
|
|
&data.img_data->endian);
|
2025-07-07 14:40:37 +02:00
|
|
|
init_player(data.map, &(data.player));
|
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);
|
2025-06-24 11:51:49 +02:00
|
|
|
mlx_loop(data.mlx);
|
2025-05-05 17:09:11 +02:00
|
|
|
}
|