Compare commits

..

66 Commits

Author SHA1 Message Date
48ef0bac9a Add README.md 2023-10-03 18:07:09 +02:00
Etienne Rey-bethbeder
4a706f938c Remove double file for maps 2023-06-19 13:43:21 +02:00
Camille Chauvet
258957059d clean: remove compile file 2023-06-16 16:35:47 +00:00
Camille Chauvet
735a8b335c Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-06-16 16:35:16 +00:00
Camille Chauvet
101018f0c1 fix: count the nb of ',' 2023-06-16 16:34:01 +00:00
Etienne Rey-bethbeder
b44f4717df jan lili -> jam lili meli 2023-06-16 17:49:22 +02:00
Camille Chauvet
217a220705 fix: je vais me pendre 2023-06-16 14:49:40 +00:00
Etienne Rey-bethbeder
6cd5b54918 Finish 2023-06-15 17:21:07 +02:00
Etienne Rey-bethbeder
4bd265e213 SIG HEIL 2023-06-13 14:19:45 +02:00
Etienne Rey-bethbeder
fe699a3cb4 Flags 2023-06-12 14:55:39 +02:00
Camille Chauvet
45521211b0 clean: norm 2023-06-12 14:31:59 +02:00
Etienne Rey-bethbeder
ab4e7e7034 ça compile maintenant 2023-06-01 13:49:12 +02:00
Etienne Rey-bethbeder
88298e79b2 Retire les .o 2023-05-19 17:54:32 +02:00
Etienne Rey-bethbeder
42d1359904 Correction leaks sur un probleme de texture 2023-05-19 17:54:06 +02:00
Camille Chauvet
415b6e42e3 bozo 2023-05-17 15:54:04 +02:00
Camille Chauvet
b95f3ee1af clean: norm 2023-05-16 19:12:54 +02:00
Camille Chauvet
7f2a0b4a0e fix: missing key 2023-05-16 12:47:04 +00:00
Camille Chauvet
4c31d80a72 fix: header error 2023-05-16 11:05:02 +00:00
Camille Chauvet
3bf2ab4318 fix 2023-05-16 10:51:48 +00:00
Camille Chauvet
b9dddba82a merge 2023-05-16 10:50:04 +00:00
Camille Chauvet
6cdb83291a fix: 2023-05-16 10:49:11 +00:00
Etienne Rey-bethbeder
f73e499a75 KYS MICHEL 2023-05-11 16:25:29 +02:00
Etienne Rey-bethbeder
5c5d23c5f7 En sois la vie tu né, tu fais rien et tu meurs 2023-05-10 13:56:34 +02:00
Etienne Rey-bethbeder
63eca2ab8e kys michel 2023-05-10 12:31:36 +02:00
Etienne Rey-bethbeder
8533722131 texture ajouté avec des incohérence a réglé, Il manque: Norme et leak 2023-05-09 14:46:49 +02:00
Camille Chauvet
f56ec19ebc fix: check if all header value is define 2023-05-05 16:33:45 +00:00
Camille Chauvet
d22ab7e610 fix: close fd 2023-05-05 16:29:17 +00:00
Camille Chauvet
e3ccd1d821 Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-05-05 15:32:38 +00:00
Camille Chauvet
5bdc48340d fix: leak && 1x1 map not surround 2023-05-05 15:32:29 +00:00
Etienne Rey-bethbeder
147c4e94c0 Cube3D Fonctionnelle, perpective, tout ça tout ça, il manque plus que les textures et la norme 2023-05-05 17:06:47 +02:00
Camille Chauvet
fbb9864ef4 fix: 2023-05-05 14:51:37 +00:00
Camille Chauvet
e500e3f440 fix: leak 2023-05-05 14:49:04 +00:00
Camille Chauvet
3bda6b4fb1 change: struct map: add img_path 2023-05-05 14:34:22 +00:00
Camille Chauvet
6d5c90a6a7 clean: remove trash file 2023-05-05 12:10:15 +00:00
Camille Chauvet
1b53e19210 fix: unintialised map.img 2023-05-05 12:08:54 +00:00
Etienne Rey-bethbeder
bef4863660 _ 2023-05-05 14:00:20 +02:00
Camille Chauvet
7b4412b977 fix: parsing color as a pro 2023-05-05 11:54:25 +00:00
Camille Chauvet
a3a2b8d9d2 clean: remove trash file 2023-05-05 11:53:30 +00:00
Camille Chauvet
a2b9d8c283 add: some test maps 2023-05-05 11:53:04 +00:00
Camille Chauvet
ab7cc5386a add parsing srcs 2023-05-05 11:52:09 +00:00
Camille Chauvet
addd78ecf2 merge 2023-05-05 11:51:22 +00:00
Camille Chauvet
c9ac445103 fix: use same t_map in all code 2023-05-05 11:35:52 +00:00
Etienne Rey-bethbeder
15da46648b Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-05-05 13:29:36 +02:00
Etienne Rey-bethbeder
0720835af0 oubli .o 2023-05-04 14:42:51 +02:00
Etienne Rey-bethbeder
d58732b159 Avancement problème déplacement, manque texture 2023-05-04 14:42:39 +02:00
Camille Chauvet
c9d9faa45c add: replace player spawn char to '0' and store spawn_direction 2023-05-04 11:58:44 +00:00
Camille Chauvet
b323b98dc0 core: check map surround/close 2023-05-04 11:47:18 +00:00
Camille Chauvet
c4d953acb5 fix: add verif when map body doesn't contain only regulare char 2023-05-04 11:17:55 +00:00
Camille Chauvet
a301809bea fix: add error message when read|open failed 2023-05-04 10:48:31 +00:00
Etienne Rey-bethbeder
ebe8b84f78 Raycasting done ! Problem of FOV 2023-05-03 17:48:36 +02:00
Camille Chauvet
900547d1cc fix: parsing work 2023-05-03 14:33:57 +00:00
Camille Chauvet
88d69ebcea add: extra function 2023-05-03 11:11:04 +00:00
Camille Chauvet
3ad6814405 init: map parsing 2023-05-03 11:09:06 +00:00
Camille Chauvet
317a17d4a3 add: map SRCS 2023-05-03 11:06:39 +00:00
Camille Chauvet
b84e391fa7 Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-05-03 10:47:52 +00:00
Etienne Rey-bethbeder
8c2f28e2ed Oubli des .o dsl 2023-05-03 12:47:41 +02:00
Etienne Rey-bethbeder
2614e35690 ça compile vraiment mtn 2023-05-03 12:47:24 +02:00
Camille Chauvet
65045662a6 Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-05-03 10:45:58 +00:00
Etienne Rey-bethbeder
78b858658a ça compile 2023-05-03 12:45:40 +02:00
Camille Chauvet
5511732657 Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-05-03 07:28:08 +00:00
Etienne Rey-bethbeder
65a622bf10 Add dda and raycasting start 2023-05-02 16:58:55 +02:00
Camille Chauvet
ea52e6c184 Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-05-02 13:40:29 +00:00
Camille Chauvet
4aa1047dca Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-05-02 13:13:18 +00:00
Camille Chauvet
df2dae6c6b Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-04-26 13:01:48 +00:00
Camille Chauvet
d7b20cddbe Merge branch 'master' of git.sr.ht:~xamora/cube3D 2023-04-26 12:27:24 +00:00
Camille Chauvet
f25286f41f add: tablen function & fix: ft_strcmp now take const char * 2023-04-26 12:25:49 +00:00
51 changed files with 1583 additions and 210 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.o
cub3D
*.swp

