no norm
This commit is contained in:
parent
7e1c6c2845
commit
bee17fcc1b
40
Makefile
Normal file
40
Makefile
Normal 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
3
cmd
@ -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
|
||||
|
||||
BIN
libft/ft_atoi.o
BIN
libft/ft_atoi.o
Binary file not shown.
BIN
libft/ft_bzero.o
BIN
libft/ft_bzero.o
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
libft/ft_itoa.o
BIN
libft/ft_itoa.o
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
libft/ft_split.o
BIN
libft/ft_split.o
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
libft/libft.a
BIN
libft/libft.a
Binary file not shown.
103
main.c
103
main.c
@ -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_int.h"
|
||||
#include "./libft/libft.h"
|
||||
@ -7,7 +19,6 @@
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
//#include "mlx_int.h"
|
||||
|
||||
typedef struct s_data {
|
||||
void *mlx;
|
||||
@ -18,29 +29,30 @@ typedef struct s_data {
|
||||
int bear;
|
||||
} 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)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int *image;
|
||||
|
||||
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_destroy_window(img->mlx, img->mlx_win);
|
||||
|
||||
|
||||
mlx_destroy_display(img->mlx);
|
||||
if (img->mlx)
|
||||
free(img->mlx);
|
||||
//if (img->mlx_win)
|
||||
// free(img->mlx_win);
|
||||
while (img->map_tab[i])
|
||||
free(img->map_tab[i++]);
|
||||
//if (img->map_tab);
|
||||
free(img->map_tab);
|
||||
exit(1);
|
||||
}
|
||||
@ -82,9 +94,7 @@ void print_case(char type, t_data *img, int y, int x)
|
||||
else
|
||||
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);
|
||||
//free(buffer);
|
||||
mlx_destroy_image(img->mlx, buffer);
|
||||
//free(buffer);
|
||||
}
|
||||
|
||||
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 == 'C')
|
||||
img->item--;
|
||||
img->move++;
|
||||
printf("%d\n", img->move);
|
||||
ft_putnbr_fd(img->move, 1);
|
||||
ft_putchar_fd('\n', 1);
|
||||
img->map_tab[y][x] = '0';
|
||||
*change = 'P';
|
||||
}
|
||||
else if (*change == 'E')
|
||||
{
|
||||
img->move++;
|
||||
printf("%d\n", img->move);
|
||||
if (img->item <= 0)
|
||||
{
|
||||
img->move++;
|
||||
ft_putnbr_fd(img->move, 1);
|
||||
ft_putchar_fd('\n', 1);
|
||||
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 j;
|
||||
@ -169,31 +169,29 @@ int is_good(char **map, int type, t_data *img)
|
||||
change_map(img, j, i, &map[j][i + 1]);
|
||||
else
|
||||
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)
|
||||
quit_game(img);
|
||||
is_good(img->map_tab, code, img);
|
||||
print_map(img->map_tab, img);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_data img;
|
||||
char *map = 0;
|
||||
char **map_tab;
|
||||
int fd = 0;
|
||||
char *swap = 0;
|
||||
char *del;
|
||||
|
||||
(void)argc;
|
||||
img.bear = 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);
|
||||
swap = get_next_line(fd);
|
||||
while (swap)
|
||||
@ -205,29 +203,12 @@ int main(int argc, char **argv)
|
||||
free(del);
|
||||
}
|
||||
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);
|
||||
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);
|
||||
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
169
parsing.c
Normal 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
13
so_long.h
Normal 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);
|
||||
Loading…
Reference in New Issue
Block a user