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-28 13:04:32 +02:00
|
|
|
/* Updated: 2025/07/28 15:21:17 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-28 13:04:32 +02:00
|
|
|
#include "cub3d_data.h"
|
2025-07-28 12:16:26 +02:00
|
|
|
#include "draw/drawutils.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"
|
2025-07-28 14:10:01 +02:00
|
|
|
#include "map/mapdata.h"
|
|
|
|
|
#include "player/player.h"
|
2025-07-17 14:19:34 +02:00
|
|
|
#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-07-24 14:37:18 +02:00
|
|
|
#include <math.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;
|
2025-07-28 14:10:01 +02:00
|
|
|
player->x = mapdata->startx * SIZE;
|
|
|
|
|
player->y = mapdata->starty * SIZE;
|
2025-07-28 11:12:24 +02:00
|
|
|
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;
|
2025-07-07 14:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-28 12:16:26 +02:00
|
|
|
bool touch(float px, float py, t_mapdata *map)
|
|
|
|
|
{
|
|
|
|
|
int x;
|
|
|
|
|
int y;
|
|
|
|
|
|
|
|
|
|
x = px / SIZE;
|
|
|
|
|
y = py / SIZE;
|
|
|
|
|
if (map->map[y][x] == '1')
|
|
|
|
|
return (true);
|
|
|
|
|
return (false);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 14:10:01 +02:00
|
|
|
void draw_line(t_cub3d_data *data, float start_angle)
|
2025-07-28 13:04:32 +02:00
|
|
|
{
|
2025-07-28 14:10:01 +02:00
|
|
|
float cos_angle = cos(start_angle);
|
|
|
|
|
float sin_angle = sin(start_angle);
|
2025-07-28 13:04:32 +02:00
|
|
|
float ray_x = data->player.x;
|
|
|
|
|
float ray_y = data->player.y;
|
|
|
|
|
|
|
|
|
|
while(!touch(ray_x, ray_y, data->map))
|
|
|
|
|
{
|
|
|
|
|
my_mlx_pixel_put(data->img_data, (int)(MAP_SIZE * (ray_x / SIZE)), (int)(MAP_SIZE * (ray_y / SIZE)), 0xcc241b);
|
|
|
|
|
ray_x += sin_angle;
|
|
|
|
|
ray_y += cos_angle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 14:59:07 +02:00
|
|
|
t_cardinal_dir angle_cardinal_dir(float angle)
|
2025-07-28 14:10:01 +02:00
|
|
|
{
|
|
|
|
|
if (angle >= g_southwest || (angle > g_south && angle < g_southeast))
|
|
|
|
|
return (SOUTH);
|
|
|
|
|
if (angle >= g_southeast && angle < g_northeast)
|
|
|
|
|
return (EAST);
|
|
|
|
|
if (angle >= g_northeast && angle < g_northwest)
|
|
|
|
|
return (NORTH);
|
|
|
|
|
return (WEST);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 14:59:07 +02:00
|
|
|
t_cardinal_dir player_cardinal_dir(t_player player)
|
2025-07-28 14:10:01 +02:00
|
|
|
{
|
2025-07-28 14:59:07 +02:00
|
|
|
return (angle_cardinal_dir(player.yaw));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t_intercardinal_dir angle_intercardinal_dir(float angle)
|
|
|
|
|
{
|
|
|
|
|
if (angle >= g_south && angle < g_east)
|
|
|
|
|
return (SOUTHEAST);
|
|
|
|
|
if (angle >= g_east && angle < g_north)
|
|
|
|
|
return (NORTHEAST);
|
|
|
|
|
if (angle >= g_north && angle < g_west)
|
|
|
|
|
return (NORTHWEST);
|
|
|
|
|
return (SOUTHWEST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t_intercardinal_dir player_intercardinal_dir(t_player player)
|
|
|
|
|
{
|
|
|
|
|
return (angle_intercardinal_dir(player.yaw));
|
2025-07-28 14:10:01 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 17:30:16 +02:00
|
|
|
int game_loop(t_cub3d_data *data)
|
|
|
|
|
{
|
2025-07-17 14:57:29 +02:00
|
|
|
mlx_destroy_image(data->mlx, data->img_data->img);
|
2025-07-22 12:48:44 +02:00
|
|
|
data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
|
2025-07-28 12:16:26 +02:00
|
|
|
|
2025-07-28 14:59:07 +02:00
|
|
|
if (player_intercardinal_dir(data->player) == SOUTHEAST)
|
|
|
|
|
ft_printf("southeast ");
|
|
|
|
|
if (player_intercardinal_dir(data->player) == SOUTHWEST)
|
|
|
|
|
ft_printf("southwest ");
|
|
|
|
|
if (player_intercardinal_dir(data->player) == NORTHEAST)
|
|
|
|
|
ft_printf("northeast ");
|
|
|
|
|
if (player_intercardinal_dir(data->player) == NORTHWEST)
|
|
|
|
|
ft_printf("northwest ");
|
|
|
|
|
|
|
|
|
|
if (player_cardinal_dir(data->player) == SOUTH)
|
2025-07-28 14:10:01 +02:00
|
|
|
ft_printf("south\n");
|
2025-07-28 14:59:07 +02:00
|
|
|
if (player_cardinal_dir(data->player) == EAST)
|
2025-07-28 14:10:01 +02:00
|
|
|
ft_printf("east\n");
|
2025-07-28 14:59:07 +02:00
|
|
|
if (player_cardinal_dir(data->player) == NORTH)
|
2025-07-28 14:10:01 +02:00
|
|
|
ft_printf("north\n");
|
2025-07-28 14:59:07 +02:00
|
|
|
if (player_cardinal_dir(data->player) == WEST)
|
2025-07-28 14:10:01 +02:00
|
|
|
ft_printf("west\n");
|
|
|
|
|
|
|
|
|
|
float ray_angle = data->player.yaw - M_PI / 6;
|
|
|
|
|
float fraction = M_PI / 3 / WIDTH;
|
|
|
|
|
int column = 0;
|
2025-07-28 13:04:32 +02:00
|
|
|
while (column < WIDTH)
|
2025-07-28 12:16:26 +02:00
|
|
|
{
|
2025-07-28 14:10:01 +02:00
|
|
|
// hline check
|
|
|
|
|
/* float ray_atan = atan(ray_angle); */
|
|
|
|
|
draw_line(data, ray_angle);
|
2025-07-28 12:16:26 +02:00
|
|
|
column++;
|
2025-07-28 14:10:01 +02:00
|
|
|
ray_angle += fraction;
|
2025-07-28 12:16:26 +02:00
|
|
|
}
|
2025-07-28 12:16:26 +02:00
|
|
|
|
2025-07-17 14:57:29 +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,
|
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-07-22 12:48:44 +02:00
|
|
|
data.mlx_win = mlx_new_window(data.mlx, WIDTH, HEIGHT, "Cub3d");
|
2025-07-17 14:57:29 +02:00
|
|
|
data.img_data = ft_calloc(sizeof(t_img_data), 1);
|
2025-07-22 12:48:44 +02:00
|
|
|
data.img_data->img = mlx_new_image(data.mlx, WIDTH, HEIGHT);
|
2025-07-17 14:57:29 +02:00
|
|
|
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
|
|
|
}
|