8
MLX42/.gitattributes vendored Normal file
View File

@ -0,0 +1,8 @@
# See https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings
# Set the default behavior, in case people don't have core.autocrlf set
* text=auto
# Declare files that will always have a certain EOL
*.sh text eol=lf
shaders/** text eol=lf

65
MLX42/.gitignore vendored Normal file
View File

@ -0,0 +1,65 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# Misc
main.c
temp/
.vscode/
lib/glfw/
.DS_Store
# Special shader files
mlx_*_shader.c
build/
main.c
test

View File

@ -1,17 +1,22 @@
SRCS_GAME = main.c game/game.c game/init.c game/manage.c \
game/manage_keys.c game/raycasting.c
SRCS_PARSING =
OBJS = ${SRCS_GAME:.c=.o} ${SRCS_PARSING:.c=.o}
CC = clang
LIBS = libftx/libftx.a MLX42/build/libmlx42.a -ldl -lglfw -lm
CFLAGS = -g -Wall -Wextra -Werror
NAME = cub3D
SRCS_GAME := main.c game/game.c game/init.c \
game/manage_keys.c game/raycasting.c game/dda.c game/draw.c game/utils.c
SRCS_MAP := parsing.c parsing_header.c parsing_header2.c parsing_meta.c parsing_body.c map.c parsing_body2.c parsing_header3.c
SRCS_MAP := $(addprefix map/, $(SRCS_MAP))
SRCS := ${SRCS_MAP} \
${SRCS_GAME}
OBJS := ${SRCS:.c=.o}
CC := gcc
LIBS := libftx/libftx.a MLX42/build/libmlx42.a -ldl -lglfw -lm
CFLAGS := -g -Wall -Wextra -Werror -Wno-conversion -Ofast -fsanitize=leak
NAME := cub3D
all: ${NAME}
${NAME}: ${OBJS}
make -C libftx all
${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${LIBS}
${CC} -o ${NAME} ${OBJS} ${LIBS}
clean:
make -C libftx clean

6
README.md Normal file
View File

@ -0,0 +1,6 @@
# cub3D
A raycasting game
## Screenshots
![](https://media.discordapp.net/attachments/501841539161522176/1158797264186245150/image.png?ex=651d8d8d&is=651c3c0d&hm=086a4b0f4e9071996776f71261f1a1031623051bae79756068c9a1fc3335a037&=&width=1185&height=612)

BIN
assets/blue.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 640 B

BIN
assets/green.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

BIN
assets/purple.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 642 B

BIN
assets/red.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

View File

@ -6,13 +6,15 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:34:04 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:41:37 by erey-bet ### ########.fr */
/* Updated: 2023/06/12 14:31:28 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CUBE3D_H
# define CUBE3D_H
# include "map/parsing.h"
# include "map/map.h"
# include "libftx/libftx.h"
# include <sys/types.h>
# include "MLX42/include/MLX42/MLX42.h"
@ -22,22 +24,7 @@
# include <stdio.h>
# include <unistd.h>
// img: 0=Nord, 1=WEST, 2=SUD, 3=EAST;
typedef struct s_map
{
char **map;
size_t size_x;
size_t size_y;
void *img[4];
long color_bot;
long color_top;
double ply_x;
double ply_y;
} t_map;
/*INIT*/
t_map *map_parsing(char *path);
int start_game(t_map *map);
int start_game(t_map map);
#endif

