Compare commits

..

No commits in common. "master" and "sadjigui" have entirely different histories.

51 changed files with 806 additions and 112447 deletions

View File

@ -6,32 +6,18 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/02/13 16:27:49 by apommier #+# #+# #
# Updated: 2022/06/16 16:38:38 by apommier ### ########.fr #
# Updated: 2022/05/04 18:08:34 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = cub3D
NAME = Cub3D
SRCS = srcs/main.c\
srcs/cast_ray/get_texture_array.c\
srcs/cast_ray/cast_ray.c\
srcs/cast_ray/find_wall.c\
srcs/cast_ray/utils.c\
srcs/cast_ray/set_back.c\
srcs/cast_ray/ray_casting.c\
srcs/key_event/key.c\
srcs/utils/cub_utils.c\
srcs/utils/quit_game.c\
srcs/parsing/parse_map.c\
srcs/parsing/is_a_file.c\
srcs/parsing/check_map.c\
srcs/parsing/check_map_utils.c\
srcs/parsing/utils_parsing.c\
srcs/parsing/check_color_texture.c
srcs/cast_ray.c\
srcs/utils.c
OBJS = ${SRCS:.c=.o}
CC = gcc
LIB = -L ./mlx -lmlx -lXext -lX11 -lm
CFLAGS = -Wall -Wextra -Werror -g
CFLAGS = -Wall -Wextra -g
RM = rm -rf
LIBFT = ./libft

131
README.md
View File

