Compare commits
18 Commits
7c7b419c1b
...
master
Author | SHA1 | Date | |
---|---|---|---|
42a0cfd5b6 | |||
595d220abd | |||
9a6d7b6ed7 | |||
b1170d5b5f | |||
d4f3a72f76 | |||
a241d049c4 | |||
d6117cfb8f | |||
8b200fb8ba | |||
7d7de10b7a | |||
1efd8e1a7b | |||
b66f8d6f5a | |||
8a0b32efda | |||
91c5ef0860 | |||
750d449f6c | |||
7addabc200 | |||
c8f6b30e1b | |||
1331231004 | |||
6312ed4cdc |
30
Makefile
30
Makefile
@ -1,27 +1,49 @@
|
||||
SRCS = asset.c draw.c key.c main.c map.c parsing2.c parsing.c xpm.c
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/01/20 14:41:36 by cchauvet #+# #+# #
|
||||
# Updated: 2023/01/20 14:41:42 by cchauvet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SRCS = mandatory/asset.c mandatory/draw.c mandatory/key.c mandatory/main.c mandatory/map.c mandatory/parsing2.c mandatory/parsing.c mandatory/xpm.c
|
||||
|
||||
BSRCS = bonus/asset.c bonus/draw.c bonus/key.c bonus/main.c bonus/map.c bonus/parsing2.c bonus/parsing.c bonus/xpm.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
BOBJS = ${BSRCS:.c=.o}
|
||||
|
||||
NAME = so_long
|
||||
|
||||
LIBS = libftx/libftx.a minilibx-linux/libmlx.a
|
||||
|
||||
CC = clang
|
||||
|
||||
FLAG = -Wall -Wextra -Werror -g
|
||||
FLAG = -Wall -Wextra -Werror
|
||||
|
||||
%.o: %.c
|
||||
${CC} ${FLAG} -c $< -o $@
|
||||
|
||||
all: ${NAME}
|
||||
|
||||
bonus: ${BOBJS}
|
||||
make -C libftx
|
||||
make -C minilibx-linux
|
||||
${CC} ${BOBJS} -o ${NAME} ${LIBS} -lXext -lX11
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
make -C libftx
|
||||
make -C minilibx-linux
|
||||
${CC} ${OBJS} -o ${NAME} ${LIBS} -lXext -lX11
|
||||
|
||||
clean:
|
||||
rm -f ${OBJS}
|
||||
rm -f ${OBJS} ${BOBJS}
|
||||
rm -f textures/*
|
||||
make -C libftx clean
|
||||
make -C minilibx-linux clean
|
||||
|
||||
@ -33,4 +55,4 @@ fclean: clean
|
||||
re: fclean
|
||||
make all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
.PHONY: all clean fclean re bonus
|
||||
|
53
README.md
Normal file
53
README.md
Normal file
@ -0,0 +1,53 @@
|
||||
# Solong
|
||||
|
||||
This is my implementation of the Solong project for the 42 school.
|
||||
## Description
|
||||
|
||||
Solong is a simple 2D game where the player has to collect all the items in a maze and reach the exit. The game is written in C using the minilibx library for the graphics.
|
||||
|
||||
The project is composed of three parts:
|
||||
|
||||
- The parsing of the map file in a format specified by the subject.
|
||||
- The rendering of the game using the graphics library.
|
||||
- The game loop and the handling of user input.
|
||||
|
||||
## Installation
|
||||
To install this project clone this repo
|
||||
``` bash
|
||||
git clone https://git.chauvet.pro/starnakin/solong
|
||||
```
|
||||
### Mandatory
|
||||
To compile the game, simply run make in the root directory of the project. This will generate an executable called solong.
|
||||
``` bash
|
||||
make
|
||||
```
|
||||
### Bonus
|
||||
To compile the game, simply run make bonus in the root directory of the project. This will generate an executable called solong.
|
||||
``` bash
|
||||
make bonus
|
||||
```
|
||||
## Launch
|
||||
|
||||
To run the game, you need to provide a valid map file as an argument. For example:
|
||||
|
||||
``` bash
|
||||
./solong maps/map.ber
|
||||
```
|
||||
The game requires the minilibx library to be installed on your system. If you're using macOS, the library should be already installed. If you're using Linux, you may need to install it manually.
|
||||
## Controls
|
||||
|
||||
The player can be moved using the arrow keys or the WASD keys. Pressing the ESC key or closing the window will exit the game.
|
||||
## Screenshots
|
||||
|
||||
Here are some screenshots of the game:
|
||||
|
||||

|
||||
|
||||
## Resources
|
||||
|
||||

|
||||

|
||||
|
||||
## Author
|
||||
|
||||
This project was created by Camille Chauvet. If you have any questions or suggestions, feel free to contact me.
|
83
bonus/asset.c
Normal file
83
bonus/asset.c
Normal file
@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* asset.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/06 14:23:00 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/19 13:32:14 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
static void *ft_gen_asset(char *color, size_t case_size, t_data *data)
|
||||
{
|
||||
t_square square;
|
||||
char *name;
|
||||
int dimmension;
|
||||
void *img;
|
||||
|
||||
square.color = color;
|
||||
square.size = case_size;
|
||||
name = ft_xpm_gen_file(square);
|
||||
if (name == NULL)
|
||||
return (NULL);
|
||||
img = mlx_xpm_file_to_image(data->mlx,
|
||||
name, &dimmension, &dimmension);
|
||||
free(name);
|
||||
return (img);
|
||||
}
|
||||
|
||||
static char **ft_color_gen(void)
|
||||
{
|
||||
size_t i;
|
||||
char **tab;
|
||||
|
||||
tab = ft_calloc(NB_COLORS + 1, sizeof(char *));
|
||||
if (tab == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (i < NB_COLORS)
|
||||
{
|
||||
tab[i] = ft_ultoa_base(((1 << 23) * i) / NB_COLORS, HEX);
|
||||
if (tab[i] == NULL)
|
||||
{
|
||||
ft_cancel(tab, i);
|
||||
return (NULL);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
tab[i] = NULL;
|
||||
return (tab);
|
||||
}
|
||||
|
||||
int ft_gen_assets(t_data *data)
|
||||
{
|
||||
char **colors;
|
||||
size_t i;
|
||||
|
||||
colors = ft_color_gen();
|
||||
if (colors == NULL)
|
||||
return (1);
|
||||
i = 0;
|
||||
while (i < NB_COLORS)
|
||||
{
|
||||
data->assets[i] = ft_gen_asset(colors[i], CASE_SIZE, data);
|
||||
if (data->assets[i] == NULL)
|
||||
{
|
||||
ft_freer_tab_ultimate(1, colors);
|
||||
return (1);
|
||||
}
|
||||
data->assets[i + NB_COLORS] = ft_gen_asset(colors[i],
|
||||
(RENDER_DISTANCE * 2 + 1) * CASE_SIZE, data);
|
||||
if (data->assets[i + NB_COLORS] == NULL)
|
||||
ft_freer_tab_ultimate(1, colors);
|
||||
if (data->assets[i + NB_COLORS] == NULL)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, colors);
|
||||
return (0);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/12/13 15:49:23 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 18:34:28 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/19 15:20:56 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -37,18 +37,16 @@ static void ft_draw_foots(t_data *data)
|
||||
{
|
||||
char *foots;
|
||||
|
||||
if (BONUS)
|
||||
data->nb_foots++;
|
||||
foots = ft_itoa(data->nb_foots);
|
||||
if (foots == NULL)
|
||||
{
|
||||
foots = ft_itoa(data->nb_foots);
|
||||
if (foots == NULL)
|
||||
{
|
||||
ft_printf("Memory error\n");
|
||||
ft_exit(data);
|
||||
}
|
||||
mlx_string_put(data->mlx, data->window, 10, 10, 255, foots);
|
||||
ft_printf("\r%s", foots);
|
||||
free(foots);
|
||||
ft_printf("Memory error\n");
|
||||
ft_exit(data);
|
||||
}
|
||||
mlx_string_put(data->mlx, data->window, 10, 10, 255, foots);
|
||||
ft_printf("\r%s", foots);
|
||||
free(foots);
|
||||
}
|
||||
|
||||
int ft_draw_map(t_data *data)
|
||||
@ -58,12 +56,12 @@ int ft_draw_map(t_data *data)
|
||||
void *img;
|
||||
|
||||
ft_fill_pos(data->map);
|
||||
if (BONUS)
|
||||
data->nb_swaps++;
|
||||
data->nb_swaps = ft_random_generator(0, NB_COLORS - 6);
|
||||
patern = ft_get_player_map(*data->map);
|
||||
if (patern == NULL)
|
||||
return (1);
|
||||
ft_draw_img(data, data->assets[data->nb_swaps + NB_COLORS], 0, 0);
|
||||
ft_draw_img(data, data->assets[data->nb_swaps % NB_COLORS + NB_COLORS], 0,
|
||||
0);
|
||||
tab[1] = -1;
|
||||
while (patern[++tab[1]] != NULL)
|
||||
{
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/09 18:35:27 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 15:40:38 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/16 12:52:02 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/12/12 17:20:17 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 18:35:20 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/19 16:54:42 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -24,14 +24,15 @@ int ft_initialised(char *path, t_data *data)
|
||||
}
|
||||
data->mlx = mlx_init();
|
||||
ft_printf("Generating assets ...");
|
||||
ft_gen_assets(data);
|
||||
if (ft_gen_assets(data))
|
||||
ft_exit(data);
|
||||
ft_printf("\rGenerating assets [FINISHED]\n");
|
||||
data->map = map;
|
||||
data->window = mlx_new_window(data->mlx, WINDOW_SIZE, WINDOW_SIZE, "long");
|
||||
data->window = mlx_new_window(data->mlx,
|
||||
(RENDER_DISTANCE * 2 + 1) * CASE_SIZE,
|
||||
(RENDER_DISTANCE * 2 + 1) * CASE_SIZE, "long");
|
||||
data->nb_foots = 0;
|
||||
data->nb_swaps = 35;
|
||||
if (BONUS)
|
||||
data->nb_swaps = ft_random_generator(0, 100);
|
||||
data->nb_swaps = ft_random_generator(0, NB_COLORS - 6);
|
||||
ft_draw_map(data);
|
||||
mlx_hook(data->window, 17, (0L), ft_exit, data);
|
||||
mlx_hook(data->window, 2, (1L << 0), ft_key, data);
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 20:14:44 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 15:47:42 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/16 18:48:43 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -43,9 +43,7 @@ int ft_test_map_content1(t_map *map)
|
||||
x = 0;
|
||||
while (x < map->x_len)
|
||||
{
|
||||
if (!BONUS && ft_is_in("01CEP", map->patern[y][x]) == 0)
|
||||
return (0);
|
||||
if (BONUS && ft_is_in("01CEPB", map->patern[y][x]) == 0)
|
||||
if (ft_is_in("01CEPB", map->patern[y][x]) == 0)
|
||||
return (0);
|
||||
if (map->patern[y][x] == 'C')
|
||||
nb_collectable++;
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/12 16:17:20 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 16:28:34 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/16 18:48:20 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,14 +14,8 @@
|
||||
|
||||
static void ft_collector(char **patern, size_t x, size_t y)
|
||||
{
|
||||
if (BONUS)
|
||||
{
|
||||
if (ft_is_in("1B", patern[y][x]))
|
||||
return ;
|
||||
}
|
||||
else
|
||||
if (patern[y][x] == '1')
|
||||
return ;
|
||||
if (ft_is_in("1B", patern[y][x]))
|
||||
return ;
|
||||
patern[y][x] = '1';
|
||||
ft_collector(patern, x, y - 1);
|
||||
ft_collector(patern, x + 1, y);
|
67
bonus/solong.h
Normal file
67
bonus/solong.h
Normal file
@ -0,0 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* solong.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 16:20:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/19 13:40:52 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SOLONG_H
|
||||
# define SOLONG_H
|
||||
# include "../libftx/libftx.h"
|
||||
# define XPM_PATH "./textures/"
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include "../minilibx-linux/mlx.h"
|
||||
# define CASE_SIZE 128
|
||||
# define XPM_HEADER "\nstatic char * XFACE[] = {\""
|
||||
# define RENDER_DISTANCE 4
|
||||
# define NB_COLORS 100
|
||||
# define HEX "0123456789abcdef"
|
||||
|
||||
typedef struct s_square
|
||||
{
|
||||
char *color;
|
||||
size_t size;
|
||||
} t_square;
|
||||
|
||||
typedef struct s_map
|
||||
{
|
||||
size_t x_len;
|
||||
size_t y_len;
|
||||
size_t nb_collectable;
|
||||
char **patern;
|
||||
size_t player_pos[2];
|
||||
size_t exit_pos[2];
|
||||
} t_map;
|
||||
|
||||
typedef struct s_data
|
||||
{
|
||||
void *mlx;
|
||||
void *window;
|
||||
void *assets[NB_COLORS * 2];
|
||||
t_map *map;
|
||||
size_t nb_swaps;
|
||||
size_t nb_foots;
|
||||
} t_data;
|
||||
|
||||
int ft_test_map_is_finishable(t_map *map);
|
||||
int ft_exit(t_data *data);
|
||||
int ft_test_map_content2(char **patern);
|
||||
int ft_key(int key, t_data *data);
|
||||
void ft_fill_pos(t_map *map);
|
||||
char *name_generator(t_square square);
|
||||
t_map *ft_getmap(char *path);
|
||||
char *ft_xpm_gen_file(t_square square);
|
||||
int ft_gen_assets(t_data *data);
|
||||
char **ft_get_player_map(t_map map);
|
||||
int ft_draw_map(t_data *data);
|
||||
char *name_generator(t_square square);
|
||||
int ft_color_changer(t_data *data);
|
||||
void ft_draw_img(t_data *data, void *img, size_t x, size_t y);
|
||||
int ft_map_is_correct(t_map *map);
|
||||
#endif
|
115
bonus/xpm.c
Normal file
115
bonus/xpm.c
Normal file
@ -0,0 +1,115 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* xpm.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 13:53:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/19 12:34:40 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
static char *ft_square(size_t size)
|
||||
{
|
||||
char *map;
|
||||
size_t x;
|
||||
size_t y;
|
||||
|
||||
map = malloc(((size + 4) * size) * sizeof(char));
|
||||
if (map == NULL)
|
||||
return (map);
|
||||
y = 0;
|
||||
while (y < size)
|
||||
{
|
||||
map[(size + 4) * y] = '"';
|
||||
x = 0;
|
||||
while (x < size)
|
||||
{
|
||||
map[y * (size + 4) + x + 1] = 'a';
|
||||
x++;
|
||||
}
|
||||
map[(size + 4) * y + size + 1] = '"';
|
||||
map[(size + 4) * y + size + 2] = ',';
|
||||
map[(size + 4) * y + size + 3] = '\n';
|
||||
y++;
|
||||
}
|
||||
map[(size + 4) * size - 2] = '\0';
|
||||
return (map);
|
||||
}
|
||||
|
||||
char *name_generator(t_square square)
|
||||
{
|
||||
char *out;
|
||||
char *size;
|
||||
char *temp;
|
||||
|
||||
size = ft_itoa(square.size);
|
||||
if (size == NULL)
|
||||
return (NULL);
|
||||
temp = ft_strmerger(3, XPM_PATH, square.color, "_");
|
||||
if (temp == NULL)
|
||||
{
|
||||
free(size);
|
||||
return (NULL);
|
||||
}
|
||||
out = ft_strmerger(5, temp, size, "x", size, ".xpm");
|
||||
free(size);
|
||||
free(temp);
|
||||
return (out);
|
||||
}
|
||||
|
||||
static char *ft_gen_xpm_content(t_square square)
|
||||
{
|
||||
char *content;
|
||||
char *temp;
|
||||
char *map;
|
||||
|
||||
temp = ft_itoa(square.size);
|
||||
if (temp == NULL)
|
||||
return (NULL);
|
||||
content = ft_strmerger(5, XPM_HEADER, temp, " ", temp, " 1 1\",\n\"");
|
||||
free(temp);
|
||||
if (content == NULL)
|
||||
return (NULL);
|
||||
temp = ft_strmerger(5, "/* XPM */", content, "a c #", square.color, "\",\n");
|
||||
free(content);
|
||||
if (temp == NULL)
|
||||
return (NULL);
|
||||
map = ft_square(square.size);
|
||||
if (map == NULL)
|
||||
{
|
||||
free(temp);
|
||||
return (NULL);
|
||||
}
|
||||
content = ft_strmerger(3, temp, map, "};");
|
||||
free(temp);
|
||||
free(map);
|
||||
return (content);
|
||||
}
|
||||
|
||||
char *ft_xpm_gen_file(t_square square)
|
||||
{
|
||||
char *file_content;
|
||||
char *path;
|
||||
int fd;
|
||||
|
||||
path = name_generator(square);
|
||||
if (path == NULL)
|
||||
return (NULL);
|
||||
fd = open(path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
{
|
||||
file_content = ft_gen_xpm_content(square);
|
||||
fd = open(path, O_WRONLY | O_CREAT, 0644);
|
||||
write(fd, file_content, ft_strlen(file_content));
|
||||
if (fd != -1)
|
||||
close(fd);
|
||||
free(file_content);
|
||||
}
|
||||
else
|
||||
close(fd);
|
||||
return (path);
|
||||
}
|
@ -6,13 +6,13 @@
|
||||
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
|
||||
# Updated: 2023/01/12 16:31:55 by cchauvet ### ########.fr #
|
||||
# Updated: 2023/01/19 12:55:43 by cchauvet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
CC = clang
|
||||
|
||||
SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c
|
||||
SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c
|
||||
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 14:03:10 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/12 16:32:35 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/19 17:37:25 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
# include <fcntl.h>
|
||||
# include "../libft/libft.h"
|
||||
|
||||
char *ft_ultoa_base(unsigned long long n, char *base);
|
||||
char *get_next_line(int fd);
|
||||
size_t ft_random_generator(size_t start, size_t stop);
|
||||
void ft_freer_tab_ultimate(size_t len, ...);
|
||||
|
0
libftx/extra/ft_tabrealloc-5f29f43f.o.tmp
Normal file
0
libftx/extra/ft_tabrealloc-5f29f43f.o.tmp
Normal file
BIN
libftx/extra/ft_ultoa.o
Normal file
BIN
libftx/extra/ft_ultoa.o
Normal file
Binary file not shown.
89
libftx/extra/ft_ultoa_base.c
Normal file
89
libftx/extra/ft_ultoa_base.c
Normal file
@ -0,0 +1,89 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ultoa_base.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/19 13:01:09 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
static size_t ft_str_size(unsigned long long n, size_t base_size)
|
||||
{
|
||||
size_t size;
|
||||
|
||||
size = 1;
|
||||
if (n == 0)
|
||||
return (2);
|
||||
while (n != 0)
|
||||
{
|
||||
n = n / base_size;
|
||||
size++;
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
|
||||
static int ft_isdup(char *str)
|
||||
{
|
||||
char c;
|
||||
size_t i;
|
||||
|
||||
while (*str != 0)
|
||||
{
|
||||
c = *str;
|
||||
i = 1;
|
||||
while (str[i] != 0)
|
||||
{
|
||||
if (str[i] == c)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static size_t ft_base_size(char *base)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
if (ft_isdup(base))
|
||||
return (0);
|
||||
len = ft_strlen(base);
|
||||
if (len < 2)
|
||||
return (0);
|
||||
return (len);
|
||||
}
|
||||
|
||||
char *ft_ultoa_base(unsigned long long n, char *base)
|
||||
{
|
||||
size_t base_size;
|
||||
int str_size;
|
||||
char *out;
|
||||
size_t i;
|
||||
|
||||
if (base == NULL)
|
||||
return (NULL);
|
||||
base_size = ft_base_size(base);
|
||||
if (base_size == 0)
|
||||
return (NULL);
|
||||
str_size = ft_str_size(n, base_size);
|
||||
out = ft_calloc(str_size + 1, sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
if (n == 0)
|
||||
out[0] = base[0];
|
||||
while (n != 0)
|
||||
{
|
||||
out[str_size - 2 - i] = base[n % base_size];
|
||||
n /= base_size;
|
||||
i++;
|
||||
}
|
||||
out[str_size - 1] = '\0';
|
||||
return (out);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/12 16:33:45 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/18 19:21:40 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
# include <unistd.h>
|
||||
# include <stdarg.h>
|
||||
|
||||
char *ft_ultoa_base(unsigned long long n, char *base);
|
||||
int ft_printf(const char *format, ...);
|
||||
char *get_next_line(int fd);
|
||||
size_t ft_random_generator(size_t start, size_t stop);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/06 14:23:00 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 16:03:54 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/18 17:32:05 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -44,12 +44,12 @@ int ft_gen_assets(t_data *data)
|
||||
data->assets[i] = ft_gen_asset(colors[i], CASE_SIZE, data);
|
||||
if (data->assets[i] == NULL)
|
||||
return (1);
|
||||
data->assets[i + NB_COLORS] = ft_gen_asset(colors[i],
|
||||
WINDOW_SIZE, data);
|
||||
if (data->assets[i + NB_COLORS] == NULL)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
data->assets[NB_COLORS] = ft_gen_asset(colors[i],
|
||||
(RENDER_DISTANCE * 2 + 1) * CASE_SIZE, data);
|
||||
if (data->assets[NB_COLORS] == NULL)
|
||||
return (1);
|
||||
ft_freer_tab_ultimate(1, colors);
|
||||
return (0);
|
||||
}
|
58
mandatory/draw.c
Normal file
58
mandatory/draw.c
Normal file
@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/12/13 15:49:23 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 15:38:37 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
void ft_draw_img(t_data *data, void *img, size_t x, size_t y)
|
||||
{
|
||||
if (img != NULL)
|
||||
mlx_put_image_to_window(data->mlx, data->window, img, x, y);
|
||||
}
|
||||
|
||||
static void *ft_char2img(t_data *data, char c)
|
||||
{
|
||||
if (c == 'C')
|
||||
return (data->assets[0]);
|
||||
if (c == '1')
|
||||
return (data->assets[1]);
|
||||
if (c == 'E')
|
||||
return (data->assets[2]);
|
||||
if (c == 'P')
|
||||
return (data->assets[3]);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int ft_draw_map(t_data *data)
|
||||
{
|
||||
ssize_t tab[2];
|
||||
char **patern;
|
||||
void *img;
|
||||
|
||||
ft_fill_pos(data->map);
|
||||
patern = ft_get_player_map(*data->map);
|
||||
if (patern == NULL)
|
||||
return (1);
|
||||
ft_draw_img(data, data->assets[NB_COLORS], 0, 0);
|
||||
tab[1] = -1;
|
||||
while (patern[++tab[1]] != NULL)
|
||||
{
|
||||
tab[0] = -1;
|
||||
while (patern[tab[1]][++tab[0]] != '\0')
|
||||
{
|
||||
img = ft_char2img(data, patern[tab[1]][tab[0]]);
|
||||
if (img != NULL)
|
||||
ft_draw_img(data, img, tab[0] * CASE_SIZE, tab[1] * CASE_SIZE);
|
||||
}
|
||||
}
|
||||
ft_freer_tab_ultimate(1, patern);
|
||||
return (0);
|
||||
}
|
63
mandatory/key.c
Normal file
63
mandatory/key.c
Normal file
@ -0,0 +1,63 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* key.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/09 18:35:27 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 15:39:43 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
static void ft_case_update(t_data *data, char *new_pos, char *player_pos)
|
||||
{
|
||||
*new_pos = 'P';
|
||||
*player_pos = '0';
|
||||
ft_printf("\r%d", ++data->nb_foots);
|
||||
}
|
||||
|
||||
static void ft_move(t_data *data, char direction)
|
||||
{
|
||||
size_t tab[2];
|
||||
char *new_pos;
|
||||
|
||||
tab[0] = data->map->player_pos[0];
|
||||
tab[1] = data->map->player_pos[1];
|
||||
if (direction == 'L')
|
||||
new_pos = &data->map->patern[tab[1]][tab[0] - 1];
|
||||
if (direction == 'U')
|
||||
new_pos = &data->map->patern[tab[1] - 1][tab[0]];
|
||||
if (direction == 'D')
|
||||
new_pos = &data->map->patern[tab[1] + 1][tab[0]];
|
||||
if (direction == 'R')
|
||||
new_pos = &data->map->patern[tab[1]][tab[0] + 1];
|
||||
if (*new_pos == 'C')
|
||||
data->map->nb_collectable--;
|
||||
if (*new_pos == '0' || *new_pos == 'C')
|
||||
ft_case_update(data, new_pos,
|
||||
&data->map->patern[tab[1]][tab[0]]);
|
||||
if (*new_pos == 'E')
|
||||
{
|
||||
if (data->map->nb_collectable == 0)
|
||||
ft_exit(data);
|
||||
}
|
||||
ft_draw_map(data);
|
||||
}
|
||||
|
||||
int ft_key(int keycode, t_data *data)
|
||||
{
|
||||
if (keycode == 65361)
|
||||
ft_move(data, 'L');
|
||||
else if (keycode == 65363)
|
||||
ft_move(data, 'R');
|
||||
else if (keycode == 65362)
|
||||
ft_move(data, 'U');
|
||||
else if (keycode == 65364)
|
||||
ft_move(data, 'D');
|
||||
else if (keycode == 65307)
|
||||
ft_exit(data);
|
||||
return (0);
|
||||
}
|
83
mandatory/main.c
Normal file
83
mandatory/main.c
Normal file
@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/12/12 17:20:17 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/18 17:31:16 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
int ft_initialised(char *path, t_data *data)
|
||||
{
|
||||
t_map *map;
|
||||
|
||||
map = ft_getmap(path);
|
||||
if (map == NULL)
|
||||
{
|
||||
ft_printf("Map error\n");
|
||||
exit(1);
|
||||
}
|
||||
data->mlx = mlx_init();
|
||||
ft_printf("Generating assets ...");
|
||||
if (ft_gen_assets(data) == 1)
|
||||
{
|
||||
ft_printf("\rGenerating assets [ERROR]");
|
||||
ft_exit(data);
|
||||
}
|
||||
ft_printf("\rGenerating assets [FINISHED]\n");
|
||||
data->map = map;
|
||||
data->window = mlx_new_window(data->mlx,
|
||||
(RENDER_DISTANCE * 2 + 1) * CASE_SIZE,
|
||||
(RENDER_DISTANCE * 2 + 1) * CASE_SIZE, "long");
|
||||
ft_draw_map(data);
|
||||
mlx_hook(data->window, 17, (0L), ft_exit, data);
|
||||
mlx_hook(data->window, 2, (1L << 0), ft_key, data);
|
||||
mlx_loop(data->mlx);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_exit(t_data *data)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < NB_COLORS + 1)
|
||||
{
|
||||
if (data->assets[i] != NULL)
|
||||
mlx_destroy_image(data->mlx, data->assets[i]);
|
||||
i++;
|
||||
}
|
||||
if (data->map != NULL && data->map->patern != NULL)
|
||||
ft_freer_tab_ultimate(1, data->map->patern);
|
||||
if (data->window != NULL && data->mlx != NULL)
|
||||
mlx_destroy_window(data->mlx, data->window);
|
||||
if (data->mlx != NULL)
|
||||
mlx_destroy_display(data->mlx);
|
||||
if (data->mlx != NULL)
|
||||
free(data->mlx);
|
||||
if (data->map != NULL)
|
||||
free(data->map);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_data data;
|
||||
|
||||
if (ac != 2 || ft_strcmp(av[1] + ft_strlen(av[1]) - 4, ".ber"))
|
||||
{
|
||||
ft_printf("Map error\n");
|
||||
return (1);
|
||||
}
|
||||
data.map = NULL;
|
||||
data.window = NULL;
|
||||
data.nb_foots = 0;
|
||||
if (ft_initialised(av[1], &data))
|
||||
ft_printf("Memory error");
|
||||
return (1);
|
||||
}
|
139
mandatory/map.c
Normal file
139
mandatory/map.c
Normal file
@ -0,0 +1,139 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* map.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 13:51:49 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 13:56:07 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
char **ft_readfile(char *path)
|
||||
{
|
||||
char **map;
|
||||
int fd;
|
||||
size_t nb_line;
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
nb_line = 1;
|
||||
map = ft_calloc(sizeof(char *), 2);
|
||||
if (map == NULL)
|
||||
return (NULL);
|
||||
map[0] = get_next_line(fd);
|
||||
while (map[nb_line - 1] != NULL)
|
||||
{
|
||||
ft_strchr(map[nb_line - 1], '\n')[0] = '\0';
|
||||
map[nb_line] = get_next_line(fd);
|
||||
map = ft_tabrealloc(map, nb_line + 1, nb_line + 2);
|
||||
if (map == NULL)
|
||||
return (NULL);
|
||||
nb_line++;
|
||||
}
|
||||
return (map);
|
||||
}
|
||||
|
||||
void ft_fill_pos(t_map *map)
|
||||
{
|
||||
size_t x;
|
||||
size_t y;
|
||||
|
||||
y = 1;
|
||||
while (map->patern[y + 1] != NULL)
|
||||
{
|
||||
x = 0;
|
||||
while (x < ft_strlen(map->patern[y]))
|
||||
{
|
||||
if (map->patern[y][x] == 'P')
|
||||
{
|
||||
map->player_pos[0] = x;
|
||||
map->player_pos[1] = y;
|
||||
}
|
||||
if (map->patern[y][x] == 'E')
|
||||
{
|
||||
map->exit_pos[0] = x;
|
||||
map->exit_pos[1] = y;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
t_map *ft_getmap(char *path)
|
||||
{
|
||||
t_map *map;
|
||||
|
||||
map = ft_calloc(sizeof(t_map), 1);
|
||||
if (map == NULL)
|
||||
return (NULL);
|
||||
map->patern = ft_readfile(path);
|
||||
if (ft_map_is_correct(map) == 0)
|
||||
{
|
||||
ft_freer_tab_ultimate(1, map->patern);
|
||||
free(map);
|
||||
return (NULL);
|
||||
}
|
||||
return (map);
|
||||
}
|
||||
|
||||
static char *ft_get_line(const char *line, ssize_t player_x)
|
||||
{
|
||||
char *out;
|
||||
ssize_t start;
|
||||
ssize_t stop;
|
||||
ssize_t i;
|
||||
ssize_t y;
|
||||
|
||||
out = ft_strgen('0', RENDER_DISTANCE * 2 + 1);
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
start = player_x - (RENDER_DISTANCE);
|
||||
if (start < 0)
|
||||
start = 0;
|
||||
stop = player_x + RENDER_DISTANCE + 1;
|
||||
if (stop > (ssize_t) ft_strlen(line))
|
||||
stop = ft_strlen(line);
|
||||
i = 0;
|
||||
y = player_x - RENDER_DISTANCE - start;
|
||||
if (y < 0)
|
||||
y = -y;
|
||||
while (start + i < stop)
|
||||
{
|
||||
out[y + i] = line[i + start];
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
char **ft_get_player_map(t_map map)
|
||||
{
|
||||
char **player_map;
|
||||
ssize_t y;
|
||||
ssize_t i;
|
||||
|
||||
player_map = ft_calloc(RENDER_DISTANCE * 2 + 2, sizeof(char *));
|
||||
if (player_map == NULL)
|
||||
return (NULL);
|
||||
i = map.player_pos[1] - RENDER_DISTANCE;
|
||||
if (i < 0)
|
||||
i = 0;
|
||||
y = -1;
|
||||
while (++y < RENDER_DISTANCE * 2 + 1)
|
||||
{
|
||||
if (map.player_pos[1] + y >= RENDER_DISTANCE && map.patern[i] != NULL)
|
||||
player_map[y] = ft_get_line(map.patern[i++], map.player_pos[0]);
|
||||
else
|
||||
player_map[y] = ft_strgen('0', RENDER_DISTANCE * 2 + 1);
|
||||
if (player_map[y] == NULL)
|
||||
{
|
||||
ft_cancel(player_map, y);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
player_map[y] = NULL;
|
||||
return (player_map);
|
||||
}
|
121
mandatory/parsing.c
Normal file
121
mandatory/parsing.c
Normal file
@ -0,0 +1,121 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 20:14:44 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 15:40:12 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
int ft_test_map_dimmention(t_map *map)
|
||||
{
|
||||
size_t i;
|
||||
size_t len;
|
||||
|
||||
len = ft_strlen(map->patern[0]);
|
||||
i = 1;
|
||||
while (map->patern[i] != NULL)
|
||||
{
|
||||
if (ft_strlen(map->patern[i]) != len)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
map->x_len = len;
|
||||
map->y_len = i;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_test_map_content1(t_map *map)
|
||||
{
|
||||
size_t x;
|
||||
size_t y;
|
||||
size_t nb_collectable;
|
||||
|
||||
nb_collectable = 0;
|
||||
y = 0;
|
||||
while (y < map->y_len)
|
||||
{
|
||||
x = 0;
|
||||
while (x < map->x_len)
|
||||
{
|
||||
if (ft_is_in("01CEP", map->patern[y][x]) == 0)
|
||||
return (0);
|
||||
if (map->patern[y][x] == 'C')
|
||||
nb_collectable++;
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
if (nb_collectable == 0)
|
||||
return (0);
|
||||
map->nb_collectable = nb_collectable;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_test_map_content2(char **patern)
|
||||
{
|
||||
size_t x;
|
||||
size_t y;
|
||||
size_t test1;
|
||||
size_t test2;
|
||||
|
||||
test1 = 0;
|
||||
test2 = 0;
|
||||
y = 0;
|
||||
while (patern[y + 1] != NULL)
|
||||
{
|
||||
x = 0;
|
||||
while (x < ft_strlen(patern[y]))
|
||||
{
|
||||
if (patern[y][x] == 'E')
|
||||
test1++;
|
||||
else if (patern[y][x] == 'P')
|
||||
test2++;
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
return (test1 == 1 && test2 == 1);
|
||||
}
|
||||
|
||||
int ft_test_map_is_surronded(t_map map)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (!ft_contain_only(map.patern[0], '1'))
|
||||
return (0);
|
||||
i = 1;
|
||||
while (map.patern[i + 1] != NULL)
|
||||
{
|
||||
if (map.patern[i][0] != '1')
|
||||
return (0);
|
||||
if (map.patern[i][ft_strlen(map.patern[i]) - 1] != '1')
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
if (!ft_contain_only(map.patern[i], '1'))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_map_is_correct(t_map *map)
|
||||
{
|
||||
if (!ft_test_map_dimmention(map))
|
||||
return (0);
|
||||
if (!ft_test_map_content1(map))
|
||||
return (0);
|
||||
if (!ft_test_map_content2(map->patern))
|
||||
return (0);
|
||||
if (!ft_test_map_dimmention(map))
|
||||
return (0);
|
||||
if (!ft_test_map_is_surronded(*map))
|
||||
return (0);
|
||||
if (!ft_test_map_is_finishable(map))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
73
mandatory/parsing2.c
Normal file
73
mandatory/parsing2.c
Normal file
@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/12 16:17:20 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 15:38:59 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
static void ft_collector(char **patern, size_t x, size_t y)
|
||||
{
|
||||
if (patern[y][x] == '1')
|
||||
return ;
|
||||
patern[y][x] = '1';
|
||||
ft_collector(patern, x, y - 1);
|
||||
ft_collector(patern, x + 1, y);
|
||||
ft_collector(patern, x, y + 1);
|
||||
ft_collector(patern, x - 1, y);
|
||||
}
|
||||
|
||||
static char **ft_patern_dup(char **patern)
|
||||
{
|
||||
char **patern2;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (patern[i] != NULL)
|
||||
i++;
|
||||
patern2 = ft_calloc(i + 1, sizeof(char *));
|
||||
if (patern2 == NULL)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (patern[i] != NULL)
|
||||
{
|
||||
patern2[i] = ft_strdup(patern[i]);
|
||||
if (patern2[i] == NULL)
|
||||
{
|
||||
ft_cancel(patern2, i);
|
||||
return (NULL);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
patern2[i] = NULL;
|
||||
return (patern2);
|
||||
}
|
||||
|
||||
int ft_test_map_is_finishable(t_map *map)
|
||||
{
|
||||
size_t i;
|
||||
int ok;
|
||||
char **patern;
|
||||
|
||||
patern = ft_patern_dup(map->patern);
|
||||
if (patern == NULL)
|
||||
return (1);
|
||||
ft_fill_pos(map);
|
||||
ft_collector(patern, map->player_pos[0], map->player_pos[1]);
|
||||
i = 1;
|
||||
ok = 1;
|
||||
while (patern[i + 1] != NULL)
|
||||
{
|
||||
if (ft_is_in(patern[i], 'C') || ft_is_in(patern[i], 'E'))
|
||||
ok = 0;
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, patern);
|
||||
return (ok);
|
||||
}
|
66
mandatory/solong.h
Normal file
66
mandatory/solong.h
Normal file
@ -0,0 +1,66 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* solong.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 16:20:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/18 17:29:43 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SOLONG_H
|
||||
# define SOLONG_H
|
||||
# include "../libftx/libftx.h"
|
||||
# define XPM_PATH "./textures/"
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include "../minilibx-linux/mlx.h"
|
||||
# define CASE_SIZE 64
|
||||
# define XPM_HEADER "\nstatic char * XFACE[] = {\""
|
||||
# define COLORS "gold|white|pink|red|gray"
|
||||
# define NB_COLORS 4
|
||||
# define RENDER_DISTANCE 5
|
||||
|
||||
typedef struct s_square
|
||||
{
|
||||
char *color;
|
||||
size_t size;
|
||||
} t_square;
|
||||
|
||||
typedef struct s_map
|
||||
{
|
||||
size_t x_len;
|
||||
size_t y_len;
|
||||
size_t nb_collectable;
|
||||
char **patern;
|
||||
size_t player_pos[2];
|
||||
size_t exit_pos[2];
|
||||
} t_map;
|
||||
|
||||
typedef struct s_data
|
||||
{
|
||||
void *mlx;
|
||||
void *window;
|
||||
void *assets[NB_COLORS + 1];
|
||||
size_t nb_foots;
|
||||
t_map *map;
|
||||
} t_data;
|
||||
|
||||
int ft_test_map_is_finishable(t_map *map);
|
||||
int ft_exit(t_data *data);
|
||||
int ft_test_map_content2(char **patern);
|
||||
int ft_key(int key, t_data *data);
|
||||
void ft_fill_pos(t_map *map);
|
||||
char *name_generator(t_square square);
|
||||
t_map *ft_getmap(char *path);
|
||||
char *ft_xpm_gen_file(t_square square);
|
||||
char **ft_readfile(char *path);
|
||||
int ft_map_is_correct(t_map *map);
|
||||
int ft_gen_assets(t_data *data);
|
||||
char **ft_get_player_map(t_map map);
|
||||
int ft_draw_map(t_data *data);
|
||||
char *name_generator(t_square square);
|
||||
void ft_draw_img(t_data *data, void *img, size_t x, size_t y);
|
||||
#endif
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 13:53:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/11 16:53:45 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/16 18:14:56 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -104,6 +104,11 @@ char *ft_xpm_gen_file(t_square square)
|
||||
{
|
||||
file_content = ft_gen_xpm_content(square);
|
||||
fd = open(path, O_WRONLY | O_CREAT, 0644);
|
||||
if (fd == -1)
|
||||
{
|
||||
free(path);
|
||||
return (NULL);
|
||||
}
|
||||
write(fd, file_content, ft_strlen(file_content));
|
||||
if (fd != -1)
|
||||
close(fd);
|
@ -1,5 +1,5 @@
|
||||
1111111111111
|
||||
10010000000C1
|
||||
1000011111001
|
||||
1000011111B01
|
||||
1P0011E0000C1
|
||||
1111111111111
|
||||
|
72
solong.h
72
solong.h
@ -1,72 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* solong.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 16:20:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 18:37:03 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SOLONG_H
|
||||
# define SOLONG_H
|
||||
# include "libftx/libftx.h"
|
||||
# define XPM_PATH "./textures/"
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include "minilibx-linux/mlx.h"
|
||||
# define CASE_SIZE 64
|
||||
# define XPM_HEADER "\nstatic char * XFACE[] = {\""
|
||||
# define COLORS "silver|gray|white|maroon|red|purple|fuchsia|green|lime|olive|yellow|navy|blue|teal|aqua|aliceblue|antiquewhite|aqua|aquamarine|azure|beige|bisque|black|blanchedalmond|blue|blueviolet|brown|burlywood|cadetblue|chartreuse|chocolate|coral|cornflowerblue|cornsilk|crimson|cyan|darkblue|darkcyan|darkgoldenrod|darkgray|darkgreen|darkgrey|darkkhaki|darkmagenta|darkolivegreen|darkorange|darkorchid|darkred|darksalmon|darkseagreen|darkslateblue|darkslategray|darkslategrey|darkturquoise|darkviolet|deeppink|deepskyblue|dimgray|dimgrey|dodgerblue|firebrick|floralwhite|forestgreen|fuchsia|gainsboro|ghostwhite|gold|goldenrod|gray|green|greenyellow|grey|honeydew|hotpink|indianred|indigo|ivory|khaki|lavender|lavenderblush|lawngreen|lemonchiffon|lightblue|lightcoral|lightcyan|lightgoldenrodyellow|lightgray|lightgreen|lightgrey|lightpink|lightsalmon|lightseagreen|lightskyblue|lightslategray|lightslategrey|lightsteelblue|lightyellow|lime|limegreen|linen|magenta|maroon|mediumaquamarine|mediumblue|mediumorchid|mediumpurple|mediumseagreen|mediumslateblue|mediumspringgreen|mediumturquoise|mediumvioletred|midnightblue|mintcream|mistyrose|moccasin|navajowhite|navy|oldlace|olive|olivedrab|orange|orangered|orchid|palegoldenrod|palegreen|paleturquoise|palevioletred|papayawhip|peachpuff|peru|pink|plum|powderblue|purple|red|rosybrown|royalblue|saddlebrown|salmon|sandybrown|seagreen|seashell|sienna|silver|skyblue|slateblue|slategray|slategrey|snow|springgreen|steelblue|tan|teal|thistle|tomato|turquoise|violet|wheat|white|whitesmoke|yellow|yellowgreen"
|
||||
# define NB_COLORS 99
|
||||
# define RENDER_DISTANCE 5
|
||||
# define WINDOW_SIZE (RENDER_DISTANCE * 2 + 1) * CASE_SIZE
|
||||
# define ABS(Value) (Value * ((Value > 0) - (Value < 0)))
|
||||
# define BONUS 0
|
||||
|
||||
typedef struct s_square
|
||||
{
|
||||
char *color;
|
||||
size_t size;
|
||||
} t_square;
|
||||
|
||||
typedef struct s_map
|
||||
{
|
||||
size_t x_len;
|
||||
size_t y_len;
|
||||
size_t nb_collectable;
|
||||
char **patern;
|
||||
size_t player_pos[2];
|
||||
size_t exit_pos[2];
|
||||
} t_map;
|
||||
|
||||
typedef struct s_data
|
||||
{
|
||||
void *mlx;
|
||||
void *window;
|
||||
void *assets[NB_COLORS * 2];
|
||||
t_map *map;
|
||||
size_t nb_swaps;
|
||||
size_t nb_foots;
|
||||
} t_data;
|
||||
|
||||
void ft_ultimate_image_destroyer(t_data *data);
|
||||
int ft_test_map_is_finishable(t_map *map);
|
||||
int ft_exit(t_data *data);
|
||||
int ft_test_map_content2(char **patern);
|
||||
int ft_key(int key, t_data *data);
|
||||
void ft_fill_pos(t_map *map);
|
||||
char *name_generator(t_square square);
|
||||
t_map *ft_getmap(char *path);
|
||||
char *ft_xpm_gen_file(t_square square);
|
||||
char **ft_readfile(char *path);
|
||||
int ft_map_is_correct(t_map *map);
|
||||
int ft_gen_assets(t_data *data);
|
||||
char **ft_get_player_map(t_map map);
|
||||
int ft_draw_map(t_data *data);
|
||||
char *name_generator(t_square square);
|
||||
int ft_color_changer(t_data *data);
|
||||
void ft_draw_img(t_data *data, void *img, size_t x, size_t y);
|
||||
#endif
|
BIN
subject.pdf
Normal file
BIN
subject.pdf
Normal file
Binary file not shown.
0
textures/.gitkeep
Normal file
0
textures/.gitkeep
Normal file
Reference in New Issue
Block a user