cub3d/src/main.c

69 lines
2.6 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/07/17 14:15:04 by kcolin ### ########.fr */
2025-05-05 17:09:11 +02:00
/* */
/* ************************************************************************** */
#include "../libft/includes/libft.h"
2025-06-24 00:17:33 +02:00
#include "../includes/structs.h"
2025-06-06 13:57:31 +02:00
#include "../mlx/mlx.h"
2025-06-04 19:16:59 +02:00
#include "../includes/maputils.h"
2025-07-09 17:08:01 +02:00
#include "../includes/cub3d_consts.h"
2025-07-15 10:33:25 +02:00
#include "../includes/cub3d.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-10 12:04:17 +02:00
mlx_destroy_image(data->mlx, data->mlx_data->img);
data->mlx_data->img = mlx_new_image(data->mlx, 800, 600);
draw_map(data->map, &data->player, data->mlx_data);
2025-07-15 10:33:25 +02:00
mlx_put_image_to_window(data->mlx, data->mlx_win,
data->mlx_data->img, 0, 0);
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);
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",
data.map->error), 1);
data.mlx = mlx_init();
data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d");
2025-06-25 18:08:19 +02:00
data.mlx_data = ft_calloc(sizeof(t_mlx_data), 1);
data.mlx_data->img = mlx_new_image(data.mlx, 800, 600);
2025-07-15 10:33:25 +02:00
data.mlx_data->addr = mlx_get_data_addr(data.mlx_data->img,
&data.mlx_data->bits_per_pixel, &data.mlx_data->line_length,
&data.mlx_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);
mlx_loop(data.mlx);
2025-05-05 17:09:11 +02:00
}