Compare commits

...

30 Commits

Author SHA1 Message Date
42a0cfd5b6 Update 'README.md' 2023-03-27 13:43:00 +00:00
595d220abd Update 'README.md' 2023-03-27 13:42:34 +00:00
9a6d7b6ed7 add: subject 2023-03-24 16:06:34 +01:00
b1170d5b5f Add 'README.md' 2023-03-24 15:03:08 +00:00
d4f3a72f76 fix 2023-01-27 13:58:11 +01:00
a241d049c4 norm 2023-01-20 14:41:52 +01:00
d6117cfb8f c'est tout bon 2023-01-19 17:38:47 +01:00
8b200fb8ba kekw 2023-01-19 13:41:58 +01:00
7d7de10b7a fix + clean 2023-01-16 19:02:09 +01:00
1efd8e1a7b fix 2023-01-16 19:01:46 +01:00
b66f8d6f5a clean 2023-01-16 18:43:53 +01:00
8a0b32efda add textures folder 2023-01-16 18:43:36 +01:00
91c5ef0860 clear texture folder 2023-01-16 18:41:03 +01:00
750d449f6c texture destroyer in makefile add 2023-01-16 18:39:55 +01:00
7addabc200 bonus and mandatory separation 2023-01-16 18:38:47 +01:00
c8f6b30e1b foot display fix 2023-01-16 12:53:52 +01:00
1331231004 kekw 2023-01-13 18:59:48 +01:00
6312ed4cdc d 2023-01-13 18:44:09 +01:00
7c7b419c1b remove img 2023-01-13 18:38:55 +01:00
66f7fb04b2 deff bonus pas bonus 2023-01-13 18:37:30 +01:00
2a8ffb82a2 normed 2023-01-13 16:28:54 +01:00
f84f98fe21 CA MARCHE 2023-01-13 16:10:11 +01:00
4ab400b5e4 Hello World 2023-01-13 15:14:58 +01:00
9e4e1b8e54 kekw 2023-01-13 15:14:11 +01:00
1088942133 kekw 2023-01-13 15:10:27 +01:00
eb54b7bc0f kekw 2023-01-11 19:10:28 +01:00
5deb78a5cf kekw 2023-01-10 19:04:46 +01:00
195f7bc66a ca marche 2023-01-09 20:41:06 +01:00
b5315e3a0a clean 2023-01-08 18:08:41 +01:00
650d9b1ab5 ca marche presques mais ça marche pas du tout 2023-01-08 18:03:40 +01:00
116 changed files with 1743 additions and 379 deletions

View File