105
game/dda.c Normal file
View File

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dda.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */
/* Updated: 2023/05/11 16:22:58 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
void init_dda(t_dda *dda, t_ply *ply, t_ray *ray)
{
dda->map_x = (int)ply->pos_x;
dda->map_y = (int)ply->pos_y;
if (ray->dir_x == 0)
dda->delta_dist_x = 1e30;
else
dda->delta_dist_x = ft_abs(1 / ray->dir_x);
if (ray->dir_y == 0)
dda->delta_dist_y = 1e30;
else
dda->delta_dist_y = ft_abs(1 / ray->dir_y);
dda->step_x = 1;
dda->step_y = 1;
if (ray->dir_x < 0)
dda->step_x = -1;
if (ray->dir_y < 0)
dda->step_y = -1;
if (ray->dir_x < 0)
dda->side_dist_x = (ply->pos_x - dda->map_x) * dda->delta_dist_x;
else
dda->side_dist_x = (dda->map_x + 1.0 - ply->pos_x) * dda->delta_dist_x;
if (ray->dir_y < 0)
dda->side_dist_y = (ply->pos_y - dda->map_y) * dda->delta_dist_y;
else
dda->side_dist_y = (dda->map_y + 1.0 - ply->pos_y) * dda->delta_dist_y;
dda->hit = 0;
}
void loop_dda(t_game *game, t_dda *dda)
{
while (dda->hit == 0)
{
if (dda->side_dist_x < dda->side_dist_y)
{
dda->side_dist_x += dda->delta_dist_x;
dda->map_x += dda->step_x;
dda->side = 0;
}
else
{
dda->side_dist_y += dda->delta_dist_y;
dda->map_y += dda->step_y;
dda->side = 1;
}
if (dda->map_x < 0 || dda->map_y < 0 || dda->map_y >= game->map.size_y
|| dda->map_x >= (int)ft_strlen(game->map.map[dda->map_y]))
{
dda->perp_wall_dist = 1e30;
return ;
}
else
dda->perp_wall_dist = 0;
if (game->map.map[dda->map_y][dda->map_x] == '1')
dda->hit = 1;
}
}
void ray_dda(t_dda *dda)
{
if (dda->perp_wall_dist != 1e30)
{
if (dda->side == 0)
dda->perp_wall_dist = dda->side_dist_x - dda->delta_dist_x;
else
dda->perp_wall_dist = dda->side_dist_y - dda->delta_dist_y;
if (dda->perp_wall_dist == 0)
dda->perp_wall_dist = 1e30;
}
dda->line_height = (int)(HEIGHT / dda->perp_wall_dist);
dda->draw_start = -(dda->line_height) / 2 + HEIGHT / 2;
if (dda->draw_start < 0)
dda->draw_start = 0;
dda->draw_end = dda->line_height / 2 + HEIGHT / 2;
if (dda->draw_end >= HEIGHT)
dda->draw_end = HEIGHT - 1;
}
void dda(t_game *game, int x)
{
t_dda dda;
t_ray *ray;
t_ply *ply;
ray = &game->ray;
ply = &game->ply;
init_dda(&dda, ply, ray);
loop_dda(game, &dda);
ray_dda(&dda);
draw(game, &dda, x);
}

54
game/draw.c Normal file
View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/11 14:41:48 by erey-bet #+# #+# */
/* Updated: 2023/06/15 12:52:23 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
void draw_texture(t_game *game, t_dda *dda, int x)
{
t_texture_draw tex;
int y;
tex.texture = game->textures[get_texture_side(dda)];
if (dda->side == 0)
tex.wall_x = game->ply.pos_y + dda->perp_wall_dist * game->ray.dir_y;
else
tex.wall_x = game->ply.pos_x + dda->perp_wall_dist * game->ray.dir_x;
tex.wall_x -= floor(tex.wall_x);
tex.x = (int)(tex.wall_x * (double)tex.texture->width);
if ((dda->side == 0 && game->ray.dir_x > 0)
|| (dda->side == 1 && game->ray.dir_y < 0))
tex.x = tex.texture->width - tex.x - 1;
tex.step = 1.0 * tex.texture->height / dda->line_height;
tex.pos = (dda->draw_start - game->window->height
/ 2 + dda->line_height / 2) * tex.step;
y = dda->draw_start;
while (y < dda->draw_end && y < HEIGHT)
{
tex.y = (int)tex.pos & (tex.texture->height - 1);
tex.pos += tex.step;
mlx_put_pixel(game->window, x, y++,
get_pixel_color(tex.texture, tex.x, tex.y));
}
}
void draw(t_game *game, t_dda *dda, int x)
{
int y;
y = 0;
while (y < dda->draw_start && y >= 0 && y < HEIGHT)
mlx_put_pixel(game->window, x, y++, game->map.color_bot);
draw_texture(game, dda, x);
y = dda->draw_end;
while (y < HEIGHT && y >= 0)
mlx_put_pixel(game->window, x, y++, game->map.color_top);
}

View File

@ -6,33 +6,68 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */
/* Updated: 2023/04/28 16:23:57 by erey-bet ### ########.fr */
/* Updated: 2023/06/15 12:40:32 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
int start_game(t_map *map)
static void destroy(t_game *game)
{
if (game->textures[0])
{
mlx_delete_texture(game->textures[0]);
if (game->textures[1])
{
mlx_delete_texture(game->textures[1]);
if (game->textures[2])
{
mlx_delete_texture(game->textures[2]);
if (game->textures[3])
{
mlx_delete_texture(game->textures[3]);
if (game->mlx)
mlx_terminate(game->mlx);
}
}
}
}
}
static void hook(void *param)
{
t_game *game;
game = param;
if (manage_keys(game))
raycasting(game);
}
int start_game(t_map map)
{
t_game game;
if (init(map, &game))
{
destroy(&game);
return (1);
}
game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT);
if (!game.window)
{
mlx_close_window(game.mlx);
return(1);
mlx_terminate(game.mlx);
destroy(&game);
return (1);
}
if (mlx_image_to_window(game.mlx, game.window, 0, 0) == -1)
{
mlx_close_window(game.mlx);
return(1);
}
mlx_key_hook(game.mlx, manage, &game);
mlx_loop(game.mlx);
mlx_terminate(game.mlx);
destroy(&game);
return (1);
}
raycasting(&game);
mlx_loop_hook(game.mlx, &hook, &game);
mlx_loop(game.mlx);
destroy(&game);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */
/* Updated: 2023/05/02 15:32:43 by erey-bet ### ########.fr */
/* Updated: 2023/06/13 14:19:18 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,9 +15,12 @@
# include "../cube3D.h"
# include <math.h>
# include <stdlib.h>
# define WIDTH 1920
# define HEIGHT 1080
# define MOVESPEED 0.10
# define ROTATIONSPEED 60
typedef struct s_player
{
@ -31,29 +34,66 @@ typedef struct s_player
typedef struct s_raycast
{
double x;
double y;
double dir_x;
double dir_y;
} t_ray;
typedef struct s_dda
{
int map_x;
int map_y;
double side_dist_x;
double side_dist_y;
double delta_dist_x;
double delta_dist_y;
double perp_wall_dist;
int step_x;
int step_y;
int hit;
int side;
int line_height;
int draw_start;
int draw_end;
} t_dda;
typedef struct s_texture_draw
{
mlx_texture_t *texture;
double wall_x;
int x;
int y;
double pos;
double step;
} t_texture_draw;
typedef struct s_game
{
mlx_t *mlx;
mlx_image_t *window;
t_ply *ply;
t_map *map;
t_ray *ray;
mlx_texture_t *textures[4];
t_ply ply;
t_map map;
t_ray ray;
} t_game;
/* INIT */
t_game *init(t_map *map);
/* MANAGE */
void manage(mlx_key_data_t keydata, void *param);
int init(t_map map, t_game *game);
/* KEYS */
int manage_keys(mlx_key_data_t keydata, t_game *game);
int manage_keys(t_game *game);
/* RAYCASTING */
int raycasting(t_game *game);
/* DDA ALGO */
void dda(t_game *game, int x);
/* UTILS */
double ft_abs(double nb);
unsigned int get_rgba(int r, int g, int b, int a);
uint32_t get_pixel_color(mlx_texture_t *texture, uint32_t x, uint32_t y);
int get_texture_side(t_dda *dda);
/* DRAW */
void draw(t_game *game, t_dda *dda, int x);
#endif

View File

