This commit is contained in:
kinou-p 2022-02-14 00:44:15 +01:00
parent 2e17e4a4d9
commit 98cf13fc65
10 changed files with 267 additions and 174 deletions

View File

@ -6,17 +6,18 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/02/13 16:27:49 by apommier #+# #+# # # Created: 2022/02/13 16:27:49 by apommier #+# #+# #
# Updated: 2022/02/13 21:59:15 by apommier ### ########.fr # # Updated: 2022/02/13 23:53:19 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
NAME = so_long NAME = so_long
SRCS = main.c\ SRCS = srcs/main.c\
parsing.c srcs/parsing.c\
srcs/utils.c\
srcs/print_map.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CC = gcc CC = gcc
LIB = -L ./mlx -lmlx -lXext -lX11 LIB = -L ./mlx -lmlx -lXext -lX11
INC = -I ./inc/ -I ./mlx
CFLAGS = -Wall -Wextra -Werror CFLAGS = -Wall -Wextra -Werror
RM = rm -rf RM = rm -rf
LIBFT = ./libft LIBFT = ./libft

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:13:31 by apommier #+# #+# */ /* Created: 2020/11/29 00:13:31 by apommier #+# #+# */
/* Updated: 2022/01/17 11:39:13 by apommier ### ########.fr */ /* Updated: 2022/02/14 00:27:01 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,3 +26,19 @@ int ft_strncmp(const char *s1, const char *s2, size_t n)
} }
return (0); return (0);
} }
int ft_strcmp(const char *s1, const char *s2)
{
int i;
i = 0;
if (!s1 || !s2)
return (1);
while (s1[i] || s2[i])
{
if (s1[i] != s2[i])
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
i++;
}
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/11 03:21:27 by apommier #+# #+# */ /* Created: 2020/12/11 03:21:27 by apommier #+# #+# */
/* Updated: 2022/01/20 21:46:04 by apommier ### ########.fr */ /* Updated: 2022/02/14 00:27:42 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -42,6 +42,7 @@ int ft_toupper(int c);
int ft_tolower(int c); int ft_tolower(int c);
char *ft_strchr(const char *s, int c); char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c); char *ft_strrchr(const char *s, int c);
int ft_strcmp(const char *s1, const char *s2);
int ft_strncmp(const char *s1, const char *s2, size_t n); 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_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size); size_t ft_strlcat(char *dst, const char *src, size_t size);

View File

@ -1,27 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* so_long.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:30:59 by apommier #+# #+# */
/* Updated: 2022/02/13 22:12:14 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SO_LONG_H
# define SO_LONG_H
typedef struct s_data {
void *mlx;
void *mlx_win;
char **map_tab;
int item;
int move;
int bear;
} t_data;
int check_map(char **map);
#endif

View File

@ -6,71 +6,11 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:42:55 by apommier #+# #+# */ /* Created: 2022/02/13 16:42:55 by apommier #+# #+# */
/* Updated: 2022/02/13 22:26:17 by apommier ### ########.fr */ /* Updated: 2022/02/14 00:41:30 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "so_long.h" #include "so_long.h"
#include "./mlx/mlx.h"
#include "./mlx/mlx_int.h"
#include "./libft/libft.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
size_t ft_strlen_double(char **s)
{
size_t i;
i = 0;
if (!s)
return (0);
while (s[i])
i++;
return (i);
}
void quit_game(t_data *img)
{
int i;
i = 0;
mlx_clear_window(img->mlx, img->mlx_win);
mlx_destroy_window(img->mlx, img->mlx_win);
mlx_destroy_display(img->mlx);
if (img->mlx)
free(img->mlx);
while (img->map_tab[i])
free(img->map_tab[i++]);
free(img->map_tab);
exit(1);
}
int *choose_bear(t_data *img)
{
int *buffer;
int img_width;
int img_height;
buffer = 0;
if (img->bear == 119)
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/back_bear.xpm",
&img_width, &img_height);
else if (img->bear == 97)
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/left_bear.xpm",
&img_width, &img_height);
else if (img->bear == 100)
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/right_bear.xpm",
&img_width, &img_height);
else
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/front_bear.xpm",
&img_width, &img_height);
if (buffer == 0)
return (0);
return (buffer);
}
void print_case(char type, t_data *img, int y, int x) void print_case(char type, t_data *img, int y, int x)
{ {
@ -96,28 +36,6 @@ void print_case(char type, t_data *img, int y, int x)
mlx_destroy_image(img->mlx, buffer); mlx_destroy_image(img->mlx, buffer);
} }
void print_map(char **map, t_data *img)
{
int i;
int j;
i = 0;
j = 0;
img->item = 0;
while (map[j])
{
i = 0;
while (map[j][i])
{
print_case(map[j][i], img, j, i);
if (map[j][i] == 'C')
img->item++;
i++;
}
j++;
}
}
void change_map(t_data *img, int y, int x, char *change) void change_map(t_data *img, int y, int x, char *change)
{ {
if (*change == '0' || *change == 'C') if (*change == '0' || *change == 'C')
@ -171,24 +89,19 @@ int is_good(char **map, int type, t_data *img)
return (1); return (1);
} }
int key_press(int code, t_data *img)
{
if (code == 65307)
quit_game(img);
is_good(img->map_tab, code, img);
print_map(img->map_tab, img);
return (1);
}
char **set_map(char **argv) char **set_map(char **argv)
{ {
char **map; char **map_tab;
char *map_line; char *map;
char *del; char *del;
char *swap; char *swap;
int fd; int fd;
swap = 0;
map = 0;
fd = open(argv[1], O_RDONLY); fd = open(argv[1], O_RDONLY);
if (fd == -1)
ft_error("Error: Open call fail", 0);
swap = get_next_line(fd); swap = get_next_line(fd);
while (swap) while (swap)
{ {
@ -198,38 +111,32 @@ char **set_map(char **argv)
swap = get_next_line(fd); swap = get_next_line(fd);
free(del); free(del);
} }
return (map); close(fd);
map_tab = ft_split(map, '\n');
free(map);
return (map_tab);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_data img; t_data img;
char *map; int height;
int fd;
char *swap;
char *del;
map = 0; height = 0;
(void)argc; (void)argc;
check_arg(argc, argv);
img.bear = 0; img.bear = 0;
img.move = 0; img.move = 0;
fd = open(argv[1], O_RDONLY); img.map_tab = set_map(argv);
swap = get_next_line(fd); while (img.map_tab[height])
while (swap) height++;
{
del = map;
map = ft_strjoin(map, swap);
free(swap);
swap = get_next_line(fd);
free(del);
}
img.map_tab = ft_split(map, '\n');
check_map(img.map_tab); check_map(img.map_tab);
img.mlx = mlx_init(); img.mlx = mlx_init();
if (!img.mlx)
free_double(img.map_tab);
img.mlx_win = mlx_new_window(img.mlx, ft_strlen(img.map_tab[0]) * 32, img.mlx_win = mlx_new_window(img.mlx, ft_strlen(img.map_tab[0]) * 32,
ft_strlen_double(img.map_tab) * 32, "Hungry Bear"); height * 32, "Hungry Bear");
print_map(img.map_tab, &img); print_map(img.map_tab, &img);
free(map);
mlx_hook(img.mlx_win, 2, 1L << 0, &key_press, &img); mlx_hook(img.mlx_win, 2, 1L << 0, &key_press, &img);
mlx_loop(img.mlx); mlx_loop(img.mlx);
} }

View File

@ -6,12 +6,11 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 22:26:34 by apommier #+# #+# */ /* Created: 2022/02/13 22:26:34 by apommier #+# #+# */
/* Updated: 2022/02/13 22:28:18 by apommier ### ########.fr */ /* Updated: 2022/02/13 23:47:44 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "so_long.h" #include "so_long.h"
#include "./libft/libft.h"
int is_player(char **map) int is_player(char **map)
{ {
@ -138,29 +137,3 @@ int is_close(char **map)
} }
return (1); return (1);
} }
void ft_error(char *error_msg, char **map)
{
int i;
i = 0;
while (map[i])
free(map[i++]);
ft_putstr_fd(error_msg, 2);
exit(1);
}
int check_map(char **map)
{
if (!is_player(map))
ft_error("You need just one player\n", map);
if (!is_exit(map))
ft_error("There is no exit\n", map);
if (!is_close(map))
ft_error("The map isn't close\n", map);
if (!is_item(map))
ft_error("There is no item\n", map);
if (!is_rectangle(map))
ft_error("The map isn't a rectangle\n", map);
return (1);
}

66
srcs/print_map.c Normal file
View File

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 23:39:18 by apommier #+# #+# */
/* Updated: 2022/02/14 00:42:06 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void print_map(char **map, t_data *img)
{
int i;
int j;
i = 0;
j = 0;
img->item = 0;
while (map[j])
{
i = 0;
while (map[j][i])
{
print_case(map[j][i], img, j, i);
if (map[j][i] == 'C')
img->item++;
i++;
}
j++;
}
}
void free_double(char **to_free)
{
int i;
i = 0;
while (to_free[i])
free(to_free[i++]);
}
void check_arg(int argc, char **argv)
{
int i;
int fd;
fd = 0;
i = 0;
if (argc != 2)
ft_error("Error: Bad number of arguments\n", 0);
while (argv[1] && argv[1][i] != '.')
i++;
if (argv[1][i] == '.' && !ft_strcmp(&argv[1][i], ".ber"))
{
fd = open(argv[1], O_RDONLY);
if (fd == -1)
ft_error("Error: The map file doesn't exist\n", 0);
close(fd);
}
else
ft_error("Error: Bad file type\n", 0);
}

65
srcs/so_long.h Normal file
View File

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* so_long.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:30:59 by apommier #+# #+# */
/* Updated: 2022/02/14 00:37:07 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SO_LONG_H
# define SO_LONG_H
# include "../mlx/mlx.h"
# include "../mlx/mlx_int.h"
# include "../libft/libft.h"
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <stdio.h>
typedef struct s_data {
void *mlx;
void *mlx_win;
char **map_tab;
int item;
int move;
int bear;
} t_data;
//parsing functions
int check_map(char **map);
int is_player(char **map);
int is_exit(char **map);
int is_item(char **map);
int is_rectangle(char **map);
int is_close(char **map);
//print map
void print_map(char **map, t_data *img);
//free double pointer
void free_double(char **to_free);
//check arguments and if map file is correct
void check_arg(int argc, char **argv);
//utils
void ft_error(char *error_msg, char **map);
int check_map(char **map);
void quit_game(t_data *img);
int key_press(int code, t_data *img);
int *choose_bear(t_data *img);
//main functions
void print_case(char type, t_data *img, int y, int x);
void change_map(t_data *img, int y, int x, char *change);
int is_good(char **map, int type, t_data *img);
char **set_map(char **argv);
int main(int argc, char **argv);
#endif

91
srcs/utils.c Normal file
View File

@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 23:37:02 by apommier #+# #+# */
/* Updated: 2022/02/14 00:39:41 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void ft_error(char *error_msg, char **map)
{
int i;
i = 0;
if (map)
{
while (map[i])
free(map[i++]);
}
ft_putstr_fd(error_msg, 2);
exit(1);
}
int check_map(char **map)
{
if (!is_rectangle(map))
ft_error("Error: The map isn't a rectangle\n", map);
if (!is_close(map))
ft_error("Error: The map isn't close\n", map);
if (!is_player(map))
ft_error("Error: You need just one player\n", map);
if (!is_exit(map))
ft_error("Error: There is no exit\n", map);
if (!is_item(map))
ft_error("Error: There is no item\n", map);
return (1);
}
void quit_game(t_data *img)
{
int i;
i = 0;
mlx_clear_window(img->mlx, img->mlx_win);
mlx_destroy_window(img->mlx, img->mlx_win);
mlx_destroy_display(img->mlx);
if (img->mlx)
free(img->mlx);
while (img->map_tab[i])
free(img->map_tab[i++]);
free(img->map_tab);
exit(1);
}
int key_press(int code, t_data *img)
{
if (code == 65307)
quit_game(img);
is_good(img->map_tab, code, img);
print_map(img->map_tab, img);
return (1);
}
int *choose_bear(t_data *img)
{
int *buffer;
int img_width;
int img_height;
buffer = 0;
if (img->bear == 119)
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/back_bear.xpm",
&img_width, &img_height);
else if (img->bear == 97)
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/left_bear.xpm",
&img_width, &img_height);
else if (img->bear == 100)
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/right_bear.xpm",
&img_width, &img_height);
else
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/front_bear.xpm",
&img_width, &img_height);
if (buffer == 0)
return (0);
return (buffer);
}