@ -1,6 +1,22 @@
SRCS = asset.c draw.c main.c map.c parsing.c shape.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 #
# #
# **************************************************************************** #
OBJ = ${SRCS:.c=.o}
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
@ -8,20 +24,26 @@ 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}
${NAME}: ${OBJ}
bonus: ${BOBJS}
make -C libftx
make -C minilibx-linux
${CC} ${OBJ} -o ${NAME} ${LIBS} -lXext -lX11
${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 OBJ
rm -f ${OBJS} ${BOBJS}
rm -f textures/*
make -C libftx clean
make -C minilibx-linux clean
@ -30,6 +52,7 @@ fclean: clean
make -C libftx fclean
make -C minilibx-linux clean
re: fclean all
re: fclean
make all
.PHONY: all clean fclean re
.PHONY: all clean fclean re bonus

53
README.md Normal file
View 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:
![](https://media.discordapp.net/attachments/501841539161522176/1089906526220734556/image.png?width=614&height=612)
## Resources
![Minilibx](https://github.com/42Paris/minilibx-linux)
![Subject](./subject.pdf)
## Author
This project was created by Camille Chauvet. If you have any questions or suggestions, feel free to contact me.

BIN
asset.o

Binary file not shown.

83
bonus/asset.c Normal file
View 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);
}

79
bonus/draw.c Normal file
View File

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 15:49:23 by cchauvet #+# #+# */
/* Updated: 2023/01/19 15:20:56 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[data->nb_swaps + 1 % NB_COLORS]);
if (c == '1')
return (data->assets[data->nb_swaps + 2 % NB_COLORS]);
if (c == 'E')
return (data->assets[data->nb_swaps + 3 % NB_COLORS]);
if (c == 'P')
return (data->assets[data->nb_swaps + 4 % NB_COLORS]);
if (c == 'B')
return (data->assets[data->nb_swaps + 5 % NB_COLORS]);
return (NULL);
}
static void ft_draw_foots(t_data *data)
{
char *foots;
data->nb_foots++;
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);
}
int ft_draw_map(t_data *data)
{
ssize_t tab[2];
char **patern;
void *img;
ft_fill_pos(data->map);
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 + 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);
ft_draw_foots(data);
return (0);
}

77
bonus/key.c Normal file
View File

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* key.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/09 18:35:27 by cchauvet #+# #+# */
/* Updated: 2023/01/16 12:52:02 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
static void ft_case_update(char *new_pos, char *player_pos)
{
*new_pos = 'P';
*player_pos = '0';
}
static void ft_epileptic_mod(t_data *data)
{
size_t i;
i = 0;
while (i < NB_COLORS * 100)
{
ft_draw_img(data, data->assets[(i % NB_COLORS) + NB_COLORS], 0, 0);
write(1, "\a\a\a\a\a", 5);
i++;
}
ft_exit(data);
}
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(new_pos, &data->map->patern[tab[1]][tab[0]]);
if (*new_pos == 'E')
{
if (data->map->nb_collectable == 0)
ft_exit(data);
}
if (*new_pos == 'B')
ft_epileptic_mod(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);
}

81
bonus/main.c Normal file
View File

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/12 17:20:17 by cchauvet #+# #+# */
/* Updated: 2023/01/19 16:54:42 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))
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");
data->nb_foots = 0;
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);
mlx_loop(data->mlx);
return (0);
}
int ft_exit(t_data *data)
{
size_t i;
i = 0;
while (i < NB_COLORS * 2)
{
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;
if (ft_initialised(av[1], &data))
ft_printf("Memory error");
return (1);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 13:51:49 by cchauvet #+# #+# */
/* Updated: 2023/01/06 19:36:36 by cchauvet ### ########.fr */
/* Updated: 2023/01/13 13:56:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,7 @@ char **ft_readfile(char *path)
fd = open(path, O_RDONLY);
nb_line = 1;
map = malloc(sizeof(char *) * 2);
map = ft_calloc(sizeof(char *), 2);
if (map == NULL)
return (NULL);
map[0] = get_next_line(fd);
@ -36,42 +36,104 @@ char **ft_readfile(char *path)
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 = malloc(sizeof(t_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 = malloc(RENDER_DISTANCE * 2 + 2);
player_map = ft_calloc(RENDER_DISTANCE * 2 + 2, sizeof(char *));
if (player_map == NULL)
return (NULL);
y = -RENDER_DISTANCE;
while (y < map.y_len - map.player_pos[1] - RENDER_DISTANCE)
i = map.player_pos[1] - RENDER_DISTANCE;
if (i < 0)
i = 0;
y = -1;
while (++y < RENDER_DISTANCE * 2 + 1)
{
i = map.player_pos[1] + y;
player_map[y] = ft_strndup(map.patern[i], 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, RENDER_DISTANCE - y);
ft_cancel(player_map, y);
return (NULL);
}
y++;
}
player_map[y] = NULL;
return (player_map);
}

121
bonus/parsing.c Normal file
View 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 18:48:43 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("01CEPB", 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
bonus/parsing2.c Normal file
View 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 18:48:20 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
static void ft_collector(char **patern, size_t x, size_t y)
{
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);
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);
}

67
bonus/solong.h Normal file
View 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
View 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);
}

BIN
draw.o

Binary file not shown.

View File

@ -6,13 +6,13 @@
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2023/01/05 20:32:04 by cchauvet ### ########.fr #
# Updated: 2023/01/19 12:55:43 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
CC = clang
SRCS = ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_is_in.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)

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:03:10 by cchauvet #+# #+# */
/* Updated: 2023/01/05 20:31:50 by cchauvet ### ########.fr */
/* Updated: 2023/01/19 17:37:25 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,13 +14,23 @@
# define EXTRA_H
# include <stdarg.h>
# include <stdlib.h>
# include <unistd.h>
# 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, ...);
void ft_freer_ultimate(size_t len, ...);
char *ft_strgen(char c, size_t len);
char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...);
int ft_is_in(char *str, char c);
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
char *ft_strndup(char *src, size_t n);
ssize_t ft_strchri(char *str, char c);
int ft_contain_only_str(char *str, char *to_find);
int ft_contain_only(char *str, char c);
int ft_strcmp(char *s1, char *s2);
#endif

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_contain_only.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/12 16:28:20 by cchauvet #+# #+# */
/* Updated: 2023/01/12 16:31:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
int ft_contain_only(char *str, char c)
{
size_t i;
i = 0;
while (str[i] != '\0')
{
if (str[i] != c)
return (0);
i++;
}
return (1);
}
int ft_contain_only_str(char *str, char *to_find)
{
size_t i;
i = 0;
while (str[i] != '\0')
{
if (!ft_is_in(to_find, str[i]))
return (0);
i++;
}
return (1);
}

58
libftx/extra/ft_freer.c Normal file
View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_freer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/08 15:01:40 by cchauvet #+# #+# */
/* Updated: 2023/01/09 17:57:52 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
void ft_freer_ultimate(size_t len, ...)
{
va_list va;
size_t i;
i = 0;
va_start(va, len);
while (i < len)
{
free(va_arg(va, char *));
i++;
}
va_end(va);
}
void ft_freer_tab(char **tab)
{
size_t i;
if (tab == NULL)
return ;
i = 0;
while (tab[i] != NULL)
{
free(tab[i]);
i++;
}
free(tab);
}
void ft_freer_tab_ultimate(size_t len, ...)
{
va_list va;
size_t i;
i = 0;
va_start(va, len);
while (i < len)
{
ft_freer_tab(va_arg(va, char **));
i++;
}
va_end(va);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 20:29:53 by cchauvet #+# #+# */
/* Updated: 2023/01/05 20:31:20 by cchauvet ### ########.fr */
/* Updated: 2023/01/08 12:15:08 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,7 @@ int ft_is_in(char *str, char c)
{
if (str[i] == c)
return (1);
i++;
}
return (0);
}

Binary file not shown.

View File

@ -1,47 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* ft_random_generator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 15:49:23 by cchauvet #+# #+# */
/* Updated: 2023/01/06 18:49:39 by cchauvet ### ########.fr */
/* Created: 2023/01/09 18:43:27 by cchauvet #+# #+# */
/* Updated: 2023/01/09 20:09:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
#include "extra.h"
void ft_draw_xpm(void *mlx, void *window, char *img_path, size_t *cord)
size_t ft_random_generator(size_t start, size_t stop)
{
int bozo;
void *img;
int fd;
char *line;
int sum;
size_t i;
img = mlx_xpm_file_to_image(mlx, img_path, &bozo, &bozo);
mlx_put_image_to_window(mlx, window, img, cord[0], cord[1]);
mlx_destroy_image(mlx, img);
}
int ft_draw_map(void *mlx, void *window, t_map map)
{
size_t x;
size_t y;
map = ft_getmap(path);
if (map == NULL)
fd = open("/dev/random", O_RDONLY);
if (fd == -1)
return (0);
if (ft_map_is_correct(map))
line = ft_calloc(sizeof(char), 10001);
if (line == NULL)
return (0);
read(fd, line, 10000);
sum = 0;
i = 0;
while (i < ft_strlen(line))
{
free(map);
return (0);
sum += (unsigned int) line[i];
i++;
}
y = 0;
while (y < 0)
{
x = 0;
while (x < 0)
x++;
return (0);
}
return (1);
free(line);
return (sum % (stop - start));
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 19:22:58 by cchauvet #+# #+# */
/* Updated: 2023/01/05 19:23:13 by cchauvet ### ########.fr */
/* Updated: 2023/01/10 18:30:35 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,4 +25,3 @@ ssize_t ft_strchri(char *str, char c)
return (-1);
return (i);
}

Binary file not shown.

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:06:04 by cchauvet #+# #+# */
/* Updated: 2023/01/05 19:38:35 by cchauvet ### ########.fr */
/* Updated: 2023/01/10 18:29:59 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,21 +18,24 @@ char *ft_strfjoin(char *s1, char *s2)
ssize_t y;
char *out;
out = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
if (s1 != NULL)
while (s1[i] != '\0')
{
{
i = -1;
while (s1[++i] != '\0')
out[i] = s1[i];
i++;
}
}
free(s1);
y = -1;
y = 0;
if (s2 != NULL)
{
y = -1;
while (s2[++y] != '\0')
out[i + y] = s2[y];
}
free(s2);
out[i + y] = '\0';
return (out);

Binary file not shown.

31
libftx/extra/ft_strgen.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strgen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/08 12:32:52 by cchauvet #+# #+# */
/* Updated: 2023/01/09 18:04:31 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
char *ft_strgen(char c, size_t len)
{
char *out;
size_t i;
out = ft_calloc((len + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (i < len)
{
out[i] = c;
i++;
}
out[len] = '\0';
return (out);
}

Binary file not shown.

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 18:58:48 by cchauvet #+# #+# */
/* Updated: 2023/01/05 18:58:59 by cchauvet ### ########.fr */
/* Updated: 2023/01/10 18:30:21 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,7 @@ char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size)
char **new;
size_t i;
new = malloc(new_size * sizeof(char *));
new = ft_calloc(new_size, sizeof(char *));
if (new == NULL)
return (NULL);
i = 0;
@ -29,5 +29,3 @@ char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size)
free(tab);
return (new);
}

Binary file not shown.

BIN
libftx/extra/ft_ultoa.o Normal file

Binary file not shown.

View 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);
}

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
/* Updated: 2023/01/05 19:27:45 by cchauvet ### ########.fr */
/* Updated: 2023/01/10 18:26:56 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,5 +19,7 @@
# endif
# include "../libft/libft.h"
# include "../extra/extra.h"
char *get_next_line(int fd);
#endif

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.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2022/09/29 18:07:15 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 13:51:09 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */

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.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2023/01/06 19:34:38 by cchauvet ### ########.fr */
/* Updated: 2023/01/18 19:21:40 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,13 +16,19 @@
# 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);
void ft_freer_tab_ultimate(size_t len, ...);
void ft_freer_ultimate(size_t len, ...);
char *ft_strgen(char c, size_t len);
int ft_is_in(char *str, char c);
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...);
int ft_contain_only_str(char *str, char *to_find);
int ft_contain_only(char *str, char c);
char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...);
int ft_strcmp(char *s1, char *s2);
ssize_t ft_strchri(char *str, char c);
char *ft_strndup(char *src, size_t n);

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.

50
main.c
View File

@ -1,50 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/12 17:20:17 by cchauvet #+# #+# */
/* Updated: 2023/01/06 19:32:14 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
/*
int maiin(int ac, char **av)
{
char **map;
int i;
if (ac)
{
map = ft_readfile(av[1]);
ft_printf("%S", map);
i = 0;
while (map[i] != NULL)
{
free(map[i]);
i++;
}
free(map);
}
return (0);
}
*/
int main(void)
{
t_data data;
ft_printf("Generating assets ...");
ft_gen_assets();
ft_printf("Generating assets [FINISHED]");
data.mlx = mlx_init();
data.window = mlx_new_window(data.mlx, WINDOW_SIZE, WINDOW_SIZE, "");
// mlx_loop(window_manager);
ft_printf("%S", ft_get_player_map());
mlx_destroy_window(data.mlx, data.window);
mlx_destroy_display(data.mlx);
free(data.mlx);
return (1);
}

BIN
main.o

Binary file not shown.

View File

@ -6,69 +6,50 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/06 14:23:00 by cchauvet #+# #+# */
/* Updated: 2023/01/06 18:20:19 by cchauvet ### ########.fr */
/* Updated: 2023/01/18 17:32:05 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
static void ft_freer(char **tab)
static void *ft_gen_asset(char *color, size_t case_size, t_data *data)
{
size_t i;
t_square square;
char *name;
int dimmension;
void *img;
i = 0;
if (tab == NULL)
return ;
while (tab[i] != NULL)
{
free(tab[i]);
i++;
}
free(tab);
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 void ft_freer_utimate(size_t len, ...)
{
va_list va;
size_t i;
i = 0;
va_start(va, len);
while (i < len)
{
ft_freer(va_arg(va, char **));
i++;
}
va_end(va);
}
int ft_gen_assets(void)
int ft_gen_assets(t_data *data)
{
char **colors;
char **shapes;
size_t tab[2];
t_shape shape;
size_t i;
colors = ft_split(COLORS, '|');
shapes = ft_split(SHAPES, '|');
if (shapes == NULL && colors == NULL)
{
ft_freer_utimate(2, shapes, colors);
if (colors == NULL)
return (1);
}
tab[0] = -1;
while (colors[++tab[0]] != NULL)
i = 0;
while (i < NB_COLORS)
{
tab[1] = -1;
while (shapes[++tab[1]] != NULL)
{
shape.color = colors[tab[0]];
shape.bcolor = BACKGROUND_COLOR;
shape.shape = shapes[tab[1]];
shape.size = CASE_SIZE;
free(ft_xpm_gen_file(shape));
}
data->assets[i] = ft_gen_asset(colors[i], CASE_SIZE, data);
if (data->assets[i] == NULL)
return (1);
i++;
}
ft_freer_utimate(2, shapes, colors);
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
View 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
View 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
View 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
View 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);
}

Some files were not shown because too many files have changed in this diff Show More