@ -6,32 +6,62 @@
/* 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 */
/* Updated: 2023/06/16 17:48:34 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
void init_ply(t_map *map, t_ply *ply)
void init_ply(t_map map, t_ply *ply)
{
ply->pos_x = map->ply_x;
ply->pos_y = map->ply_y;
ply->pos_x = map.ply_x + 0.5;
ply->pos_y = map.ply_y + 0.5;
ply->dir_x = 0.0;
ply->dir_y = 0.0;
if (map.direction == 'N')
ply->dir_y = -1.0;
if (map.direction == 'S')
ply->dir_y = 1.0;
if (map.direction == 'E')
ply->dir_x = 1.0;
if (map.direction == 'W')
ply->dir_x = -1.0;
ply->pla_x = -(ply->dir_y) * 0.66;
ply->pla_y = (ply->dir_x) * 0.66;
}
void init_ray(t_ray *ray)
{
ray->x = 0;
ray->y = 0;
ray->dir_x = 0;
ray->dir_y = 0;
}
void init(t_map *map, t_game &game)
int init_textures(t_map map, mlx_texture_t *textures[4])
{
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);
textures[0] = mlx_load_png(map.img_path[0]);
if (!textures[0])
return (1);
textures[1] = mlx_load_png(map.img_path[1]);
if (!textures[1])
return (1);
textures[2] = mlx_load_png(map.img_path[2]);
if (!textures[2])
return (1);
textures[3] = mlx_load_png(map.img_path[3]);
if (!textures[3])
return (1);
return (0);
}
int init(t_map map, t_game *game)
{
if (init_textures(map, game->textures))
return (2);
game->mlx = mlx_init(WIDTH, HEIGHT, "jan lili meli", false);
if (!game->mlx)
return (1);
game->map = map;
init_ply(map, &game->ply);
init_ray(&game->ray);
return (0);
}

View File

@ -6,42 +6,74 @@
/* 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 */
/* Updated: 2023/06/15 13:00:31 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
int movement(t_ply *ply, int key)
static int rotate(t_ply *ply, bool right)
{
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;
double old_dir_x;
double old_dir_y;
double old_plane_x;
double old_plane_y;
double speed;
old_dir_x = ply->dir_x;
old_dir_y = ply->dir_y;
old_plane_x = ply->pla_x;
old_plane_y = ply->pla_y;
speed = ROTATIONSPEED * (M_1_PI / 180.0);
if (!right)
speed = -speed;
ply->dir_x = old_dir_x * cos(speed) - old_dir_y * sin(speed);
ply->dir_y = old_dir_x * sin(speed) + old_dir_y * cos(speed);
ply->pla_x = old_plane_x * cos(speed) - old_plane_y * sin(speed);
ply->pla_y = old_plane_x * sin(speed) + old_plane_y * cos(speed);
return (1);
}
int manage_keys(mlx_key_data_t keys, t_game *game)
static int movement(t_ply *ply, double x, double y)
{
ply->pos_x += x;
ply->pos_y += y;
return (1);
}
int manage_movement(t_game *game)
{
t_ply *ply;
int is_moving;
ply = &game->ply;
is_moving = 0;
if (keys.key == MLX_KEY_ESCAPE)
if (mlx_is_key_down(game->mlx, MLX_KEY_W))
is_moving = movement(ply, (ply->dir_x * MOVESPEED),
(ply->dir_y * MOVESPEED));
if (mlx_is_key_down(game->mlx, MLX_KEY_S))
is_moving = movement(ply, -(ply->dir_x * MOVESPEED),
-(ply->dir_y * MOVESPEED));
if (mlx_is_key_down(game->mlx, MLX_KEY_D))
is_moving = movement(ply, (ply->pla_x * MOVESPEED),
(ply->pla_y * MOVESPEED));
if (mlx_is_key_down(game->mlx, MLX_KEY_A))
is_moving = movement(ply, -(ply->pla_x * MOVESPEED),
-(ply->pla_y * MOVESPEED));
if (mlx_is_key_down(game->mlx, MLX_KEY_RIGHT)
|| mlx_is_key_down(game->mlx, MLX_KEY_L))
is_moving = rotate(ply, true);
else if (mlx_is_key_down(game->mlx, MLX_KEY_LEFT)
|| mlx_is_key_down(game->mlx, MLX_KEY_J))
is_moving = rotate(ply, false);
return (is_moving);
}
int manage_keys(t_game *game)
{
if (mlx_is_key_down(game->mlx, 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)
if (manage_movement(game))
return (1);
return (0);
}

View File

@ -6,34 +6,29 @@
/* 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 */
/* Updated: 2023/05/05 13:59:57 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
int ray(t_game *game)
int raycasting(t_game *game)
{
t_ply *p;
t_ray *ray;
double camera;
int i;
int x;
i = 0;
p = game->ply;
ray = game->ray;
while (i <= WIDTH)
x = 0;
p = &game->ply;
ray = &game->ray;
while (x < 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;
camera = 2 * x / (double)WIDTH - 1;
ray->dir_x = p->dir_x + p->pla_x * camera;
ray->dir_y = p->dir_y + p->pla_y * camera;
dda(game, x);
x++;
}
}
int raycasting(t_game *game)
{
(void)game;
ray(game)
write(1, "raycasting...\n", 14);
return (0);
}

50
game/utils.c Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/11 14:19:16 by erey-bet #+# #+# */
/* Updated: 2023/06/16 17:45:58 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
double ft_abs(double nb)
{
if (nb < 0)
return (-nb);
return (nb);
}
unsigned int get_rgba(int r, int g, int b, int a)
{
return (r << 24 | g << 16 | b << 8 | a);
}
static uint32_t switch_color_bytes(uint32_t color)
{
return (((color & 0xff) << 24) | (((color >> 8) & 0xff) << 16)
| (((color >> 16) & 0xff) << 8) | color >> 24);
}
uint32_t get_pixel_color(mlx_texture_t *texture, uint32_t x, uint32_t y)
{
return (switch_color_bytes(*(uint32_t *)(texture->pixels
+ (y * texture->width + x) * texture->bytes_per_pixel)));
}
int get_texture_side(t_dda *dda)
{
if (dda->side == 0 && dda->step_x < 0)
return (3);
if (dda->side == 0 && dda->step_x > 0)
return (1);
if (dda->side == 1 && dda->step_y < 0)
return (2);
if (dda->side == 1 && dda->step_y > 0)
return (0);
return (0);
}

View File

@ -6,13 +6,13 @@
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2023/01/19 12:55:43 by cchauvet ### ########.fr #
# Updated: 2023/06/16 16:32:17 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
CC = clang
SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c ft_swap.c
SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c ft_swap.c ft_tablen.c ft_atoul.c ft_atoul_check.c ft_count.c
OBJS = $(SRCS:.c=.o)

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:03:10 by cchauvet #+# #+# */
/* Updated: 2023/01/19 17:35:09 by cchauvet ### ########.fr */
/* Updated: 2023/06/16 16:31:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,10 +14,15 @@
# define EXTRA_H
# include <stdarg.h>
# include <stdlib.h>
# include <sys/types.h>
# include <unistd.h>
# include <fcntl.h>
# include "../libft/libft.h"
size_t ft_count(const char *str, char c);
unsigned long ft_atoul(const char str[]);
int ft_atoul_check(const char str[]);
size_t ft_tablen(const void **tab);
char *ft_ultoa_base(unsigned long long n, char *base);
char *get_next_line(int fd);
size_t ft_random_generator(size_t start, size_t stop);
@ -26,13 +31,14 @@ void ft_freer_ultimate(size_t len, ...);
char *ft_strgen(char c, size_t len);
char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...);
int ft_is_in(char *str, char c);
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
int ft_is_in(const char *str, char c);
char **ft_tabrealloc(char **tab, size_t current_size,
size_t new_size);
char *ft_strndup(const char *src, size_t n);
ssize_t ft_strchri(char *str, char c);
int ft_contain_only_str(char *str, char *to_find);
int ft_contain_only(char *str, char c);
int ft_strcmp(char *s1, char *s2);
int ft_contain_only_str(const char *str, const char *to_find);
int ft_contain_only(const char *str, char c);
int ft_strcmp(const char *s1, const char *s2);
void ft_swap(void *a, void *b);
void ft_swap_int(int *a, int *b);
void ft_swap_char(char *a, char *b);

38
libftx/extra/ft_atoul.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoul.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:14:34 by cchauvet #+# #+# */
/* Updated: 2023/05/03 11:03:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
unsigned long ft_atoul(const char *str)
{
unsigned long out;
char sign;
size_t i;
sign = 1;
i = 0;
while (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
sign = -1;
if (str[i] == '+')
sign = 1;
i++;
}
out = 0;
while (str[i] >= '0' && str[i] <= '9')
{
out = out * 10 + str[i] - 48;
i++;
}
return (out * sign);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoul_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 14:43:33 by cchauvet #+# #+# */
/* Updated: 2023/05/02 13:15:12 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./extra.h"
int ft_atoul_check(const char str[])
{
size_t i;
unsigned long num;
num = 0;
i = 0;
while (str[i] != '\0')
{
if (str[i] > '9' || str[i] < '0')
return (0);
if (num > num * 10 + str[i] - '0')
return (0);
num = num * 10 + str[i] - 48;
i++;
}
return (1);
}

View File

@ -6,13 +6,13 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/12 16:28:20 by cchauvet #+# #+# */
/* Updated: 2023/01/12 16:31:22 by cchauvet ### ########.fr */
/* Updated: 2023/04/26 13:07:06 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
int ft_contain_only(char *str, char c)
int ft_contain_only(const char *str, char c)
{
size_t i;
@ -26,7 +26,7 @@ int ft_contain_only(char *str, char c)
return (1);
}
int ft_contain_only_str(char *str, char *to_find)
int ft_contain_only_str(const char *str, const char *to_find)
{
size_t i;

29
libftx/extra/ft_count.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_count.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/16 18:27:16 by cchauvet #+# #+# */
/* Updated: 2023/06/16 16:29:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./extra.h"
size_t ft_count(const char *str, char c)
{
size_t i;
size_t out;
i = 0;
out = 0;
while (str[i] != '\0')
{
if (str[i] == c)
out++;
i++;
}
return (out);
}

View File

@ -6,13 +6,13 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 20:29:53 by cchauvet #+# #+# */
/* Updated: 2023/01/08 12:15:08 by cchauvet ### ########.fr */
/* Updated: 2023/04/26 13:07:30 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
int ft_is_in(char *str, char c)
int ft_is_in(const char *str, char c)
{
int i;

View File

@ -6,13 +6,13 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 19:20:47 by cchauvet #+# #+# */
/* Updated: 2023/01/05 13:20:20 by cchauvet ### ########.fr */
/* Updated: 2023/04/26 12:23:52 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
int ft_strcmp(char *s1, char *s2)
int ft_strcmp(const char *s1, const char *s2)
{
int i;

View File

@ -1,22 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* manage.c :+: :+: :+: */
/* ft_tablen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 12:52:14 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:31:50 by erey-bet ### ########.fr */
/* Created: 2023/04/26 12:12:19 by cchauvet #+# #+# */
/* Updated: 2023/04/28 12:58:24 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
#include "./extra.h"
void manage(mlx_key_data_t keydata, void *param)
size_t ft_tablen(const void **tab)
{
t_game *game;
size_t i;
game = param;
if (manage_keys(keydata, game))
raycasting(game);
i = 0;
while (tab[i] != NULL)
i++;
return (i);
}

View File

@ -6,7 +6,7 @@
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/12/14 15:49:18 by cchauvet #+# #+# #
# Updated: 2023/01/05 19:22:40 by cchauvet ### ########.fr #
# Updated: 2023/06/01 13:23:51 by erey-bet ### ########.fr #
# #
# **************************************************************************** #
@ -16,7 +16,7 @@ OBJS = ${SRCS:.c=.o}
NAME = get_next_line.a
CFLAGS = -Wall -Werror -Wextra -g
CFLAGS = -fPIE -Wall -Werror -Wextra -g
CC = clang

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2023/02/21 13:28:19 by cchauvet ### ########.fr */
/* Updated: 2023/05/02 13:17:30 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,9 @@
# include <unistd.h>
# include <stdarg.h>
# include "./libft/libft.h"
# include "./extra/extra.h"
size_t ft_tablen(const void **tab);
char *ft_ultoa_base(unsigned long long n, char *base);
int ft_printf(const char *format, ...);
int ft_eprintf(const char *format, ...);
@ -27,54 +29,17 @@ size_t ft_random_generator(size_t start, size_t stop);
void ft_freer_tab_ultimate(size_t len, ...);
void ft_freer_ultimate(size_t len, ...);
char *ft_strgen(char c, size_t len);
int ft_is_in(char *str, char c);
int ft_is_in(const char *str, char c);
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
int ft_contain_only_str(char *str, char *to_find);
int ft_contain_only(char *str, char c);
int ft_contain_only_str(const char *str, const char *to_find);
int ft_contain_only(const char *str, char c);
char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...);
int ft_strcmp(char *s1, char *s2);
int ft_strcmp(const char *s1, const char *s2);
ssize_t ft_strchri(char *str, char c);
char *ft_strndup(const char *src, size_t n);
void ft_swap(void *a, void *b);
void ft_swap_int(int *a, int *b);
void ft_swap_char(char *a, char *b);
/* void *ft_cancel(void **tab, size_t len); */
/* int ft_atoi(const char *nptr); */
/* void ft_bzero(void *s, size_t n); */
/* void *ft_calloc(size_t nmemb, size_t size); */
/* int ft_isalnum(int c); */
/* int ft_isalpha(int c); */
/* int ft_isascii(int c); */
/* int ft_isdigit(int c); */
/* int ft_isprint(int c); */
/* void *ft_memchr(const void *s, int c, size_t n); */
/* int ft_memcmp(const void *s1, const void *s2, size_t n); */
/* void *ft_memcpy(void *dest, const void *src, size_t n); */
/* void *ft_memmove(void *dest, const void *src, size_t n); */
/* void *ft_memset(void *s, int c, size_t n); */
/* char *ft_strchr(const char *s, int c); */
/* char *ft_strdup(const char *s); */
/* size_t ft_strlcat(char *dst, const char *src, size_t size); */
/* size_t ft_strlcpy(char *dst, const char *src, size_t size); */
/* size_t ft_strlen(const char *s); */
/* int ft_strncmp(const char *s1, const char *s2, size_t n); */
/* char *ft_strnstr(const char *big, const char *little, size_t len); */
/* char *ft_strrchr(const char *s, int c); */
/* int ft_tolower(int c); */
/* int ft_toupper(int c); */
/* char *ft_substr(char const *s, unsigned int start, size_t len); */
/* char *ft_strjoin(char const *s1, char const *s2); */
/* char *ft_strtrim(char const *s1, char const *set); */
/* char **ft_split(char const *s, char c); */
/* char *ft_itoa(int n); */
/* char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); */
/* void ft_striteri(char *s, void (*f)(unsigned int, char*)); */
/* void ft_putchar_fd(char c, int fd); */
/* void ft_putstr_fd(char *s, int fd); */
/* void ft_putendl_fd(char *s, int fd); */
/* void ft_putnbr_fd(int n, int fd); */
#endif