@ -1,131 +0,0 @@
# Cub3D
## Description
Cub3D est un projet de l'École 42 qui consiste à créer un moteur de rendu 3D inspiré du jeu Wolfenstein 3D. Le projet utilise la technique du raycasting pour créer un environnement 3D navigable en temps réel.
## Objectifs pédagogiques
- Comprendre les bases du rendu graphique 3D
- Maîtriser la technique du raycasting
- Manipuler des textures et des sprites
- Gérer les événements clavier et souris
- Optimiser les performances en temps réel
## Fonctionnalités
- **Rendu 3D** en temps réel avec raycasting
- **Déplacement fluide** du joueur (WASD)
- **Rotation de la caméra** avec les flèches directionnelles
- **Textures murales** différenciées selon l'orientation
- **Système de collision** avec les murs
- **Parsing de map** depuis un fichier .cub
- **Gestion des couleurs** pour le sol et le plafond
## Technologies utilisées
- **Langage** : C
- **Bibliothèque graphique** : MiniLibX
- **Système de build** : Makefile
- **Mathématiques** : Trigonométrie, géométrie vectorielle
## Prérequis
- GCC compiler
- MiniLibX library
- Make
- Système X11 (Linux) ou équivalent
## Installation et compilation
### 1. Cloner le projet
```bash
git clone <repository-url>
cd Cub3D
```
### 2. Compiler le projet
```bash
make
```
### 3. Nettoyer les fichiers objets
```bash
make clean # Supprime les .o
make fclean # Supprime tout + l'exécutable
make re # Recompile tout
```
## Utilisation
### Lancement du jeu
```bash
./cub3D maps/map.cub
```
### Contrôles
- **W, A, S, D** : Déplacement
- **Flèches gauche/droite** : Rotation de la caméra
- **ESC** : Quitter le jeu
### Format de map (.cub)
```
NO ./textures/north_texture.xpm
SO ./textures/south_texture.xpm
WE ./textures/west_texture.xpm
EA ./textures/east_texture.xpm
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
```
## Structure du projet
```
Cub3D/
├── Makefile # Compilation
├── includes/ # Headers (.h)
├── srcs/ # Code source
│ ├── main.c
│ ├── cast_ray/ # Algorithme de raycasting
│ ├── key_event/ # Gestion des événements
│ ├── parsing/ # Lecture et validation de la map
│ └── utils/ # Fonctions utilitaires
├── libft/ # Bibliothèque personnelle
├── mlx/ # MiniLibX
├── map/ # Fichiers de cartes
└── sprite/ # Textures et sprites
```
## Algorithme de raycasting
Le raycasting fonctionne en :
1. **Lance un rayon** pour chaque colonne de l'écran
2. **Calcule la distance** jusqu'au premier mur touché
3. **Détermine la hauteur** du mur à afficher
4. **Applique la texture** appropriée selon l'orientation
## Normes et contraintes 42
- Code conforme à la **Norme 42**
- Gestion stricte de la **mémoire** (pas de leaks)
- **Gestion d'erreurs** robuste
- Interdiction des **variables globales**
- Utilisation limitée des **fonctions autorisées**
## Compétences développées
- Programmation graphique en C
- Mathématiques appliquées (trigonométrie)
- Optimisation d'algorithmes
- Gestion mémoire rigoureuse
- Architecture logicielle
## Auteur
Alexandre Pommier (apommier) - École 42

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:30:59 by apommier #+# #+# */
/* Updated: 2022/06/16 16:38:18 by apommier ### ########.fr */
/* Updated: 2022/05/05 02:58:26 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,169 +25,42 @@
# define PI 3.1415926535
typedef struct s_root {
int size;
int height;
int error;
} t_root;
typedef struct ray_info{
double ty;
double tx;
double mp;
double dist;
int index;
int wall_type;
int pixel;
char texture_type;
} t_ray;
typedef struct all_wall_texture{
unsigned char *north;
unsigned char *east;
unsigned char *west;
unsigned char *south;
} t_sprite;
typedef struct s_color{
unsigned char r;
unsigned char g;
unsigned char b;
int set;
} t_color;
typedef struct map_information{
char **map;
t_sprite texture;
t_color floor;
t_color sky;
int floor;
int sky;
char *simple_map;
int size;
int x;
int y;
int max;
int error;
} t_map_info;
} map_info;
typedef struct player_position
{
double x;
double y;
double angle;
double vx;
double vy;
int front;
int side;
int angle_side;
int shift;
} t_player;
typedef struct need_to_be_free
{
char **tab;
char **tab_two;
unsigned char *str;
char *string;
int fd;
} t_to_free;
float x;
float y;
float angle;
float vx;
float vy;
} player;
typedef struct s_data {
void *mlx;
void *mlx_win;
void *mlx_test;
int bits_per_pixel;
int size_line;
int endian;
char *buffer;
void *mlx_win_test;
char **double_map;
void *image;
t_map_info map;
t_player player;
t_to_free to_be_free;
int err;
map_info map;
player player;
} t_data;
typedef struct draw_ray_3D {
double line_height;
double line_offset;
int x;
double y;
int mx;
int my;
int color;
double gap;
double myy;
} t_draw_ray_info;
typedef struct draw_ray_var {
double ray_angle;
double ray_y;
double ray_x;
double next_x;
double next_y;
double dist_v;
double dist_h;
double dist_f;
double vx;
double vy;
int count;
double tan;
int nb_ray;
int my;
int mx;
int mp;
char vertical_type;
char horizontal_type;
} t_var_draw_ray;
char *transform_map(char **double_map, t_data *img);
int check_texture_color(char **tab, t_data *img);
int check_map(char **av, t_data *img);
void ft_exit(char *str, t_data *img);
void find_angle(char c, t_data *img);
void size_line(char *str, t_data *img);
void check_inner(char **map, t_data *img);
void check_border(char **tab, t_data *img);
void verify_texture_color(t_data *img);
void error_msg(t_data *img);
void inter_map(char **split, char **tmp, t_data *img);
void check_empty_line(char **split, t_data *img, int *i);
void check_zero_one(char **split, t_data *img);
char *charge_new(t_data *img, char **map, char **tmp_map);
char **isafile(char **av, t_data *img);
void set_back(t_data *img);
unsigned char *get_texture(char type, char *path, t_data *img);
int get_color(char one, char two, char three);
void set_pixel(t_data *img, int color, int x, int y);
int set_color_texture(t_ray *ray, t_sprite img);
void get_file_size(char *path, t_data *img, int *count, int fd);
void get_map_size(char *path, t_data *img, int *count, int fd);
char next_space(char *str, int i);
int next_space_index(char *str, int i);
double reset_angle(double angle);
double deg_to_rad(double angle);
int reset_angle(int angle);
float deg_to_rad(int angle);
void draw_ray(t_data *img);
void set_info_draw(t_var_draw_ray *info);
void print_ray(t_data *img);
void vertical_ray(t_var_draw_ray *vr, t_data *img);
void horizontal_ray(t_var_draw_ray *hr, t_data *img);
int update_pos(t_data *img);
//int key_press(int code, t_data *img);
int key_press(int code, t_data *img);
int quit_game(t_data *img);
void ft_error(char *error_msg);
void check_dir(char *path, t_data *img);
int key_pressed(int type, t_data *img);
int key_released(int type, t_data *img);
void print_player(player player, t_data *img);
void print_map(map_info map, t_data *img);
#endif

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 18:16:18 by apommier #+# #+# */
/* Updated: 2022/06/15 17:49:23 by apommier ### ########.fr */
/* Updated: 2022/05/02 18:16:28 by apommier ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:09:17 by apommier #+# #+# */
/* Updated: 2022/05/06 20:01:08 by apommier ### ########.fr */
/* Updated: 2022/01/18 06:50:22 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,8 +21,6 @@ long ft_atoi(const char *nptr)
minus = 1;
nbr = 0;
i = 0;
if (!nptr)
return (0);
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == 32)
i++;
if (nptr[i] == '+')

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/09 19:58:04 by apommier #+# #+# */
/* Updated: 2022/06/15 13:14:54 by apommier ### ########.fr */
/* Updated: 2022/01/17 11:28:39 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,7 @@ void ft_lstclear(t_list **lst, void (*del)(void*))
while (*lst)
{
chr = (*lst)->next;
del((*lst)->content);
del((*lst)->nbr);
free(*lst);
*lst = chr;
}

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/09 19:54:40 by apommier #+# #+# */
/* Updated: 2022/06/15 13:15:15 by apommier ### ########.fr */
/* Updated: 2020/12/12 09:10:11 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,6 @@ void ft_lstdelone(t_list *lst, void (*del)(void*))
{
if (!lst)
return ;
del(lst->content);
del(lst->nbr);
free(lst);
}

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/09 20:14:47 by apommier #+# #+# */
/* Updated: 2022/06/15 13:15:24 by apommier ### ########.fr */
/* Updated: 2020/12/11 17:47:39 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@ void ft_lstiter(t_list *lst, void (*f)(void *))
{
while (lst)
{
f(lst->content);
f(lst->nbr);
lst = lst->next;
}
}

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/11 00:10:12 by apommier #+# #+# */
/* Updated: 2022/06/15 13:15:33 by apommier ### ########.fr */
/* Updated: 2020/12/13 22:37:30 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,7 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
begin = 0;
while (lst)
{
new = ft_lstnew(f(lst->content));
new = ft_lstnew(f(lst->nbr));
if (!new)
{
ft_lstclear(&begin, *del);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/09 01:06:20 by apommier #+# #+# */
/* Updated: 2022/06/15 13:15:45 by apommier ### ########.fr */
/* Updated: 2022/01/17 11:29:09 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,8 @@ t_list *ft_lstnew(void *content)
new = (t_list *)malloc(sizeof(t_list));
if (!new)
return (0);
new->content = content;
new->swap = 0;
new->nbr = content;
new->next = 0;
return (new);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/20 21:44:01 by apommier #+# #+# */
/* Updated: 2022/05/06 19:29:27 by apommier ### ########.fr */
/* Updated: 2022/01/21 08:07:04 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,27 +39,3 @@ char *ft_strjoin(char *save, char *s2)
dest[j] = 0;
return (dest);
}
char *ft_strjoin_delete(char *save, char *s2)
{
char *dest;
int i;
int j;
i = 0;
j = 0;
if (!save && !s2)
return (0);
dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1);
while (save && save[i])
dest[j++] = save[i++];
i = 0;
while (s2 && s2[i])
dest[j++] = s2[i++];
dest[j] = 0;
if (save)
free(save);
if (s2)
free(s2);
return (dest);
}

View File

@ -14,15 +14,12 @@
char *ft_free(char *save, int *end)
{
if (!end || !*end)
if (!*end)
{
if (save)
free(save);
if (end)
free(end);
return (0);
}
if (end)
free(end);
return (save);
}
@ -84,10 +81,7 @@ char *get_next_line(int fd)
line = 0;
if (fd < 0)
{
ft_free(save, 0);
return (0);
}
end = malloc(sizeof(int *));
*end = 1;
if (save == NULL)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/11 03:21:27 by apommier #+# #+# */
/* Updated: 2022/06/15 13:14:31 by apommier ### ########.fr */
/* Updated: 2022/05/02 18:17:12 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,8 @@
typedef struct t_slist
{
void *content;
void *nbr;
int index;
int swap;
struct t_slist *next;
} t_list;
@ -49,7 +50,6 @@ int ft_strncmp(const char *s1, const char *s2, size_t n);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strjoin(char *save, char *s2);
char *ft_strjoin_delete(char *save, char *s2);
char *ft_strnstr(const char *big, const char *little, size_t len);
long ft_atoi(const char *nptr);
void *ft_calloc(size_t nmenb, size_t size);

View File

@ -1,12 +0,0 @@
NO ./sprite/magma.ppm
SO ./sprite/magma.ppm
EA ./sprite/magma.ppm
WE ./sprite/magma.ppm
F 210,37,0
C 24,231,27
1111
1001
10E1
1111

8
map/map Normal file
View File

@ -0,0 +1,8 @@
11111111
10100001
10100001
10000001
10000001
10100101
10000101
11111111

View File

@ -1,14 +0,0 @@
NO ./sprite/magma.ppm
SO ./sprite/magma.ppm
EA ./sprite/magma.ppm
WE ./sprite/magma.ppm
F 210,37,0
C 24,231,27
111111111111111111
11110000000001111
1100000000000111111111
1111000000000000000000001
11000000E0000000011111111
111111111111111111

View File

@ -1,14 +0,0 @@
NO ./sprite/brick_wall.ppm
SO ./sprite/brick_wall.ppm
EA ./sprite/brick_wall.ppm
WE ./sprite/brick_wall.ppm
11111111
11111001
10131001
10111001
1000E001
11000001
11000001
11111111

View File

@ -1,22 +0,0 @@
NO ./sprite/magma.ppm
SO ./sprite/stone_bricks.ppm
EA ./sprite/glowstone.ppm
WE ./sprite/obsidian.ppm
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

View File

@ -1,22 +0,0 @@
NO ./sprite/mossy_stone_bricks.ppm
SO ./sprite/stone_bricks.ppm
EA ./sprite/mossy_cobblestone.ppm
WE ./sprite/obsidian.ppm
F 210,37,0
C 24,231,27
1111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011100001110111111111111
11110111111 11011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111

View File

@ -1,20 +0,0 @@
NO ./sprite/brick_wall.ppm
SO ./sprite/magma.ppm
EA ./sprite/obsidian.ppm
WE ./sprite/glowstone.ppm
1111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111

View File

@ -1,22 +0,0 @@
NO ./sprite/syd.ppm
SO ./sprite/syd.ppm
EA ./sprite/syd.ppm
WE ./sprite/syd.ppm
F 0,0,0,
C 255,255,255
1111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011100001110111111111111
11110111111 11011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111

75
sprite/back.xpm Normal file
View File

@ -0,0 +1,75 @@
/* XPM */
static char *a1ddad0c418f4a35e76242f25979a7e2fCUuqr6bneEEiT2H[] = {
/* columns rows colors chars-per-pixel */
"64 64 5 1 ",
" c #808080",
". c #F9F9F9",
"X c #FDFDFD",
"o c #FEFEFE",
"O c white",
/* pixels */
" ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOO.OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ",
" "
};

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

15
sprite/player.xpm Normal file
View File

@ -0,0 +1,15 @@
/* XPM */
static char *bd85319af077459cedaeeaa7648f429dGIdxmjBFsN6lbeLv[] = {
/* columns rows colors chars-per-pixel */
"8 8 1 1 ",
" c #12E22A",
/* pixels */
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
};

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

76
sprite/wall.xpm Normal file
View File

