This commit is contained in:
kinou-p 2022-02-13 22:07:59 +01:00
parent 7e1c6c2845
commit bee17fcc1b
43 changed files with 264 additions and 64 deletions

40
Makefile Normal file
View File

@ -0,0 +1,40 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/02/13 16:27:49 by apommier #+# #+# #
# Updated: 2022/02/13 21:59:15 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = so_long
SRCS = main.c\
parsing.c
OBJS = ${SRCS:.c=.o}
CC = gcc
LIB = -L ./mlx -lmlx -lXext -lX11
INC = -I ./inc/ -I ./mlx
CFLAGS = -Wall -Wextra -Werror
RM = rm -rf
LIBFT = ./libft
${NAME}:${OBJS}
make bonus -C ${LIBFT}
gcc -g ${OBJS} ${LIBFT}/libft.a ${LIB} -o so_long
all: ${NAME} bonus
clean:
${RM} ${OBJS}
make clean -C ${LIBFT}
fclean: clean
${RM} ${NAME}
make fclean -C ${LIBFT}
re: fclean all
.PHONY: all clean fclean re bonus

3
cmd
View File

@ -1,3 +0,0 @@
gcc -I ./mlx -I ./libft -o main.o -c main.c
gcc -o main main.o -L ./mlx -lmlx -lXext -lX11 -L ./libft -lft

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
main

Binary file not shown.

103
main.c
View File

@ -1,4 +1,16 @@
//#include <mlx.h> /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:42:55 by apommier #+# #+# */
/* Updated: 2022/02/13 22:02:38 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include "./mlx/mlx.h" #include "./mlx/mlx.h"
#include "./mlx/mlx_int.h" #include "./mlx/mlx_int.h"
#include "./libft/libft.h" #include "./libft/libft.h"
@ -7,7 +19,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
//#include "mlx_int.h"
typedef struct s_data { typedef struct s_data {
void *mlx; void *mlx;
@ -18,29 +29,30 @@ typedef struct s_data {
int bear; int bear;
} t_data; } t_data;
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) void quit_game(t_data *img)
{ {
int i; int i;
int j;
int *image;
i = 0; i = 0;
j = 0;
printf("quit game\n");
//image = mlx_new_image(img->mlx, 13 * 32, 10 * 32);
//mlx_put_image_to_window(img->mlx, img->mlx_win, image, 0, 0)
mlx_clear_window(img->mlx, img->mlx_win); mlx_clear_window(img->mlx, img->mlx_win);
mlx_destroy_window(img->mlx, img->mlx_win); mlx_destroy_window(img->mlx, img->mlx_win);
mlx_destroy_display(img->mlx); mlx_destroy_display(img->mlx);
if (img->mlx) if (img->mlx)
free(img->mlx); free(img->mlx);
//if (img->mlx_win)
// free(img->mlx_win);
while (img->map_tab[i]) while (img->map_tab[i])
free(img->map_tab[i++]); free(img->map_tab[i++]);
//if (img->map_tab);
free(img->map_tab); free(img->map_tab);
exit(1); exit(1);
} }
@ -82,9 +94,7 @@ void print_case(char type, t_data *img, int y, int x)
else else
buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/back.xpm", &img_width, &img_height); buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/back.xpm", &img_width, &img_height);
mlx_put_image_to_window(img->mlx, img->mlx_win, buffer, x * 32, y * 32); mlx_put_image_to_window(img->mlx, img->mlx_win, buffer, x * 32, y * 32);
//free(buffer);
mlx_destroy_image(img->mlx, buffer); mlx_destroy_image(img->mlx, buffer);
//free(buffer);
} }
void print_map(char **map, t_data *img) void print_map(char **map, t_data *img)
@ -109,40 +119,30 @@ void print_map(char **map, t_data *img)
} }
} }
int 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')
{ {
if (*change == 'C') if (*change == 'C')
img->item--; img->item--;
img->move++; img->move++;
printf("%d\n", img->move); ft_putnbr_fd(img->move, 1);
ft_putchar_fd('\n', 1);
img->map_tab[y][x] = '0'; img->map_tab[y][x] = '0';
*change = 'P'; *change = 'P';
} }
else if (*change == 'E') else if (*change == 'E')
{ {
img->move++;
printf("%d\n", img->move);
if (img->item <= 0) if (img->item <= 0)
{
img->move++;
ft_putnbr_fd(img->move, 1);
ft_putchar_fd('\n', 1);
quit_game(img); quit_game(img);
}
} }
} }
/*void choose_bear(t_data *img, int type)
{
img->bear = 0;
if (type == 119)
;
else if (type == 97)
change_map(img, j, i, &map[j][i - 1]);
else if (type == 115)
change_map(img, j, i, &map[j + 1][i]);
else if (type == 100)
change_map(img, j, i, &map[j][i + 1]);
}*/
int is_good(char **map, int type, t_data *img) int is_good(char **map, int type, t_data *img)
{ {
int j; int j;
@ -169,31 +169,29 @@ int is_good(char **map, int type, t_data *img)
change_map(img, j, i, &map[j][i + 1]); change_map(img, j, i, &map[j][i + 1]);
else else
return (0); return (0);
return 1; return (1);
} }
int test(int code, t_data *img) int key_press(int code, t_data *img)
{ {
printf("code = %d\n", code);
if (code == 65307) if (code == 65307)
quit_game(img); quit_game(img);
is_good(img->map_tab, code, img); is_good(img->map_tab, code, img);
print_map(img->map_tab, img); print_map(img->map_tab, img);
return (1);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_data img; t_data img;
char *map = 0; char *map = 0;
char **map_tab;
int fd = 0; int fd = 0;
char *swap = 0; char *swap = 0;
char *del; char *del;
(void)argc;
img.bear = 0; img.bear = 0;
img.move = 0; img.move = 0;
img.mlx = mlx_init();
img.mlx_win = mlx_new_window(img.mlx, 13 * 32, 10 * 32, "Hungry Bear");
fd = open(argv[1], O_RDONLY); fd = open(argv[1], O_RDONLY);
swap = get_next_line(fd); swap = get_next_line(fd);
while (swap) while (swap)
@ -205,29 +203,12 @@ int main(int argc, char **argv)
free(del); free(del);
} }
img.map_tab = ft_split(map, '\n'); img.map_tab = ft_split(map, '\n');
check_map(img.map_tab);
img.mlx = mlx_init();
img.mlx_win = mlx_new_window(img.mlx, ft_strlen(img.map_tab[0]) * 32,
ft_strlen_double(img.map_tab) * 32, "Hungry Bear");
print_map(img.map_tab, &img); print_map(img.map_tab, &img);
free(map); free(map);
mlx_key_hook(img.mlx_win, &test, &img); mlx_hook(img.mlx_win, 2, 1L<<0, &key_press, &img);
mlx_loop(img.mlx); mlx_loop(img.mlx);
printf("heeeeeeeeeeeeeeerreeeee\n\n\n\n");
} }
/*img.img = mlx_xpm_file_to_image(mlx, "./sprite/back.xpm", &img_width, &img_height);
if (!img.img)
return 0;
//img.addr = mlx_get_data_addr(img.img, &img.bits_per_pixel, &img.line_length, &img.endian);
//my_mlx_pixel_put(&img, 5, 5, 0x00FF0000);
/*while (i != 320)
{
j = 0;
while (j != 320)
{
mlx_put_image_to_window(mlx, mlx_win, img.img, i, j);
j += 32;
}
i += 32;
}*/
//offset = (1920 * line_length + 1080 * (bits_per_pixel / 8));