View File

@ -6,7 +6,7 @@
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2023/02/01 16:19:19 by cchauvet ### ########.fr #
# Updated: 2023/06/01 13:24:02 by erey-bet ### ########.fr #
# #
# **************************************************************************** #
@ -18,7 +18,7 @@ OBJS = ${SRCS:.c=.o}
NAME = ft_printf.a
CFLAG = -Wall -Werror -Wextra -g
CFLAG = -fPIE -Wall -Werror -Wextra -g
%.o: %.c
${CC} ${CFLAG} -c -o $@ $<

29
main.c
View File

@ -6,41 +6,28 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:44:55 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:43:10 by erey-bet ### ########.fr */
/* Updated: 2023/05/19 17:03:29 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "cube3D.h"
#include "map/map.h"
int main(int argc, char **argv)
{
t_map *map;
t_map map;
if (argc != 2)
{
printf("No argument");
ft_eprintf("No argument");
return (1);
}
/*map = map_parsing(argv[1]);
if (!map)
if (map_parsing(argv[1], &map)
|| start_game(map))
{
map_freer(&map);
return (2);
}*/
(void)argv;
map = malloc(sizeof(t_map));
map->map = malloc(sizeof(char *) * 5);
map->map[0] = "11111";
map->map[1] = "10001";
map->map[2] = "10S01";
map->map[3] = "10101";
map->map[4] = "11111";
map->size_x = 5;
map->size_y = 5;
map->ply_x = 3;
map->ply_y = 3;
if (start_game(map))
{
return (3);
}
map_freer(&map);
return (0);
}