@ -0,0 +1,76 @@
/* XPM */
static char *f43facccf9fb4a3d880a26056a6bb63funGjxXraBfJ5iPVR[] = {
/* columns rows colors chars-per-pixel */
"64 64 6 1 ",
" c gray24",
". c #3E3E3E",
"X c #3F3F3F",
"o c gray50",
"O c #808080",
"+ c #818181",
/* pixels */
"OOOOoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
"OXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXXX XXXO",
"oXXXX XX X X XXX X X X X XXX X X X X XXX X X X X XXX X X XXXXXo",
"o XX X XX X X X X XXX X X X X XXX X X X X XXX X X X X XXX XXXo",
"oXX X XX X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX XXX X X X X X X X X X X X X X X X X X X X X X X X XXX X XXo",
"oXXX XXX X X X X X X X X X X X X X X X X X X X X X X X XX X Xo",
"oXXXX X X X X X X X X X X X X X X X X X X X X X X X X XX XXXo",
"o X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXXXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"o X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oXXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"o X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXXXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"o X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oXXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"o X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXXXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"o X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oXXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"o X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXXXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"o X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oXXX X X X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX XXX X X X X X X X X X X X X X X X X X X X X X X X X X X Xo",
"oX XXX X X X X X X X X X X X X X X X X X X X X X X X X X X XXo",
"oXX X XX X X X X X X X X X X X X X X X X X X X X X X X X XXXo",
"oX X XX X X X X X X X X X X X X X X X X X X X X X X X XXX X XXo",
"oXXXX XX XXX X X X X XXX X X X X XXX X X X X XXX X X X X XXX Xo",
"oXX XXX X X X XXX X X X X XXX X X X X XXX X X X X XXX X X X XXo",
"OX XXXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXXo",
"ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooOooooo"
};

251
srcs/cast_ray.c Normal file
View File

