Pushing from the vogsphere to the github

This commit is contained in:
Raphaël 2024-03-25 18:26:38 +00:00 committed by GitHub
parent d9986a5674
commit c86fe1ad3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 2093 additions and 0 deletions

105
sources/ft_check_map.c Normal file
View file

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 12:52:02 by rparodi #+# #+# */
/* Updated: 2024/03/10 18:24:11 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/so_long.h"
size_t what_is_it(char *str, size_t i, size_t line, t_mlx *mlx)
{
if (str[i] == '\n')
{
if (line == 0)
line = i;
else if (str[i - 1] == '1' && (str[i + 1] == '1' || str[i + 1] == '\0'))
return (line);
else
ft_exit(mlx, str, 1, "The map is invalid (not a rectangle)");
}
else if (str[i] == '1')
return (line);
else if (str[i] == '0')
return (line);
else if (str[i] == 'P')
return (line);
else if (str[i] == 'C')
return (line);
else if (str[i] == 'E')
return (line);
else
ft_exit(mlx, str, 1, "The map is invalid (unassigned char)");
return (line);
}
int check_border(char *str, t_mlx *mlx)
{
size_t i;
i = 0;
while (str[i] != '\n')
{
if (str[i] != '1')
ft_exit(mlx, str, 1, "The first line isn't good !");
i++;
}
i = ft_strlen(str) - 2;
while (str[i] != '\n')
{
if (str[i] != '1')
ft_exit(mlx, str, 1, "The last line isn't good !");
i--;
}
ft_parsing(str, mlx);
return (1);
}
size_t ft_linelen(t_mlx *mlx)
{
size_t line;
size_t i;
char *str;
size_t nb_coins;
size_t nb_exit;
i = 0;
line = 0;
nb_coins = 0;
nb_exit = 0;
str = ft_reverse_split(mlx);
while (str[i] != '\0')
{
line = what_is_it(str, i, line, mlx);
if (str[i] == 'C')
nb_coins++;
if (str[i] == 'E')
nb_exit++;
i++;
}
if (nb_coins == 0 || nb_exit == 0)
ft_exit(mlx, str, 1, "The map isn't valid (No Coins or Exit)");
check_border(str, mlx);
return (line);
}
void check_args(int argc, char *argv[], t_mlx *mlx)
{
mlx->fd_map = 0;
if (argc == 2)
{
mlx->fd_map = open(argv[1], O_RDONLY);
if (mlx->fd_map < 3)
ft_exit(mlx, NULL, 1, "File opening failed !");
else
{
ft_init_map(mlx, argv[1]);
ft_linelen(mlx);
}
}
}

65
sources/ft_exit.c Normal file
View file

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/18 18:07:39 by rparodi #+# #+# */
/* Updated: 2024/03/10 17:53:42 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/so_long.h"
void ft_free_maps(char **strs, char **copy)
{
size_t i;
i = 0;
while (strs[i] != NULL)
{
free(strs[i]);
i++;
}
free(strs);
if (copy != NULL)
{
i = 0;
while (copy[i] != NULL)
{
free(copy[i]);
i++;
}
free(copy);
}
}
void ft_exit(t_mlx *mlx, void *p2, short int exit_status, char *leaving_msg)
{
if (mlx != NULL)
{
if (mlx->fd_map > 0)
close(mlx->fd_map);
ft_free_image(mlx);
if (mlx->window)
mlx_destroy_window(mlx->init, mlx->window);
if (mlx->init)
mlx_destroy_display(mlx->init);
ft_free_maps(mlx->map, NULL);
free(mlx->init);
free(mlx);
}
free(p2);
if (exit_status == 1)
ft_printf("\n\nError:\n> %s\n", leaving_msg);
else if (exit_status == 0)
ft_printf("\n\n> %s\n", leaving_msg);
exit(exit_status);
}
int ft_close(t_mlx *mlx)
{
ft_exit(mlx, NULL, 0, "Your leaving? see you! (Reason: Red Cross)");
return (42);
}

102
sources/ft_init.c Normal file
View file

@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/19 13:25:35 by rparodi #+# #+# */
/* Updated: 2024/03/17 00:16:49 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/so_long.h"
char ft_init_image(t_mlx *mlx)
{
mlx->store.wall = load_image(mlx, "./textures/wall.xpm");
mlx->store.floor = load_image(mlx, "./textures/floor.xpm");
mlx->store.coins = load_image(mlx, "./textures/coins.xpm");
mlx->store.player = load_image(mlx, "./textures/steve.xpm");
mlx->store.lock = load_image(mlx, "./textures/exit_close.xpm");
mlx->store.open = load_image(mlx, "./textures/exit_open.xpm");
if (mlx->store.wall.img_ptr == NULL || mlx->store.floor.img_ptr == NULL \
|| mlx->store.coins.img_ptr == NULL || mlx->store.player.img_ptr == NULL \
|| mlx->store.lock.img_ptr == NULL || mlx->store.open.img_ptr == NULL)
return (1);
return (0);
}
t_mlx *ft_init_mlx(t_mlx *mlx, int argc, char *argv[])
{
mlx->init = mlx_init();
if (!mlx->init)
ft_exit(mlx, NULL, 1, "The allocation of mlx has crashed");
ft_init_map(mlx, argv[1]);
check_args(argc, argv, mlx);
map_size(mlx);
mlx->window = mlx_new_window(mlx->init, mlx->map_l, mlx->map_h, \
"so_long by rparodi");
if (!mlx->window)
ft_exit(mlx, NULL, 1, "The allocation of the window has crashed");
if (ft_init_image(mlx))
ft_exit(mlx, NULL, 1, "The allocation of the window has crashed");
display_map(mlx);
mlx_hook(mlx->window, 2, 1L << 0, key_hook, mlx);
mlx_hook(mlx->window, 17, 1L << 17, ft_close, mlx);
mlx_loop(mlx->init);
return (mlx);
}
void display_map(t_mlx *mlx)
{
t_image image;
size_t i;
size_t j;
i = 0;
j = 0;
while (j < mlx->map_h && mlx->map[j])
{
while (i + 1 < mlx->map_l && mlx->map[j][i] != '\0'\
&& mlx->map[j][i] != '\n')
{
image = ft_whitch_case(mlx->map[j][i], mlx);
mlx_put_image_to_window(mlx->init, mlx->window, image.img_ptr, \
i * image.w, j * image.h);
i++;
}
i = 0;
j++;
}
}
t_image new_img(int w, int h, t_mlx *mlx)
{
t_image image;
image.img_ptr = mlx_new_image(mlx->init, w, h);
image.addr = mlx_get_data_addr(image.img_ptr, &(image.bpp),
&(image.line_len), &(image.endian));
image.w = w;
image.h = h;
return (image);
}
t_image load_image(t_mlx *mlx, char *path)
{
t_image img;
int width;
int height;
width = LONGUEUR / mlx->map_l;
height = HAUTEUR / mlx->map_h;
img.img_ptr = mlx_xpm_file_to_image(mlx->init, path, &width, &height);
if (img.img_ptr == NULL)
ft_exit(mlx, NULL, 1, "The allocation of the image has crashed");
img.addr = mlx_get_data_addr(img.img_ptr, &(img.bpp), &(img.line_len),
&(img.endian));
img.w = width;
img.h = height;
return (img);
}

70
sources/ft_map.c Normal file
View file

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/21 22:51:53 by rparodi #+# #+# */
/* Updated: 2024/03/10 18:24:45 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/so_long.h"
void ft_free_image(t_mlx *mlx)
{
if (mlx->store.wall.img_ptr)
mlx_destroy_image(mlx->init, mlx->store.wall.img_ptr);
if (mlx->store.floor.img_ptr)
mlx_destroy_image(mlx->init, mlx->store.floor.img_ptr);
if (mlx->store.coins.img_ptr)
mlx_destroy_image(mlx->init, mlx->store.coins.img_ptr);
if (mlx->store.player.img_ptr)
mlx_destroy_image(mlx->init, mlx->store.player.img_ptr);
if (mlx->store.lock.img_ptr)
mlx_destroy_image(mlx->init, mlx->store.lock.img_ptr);
if (mlx->store.open.img_ptr)
mlx_destroy_image(mlx->init, mlx->store.open.img_ptr);
}
void map_size(t_mlx *mlx)
{
size_t i;
size_t prev_i;
size_t j;
i = 0;
j = 0;
prev_i = 0;
while (mlx->map[j] != NULL)
{
while (mlx->map[j][i] != '\0' )
i++;
if (mlx->map[j][i - 1] == '\n')
i--;
if (i != prev_i && prev_i != 0)
ft_exit(mlx, NULL, 1, "The map is not a rectangle");
if (j == 0)
prev_i = i;
j++;
}
mlx->map_h = j * 32;
mlx->map_l = i * 32;
}
t_image ft_whitch_case(char c, t_mlx *mlx)
{
if (c == '1')
return (mlx->store.wall);
else if (c == '0')
return (mlx->store.floor);
else if (c == 'C')
return (mlx->store.coins);
else if (c == 'P')
return (mlx->store.player);
else if (c == 'E')
return (mlx->store.lock);
else
return (mlx->store.open);
}

86
sources/ft_move.c Normal file
View file

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 15:19:03 by rparodi #+# #+# */
/* Updated: 2024/03/07 11:23:21 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/so_long.h"
void ft_move_up(int x, int y, t_mlx *mlx, unsigned int *count)
{
int coins;
coins = count_coins(mlx);
if (y > 0 && mlx->map[y - 1][x] != '1' && mlx->map[y - 1][x] != 'E')
{
ft_printf("You have use %d moves\r", ++(*count));
if (mlx->map[y - 1][x] != 'E' && mlx->map[y - 1][x] != 'F')
{
mlx->map[y][x] = '0';
mlx->map[y - 1][x] = 'P';
}
else if (mlx->map[y - 1][x] == 'F' && coins == 0)
ft_exit(mlx, NULL, 0, "You have finish the game !");
}
}
void ft_move_down(int x, int y, t_mlx *mlx, unsigned int *count)
{
int coins;
coins = count_coins(mlx);
if (mlx->map[y + 1] && mlx->map[y + 1][x] != '1'\
&& mlx->map[y + 1][x] != 'E')
{
ft_printf("You have use %d moves\r", ++(*count));
if (mlx->map[y + 1][x] != 'E' && mlx->map[y + 1][x] != 'F')
{
mlx->map[y][x] = '0';
mlx->map[y + 1][x] = 'P';
}
else if (mlx->map[y + 1][x] == 'F' && coins == 0)
ft_exit(mlx, NULL, 0, "You have finish the game !");
}
}
void ft_move_left(int x, int y, t_mlx *mlx, unsigned int *count)
{
int coins;
coins = count_coins(mlx);
if (x > 0 && mlx->map[y][x - 1] != '1' && mlx->map[y][x - 1] != 'E')
{
ft_printf("You have use %d moves\r", ++(*count));
if (mlx->map[y][x - 1] != 'E' && mlx->map[y][x - 1] != 'F')
{
mlx->map[y][x] = '0';
mlx->map[y][x - 1] = 'P';
}
else if (mlx->map[y][x - 1] == 'F' && coins == 0)
ft_exit(mlx, NULL, 0, "You have finish the game !");
}
}
void ft_move_right(int x, int y, t_mlx *mlx, unsigned int *count)
{
int coins;
coins = count_coins(mlx);
if (x > 0 && mlx->map[y][x + 1] != '1' && mlx->map[y][x + 1] != 'E')
{
ft_printf("You have use %d moves\r", ++(*count));
if (mlx->map[y][x + 1] != 'E' && mlx->map[y][x + 1] != 'F')
{
mlx->map[y][x] = '0';
mlx->map[y][x + 1] = 'P';
}
else if (mlx->map[y][x + 1] == 'F' && coins == 0)
ft_exit(mlx, NULL, 0, "You have finish the game !");
}
}

118
sources/ft_parse_map.c Normal file
View file

@ -0,0 +1,118 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/17 17:27:10 by rparodi #+# #+# */
/* Updated: 2024/03/10 18:23:31 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/so_long.h"
void flood_fill(char **map, int x, int y, const char *old_value)
{
if (x < 0 || y < 0 || x >= (int)(sizeof(map) / ft_strlen(map[0]))
|| y >= (int)(ft_strlen(map[0])))
{
if (ft_index_strchr(old_value, map[x][y]) == 0)
return ;
map[x][y] = '.';
flood_fill(map, x + 1, y, old_value);
flood_fill(map, x - 1, y, old_value);
flood_fill(map, x, y + 1, old_value);
flood_fill(map, x, y - 1, old_value);
}
}
void ft_access(char **map, t_mlx *mlx, size_t i, size_t j)
{
size_t k;
const char old_value[4] = {'P', 'C', 'E', '0'};
k = 0;
flood_fill(map, j, i, old_value);
while (map[k] != NULL)
{
if (ft_index_strchr(map[k], 'C') != 0 || \
ft_index_strchr(map[k], 'E') != 0)
{
ft_free_maps(map, NULL);
ft_exit(mlx, NULL, 1, "The map is not finishable !");
}
k++;
}
}
void ft_check_finishable(char **map, char **copy, t_mlx *mlx)
{
size_t j;
size_t i;
j = 1;
i = 0;
while (map[j] && map[j][0] != '\0')
{
while (map[j][i] != '\0')
{
if (map[j][i] == 'P')
break ;
i++;
}
if (map[j][i] == 'P')
break ;
i = 0;
j++;
}
if (map[j] && map[j][i] == 'P')
ft_access(copy, mlx, i, j);
else
{
ft_free_maps(copy, NULL);
ft_exit(mlx, NULL, 1, "No Spawn found");
}
}
char ft_check_minimal(char *str, t_mlx *mlx)
{
size_t i;
size_t nb_exit;
size_t nb_spawn;
i = 0;
nb_exit = 0;
nb_spawn = 0;
while (str[i] != '\0')
{
if (str[i] == 'E')
{
if (nb_exit != 0)
ft_exit(mlx, str, 1, "Too many exit are detected !");
nb_exit++;
}
else if (str[i] == 'P')
{
if (nb_spawn != 0)
ft_exit(mlx, str, 1, "Too many spawn are detected !");
nb_spawn++;
}
i++;
}
return (1);
}
void ft_parsing(char *str, t_mlx *mlx)
{
char **copy;
ft_check_minimal(str, mlx);
mlx->size_map = ft_strlen(str);
copy = ft_strsdup(mlx->map, mlx);
free(str);
if (copy == NULL)
ft_exit(mlx, NULL, 1, "The allocation of the map has crahsed !");
ft_check_finishable(mlx->map, copy, mlx);
ft_free_maps(copy, NULL);
}

39
sources/ft_value.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_value.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/17 15:16:20 by rparodi #+# #+# */
/* Updated: 2024/03/08 12:34:08 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/so_long.h"
void ft_init_value_image(t_image *img)
{
img->img_ptr = NULL;
img->addr = NULL;
img->h = 0;
img->w = 0;
img->bpp = 0;
img->endian = 0;
img->line_len = 0;
}
void ft_init_value_mlx(t_mlx *mlx)
{
mlx->init = NULL;
mlx->window = NULL;
mlx->w_h = 0;
mlx->w_l = 0;
mlx->fd_map = -1;
mlx->size_map = 0;
mlx->map = NULL;
mlx->map_h = 0;
mlx->map_l = 0;
mlx->img = NULL;
mlx->count = 0;
}

129
sources/main.c Normal file
View file

@ -0,0 +1,129 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 11:13:09 by rparodi #+# #+# */
/* Updated: 2024/03/10 17:50:02 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/so_long.h"
#include <strings.h>
unsigned int count_coins(t_mlx *mlx)
{
unsigned int i;
unsigned int j;
unsigned int coins;
i = 0;
j = 0;
coins = 0;
while (mlx->map[j])
{
while (mlx->map[j][i] != '\0')
{
if (mlx->map[j][i] == 'C')
coins++;
i++;
}
i = 0;
j++;
}
return (coins);
}
void ft_check_end(t_mlx *mlx, unsigned int coins)
{
int x;
int y;
x = 0;
y = 0;
while (mlx->map[y] && coins == 0)
{
x = 0;
while (mlx->map[y][x])
{
if (mlx->map[y][x] == 'E' || mlx->map[y][x] == 'F')
break ;
x++;
}
if (mlx->map[y][x] == 'E' || mlx->map[y][x] == 'F')
break ;
y++;
}
if (mlx->map[y][x] == 'E' && coins == 0)
mlx->map[y][x] = 'F';
}
void ft_check_pos(t_mlx *mlx, int *y, int *x)
{
while (mlx->map[*y])
{
*x = 0;
while (mlx->map[*y][*x])
{
if (mlx->map[*y][*x] == 'P')
break ;
(*x)++;
}
if (mlx->map[*y][*x] == 'P')
break ;
(*y)++;
}
}
int key_hook(int keycode, t_mlx *mlx)
{
unsigned int coins;
int x;
int y;
x = 0;
y = 0;
if (keycode == 53 || keycode == 65307)
ft_exit(mlx, NULL, 0, "Your leaving? see you! (Reason: Esc)");
ft_check_pos(mlx, &y, &x);
coins = count_coins(mlx);
if (keycode == 126 || keycode == 65362 || keycode == 119 || keycode == 122)
ft_move_up(x, y, mlx, &mlx->count);
else if (keycode == 125 || keycode == 65364 || keycode == 115)
ft_move_down(x, y, mlx, &mlx->count);
else if (keycode == 65361 || keycode == 97 || keycode == 113)
ft_move_left(x, y, mlx, &mlx->count);
else if (keycode == 124 || keycode == 65363 || keycode == 100)
ft_move_right(x, y, mlx, &mlx->count);
ft_check_end(mlx, coins);
display_map(mlx);
return (0);
}
int main(int argc, char *argv[])
{
t_mlx *mlx;
int temp;
if (argc > 2)
ft_exit(NULL, NULL, 1, "You have to give a single maps");
if (argc < 2)
ft_exit(NULL, NULL, 1, "You have to give a maps to see my project !");
temp = open(argv[1], O_RDONLY);
if (temp < 3)
ft_exit(NULL, NULL, 1, "The file doesn't exist");
close(temp);
mlx = (t_mlx *)malloc(sizeof(t_mlx));
bzero(mlx, sizeof(t_mlx));
if (argc == 2)
mlx->path = argv[1];
else
ft_exit(mlx, NULL, 1, "You have to give a map to see my project");
ft_init_value_mlx(&(*mlx));
if (!mlx)
ft_exit(mlx, NULL, 1, "The allocation of mlx has crashed");
mlx = ft_init_mlx(mlx, argc, argv);
return (0);
}