28
map/map.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 14:50:15 by cchauvet #+# #+# */
/* Updated: 2023/05/16 14:50:16 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "map.h"
#include "./parsing_private.h"
void map_freer(t_map *map)
{
size_t i;
ft_freer_tab_ultimate(1, map->map);
i = 0;
while (i < 4)
{
if (map->img_path[i] != NULL)
free(map->img_path[i]);
i++;
}
}

34
map/map.h Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 14:50:41 by cchauvet #+# #+# */
/* Updated: 2023/05/16 14:50:42 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAP_H
# define MAP_H
# include<sys/types.h>
// img: 0=Nord, 1=WEST, 2=SOUTH, 3=EAST;
typedef struct s_map
{
char **map;
char *img_path[4];
long long color_bot;
long long color_top;
int size_x;
int size_y;
double ply_x;
double ply_y;
char direction;
} t_map;
void map_freer(t_map *map);
#endif

110
map/parsing.c Normal file
View File

@ -0,0 +1,110 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 14:53:00 by cchauvet #+# #+# */
/* Updated: 2023/05/16 14:54:02 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./parsing_private.h"
#include "map.h"
#include <stddef.h>
#include <stdlib.h>
static ssize_t get_nb_line(const char *path)
{
int fd;
char readed[1];
size_t i;
fd = open(path, O_RDONLY);
if (fd == -1)
return (-1);
i = 1;
while (read(fd, readed, 1))
{
if (readed[0] == '\n')
i++;
}
close(fd);
return (i);
}
static char **read_map(const char *path)
{
int fd;
size_t i;
char **map;
map = malloc(sizeof(char *) * (get_nb_line(path) + 1));
if (map == NULL)
return (NULL);
fd = open(path, O_RDONLY);
if (fd == -1)
{
free(map);
return (NULL);
}
i = 0;
map[i] = get_next_line(fd);
while (map[i] != NULL)
{
map[i][ft_strlen(map[i]) - 1] = '\0';
i++;
map[i] = get_next_line(fd);
}
close(fd);
return (map);
}
void header_freer(char ***header)
{
size_t i;
size_t j;
i = 0;
while (header[i] != NULL)
{
j = 0;
while (header[i][j] != NULL)
{
free(header[i][j]);
j++;
}
free(header[i]);
i++;
}
free(header);
}
int map_parsing(const char *path, t_map *map)
{
char **file_content;
char ***header;
size_t header_size;
ft_bzero(map, sizeof(t_map));
if (parsing_meta(path))
return (1);
file_content = read_map(path);
if (file_content == NULL)
{
ft_eprintf("map: file error");
return (1);
}
header = get_header((const char **) file_content, &header_size);
if (header_is_valid(header, map) == 1)
{
header_freer(header);
ft_freer_tab_ultimate(1, file_content);
return (1);
}
header_freer(header);
map->map = get_body((const char **) file_content, header_size);
ft_freer_tab_ultimate(1, file_content);
return (body_is_valid(map) == 0);
}

19
map/parsing.h Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 14:54:09 by cchauvet #+# #+# */
/* Updated: 2023/05/16 14:54:10 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSING_H
# define PARSING_H
# include "map.h"
int map_parsing(const char *path, t_map *map);
#endif

85
map/parsing_body.c Normal file
View File

@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_body.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 14:51:13 by cchauvet #+# #+# */
/* Updated: 2023/06/16 14:39:18 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing_private.h"
char **get_body(const char **file_content, size_t header_size)
{
char **body;
int i;
int len;
len = 0;
while (file_content[header_size + len] != NULL)
len++;
body = malloc((len + 1) * sizeof(char *));
if (body == NULL)
return (NULL);
body[len] = NULL;
i = 0;
while (i < len)
{
body[i] = ft_strdup(file_content[header_size + i]);
if (body[i] == NULL)
{
ft_cancel((void **) body, i);
return (NULL);
}
i++;
}
return (body);
}
int get_spawn_position(const char **body, double *spawn_x, double *spawn_y)
{
int x;
int y;
*spawn_x = -1;
*spawn_y = -1;
y = 0;
while (body[y] != NULL)
{
x = 0;
while (body[y][x] != '\0')
{
if (ft_is_in("NSEW", body[y][x]))
{
if (*spawn_x != -1)
return (1);
*spawn_x = x;
*spawn_y = y;
}
x++;
}
y++;
}
if (*spawn_x == -1)
return (2);
return (0);
}
int map_is_in_one_part(const char **body)
{
int y;
y = ft_tablen((const void **) body) - 1;
while (body[y][0] == '\0')
y--;
while (y > 0)
{
if (body[y][0] == '\0')
return (1);
y--;
}
return (0);
}

129
map/parsing_body2.c Normal file
View File

@ -0,0 +1,129 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_body2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 16:30:55 by cchauvet #+# #+# */
/* Updated: 2023/06/16 14:47:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing_private.h"
static int map_surround(const char **body)
{
size_t y;
size_t x;
y = 0;
while (body[y] != NULL)
{
x = 0;
while (body[y][x] != '\0')
{
if (body[y][x] == '0')
{
if (body[y + 1] == NULL || (y == 0)
|| (body[y][x + 1] == '\0') || (x == 0)
|| (x >= ft_strlen(body[y - 1])) || (body[y - 1][x] == ' ')
|| (x >= ft_strlen(body[y + 1])) || (body[y + 1][x] == ' ')
|| (body[y][x - 1] == ' ' || body[y][x - 1] == '\0')
|| (body[y][x + 1] == ' ' || body[y][x + 1] == '\0'))
return (1);
}
x++;
}
y++;
}
return (0);
}
static int body_contain_normal_char(const char **body)
{
unsigned int y;
unsigned int x;
y = 0;
while (body[y] != NULL)
{
x = 0;
while (body[y][x] != '\0')
{
if (!ft_is_in(" 01NEWS", body[y][x]))
{
ft_eprintf("map: %c: invalid character at rows=%u, colums=%u",
body[y][x], y, x);
return (0);
}
x++;
}
y++;
}
return (1);
}
void get_size(const char **body, t_map *map)
{
int y;
int x;
map->size_x = 0;
y = 0;
while (body[y] != NULL)
{
x = ft_strlen(body[y]);
if (x > map->size_x)
map->size_x = x;
y++;
}
map->size_y = y;
}
int body_is_valid2(t_map *map)
{
int error;
error = get_spawn_position((const char **) map->map,
&map->ply_x, &map->ply_y);
if (error == 1)
{
ft_eprintf("map: spawn position: multiple definition");
return (0);
}
else if (error == 2)
{
ft_eprintf("map: spawn position: not defined");
return (0);
}
map->direction = map->map[(int) map->ply_y][(int) map->ply_x];
map->map[(int) map->ply_y][(int) map->ply_x] = '0';
error = map_is_in_one_part((const char **)map->map);
if (error)
{
ft_eprintf("map: splitted");
return (0);
}
if (body_contain_normal_char((const char **)map->map) == 0)
return (0);
return (1);
}
int body_is_valid(t_map *map)
{
int error;
if (map->map == NULL)
return (0);
if (body_is_valid2(map) == 0)
return (0);
error = map_surround((const char **)map->map);
if (error)
{
ft_eprintf("map: not surrounded");
return (0);
}
get_size((const char **)map->map, map);
return (1);
}

104
map/parsing_header.c Normal file
View File

@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_header.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 16:28:10 by cchauvet #+# #+# */
/* Updated: 2023/05/16 16:54:16 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./parsing_private.h"
#include <sys/types.h>
int get_token(const char *key)
{
if (ft_strcmp(key, "NO") == 0)
return (1);
if (ft_strcmp(key, "SO") == 0)
return (3);
if (ft_strcmp(key, "WE") == 0)
return (2);
if (ft_strcmp(key, "EA") == 0)
return (4);
if (ft_strcmp(key, "F") == 0)
return (5);
if (ft_strcmp(key, "C") == 0)
return (6);
return (0);
}
static int is_header(const char *line)
{
char **list;
list = ft_split(line, ' ');
if (list == NULL)
{
ft_eprintf("malloc failed");
ft_freer_tab_ultimate(1, list);
return (0);
}
if (ft_tablen((const void **) list) != 2)
{
ft_freer_tab_ultimate(1, list);
return (0);
}
if (get_token(list[0]) == 0)
{
ft_freer_tab_ultimate(1, list);
return (0);
}
ft_freer_tab_ultimate(1, list);
return (1);
}
static size_t get_header_size(const char **file_content, size_t *header_size)
{
ssize_t i;
size_t len;
len = 0;
i = -1;
while (file_content[++i] != NULL)
{
if (ft_contain_only(file_content[i], ' '))
continue ;
if (!is_header(file_content[i]))
break ;
len++;
}
*header_size = i;
return (len);
}
char ***get_header(const char **file_content, size_t *header_size)
{
char ***header;
size_t len;
size_t i;
ssize_t y;
len = get_header_size(file_content, header_size);
header = malloc(sizeof(char *) * (len + 1));
if (header == NULL)
return (NULL);
header[len] = NULL;
y = -1;
i = 0;
while (++y < (ssize_t) len)
{
while (ft_contain_only(file_content[i], ' '))
i++;
header[y] = ft_split(file_content[i], ' ');
if (header[y] == NULL)
{
ft_cancel((void **) header, y);
return (NULL);
}
i++;
}
return (header);
}

98
map/parsing_header2.c Normal file
View File

@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_header2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 17:28:58 by cchauvet #+# #+# */
/* Updated: 2023/06/16 16:31:06 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./parsing_private.h"
int set_texture(t_map *map, int token, const char *key, const char *value)
{
if (map->img_path[token - 1] != NULL)
{
ft_eprintf("redefinition of %s", key);
return (1);
}
map->img_path[token - 1] = ft_strdup(value);
if (map->img_path[token - 1] == NULL)
return (1);
return (0);
}
static unsigned int get_rgb(unsigned char red, unsigned char green,
unsigned char blue)
{
return (red << 24 | green << 16 | blue << 8 | 255);
}
static long long get_color(const char **tab)
{
long long rgb[3];
size_t i;
i = 0;
while (i < 3)
{
if (ft_atoul_check(tab[i]) == 0)
{
ft_eprintf("map: %s color invalid format", tab[i]);
return (-1);
}
rgb[i] = ft_atoul(tab[i]);
if (rgb[i] > 255)
{
ft_eprintf("map: %s is a to high to be a color", tab[i]);
return (-1);
}
i++;
}
return (get_rgb((unsigned char) rgb[0],
(unsigned char) rgb[1], (unsigned char) rgb[2]));
}
int set_color(long long *color, const char *key, const char *value)
{
char **tab;
if (*color != -1)
{
ft_eprintf("map: %s redefinition", key);
return (1);
}
if (ft_count(value, ',') != 2)
{
ft_eprintf("map: invalid format %s %s", key, value);
return (1);
}
tab = ft_split(value, ',');
if (tab == NULL)
return (1);
*color = get_color((const char **) tab);
ft_freer_tab_ultimate(1, tab);
return (*color == -1);
}
int check_header_is_complete(t_map *map)
{
if (map->img_path[0] == NULL)
ft_eprintf("map: incomplete: Nord texture is missing");
else if (map->img_path[1] == NULL)
ft_eprintf("map: incomplete: WEST texture is missing");
else if (map->img_path[2] == NULL)
ft_eprintf("map: incomplete: SOUTH texture is missing");
else if (map->img_path[3] == NULL)
ft_eprintf("map: incomplete: EAST texture is missing");
else if (map->color_bot == -1)
ft_eprintf("map: incomplete: floor color is missing");
else if (map->color_top == -1)
ft_eprintf("map: incomplete: roof color is missing");
else
return (0);
return (1);
}

56
map/parsing_header3.c Normal file
View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_header3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 16:41:51 by cchauvet #+# #+# */
/* Updated: 2023/05/16 19:07:12 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./parsing_private.h"
static int set_header_data(char ***header, size_t i, t_map *map)
{
int token;
token = get_token(header[i][0]);
if (token > 0 && token < 5)
{
if (set_texture(map, token, header[i][0], header[i][1]))
return (1);
}
else if (token == 5)
{
if (set_color(&map->color_bot,
header[i][0], header[i][1]))
return (1);
}
else if (token == 6)
{
if (set_color(&map->color_top,
header[i][0], header[i][1]))
return (1);
}
return (0);
}
int header_is_valid(char ***header, t_map *map)
{
size_t i;
if (header == NULL)
return (1);
map->color_bot = -1;
map->color_top = -1;
i = 0;
while (i < ft_tablen((const void **) header))
{
if (set_header_data(header, i, map))
return (1);
i++;
}
return (check_header_is_complete(map));
}

32
map/parsing_meta.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_meta.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 16:28:39 by cchauvet #+# #+# */
/* Updated: 2023/05/16 16:28:41 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./parsing_private.h"
static int name_check(const char *path)
{
size_t len;
len = ft_strlen(path);
if (len < 4
|| (ft_strcmp(".cub", path + len - 4) != 0))
{
ft_eprintf("map: name doesn't finish by .cub");
return (1);
}
return (0);
}
int parsing_meta(const char *path)
{
return (name_check(path));
}

30
map/parsing_private.h Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_private.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 16:27:57 by cchauvet #+# #+# */
/* Updated: 2023/05/16 18:46:21 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSING_PRIVATE_H
# define PARSING_PRIVATE_H
# include <sys/types.h>
# include "../libftx/libftx.h"
# include "./parsing.h"
int parsing_meta(const char *path);
int get_token(const char *str);
char ***get_header(const char **file_content, size_t *header_size);
int header_is_valid(char ***header, t_map *map);
char **get_body(const char **file_content, size_t header_size);
int body_is_valid(t_map *map);
int get_spawn_position(const char **body, double *x, double *y);
int map_is_in_one_part(const char **body);
int set_texture(t_map *map, int token, const char *key, const char *value);
int set_color(long long *color, const char *key, const char *value);
int check_header_is_complete(t_map *map);
#endif

View File

@ -0,0 +1,23 @@
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture
F 220,100,0
C 225,30,0
1111111111111111111111111
1000000000110000000000001
01011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111
bozoman

View File

@ -0,0 +1,9 @@
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture
F 200,100,0
C 225,30,0
N

22
maps/body_surrond2.cub Normal file
View File

@ -0,0 +1,22 @@
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture
F 220,100,0
C 225,30,0
1111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 1011110100010
11111111 1111111 111111111111

10
maps/header_missing.cub Normal file
View File

@ -0,0 +1,10 @@
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
EA ./path_to_the_east_texture
F 200,100,0
C 225,30,0
1
1N1
1

View File

@ -0,0 +1,23 @@
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
WE ./path_to_the_west_texture
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture
F 220,100,0
C 225,30,0
1111111111111111111111111
1000000000110000000000001
01011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111

20
maps/test1.cub Normal file
View File

@ -0,0 +1,20 @@
NO ./assets/red.png
SO ./assets/blue.png
WE ./assets/green.png
EA ./assets/purple.png
F 200,100,0
C 225,30,0
11111111111111111111111111
101100000111000000000000001
101100000111000000000000001
101100000111000000000000001 1111
1011000001110000000000000011111100001
101100000111000000000000000000000N001
1011000001110000000000000011111100001
101100000111000000000000001 10101
101100000111000000000000001 11111
101100000111000000000000001
111111111111111111111111111

View File

@ -0,0 +1,23 @@
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
WE ./path_to_the_west_texture
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture
F 220,100,0
C 225,30,0
1111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N00
11110111 1110101 101111010001
11111111 1111111 111111111111

View File

@ -0,0 +1,22 @@
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture
F 220,100,0
C 225,30,0
1111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N00
11110111 1110101 101111010001
11111111 1111111 111111111111

23
maps/test_splitted.cub Normal file
View File

@ -0,0 +1,23 @@
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture
F 220,100,0
C 225,30,0
1111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111