@ -0,0 +1,251 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cast_ray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/04 18:08:14 by apommier #+# #+# */
/* Updated: 2022/05/05 02:59:14 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/Cub3D.h"
void print_ray2(t_data *img, float vx, float vy, float dist)
{
int i = -1;
int red = 0;
red = red << 8;
red +=255;
red = red << 8;
red = red << 8;
while (++i < dist)
{
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) , red);
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (vx) * i) + 1, (img->player.y + (vy) * i) , red);
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) + 1, red);
}
}
int get_color(char one, char two, char three)
{
int color = 0;
color = color << 8;
color += one;
color = color << 8;
color += two;
color = color << 8;
color += three;
return (color);
}
int get_red()
{
int red = 0;
red = red << 8;
red +=255;
red = red << 8;
red = red << 8;
return (red);
}
int get_dark_red()
{
int red = 0;
red = red << 8;
red +=139;
red = red << 8;
red = red << 8;
return (red);
}
void draw_ray3d(t_data *img, float dist, int ray, int type)
{
float line_height;
float line_offset;
int i;
int y;
i = 0;
line_height = img->map.size * 320 / dist;
if (line_height > 320)
line_height = 320;
line_offset = 160 - line_height / 2;
while (i < 8)
{
y = 0;
while (y < line_height)
{
if (type)
mlx_pixel_put(img->mlx, img->mlx_win, ray * 8 + 530 + i, y + line_offset , get_red());
else
mlx_pixel_put(img->mlx, img->mlx_win, ray * 8 + 530 + i, y + line_offset , get_dark_red());
y++;
}
i++;
}
}
void draw_ray(t_data *img)
{
float ray_angle = 0; //ray angle
float ray_y = 0; //where ray touch x
float ray_x = 0; //where ray touch y
float next_x = 0;
float next_y = 0;
float dist_v;
float dist_h;
float dist_f;
float vx = 0;
float vy = 0;
int count = 0;
float aTan = 0;
int nb_ray = -1;
int my = 0;
int mx = 0;
int mp = 0;
(void)dist_f;
//printf("\nENTER DRAW RAY\n");
//while (++k < ft_strlen(img->map.simple_map))
// printf("%d--- %c\n", k, img->map.simple_map[k]);
//printf("map = -%s-\n", img->map.simple_map);
count = 0;
ray_angle = reset_angle(img->player.angle + 30);
//ray_angle = reset_angle(img->player.angle);
while (++nb_ray < 60)
{
//if (nb_ray)
// ray_angle -= 30;
count = 0;
dist_v = -1;
dist_h = -1;
//printf("------RAY N0 %d-------\n", nb_ray);
//printf("player_angle= %f ray_angle= %f\n", img->player.angle, ray_angle);
//----------start vertical ray----------
aTan = tan(deg_to_rad(ray_angle));
if (cos(deg_to_rad(ray_angle)) > 0.001)//looking left
{
ray_x = (((int)img->player.x>>6)<<6) + 64;
ray_y = (img->player.x - ray_x) * aTan + img->player.y;
next_x = 64;
next_y = -next_x * aTan;
}
else if (cos(deg_to_rad(ray_angle)) < -0.001)//looking right
{
ray_x = (((int)img->player.x>>6)<<6) - 0.0001;
ray_y = (img->player.x - ray_x) * aTan + img->player.y;
next_x = -64;
next_y = -next_x * aTan;
}
else
{
ray_x = img->player.x;
ray_y = img->player.y;
count = 8;
}
//if (next_x > 0)
// printf("for hroizon looking left\n");
//else
// printf("for hroizon looking right\n");
//printf("\nray_y= %f ray_x= %f\n", ray_y, ray_x);
//printf("next_y= %f next_x= %f\n", next_y, next_x);
//printf("BASE p_y= %f p_x= %f\n", img->player.y, img->player.x);
while (count < 8)
{
//printf("count = %d\n", count);
mx = (int)(ray_x)>>6;
my = (int)(ray_y)>>6;
mp = my * img->map.x + mx;
//printf("mx=%d my=%d mp= %d\n", mx, my, mp);
if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1')//hit wall
{
count = 8;
//printf("vertical wall\n");
dist_v = cos(deg_to_rad(ray_angle)) * (ray_x-img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y-img->player.y);
}
else
{
ray_x += next_x;
ray_y += next_y;
count += 1;
}
}
//print_ray2(img, img->player.vx, img->player.vy, dist_v);
vx = ray_x;
vy = ray_y;
//-------start horizontal ray---------
count = 0;
aTan = 1.0 / aTan;
if (sin(deg_to_rad(ray_angle)) > 0.001)//looking up
{
ray_y = (((int)img->player.y>>6)<<6) - 0.0001;
ray_x = (img->player.y - ray_y) * aTan + img->player.x;
next_y = -64;
next_x = -next_y * aTan;
}
else if (sin(deg_to_rad(ray_angle))<-0.001)//looking down
{
ray_y = (((int)img->player.y>>6)<<6) + 64;
ray_x = (img->player.y - ray_y) * aTan + img->player.x;
next_y = 64;
next_x = -next_y * aTan;
}
else
{
ray_x = img->player.x;
ray_y = img->player.y;
count = 8;
}//looking straight left or right
while (count<8)
{
//printf("ray_y= %f ray_x= %f\n", ray_y, ray_x);
mx = (int)(ray_x)>>6;
my = (int)(ray_y)>>6;
mp = my * img->map.x + mx;
//printf("mx=%d my=%d mp= %d\n", mx, my, mp);
if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1')
{
count = 8;
//printf ("horizontal wall\n");
//printf("case: x= %d, y= %d mp= %d\n", mx, my, mp);
dist_h = cos(deg_to_rad(ray_angle)) * (ray_x - img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y - img->player.y);
}//hit
else
{
ray_x += next_x;
ray_y += next_y;
count += 1;
} //check next horizontal
}
//printf("dist_h= %f dist_v= %f\n", dist_h, dist_v);
vx = cos(deg_to_rad(ray_angle));
vy = -sin(deg_to_rad(ray_angle));
//printf("player.vx= %f vx= %f player.vy= %f vy= %f\n", img->player.vx, vx, img->player.vy, vy);
int wall_type;
if (dist_h != -1 && (dist_h < dist_v || dist_v == -1))
{
print_ray2(img, vx, vy, fabs(dist_h));
dist_f = dist_h;
wall_type = 0;
}
else if (dist_v != -1)
{
dist_f = dist_v;
print_ray2(img, vx, vy, fabs(dist_v));
wall_type = 1;
}
else
dist_f = 0;
int ca = reset_angle(img->player.angle - ray_angle); //fisheye
dist_f = dist_f * cos(deg_to_rad(ca)); //fisheye
draw_ray3d(img, dist_f, nb_ray, wall_type);
ray_angle = reset_angle(ray_angle - 1);
}
}

View File

@ -1,120 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cast_ray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/15 23:56:03 by apommier #+# #+# */
/* Updated: 2022/06/16 16:18:48 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
void set_draw_info(t_draw_ray_info *info, t_ray *ray)
{
info->line_height = 64 * 960 / ray->dist;
info->line_offset = 256 - ((int)info->line_height) / 2;
info->x = 0;
info->y = 0;
info->mx = ((int)ray->mp) % 64;
info->my = 0;
info->gap = (64 / info->line_height);
info->myy = 0;
if (info->line_height > 512)
{
info->line_offset = 0;
info->myy = info->gap * ((info->line_height - 512) / 2);
}
}
void draw_ray3d(t_data *img, t_ray ray)
{
t_draw_ray_info info;
set_draw_info(&info, &ray);
while (info.y < info.line_height - 8 && info.y < 512)
{
info.myy += info.gap;
info.my = (int)info.myy;
ray.pixel = ((info.my) * 64 + info.mx) * 3 + 1;
info.color = set_color_texture(&ray, img->map.texture);
if (info.color == -1)
return ;
info.x = -1;
while (++info.x < 4)
set_pixel(img, info.color, ray.index * 4 + info.x,
info.y + info.line_offset);
info.y++;
}
info.x++;
}
void set_dist_ray(t_var_draw_ray *info, t_ray *ray_info, t_data *img)
{
ray_info->ty = info->ray_y;
ray_info->pixel = 0;
ray_info->tx = info->ray_x;
ray_info->index = info->nb_ray;
info->dist_f = info->dist_f
* cos(deg_to_rad(reset_angle(img->player.angle - info->ray_angle)));
ray_info->dist = info->dist_f;
if (info->dist_f > 0)
draw_ray3d(img, *ray_info);
info->ray_angle = reset_angle(info->ray_angle - 0.25);
}
void set_ray_info(t_var_draw_ray *info, t_data *img)
{
t_ray ray_info;
ray_info.texture_type = 0;
if (info->dist_h != -1 && (info->dist_h < info->dist_v
|| info->dist_v == -1))
{
info->dist_f = info->dist_h;
ray_info.mp = info->ray_x;
ray_info.texture_type = info->horizontal_type;
}
else if (info->dist_v != -1)
{
info->dist_f = info->dist_v;
info->ray_x = info->vx;
info->ray_y = info->vy;
ray_info.mp = info->vy;
ray_info.texture_type = info->vertical_type;
}
else
{
ray_info.mp = 0;
info->dist_f = 0;
}
set_dist_ray(info, &ray_info, img);
}
void draw_ray(t_data *img)
{
t_var_draw_ray info;
set_info_draw(&info);
img->image = mlx_new_image(img->mlx, 960, 512);
if (!img->image)
ft_exit("Error\nmlx_new_image failed\n", img);
img->buffer = mlx_get_data_addr(img->image, &img->bits_per_pixel,
&img->size_line, &img->endian);
set_back(img);
info.count = 0;
info.ray_angle = reset_angle(img->player.angle + 30);
while (++info.nb_ray < 240)
{
info.count = 0;
info.dist_v = -1;
info.dist_h = -1;
vertical_ray(&info, img);
horizontal_ray(&info, img);
set_ray_info(&info, img);
}
mlx_put_image_to_window(img->mlx, img->mlx_win, img->image, 0, 0);
mlx_destroy_image(img->mlx, img->image);
}

View File

@ -1,144 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_wall.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/16 16:15:16 by apommier #+# #+# */
/* Updated: 2022/06/16 16:21:20 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
void find_horizontal_wall(t_var_draw_ray *info, t_data *img)
{
while (info->count < img->map.max)
{
info->mx = (int)(info->ray_x) >> 6;
info->my = (int)(info->ray_y) >> 6;
info->mp = info->my * img->map.x + info->mx;
if (info->mp > 0 && info->mp < img->map.size
&& img->map.simple_map[info->mp] == '1')
{
info->count = img->map.max;
info->dist_h = cos(deg_to_rad(info->ray_angle))
* (info->ray_x - img->player.x)
- sin(deg_to_rad(info->ray_angle))
* (info->ray_y - img->player.y);
}
else
{
info->ray_x += info->next_x;
info->ray_y += info->next_y;
info->count += 1;
}
}
}
void horizontal_ray(t_var_draw_ray *hr, t_data *img)
{
hr->count = 0;
hr->tan = 1.0 / hr->tan;
if (sin(deg_to_rad(hr->ray_angle)) > 0.001)
{
hr->ray_y = (((int)img->player.y >> 6) << 6) - 0.0001;
hr->ray_x = (img->player.y - hr->ray_y) * hr->tan + img->player.x;
hr->next_y = -64;
hr->next_x = -hr->next_y * hr->tan;
hr->horizontal_type = 'N';
}
else if (sin(deg_to_rad(hr->ray_angle)) < -0.001)
{
hr->ray_y = (((int)img->player.y >> 6) << 6) + 64;
hr->ray_x = (img->player.y - hr->ray_y) * hr->tan + img->player.x;
hr->next_y = 64;
hr->next_x = -hr->next_y * hr->tan;
hr->horizontal_type = 'S';
}
else
{
hr->ray_x = img->player.x;
hr->ray_y = img->player.y;
hr->count = img->map.max;
}
find_horizontal_wall(hr, img);
}
void find_vertical_wall(t_var_draw_ray *info, t_data *img)
{
while (info->count < img->map.max)
{
info->mx = (int)(info->ray_x) >> 6;
info->my = (int)(info->ray_y) >> 6;
info->mp = info->my * img->map.x + info->mx;
if (info->mp > 0 && info->mp < img->map.size
&& img->map.simple_map[info->mp] == '1')
{
info->count = img->map.max;
info->dist_v = cos(deg_to_rad(info->ray_angle))
* (info->ray_x - img->player.x)
- sin(deg_to_rad(info->ray_angle))
* (info->ray_y - img->player.y);
}
else
{
info->ray_x += info->next_x;
info->ray_y += info->next_y;
info->count += 1;
}
}
info->vx = info->ray_x;
info->vy = info->ray_y;
}
void vertical_ray(t_var_draw_ray *vr, t_data *img)
{
vr->tan = tan(deg_to_rad(vr->ray_angle));
if (cos(deg_to_rad(vr->ray_angle)) > 0.001)
{
vr->ray_x = (((int)img->player.x >> 6) << 6) + 64;
vr->ray_y = (img->player.x - vr->ray_x) * vr->tan + img->player.y;
vr->next_x = 64;
vr->next_y = -vr->next_x * vr->tan;
vr->vertical_type = 'E';
}
else if (cos(deg_to_rad(vr->ray_angle)) < -0.001)
{
vr->ray_x = (((int)img->player.x >> 6) << 6) - 0.0001;
vr->ray_y = (img->player.x - vr->ray_x) * vr->tan + img->player.y;
vr->next_x = -64;
vr->next_y = -vr->next_x * vr->tan;
vr->vertical_type = 'W';
}
else
{
vr->ray_x = img->player.x;
vr->ray_y = img->player.y;
vr->count = img->map.max;
}
find_vertical_wall(vr, img);
}
void set_info_draw(t_var_draw_ray *info)
{
info->ray_angle = 0;
info->ray_y = 0;
info->ray_x = 0;
info->next_x = 0;
info->next_y = 0;
info->dist_v = 0;
info->dist_h = 0;
info->dist_f = 0;
info->vx = 0;
info->vy = 0;
info->count = 0;
info->tan = 0;
info->nb_ray = -1;
info->my = 0;
info->mx = 0;
info->mp = 0;
info->vertical_type = 0;
info->horizontal_type = 0;
}

View File

@ -1,132 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_texture_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 16:10:11 by apommier #+# #+# */
/* Updated: 2022/06/15 21:38:53 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
void put_texture_in_struct(char type, unsigned char *texture, t_data *img)
{
if (type == 'N')
{
if (img->map.texture.north)
ft_exit("Error\nMultiple declaration of texture\n", img);
img->map.texture.north = texture;
}
if (type == 'S')
{
if (img->map.texture.south)
ft_exit("Error\nMultiple declaration of texture\n", img);
img->map.texture.south = texture;
}
if (type == 'W')
{
if (img->map.texture.west)
ft_exit("Error\nMultiple declaration of texture\n", img);
img->map.texture.west = texture;
}
if (type == 'E')
{
if (img->map.texture.east)
ft_exit("Error\nMultiple declaration of texture\n", img);
img->map.texture.east = texture;
}
}
int is_nbr(char *str)
{
int i;
i = 0;
while (str[i])
{
if (!ft_isdigit(str[i]))
return (0);
i++;
}
return (1);
}
void get_file_size(char *path, t_data *img, int *count, int fd)
{
char *swap;
swap = 0;
if (!path)
ft_exit("Error\nNo path for texture\n", img);
check_dir(path, img);
fd = open(path, O_RDONLY);
if (fd == -1)
ft_exit("Error\nBad path for texture\n", img);
img->to_be_free.fd = fd;
while (swap || !(*count))
{
if (swap)
free(swap);
(*count)++;
swap = get_next_line(fd);
}
close(fd);
img->to_be_free.fd = -1;
if (*count != 12291)
ft_exit("Error\nBad texture file (too much or not enough line)\n", img);
}
void fill_ret(int count, t_data *img, unsigned char **ret, char *swap)
{
while (swap || !count)
{
if (swap)
free(swap);
swap = get_next_line(img->to_be_free.fd);
if (!count)
{
free(swap);
swap = get_next_line(img->to_be_free.fd);
}
if (swap)
swap[ft_strlen(swap) - 1] = 0;
if ((swap && is_nbr(swap) && ft_strlen(swap) <= 3
&& ft_atoi(swap) <= 255 && ft_atoi(swap) >= 0) || !count)
(*ret)[count] = (unsigned char)ft_atoi(swap);
else if (swap)
{
get_next_line(-1);
free(swap);
ft_exit("Error\nBad texture file (not numbers or to high)\n", img);
}
count++;
}
}
unsigned char *get_texture(char type, char *path, t_data *img)
{
int fd;
unsigned char *ret;
int count;
(void)type;
count = 0;
get_file_size(path, img, &count, 0);
ret = ft_calloc(sizeof(char), count);
if (!ret)
quit_game(img);
img->to_be_free.str = ret;
fd = open(path, O_RDONLY);
if (fd == -1)
ft_exit("Error\nBad texture file", img);
img->to_be_free.fd = fd;
fill_ret(0, img, &ret, 0);
close(fd);
img->to_be_free.fd = -1;
put_texture_in_struct(type, ret, img);
img->to_be_free.str = 0;
return (ret);
}

View File

@ -1,38 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ray_casting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/16 13:55:39 by apommier #+# #+# */
/* Updated: 2022/06/16 14:28:47 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
int set_color_texture(t_ray *ray, t_sprite img)
{
int color;
color = 0;
if (ray->pixel >= 12290 || ray->pixel < 0)
return (-1);
else if (ray->pixel > 0)
{
if (ray->texture_type == 'N' && img.north)
color = get_color(img.north[ray->pixel],
img.north[ray->pixel + 1], img.north[ray->pixel + 2]);
else if (ray->texture_type == 'S' && img.south)
color = get_color(img.south[ray->pixel],
img.south[ray->pixel + 1], img.south[ray->pixel + 2]);
else if (ray->texture_type == 'W' && img.west)
color = get_color(img.west[ray->pixel],
img.west[ray->pixel + 1], img.west[ray->pixel + 2]);
else if (ray->texture_type == 'E' && img.east)
color = get_color(img.east[ray->pixel],
img.east[ray->pixel + 1], img.east[ray->pixel + 2]);
}
return (color);
}

View File

@ -1,38 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/15 18:46:05 by apommier #+# #+# */
/* Updated: 2022/06/17 15:26:41 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
void set_back(t_data *img)
{
int x;
x = 0;
while (x < 512 * 960 * 4)
{
if (x > 512 * 960 * 2)
{
img->buffer[x + 0] = img->map.floor.b;
img->buffer[x + 1] = img->map.floor.g;
img->buffer[x + 2] = img->map.floor.r;
img->buffer[x + 3] = 0;
}
else
{
img->buffer[x + 0] = img->map.sky.b;
img->buffer[x + 1] = img->map.sky.g;
img->buffer[x + 2] = img->map.sky.r;
img->buffer[x + 3] = 0;
}
x += 4;
}
}

View File

@ -1,73 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 23:37:02 by apommier #+# #+# */
/* Updated: 2022/06/15 21:39:46 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
int get_color(char R, char G, char B)
{
int color;
color = 0;
color = color << 8;
color += R;
color = color << 8;
color += G;
color = color << 8;
color += B;
return (color);
}
void set_pixel(t_data *img, int color, int x, int y)
{
int pixel;
if (y < 0 || y > 520 || x < 0 || x > 960)
return ;
pixel = (y * img->size_line) + (x * 4);
if (img->endian == 1)
{
img->buffer[pixel + 0] = 0;
img->buffer[pixel + 1] = (color >> 16) & 0xFF;
img->buffer[pixel + 2] = (color >> 8) & 0xFF;
img->buffer[pixel + 3] = (color) & 0xFF;
}
else if (img->endian == 0)
{
img->buffer[pixel + 0] = (color) & 0xFF;
img->buffer[pixel + 1] = (color >> 8) & 0xFF;
img->buffer[pixel + 2] = (color >> 16) & 0xFF;
img->buffer[pixel + 3] = 0;
}
}
void get_map_size(char *path, t_data *img, int *count, int fd)
{
char *swap;
swap = 0;
if (!path)
ft_exit("Error\nNo path for map\n", img);
check_dir(path, img);
fd = open(path, O_RDONLY);
if (fd == -1)
ft_exit("Error\nBad path for map\n", img);
img->to_be_free.fd = fd;
while (swap || !(*count))
{
if (swap)
free(swap);
(*count)++;
swap = get_next_line(fd);
}
close(fd);
img->to_be_free.fd = -1;
}

View File

@ -1,108 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* key.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/15 18:40:33 by apommier #+# #+# */
/* Updated: 2022/06/17 15:19:46 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
int key_pressed(int type, t_data *img)
{
if (type == 65307)
quit_game(img);
if (type == 'w')
img->player.front = 1;
else if (type == 's')
img->player.front = -1;
else if (type == 'a')
img->player.side = -1;
else if (type == 'd')
img->player.side = 1;
else if (type == 65361)
img->player.angle_side = -1;
else if (type == 65363)
img->player.angle_side = 1;
else if (type == 65505)
img->player.shift = 1;
return (0);
}
int key_released(int type, t_data *img)
{
if (type == 'w')
img->player.front = 0;
else if (type == 's')
img->player.front = 0;
else if (type == 'a')
img->player.side = 0;
else if (type == 'd')
img->player.side = 0;
else if (type == 65361)
img->player.angle_side = 0;
else if (type == 65363)
img->player.angle_side = 0;
else if (type == 65505)
img->player.shift = 0;
return (0);
}
void update_side(t_data *img)
{
if (img->player.side == 1)
{
img->player.x -= img->player.vy;
img->player.y += img->player.vx;
}
else if (img->player.side == -1)
{
img->player.x += img->player.vy;
img->player.y -= img->player.vx;
}
}
void update_angle_side(t_data *img)
{
if (img->player.angle_side == -1)
{
img->player.angle += 1;
img->player.angle = reset_angle(img->player.angle);
img->player.vx = cos(deg_to_rad(img->player.angle));
img->player.vy = -sin(deg_to_rad(img->player.angle));
}
else if (img->player.angle_side == 1)
{
img->player.angle -= 1;
img->player.angle = reset_angle(img->player.angle);
img->player.vx = cos(deg_to_rad(img->player.angle));
img->player.vy = -sin(deg_to_rad(img->player.angle));
}
}
int update_pos(t_data *img)
{
int multiplicator;
if (img->player.shift == 1)
multiplicator = 4;
else
multiplicator = 2;
if (img->player.front == 1)
{
img->player.x += img->player.vx * multiplicator;
img->player.y += img->player.vy * multiplicator;
}
else if (img->player.front == -1)
{
img->player.x -= img->player.vx * multiplicator;
img->player.y -= img->player.vy * multiplicator;
}
update_angle_side(img);
update_side(img);
return (0);
}

View File

@ -6,41 +6,184 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:42:55 by apommier #+# #+# */
/* Updated: 2022/06/17 15:16:40 by apommier ### ########.fr */
/* Updated: 2022/05/05 02:55:40 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/Cub3D.h"
void set_map_player(t_data *img)
void print_ray(t_data *img)
{
img->map.texture.north = 0;
img->map.texture.east = 0;
img->map.texture.south = 0;
img->map.texture.west = 0;
img->map.simple_map = 0;
img->player.x = 0;
img->player.y = 0;
img->player.angle = 0;
img->player.vx = 0;
img->player.vy = 0;
img->player.front = 0;
img->player.side = 0;
img->player.angle_side = 0;
img->player.shift = 0;
img->to_be_free.str = 0;
img->to_be_free.tab = NULL;
img->to_be_free.tab_two = NULL;
img->to_be_free.fd = -1;
img->mlx = 0;
img->mlx_win = 0;
int i = -1;
while (++i < 15)
{
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (img->player.vx) * i), (img->player.y + (img->player.vy) * i) , 255 << 8);
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (img->player.vx) * i) + 1, (img->player.y + (img->player.vy) * i) , 255 << 8);
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (img->player.vx) * i), (img->player.y + (img->player.vy) * i) + 1, 255 << 8);
}
}
int loop(t_data *img)
/*map_info set_map(char **argv) //simple local allocation
{
update_pos(img);
draw_ray(img);
return (0);
map_info map;
char ret_map[]=
{
'1','1','1','1','1','1','1','1',
'1','0','1','0','0','0','0','1',
'1','0','1','0','0','0','0','1',
'1','0','0','0','0','0','0','1',
'1','0','0','0','0','0','0','1',
'1','0','1','0','0','1','0','1',
'1','0','0','0','0','1','0','1',
'1','1','1','1','1','1','1','1',
};
map.simple_map = ret_map;
map.x = 8;
map.y = 8;
map.size = map.x * map.y;
return (map);
}*/
/*map_info set_map(char **argv)
{
char **map_tab;
char *map;
char *del;
char *swap;
int fd;
map = 0;
fd = open(argv[1], O_RDONLY);
if (fd == -1)
ft_error("Error: Open call fail");
swap = get_next_line(fd);
while (swap)
{
del = map;
map = ft_strjoin(map, swap);
free(swap);
swap = get_next_line(fd);
free(del);
}
close(fd);
map_tab = ft_split(map, '\n');
free(map);
if (!map_tab)
ft_error("Error: Map file is empty");
return (map_tab);
}*/
map_info set_map(char **argv)
{
//char **map_tab;
char *map;
char *del;
char *swap;
int fd;
map_info ret_map;
map = 0;
fd = open(argv[1], O_RDONLY);
if (fd == -1)
ft_error("Error: Open call fail");
swap = get_next_line(fd);
while (swap)
{
del = map;
if (swap[ft_strlen(swap) - 1] == '\n')
swap[ft_strlen(swap) - 1] = 0;
map = ft_strjoin(map, swap);
free(swap);
swap = get_next_line(fd);
free(del);
}
close(fd);
ret_map.simple_map = map;
ret_map.x = 8;
ret_map.y = 8;
ret_map.size = ret_map.x * ret_map.y;
//map_tab = ft_split(map, '\n');
//free(map);
//if (!map_tab)
// ft_error("Error: Map file is empty");
return (ret_map);
}
void print_case(char type, t_data *img, int y, int x)
{
int *buffer = 0;
int img_width;
int img_height;
//printf("type= %c\n", type);
if (type == '1')
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/wall.xpm",
&img_width, &img_height);
else if (type == '0')
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/back.xpm",
&img_width, &img_height);
if (!buffer)
ft_error("no buffer");
mlx_put_image_to_window(img->mlx, img->mlx_win, buffer, x, y);
mlx_destroy_image(img->mlx, buffer);
}
void print_map(map_info map, t_data *img)
{
int i = 0;
int j;
int x = 0;
int y = 0;
//printf("map= -%s-\n", map.simple_map);
while (i < map.size)
{
j = -1;
x = 0;
while (++j < 8)
{
print_case(map.simple_map[i], img, (y * 64), (x * 64));
i++;
x++;
}
y++;
}
/*i = 0;
j = 0;
while (map[j])
{
x = 0;
i = 0;
while (map[j][i])
{
print_case(map[j][i], img, (j * 64), (i * 64));
i++;
x++;
}
j++;
y++;
}*/
}
void print_line(t_data *img, float x, float y)
{
float i = -1;
int j = -1;
while (++i < x && ++j < y)
mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (img->player.vx / 5) * i) + 2, (img->player.y + (img->player.vy / 5) * i) + 2, 65);
}
void print_player(player player, t_data *img)
{
int *buffer = 0;
int img_width;
int img_height;
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/player.xpm",
&img_width, &img_height);
mlx_put_image_to_window(img->mlx, img->mlx_win, buffer, player.x - 3.5 , player.y -3.5);
mlx_destroy_image(img->mlx, buffer);
}
int main(int argc, char **argv)
@ -48,19 +191,23 @@ int main(int argc, char **argv)
t_data img;
if (argc != 2)
ft_error("Error\nBad number of arguments, only need a map");
set_map_player(&img);
if (check_map(argv, &img))
return (0);
ft_error("Error: bad number of arguments, only need a map");
img.mlx = mlx_init();
if (!img.mlx)
ft_exit("Error\nmlx_init fail\n", &img);
img.mlx_win = mlx_new_window(img.mlx, 960, 512, "Cub3D");
ft_error("Error: mlx_init fail");
img.map = set_map(argv);
img.mlx_win = mlx_new_window(img.mlx, 1024, 512, "Cub3D");
//img->player.x=150; img->player.y=400; pa=90;
//pdx=cos(deg_to_rad(pa)); pdy=-sin(deg_to_rad(pa));
img.player.x = 150;
img.player.y = 400;
img.player.angle = 90;
img.player.vx = cos(deg_to_rad(img.player.angle));
img.player.vy = -sin(deg_to_rad(img.player.angle));
mlx_hook(img.mlx_win, 2, 1L << 0, key_pressed, &img);
mlx_hook(img.mlx_win, 3, 1L << 1, key_released, &img);
mlx_loop_hook(img.mlx, loop, &img);
img.player.vy = sin(deg_to_rad(img.player.angle));
print_map(img.map, &img);
print_player(img.player, &img);
//print_ray(&img);
mlx_hook(img.mlx_win, 2, 1L << 0, &key_press, &img);
mlx_hook(img.mlx_win, 17, 0L, &quit_game, &img);
mlx_loop(img.mlx);
}