169
parsing.c Normal file
View File

@ -0,0 +1,169 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.4map.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: map0map/0map/13 16:map1:43 by apommier #+# #+# */
/* Updated: map0map/0map/13 map1:map4:44 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include "./libft/libft.h"
#include <stdio.h>
int is_player(char **map)
{
int i;
int j;
int player;
player = 0;
i = 0;
j = 0;
while (map[j])
{
i = 0;
while (map[j][i++])
{
if (map[j][i] == 'P')
player++;
}
j++;
}
if (player == 1)
return (1);
return (0);
}
int is_exit(char **map)
{
int i;
int j;
int exit;
exit = 0;
i = 0;
j = 0;
while (map[j])
{
i = 1;
while (map[j][i])
{
if (map[j][i++] == 'E')
exit++;
}
j++;
}
if (exit > 0)
return (1);
return (0);
}
int is_item(char **map)
{
int i;
int j;
int item;
item = 0;
i = 0;
j = 0;
while (map[j])
{
i = 1;
while (map[j][i])
{
if (map[j][i++] == 'E')
item++;
}
j++;
}
return (item);
}
int is_rectangle(char **map)
{
int i;
int j;
i = 0;
j = 0;
while (map[j])
{
i = 1;
while (map[j][i])
{
if (map[j][i] != '0' && map[j][i] != '1' && map[j][i] != 'P' && map[j][i] != 'E' && map[j][i] != 'C')
return (0);
i++;
}
j++;
}
j = 0;
while (map[j + 1])
{
if (ft_strlen(map[j]) != ft_strlen(map[j + 1]))
return (0);
j++;
}
return (1);
}
int is_close(char **map)
{
int i;
int j;
i = 0;
j = 0;
while (map[j][i])
{
if (map[j][i++] != '1')
return (0);
}
while (map[j])
{
if (map[j][0] != '1' || map[j][ft_strlen(map[j]) - 1] != '1')
return (0);
j++;
}
i = 0;
while (map[j - 1][i])
{
if (map[j - 1][i++] != '1')
return (0);
}
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);
}

13
so_long.h Normal file
View File

@ -0,0 +1,13 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* so_long.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:30:59 by apommier #+# #+# */
/* Updated: 2022/02/13 19:14:29 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
int check_map(char **map);