mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
79 lines
3 KiB
C
79 lines
3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* main.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
|
|
/* Updated: 2025/07/24 14:31:07 by tchampio ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../libft/includes/libft.h"
|
|
#include "../mlx/mlx.h"
|
|
#include "consts.h"
|
|
#include "map/map_checker.h"
|
|
#include "draw/draw_map.h"
|
|
#include "utils/hooks.h"
|
|
#include "utils/frees.h"
|
|
#include <stdbool.h>
|
|
#include <X11/keysym.h>
|
|
#include <X11/X.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <math.h>
|
|
|
|
void init_player(t_mapdata *mapdata, t_player *player)
|
|
{
|
|
player->health = 100;
|
|
player->x = mapdata->startx;
|
|
player->y = mapdata->starty;
|
|
if (mapdata->map[mapdata->starty][mapdata->startx] == 'N')
|
|
player->yaw = M_PI;
|
|
else if (mapdata->map[mapdata->starty][mapdata->startx] == 'S')
|
|
player->yaw = 0;
|
|
else if (mapdata->map[mapdata->starty][mapdata->startx] == 'E')
|
|
player->yaw = M_PI / 2;
|
|
else if (mapdata->map[mapdata->starty][mapdata->startx] == 'W')
|
|
player->yaw = 3 * M_PI / 2;
|
|
}
|
|
|
|
int game_loop(t_cub3d_data *data)
|
|
{
|
|
mlx_destroy_image(data->mlx, data->img_data->img);
|
|
data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
|
|
draw_map(data->map, &data->player, data->img_data);
|
|
mlx_put_image_to_window(data->mlx, data->mlx_win,
|
|
data->img_data->img, 0, 0);
|
|
mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT);
|
|
return (0);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
t_cub3d_data data;
|
|
|
|
if (argc < 2)
|
|
return (ft_printf("Error: Missing cub3d file\n"), 1);
|
|
data.map = ft_calloc(sizeof(t_mapdata), 1);
|
|
if (!check_cubfile(argv[1], data.map))
|
|
return (ft_printf("Error: Wrong map file. Reason: %s\n",
|
|
data.map->error), free_map(data.map), 1);
|
|
data.mlx = mlx_init();
|
|
if (data.mlx == NULL)
|
|
return (ft_printf("Error: Failed to initalize mlx\n"),
|
|
free_map(data.map), 1);
|
|
data.mlx_win = mlx_new_window(data.mlx, WIDTH, HEIGHT, "Cub3d");
|
|
data.img_data = ft_calloc(sizeof(t_img_data), 1);
|
|
data.img_data->img = mlx_new_image(data.mlx, WIDTH, HEIGHT);
|
|
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);
|
|
init_player(data.map, &(data.player));
|
|
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data);
|
|
mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask,
|
|
keyrelease_handler, &data);
|
|
mlx_loop_hook(data.mlx, game_loop, &data);
|
|
mlx_loop(data.mlx);
|
|
}
|