View File

@ -1,132 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_color_texture.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/11 01:00:17 by apommier #+# #+# */
/* Updated: 2022/06/17 15:55:51 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
void set_texture_file(char *str, t_data *img, char c)
{
int index;
index = next_space_index(str, 0);
if (c == 'N')
{
if (str[index + 1] != 'O' || str[index + 2] != ' ')
ft_exit("Error\nBad syntax in map file\n", img);
}
if (c == 'S')
{
if (str[index + 1] != 'O' || str[index + 2] != ' ')
ft_exit("Error\nBad syntax in map file\n", img);
}
if (c == 'W')
{
if (str[index + 1] != 'E' || str[index + 2] != ' ')
ft_exit("Error\nBad syntax in map file\n", img);
}
if (c == 'E')
{
if (str[index + 1] != 'A' || str[index + 2] != ' ')
ft_exit("Error\nBad syntax in map file\n", img);
}
get_texture(c, str + index + 3, img);
}
void check_value(char **tab, t_data *img)
{
int i;
int j;
i = 0;
while (tab[i])
{
j = 0;
while (tab[i][j])
{
if (!ft_isdigit(tab[i][j]))
ft_exit("Error\nRGB parameters need to be only digits\n", img);
j++;
}
i++;
}
}
void set_color_utils(char **tab, t_color *rgb, t_data *img)
{
if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[0]) >= 0
&& ft_atoi(tab[0]) <= 255))
ft_exit("Error\nBad parameter in map file identifier(RGB)\n", img);
if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[1]) >= 0
&& ft_atoi(tab[1]) <= 255))
ft_exit("Error\nBad parameter in map file identifier(RGB)\n", img);
if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[2]) >= 0
&& ft_atoi(tab[2]) <= 255))
ft_exit("Error\nBad parameter in map file identifier(RGB)\n", img);
check_value(tab, img);
rgb->r = (unsigned char)ft_atoi(tab[0]);
rgb->g = (unsigned char)ft_atoi(tab[1]);
rgb->b = (unsigned char)ft_atoi(tab[2]);
rgb->set = 1;
}
void set_color(char *str, t_data *img)
{
char c;
int index;
char **tab;
index = -1;
while (str[++index])
{
if (str[index] == ',' && str[index + 1] == ',')
ft_exit("Error\nBad syntax for RGB identifier\n", img);
}
c = next_space(str, 0);
index = next_space_index(str + 1, 0);
tab = ft_split(str + index + 1, ',');
if (!tab || double_size(tab) != 3)
ft_exit("Error\nBad syntax for RGB identifier\n", img);
img->to_be_free.tab_two = tab;
if (c == 'F')
set_color_utils(tab, &img->map.floor, img);
if (c == 'C')
set_color_utils(tab, &img->map.sky, img);
free_double(tab);
img->to_be_free.tab_two = 0;
}
int check_texture_color(char **tab, t_data *img)
{
int next;
int index;
index = 0;
next = 0;
while (tab && tab[next] && next_space(tab[next], 0) != '1')
next++;
if (!tab || !tab[next])
ft_exit("Error\nBad syntax in map file (identifier)\n", img);
while (index < next)
{
if (next_space(tab[index], 0) == 'N'
|| next_space(tab[index], 0) == 'S'
|| next_space(tab[index], 0) == 'W'
|| next_space(tab[index], 0) == 'E')
set_texture_file(tab[index], img, next_space(tab[index], 0));
else if (next_space(tab[index], 0) == 'F'
|| next_space(tab[index], 0) == 'C')
set_color(tab[index], img);
else if (next_space(tab[index], 0))
ft_exit("Error\nBad syntax in map file (identifier)\n", img);
index++;
}
return (next);
}

View File

@ -1,92 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/16 16:26:58 by apommier #+# #+# */
/* Updated: 2022/06/16 16:29:06 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
void close_or_not(char **tab, int i, int j, t_data *img)
{
if (tab[i + 1][j] == '3' || tab[i - 1][j] == '3')
img->map.error = 1;
if (tab[i][j + 1] == '3' || tab[i][j - 1] == '3')
img->map.error = 1;
if (tab[i + 1][j + 1] == '3' || tab[i + 1][j - 1] == '3')
img->map.error = 1;
if (tab[i - 1][j + 1] == '3' || tab[i - 1][j - 1] == '3')
img->map.error = 1;
if (img->map.error != 1)
img->map.error = 0;
}
void check_border(char **tab, t_data *img)
{
int i;
int j;
i = 0;
while (tab[i])
{
j = 0;
while (tab[i][j])
{
if (tab[i][j] == '0' || tab[i][j] == 'W' || tab[i][j] == 'N'
|| tab[i][j] == 'S' || tab[i][j] == 'E')
close_or_not(tab, i, j, img);
j++;
}
i++;
}
}
int check_inner_utils(char *line, t_data *img)
{
int i;
int player;
i = 0;
player = 0;
while (line[i])
{
if (line[i] == 'N' || line[i] == 'S'
|| line[i] == 'E' || line[i] == 'W')
{
find_angle(line[i], img);
img->player.x = i * 64 - 32;
player++;
}
else if (line[i] != '3' && line[i] != '0' && line[i] != '1')
return (100);
i++;
}
return (player);
}
void check_inner(char **map, t_data *img)
{
int i;
int player;
i = 0;
player = 0;
while (map[i])
{
player += check_inner_utils(map[i], img);
if (player == 1 && !img->player.y)
img->player.y = i * 64 - 32;
i++;
}
if (player == 0)
img->map.error = 2;
if (player > 1 && player < 100)
img->map.error = 3;
if (player >= 100)
img->map.error = -1;
}

View File

@ -1,102 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_map_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/16 16:30:52 by apommier #+# #+# */
/* Updated: 2022/06/16 16:32:55 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
void verify_texture_color(t_data *img)
{
if (img->map.texture.north == NULL)
ft_exit("Error\nTexture isn't loaded properly\n", img);
if (img->map.texture.east == NULL)
ft_exit("Error\nTexture isn't loaded properly\n", img);
if (img->map.texture.south == NULL)
ft_exit("Error\nTexture isn't loaded properly\n", img);
if (img->map.texture.west == NULL)
ft_exit("Error\nTexture isn't loaded properly\n", img);
if (img->map.floor.set != 1)
ft_exit("Error\nColor not set properly\n", img);
if (img->map.sky.set != 1)
ft_exit("Error\nColor not set properly\n", img);
}
void error_msg(t_data *img)
{
if (img->map.error == 1)
ft_exit("Error\nMap isn't closed\n", img);
if (img->map.error == 2)
ft_exit("Error\nMissing player\n", img);
if (img->map.error == 3)
ft_exit("Error\nToo many players\n", img);
if (img->map.error == -1)
ft_exit("Error\nBad character in map\n", img);
}
void inter_map(char **split, char **tmp, t_data *img)
{
int i;
int j;
i = 0;
while (split[i])
{
j = 0;
while (split[i][j])
{
if (split[i][j] == '3')
img->map.error = -1;
else if (split[i][j] != ' ')
tmp[i + 1][j + 1] = split[i][j];
j++;
}
i++;
}
}
void check_empty_line(char **split, t_data *img, int *i)
{
while (split[*i])
{
if (!ft_strchr(split[*i], '1'))
ft_exit("Error\nInvalid line in map file\n", img);
size_line(split[*i], img);
(*i)++;
}
}
void check_zero_one(char **split, t_data *img)
{
char **tmp;
int i;
i = 0;
check_empty_line(split, img, &i);
img->map.y = i;
tmp = malloc(sizeof(char *) * (i + 3));
if (!tmp)
quit_game(img);
i = 0;
while (i < img->map.y + 2)
{
tmp[i] = charge_new(img, split, tmp);
i++;
}
tmp[i] = NULL;
inter_map(split, tmp, img);
if (img->map.error == -1)
{
free_double(tmp);
return ;
}
check_border(tmp, img);
check_inner(tmp, img);
free_double(tmp);
}

View File

@ -1,52 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* is_a_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/16 16:37:43 by apommier #+# #+# */
/* Updated: 2022/06/16 16:38:57 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
char **fill_tab(char *swap, int count, char **ret, int fd)
{
while (swap || !count)
{
swap = get_next_line(fd);
if (ft_strlen(swap) >= 1 && swap[ft_strlen(swap) - 1] == '\n')
swap[ft_strlen(swap) - 1] = 0;
ret[count] = swap;
count++;
}
return (ret);
}
char **isafile(char **av, t_data *img)
{
int fd;
char **ret;
int count;
int pass;
count = 0;
get_map_size(av[1], img, &count, 0);
ret = ft_calloc(sizeof(char *), count);
if (!ret)
quit_game(img);
img->to_be_free.tab = ret;
fd = open(av[1], O_RDONLY);
if (fd == -1)
ft_exit("Error\nBad texture file", img);
ret = fill_tab(0, 0, ret, fd);
close(fd);
pass = check_texture_color(ret, img);
check_zero_one(ret + pass, img);
transform_map(ret + pass, img);
free_double(ret);
img->to_be_free.tab = 0;
return (0);
}

View File

@ -1,105 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/10 17:54:25 by sadjigui #+# #+# */
/* Updated: 2022/06/16 16:38:05 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
char *charge_new(t_data *img, char **map, char **tmp_map)
{
char *str;
int i;
i = 0;
str = malloc(sizeof(char) * (img->map.x + 3));
if (!str)
{
free_double(map);
free_double(tmp_map);
quit_game(img);
}
while (i < img->map.x + 2)
{
str[i] = '3';
i++;
}
str[i] = '\0';
return (str);
}
int reverse_comp(char *s1, char *s2)
{
int size_s1;
int size_s2;
size_s1 = ft_strlen(s1);
size_s2 = ft_strlen(s2);
while (size_s2 >= 0)
{
if (!(s2[size_s2] == s1[size_s1]))
return (1);
size_s1--;
size_s2--;
}
return (0);
}
int is_in_charset(char c, char *charset)
{
while (*charset)
{
if (c == *charset)
return (1);
charset++;
}
return (0);
}
int detect_map_line(char *line)
{
int i;
i = 0;
if (!line)
return (0);
if (*line == '\0')
return (0);
while (line[i])
{
if (is_in_charset(line[i], " 01NSEW\n") == 0)
return (0);
i++;
}
return (1);
}
int check_map(char **av, t_data *img)
{
img->map.x = 0;
img->map.y = 0;
img->map.error = 0;
if (reverse_comp(av[1], ".cub") || (ft_strlen(av[1]) == ft_strlen(".cub")))
{
ft_exit("Error\nNot a valid file \".cub\"\n", img);
return (1);
}
isafile(av, img);
if (img->map.x > img->map.y)
img->map.max = img->map.x;
else
img->map.max = img->map.y;
if (img->map.error != 0)
{
error_msg(img);
quit_game(img);
}
verify_texture_color(img);
return (0);
}

View File

@ -1,76 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/15 23:34:49 by apommier #+# #+# */
/* Updated: 2022/06/16 16:25:36 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
char next_space(char *str, int i)
{
while (str[i] == ' ')
i++;
return (str[i]);
}
int next_space_index(char *str, int i)
{
while (str[i] == ' ')
i++;
return (i);
}
char *transform_map(char **double_map, t_data *img)
{
char *map;
int i;
int j;
int index;
i = -1;
index = 0;
img->map.size = img->map.x * img->map.y;
map = ft_calloc(sizeof(char), img->map.size + 1);
if (!map)
quit_game(img);
while (double_map[++i])
{
j = -1;
while (double_map[i][++j])
{
map[i * img->map.x + j] = double_map[i][j];
index++;
}
}
img->map.simple_map = map;
return (0);
}
void find_angle(char c, t_data *img)
{
if (c == 'N')
img->player.angle = 90;
if (c == 'E')
img->player.angle = 0;
if (c == 'S')
img->player.angle = 270;
if (c == 'W')
img->player.angle = 180;
}
void size_line(char *str, t_data *img)
{
int i;
i = 0;
while (str[i])
i++;
if (i > img->map.x)
img->map.x = i;
}

137
srcs/utils.c Normal file
View File

@ -0,0 +1,137 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 23:37:02 by apommier #+# #+# */
/* Updated: 2022/05/05 03:05:12 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/Cub3D.h"
int quit_game(t_data *img)
{
mlx_destroy_window(img->mlx, img->mlx_win);
mlx_destroy_display(img->mlx);
if (img->mlx)
free(img->mlx);
free(img->map.simple_map);
//free_double(img->map);
exit(0);
}
float deg_to_rad(int angle)
{
return (angle * M_PI / 180.0);
}
int reset_angle(int angle)
{
if (angle > 359)
angle -= 360;
if (angle < 0)
angle += 360;
return (angle);
}
void ft_error(char *error_msg)
{
/*int i;
i = 0;*/
ft_putendl_fd(error_msg, 2);
exit(1);
}
int is_good(t_data *img, int type)
{
//printf("touche= %c\n", type);
//printf("vx= %f vy= %f\n", img->player.vx, img->player.vy);
//printf("player: x= %f y= %f\n", img->player.x, img->player.y);
//
/*if(key=='a'){ pa+=5; pa=FixAng(pa); pdx=cos(deg_to_rad(pa)); pdy=-sin(deg_to_rad(pa));}
if(key=='d'){ pa-=5; pa=FixAng(pa); pdx=cos(deg_to_rad(pa)); pdy=-sin(deg_to_rad(pa));}
if(key=='w'){ img->player.x+=pdx*5; img->player.y+=pdy*5;}
if(key=='s'){ img->player.x-=pdx*5; img->player.y-=pdy*5;}*/
//
if (type == 'w')
{
img->player.x += img->player.vx * 5;
img->player.y += img->player.vy * 5;
}
else if (type == 's')
{
img->player.x -= img->player.vx * 5;
img->player.y -= img->player.vy * 5;
}
else if (type == 'a')
{
img->player.x += img->player.vy * 5;
img->player.y -= img->player.vx * 5;
}
else if (type == 'd')
{
img->player.x -= img->player.vy * 5;
img->player.y += img->player.vx * 5;
}
else if (type == 65361)//fleche gauche
{
img->player.angle += 5;
img->player.angle = reset_angle(img->player.angle);
img->player.vx = cos(deg_to_rad(img->player.angle));
img->player.vy = -sin(deg_to_rad(img->player.angle));
}
else if (type == 65363)//fleche droite
{
img->player.angle -= 5;
img->player.angle = reset_angle(img->player.angle);
img->player.vx = cos(deg_to_rad(img->player.angle));
img->player.vy = -sin(deg_to_rad(img->player.angle));
}
else
return (0);
//printf("after player: x= %f y= %f\n", img->player.x, img->player.y);
return (1);
}
void print_back(t_data *img)
{
int x = 530;
int y = 160;
while (x < 1024)
{
y = 0;
while (y < 320)
{
mlx_pixel_put(img->mlx, img->mlx_win, x , y, get_color(128, 128, 128));
//mlx_pixel_put(img->mlx, img->mlx_win, x , y - 160, get_color(0, 255, 255));
y++;
}
x++;
}
}
int key_press(int code, t_data *img)
{
if (code == 65307)
quit_game(img);
else
{
if (is_good(img, code))
{
//printf("code = %d\n", code);
//mlx_clear_window(img->mlx, img->mlx_win);
print_map(img->map, img);
print_player(img->player, img);
print_back(img);
draw_ray(img);
print_ray(img);
}
}
return (1);
}

