Merge branch 'master' of git.sr.ht:~xamora/cube3D
This commit is contained in:
commit
ea52e6c184
5
Makefile
5
Makefile
@ -1,9 +1,10 @@
|
|||||||
SRCS_GAME = main.c game/game.c
|
SRCS_GAME = main.c game/game.c game/init.c game/manage.c \
|
||||||
|
game/manage_keys.c game/raycasting.c
|
||||||
SRCS_PARSING =
|
SRCS_PARSING =
|
||||||
OBJS = ${SRCS_GAME:.c=.o} ${SRCS_PARSING:.c=.o}
|
OBJS = ${SRCS_GAME:.c=.o} ${SRCS_PARSING:.c=.o}
|
||||||
CC = clang
|
CC = clang
|
||||||
LIBS = libftx/libftx.a MLX42/build/libmlx42.a -ldl -lglfw -lm
|
LIBS = libftx/libftx.a MLX42/build/libmlx42.a -ldl -lglfw -lm
|
||||||
CFLAGS = -g -Wall -Wextra -Werror -Wno-conversion -Ofast
|
CFLAGS = -g -Wall -Wextra -Werror
|
||||||
NAME = cub3D
|
NAME = cub3D
|
||||||
|
|
||||||
all: ${NAME}
|
all: ${NAME}
|
||||||
|
7
cube3D.h
7
cube3D.h
@ -6,19 +6,21 @@
|
|||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/26 12:34:04 by erey-bet #+# #+# */
|
/* Created: 2023/04/26 12:34:04 by erey-bet #+# #+# */
|
||||||
/* Updated: 2023/04/26 15:20:47 by erey-bet ### ########.fr */
|
/* Updated: 2023/04/27 14:41:37 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef CUBE3D_H
|
#ifndef CUBE3D_H
|
||||||
# define CUBE3D_H
|
# define CUBE3D_H
|
||||||
|
|
||||||
|
# include "libftx/libftx.h"
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
# include "MLX42/include/MLX42/MLX42.h"
|
# include "MLX42/include/MLX42/MLX42.h"
|
||||||
# include <X11/keysym.h>
|
# include <X11/keysym.h>
|
||||||
# include <X11/X.h>
|
# include <X11/X.h>
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
// img: 0=Nord, 1=WEST, 2=SUD, 3=EAST;
|
// img: 0=Nord, 1=WEST, 2=SUD, 3=EAST;
|
||||||
|
|
||||||
@ -30,8 +32,11 @@ typedef struct s_map
|
|||||||
void *img[4];
|
void *img[4];
|
||||||
long color_bot;
|
long color_bot;
|
||||||
long color_top;
|
long color_top;
|
||||||
|
double ply_x;
|
||||||
|
double ply_y;
|
||||||
} t_map;
|
} t_map;
|
||||||
|
|
||||||
|
/*INIT*/
|
||||||
t_map *map_parsing(char *path);
|
t_map *map_parsing(char *path);
|
||||||
int start_game(t_map *map);
|
int start_game(t_map *map);
|
||||||
|
|
||||||
|
26
game/game.c
26
game/game.c
@ -6,35 +6,33 @@
|
|||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */
|
/* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */
|
||||||
/* Updated: 2023/04/26 15:44:55 by erey-bet ### ########.fr */
|
/* Updated: 2023/04/28 16:23:57 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../cube3D.h"
|
#include "game.h"
|
||||||
|
|
||||||
int start_game(t_map *map)
|
int start_game(t_map *map)
|
||||||
{
|
{
|
||||||
t_game *game;
|
t_game game;
|
||||||
|
|
||||||
(void)map;
|
if (init(map, &game))
|
||||||
game->mlx = mlx_init(1920, 1080, "jan lili", true)
|
|
||||||
if (mlx)
|
|
||||||
return (1);
|
return (1);
|
||||||
game->window = mlx_new_image(game->mlx, 128, 128);
|
game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT);
|
||||||
if (game->image)
|
if (!game.window)
|
||||||
{
|
{
|
||||||
mlx_close_window(game->mlx);
|
mlx_close_window(game.mlx);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
if (mlx_image_to_window(game->mlx, game->window, 0, 0) == -1)
|
if (mlx_image_to_window(game.mlx, game.window, 0, 0) == -1)
|
||||||
{
|
{
|
||||||
mlx_close_window(game->mlx);
|
mlx_close_window(game.mlx);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
mlx_loop_hook(game->mlx, manage, game)
|
mlx_key_hook(game.mlx, manage, &game);
|
||||||
mlx_loop(game->mlx);
|
mlx_loop(game.mlx);
|
||||||
mlx_terminate(game->mlx);
|
mlx_terminate(game.mlx);
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
34
game/game.h
34
game/game.h
@ -6,24 +6,54 @@
|
|||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */
|
/* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */
|
||||||
/* Updated: 2023/04/26 15:38:47 by erey-bet ### ########.fr */
|
/* Updated: 2023/05/02 15:32:43 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef GAME_H
|
#ifndef GAME_H
|
||||||
# define GAME_H
|
# define GAME_H
|
||||||
|
|
||||||
|
# include "../cube3D.h"
|
||||||
|
# include <math.h>
|
||||||
|
|
||||||
|
# define WIDTH 1920
|
||||||
|
# define HEIGHT 1080
|
||||||
|
|
||||||
typedef struct s_player
|
typedef struct s_player
|
||||||
{
|
{
|
||||||
double pos_x;
|
double pos_x;
|
||||||
double pos_y;
|
double pos_y;
|
||||||
|
double dir_x;
|
||||||
|
double dir_y;
|
||||||
|
double pla_x;
|
||||||
|
double pla_y;
|
||||||
} t_ply;
|
} t_ply;
|
||||||
|
|
||||||
|
typedef struct s_raycast
|
||||||
|
{
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
} t_ray;
|
||||||
|
|
||||||
typedef struct s_game
|
typedef struct s_game
|
||||||
{
|
{
|
||||||
mlx_t *mlx;
|
mlx_t *mlx;
|
||||||
mlx_image_t *windows;
|
mlx_image_t *window;
|
||||||
t_ply *ply;
|
t_ply *ply;
|
||||||
|
t_map *map;
|
||||||
|
t_ray *ray;
|
||||||
} t_game;
|
} t_game;
|
||||||
|
|
||||||
|
/* INIT */
|
||||||
|
t_game *init(t_map *map);
|
||||||
|
|
||||||
|
/* MANAGE */
|
||||||
|
void manage(mlx_key_data_t keydata, void *param);
|
||||||
|
|
||||||
|
/* KEYS */
|
||||||
|
int manage_keys(mlx_key_data_t keydata, t_game *game);
|
||||||
|
|
||||||
|
/* RAYCASTING */
|
||||||
|
int raycasting(t_game *game);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
37
game/init.c
Normal file
37
game/init.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* init.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 14:50:22 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/05/02 15:18:05 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
|
void init_ply(t_map *map, t_ply *ply)
|
||||||
|
{
|
||||||
|
ply->pos_x = map->ply_x;
|
||||||
|
ply->pos_y = map->ply_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_ray(t_ray *ray)
|
||||||
|
{
|
||||||
|
ray->x = 0;
|
||||||
|
ray->y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void init(t_map *map, t_game &game)
|
||||||
|
{
|
||||||
|
t_ply ply;
|
||||||
|
t_ray ray;
|
||||||
|
|
||||||
|
game.mlx = mlx_init(WIDTH, HEIGHT, "jan lili", true);
|
||||||
|
if (!game.mlx)
|
||||||
|
return (NULL);
|
||||||
|
game.map = map;
|
||||||
|
init_ply(map, &ply);
|
||||||
|
}
|
22
game/manage.c
Normal file
22
game/manage.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* manage.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 12:52:14 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 14:31:50 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
|
void manage(mlx_key_data_t keydata, void *param)
|
||||||
|
{
|
||||||
|
t_game *game;
|
||||||
|
|
||||||
|
game = param;
|
||||||
|
if (manage_keys(keydata, game))
|
||||||
|
raycasting(game);
|
||||||
|
}
|
47
game/manage_keys.c
Normal file
47
game/manage_keys.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* manage_keys.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/04/28 12:50:21 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
|
int movement(t_ply *ply, int key)
|
||||||
|
{
|
||||||
|
else if (key == MLX_KEY_W)
|
||||||
|
ply->pos_y -= 0.1;
|
||||||
|
else if (key == MLX_KEY_S)
|
||||||
|
ply->pos_y += 0.1;
|
||||||
|
else if (key == MLX_KEY_D)
|
||||||
|
ply->pos_x += 0.1;
|
||||||
|
else if (key == MLX_KEY_A)
|
||||||
|
ply->pos_x -= 0.1;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int manage_keys(mlx_key_data_t keys, t_game *game)
|
||||||
|
{
|
||||||
|
int is_moving;
|
||||||
|
|
||||||
|
is_moving = 0;
|
||||||
|
if (keys.key == MLX_KEY_ESCAPE)
|
||||||
|
mlx_close_window(game->mlx);
|
||||||
|
else if (keys.key == MLX_KEY_W
|
||||||
|
|| keys.key == MLX_KEY_S
|
||||||
|
|| keys.key == MLX_KEY_D
|
||||||
|
|| keys.key == MLX_KEY_A)
|
||||||
|
is_moving = movement(game->ply, keys.key);
|
||||||
|
/*else if (key.key == MLX_KEY_RIGHT)
|
||||||
|
game->ply->direc += 0.1;
|
||||||
|
else if (key.key == MLX_KEY_LEFT)
|
||||||
|
game->ply->direc -= 0.1;*/
|
||||||
|
if (is_moving)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
39
game/raycasting.c
Normal file
39
game/raycasting.c
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* raycasting.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/05/02 13:12:15 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
|
int ray(t_game *game)
|
||||||
|
{
|
||||||
|
t_ply *p;
|
||||||
|
t_ray *ray;
|
||||||
|
double camera;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
p = game->ply;
|
||||||
|
ray = game->ray;
|
||||||
|
while (i <= WIDTH)
|
||||||
|
{
|
||||||
|
camera = 2 * i / (double)WIDTH - 1; //x-coordinate in camera space
|
||||||
|
ray->x = p->dir_x + p->pla_x * camera;
|
||||||
|
ray->y = p->dir_y + p->pla_y * camera;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int raycasting(t_game *game)
|
||||||
|
{
|
||||||
|
(void)game;
|
||||||
|
ray(game)
|
||||||
|
write(1, "raycasting...\n", 14);
|
||||||
|
return (0);
|
||||||
|
}
|
4
main.c
4
main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/26 12:44:55 by erey-bet #+# #+# */
|
/* Created: 2023/04/26 12:44:55 by erey-bet #+# #+# */
|
||||||
/* Updated: 2023/04/26 15:20:32 by erey-bet ### ########.fr */
|
/* Updated: 2023/04/27 14:43:10 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -36,6 +36,8 @@ int main(int argc, char **argv)
|
|||||||
map->map[4] = "11111";
|
map->map[4] = "11111";
|
||||||
map->size_x = 5;
|
map->size_x = 5;
|
||||||
map->size_y = 5;
|
map->size_y = 5;
|
||||||
|
map->ply_x = 3;
|
||||||
|
map->ply_y = 3;
|
||||||
if (start_game(map))
|
if (start_game(map))
|
||||||
{
|
{
|
||||||
return (3);
|
return (3);
|
||||||
|
Loading…
Reference in New Issue
Block a user