View File

@ -1,51 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/15 18:48:45 by apommier #+# #+# */
/* Updated: 2022/06/15 23:41:56 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
double deg_to_rad(double angle)
{
return (angle * 3.14159265358979323846 / 180.0);
}
double reset_angle(double angle)
{
if (angle > 359)
angle -= 360.0;
if (angle < 0)
angle += 360.0;
return (angle);
}
void ft_error(char *error_msg)
{
ft_putendl_fd(error_msg, 2);
exit(1);
}
void ft_exit(char *str, t_data *img)
{
ft_putstr_fd(str, 2);
quit_game(img);
}
void check_dir(char *path, t_data *img)
{
int fd;
fd = open(path, O_DIRECTORY);
if (fd >= 0)
{
close(fd);
ft_exit("Error\nPath is a directory and not a file\n", img);
}
}

View File

@ -1,48 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* quit_game.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/15 18:48:35 by apommier #+# #+# */
/* Updated: 2022/06/15 18:54:20 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Cub3D.h"
void free_texture(t_data *img)
{
if (img->map.texture.north)
free(img->map.texture.north);
if (img->map.texture.south)
free(img->map.texture.south);
if (img->map.texture.east)
free(img->map.texture.east);
if (img->map.texture.west)
free(img->map.texture.west);
}
int quit_game(t_data *img)
{
if (img->mlx)
{
if (img->mlx_win)
mlx_destroy_window(img->mlx, img->mlx_win);
mlx_destroy_display(img->mlx);
free(img->mlx);
}
free_texture(img);
if (img->map.simple_map)
free(img->map.simple_map);
if (img->to_be_free.tab)
free_double(img->to_be_free.tab);
if (img->to_be_free.tab_two)
free_double(img->to_be_free.tab_two);
if (img->to_be_free.str)
free(img->to_be_free.str);
if (img->to_be_free.fd != -1)
close(img->to_be_free.fd);
exit(1);
}