diff --git a/collectible_utils.c b/collectible_utils.c index 1eb9373..dd3c2e9 100644 --- a/collectible_utils.c +++ b/collectible_utils.c @@ -6,106 +6,106 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 03:24:55 by jbadaire #+# #+# */ -/* Updated: 2023/10/05 16:31:17 by jbadaire ### ########.fr */ +/* Updated: 2023/10/10 19:35:28 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -t_list *load_collectibles(t_world world) { +t_collectible *load_collectibles(t_world *world) { int pos_x; int pos_y; - t_list **list; - t_collectible collectible; + t_collectible *collectible; pos_x = 0; pos_y = 0; - list = malloc(sizeof (t_list *)); - if(!list) + collectible = malloc(sizeof (t_collectible)); + if(!collectible) return (NULL); - while (world.map[pos_y]) { - while (world.map[pos_y] && pos_x < (int) ft_strlen(world.map[0])) { - if (world.map[pos_y][pos_x] == 'C') { + while (world->map[pos_y]) { + while (world->map[pos_y] && pos_x < (int) ft_strlen(world->map[0])) { + if (world->map[pos_y][pos_x] == 'C') { t_location location; location.x = ++pos_x; location.y = ++pos_y; - collectible = create_collectible(location); - ft_lstadd_front(list, ft_lstnew(&collectible)); + if(!collectible->location) + collectible = create_collectible(location); + else + collectible->next = create_collectible(location); } pos_x++; } pos_x = 0; pos_y++; } - return (*list); + return (collectible); } -t_collectible create_collectible(t_location location) +t_collectible *create_collectible(t_location location) { - t_collectible collectible; + t_collectible *collectible; + + collectible = malloc(sizeof (t_collectible)); + if(!collectible) + return (NULL); - collectible.location = location; - collectible.collected = 0; + collectible->location = create_location(location.x, location.y); + collectible->collected = 0; + collectible->next = NULL; return (collectible); } -int count_collectibles(t_list *collectibles, t_boolean o_uncollected, t_boolean o_collected) +int count_collectibles(t_collectible *collectibles, t_boolean o_uncollected, t_boolean o_collected) { - t_collectible *col; + t_collectible *copy; int count; count = 0; - col = NULL; + copy = collectibles; - if (!collectibles) + if (!copy) return (0); - while (collectibles) + while (copy) { - col = ((t_collectible*) collectibles->content); - if (col == NULL) - continue; - if(o_uncollected && !col->collected) + if(o_uncollected && !copy->collected) count++; - if(o_collected && col->collected) + if(o_collected && copy->collected) count++; - collectibles = collectibles->next; + copy = copy->next; } return (count); } -t_collectible *get_collectible_at(t_world world, t_location location) +t_collectible *get_collectible_at(t_world *world, t_location *location) { - t_list *collectibles = world.player->collectibles; - t_collectible *collectible; - - while (collectibles) + t_collectible *copy; + copy = world->player->collectibles; + while (copy) { - collectible = ((t_collectible*) collectibles->content); - if(loc_equals(location, collectible->location)) - return collectible; - collectibles = collectibles->next; + if(loc_equals(location, copy->location)) + return copy; + copy = copy->next; } return (NULL); } -void update_collectible(t_list **collectibles, t_location location, t_boolean collected) + +void update_collectible(t_collectible *collectibles, t_location location, t_boolean collected) { - t_list *collectibles_copy; - t_collectible *collectible; + t_collectible *copy; - collectibles_copy = *collectibles; - while (collectibles_copy->next) + copy = collectibles; + while (copy->next) { - collectible = ((t_collectible*) collectibles_copy->content); - if(collectible->location.x == location.x && collectible->location.y == location.y) { - collectible->collected = collected; + if(copy->location->x == location.x && copy->location->y == location.y) { + copy->collected = collected; break; } - collectibles_copy = collectibles_copy->next; + copy = copy->next; } } \ No newline at end of file diff --git a/frees.c b/frees.c index cc78c22..c67f2c7 100644 --- a/frees.c +++ b/frees.c @@ -6,21 +6,21 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 10:21:10 by jbadaire #+# #+# */ -/* Updated: 2023/10/05 15:06:29 by jbadaire ### ########.fr */ +/* Updated: 2023/10/10 14:58:36 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -void free_map(t_world world) { +void free_map(t_world *world) { int length_y; length_y = 0; - while (world.length_y < length_y) { - free(world.map[length_y]); + while (world->length_y < length_y) { + free(world->map[length_y]); length_y++; } - free(world.map); + free(world->map); } void free_collectibles(t_list **collectibles) { @@ -41,4 +41,30 @@ void free_collectibles(t_list **collectibles) { } free(list->content); free(list); +} + +void free_textures(t_textures *textures) +{ + if(textures == NULL) return; + if(textures->wall != NULL) + { + free(textures->wall->texture); + free(textures->wall); + } + if(textures->player != NULL) + { + free(textures->player->texture); + free(textures->player); + } + if(textures->collectible != NULL) + { + free(textures->collectible->texture); + free(textures->collectible); + } + if(textures->grass != NULL) + { + free(textures->grass->texture); + free(textures->grass); + } + free(textures); } \ No newline at end of file diff --git a/graphics_utils.c b/graphics_utils.c index ed2a07e..9e775fa 100644 --- a/graphics_utils.c +++ b/graphics_utils.c @@ -6,43 +6,52 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 12:56:12 by jbadaire #+# #+# */ -/* Updated: 2023/10/05 19:26:44 by jbadaire ### ########.fr */ +/* Updated: 2023/10/10 15:20:11 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" #include "minilibx-linux/mlx.h" -t_texture *load_textures(void *mlx) +t_textures *load_textures(void *mlx) { - t_texture *list; + t_textures *textures; - list = malloc(sizeof (t_texture *) * 5); - if(!list) + textures = malloc(sizeof (t_textures)); + if(!textures) return (NULL); - ft_lstadd_texture(&list, ft_lstnew(load_texture("./textures/wall.xpm", mlx))); - ft_lstadd_texture(&list, ft_lstnew(load_texture("./textures/grass.xpm", mlx))); - ft_lstadd_texture(&list, ft_lstnew(load_texture("./textures/player.xpm", mlx))); - ft_lstadd_texture(&list, ft_lstnew(load_texture("./textures/collectible.xpm", mlx))); - ft_lstadd_texture(&list, ft_lstnew(load_texture("./textures/exit.xpm", mlx))); + textures->wall = load_texture(mlx, '1', "./textures/wall.xpm"); + textures->grass = load_texture(mlx, '0', "./textures/grass.xpm"); + textures->player = load_texture(mlx, 'P', "./textures/player.xpm"); + textures->collectible = load_texture(mlx, 'C', "./textures/collectible.xpm"); + textures->exit = load_texture(mlx, 'E', "./textures/exit.xpm"); - return (list); + if (textures->wall == NULL || textures->grass == NULL || textures->exit == NULL) + return (NULL); + if (textures->collectible == NULL || textures->player == NULL) + return (NULL); + + return (textures); } -void *load_texture(char *path, void *mlx) { +t_texture *load_texture(void *mlx, char character, char *path) +{ + int img_width; + int img_height; + t_texture *texture; - void *img; - char *relative_path = path; - int img_width; - int img_height; + texture = malloc(sizeof(t_texture)); + if(!texture) + return (NULL); - img = mlx_xpm_file_to_image(mlx, relative_path, &img_width, &img_height); - return (img); + texture->character = character; + texture->texture = mlx_xpm_file_to_image(mlx, path, &img_width, &img_height); + return (texture); } -void draw_type(void *mlx, void *mlx_window, void *texture, t_world world, char c) +void draw_type(void *mlx, void *mlx_window, t_world *world, t_texture texture) { int index_y; int index_x; @@ -50,10 +59,10 @@ void draw_type(void *mlx, void *mlx_window, void *texture, t_world world, char c index_y = 0; index_x = 0; - while (world.map[index_y]) { - while (index_x < (int) ft_strlen(world.map[index_y])) { - if (world.map[index_y][index_x] == c) { - mlx_put_image_to_window(mlx, mlx_window, texture, index_x * 128, index_y * 128); + while (world->map[index_y]) { + while (index_x < (int) ft_strlen(world->map[index_y])) { + if (world->map[index_y][index_x] == texture.character) { + mlx_put_image_to_window(mlx, mlx_window, texture.texture, index_x * 128, index_y * 128); } index_x++; } @@ -62,26 +71,3 @@ void draw_type(void *mlx, void *mlx_window, void *texture, t_world world, char c } } -void destroy_texture() { - -} - - -void ft_lstadd_texture(t_list **lst, t_list *new) -{ - new->next = *lst; - *lst = new; -} - -t_texture_list *ft_newtexture(char *name, void *content) -{ - t_texture_list *list; - - list = malloc(sizeof (t_list)); - if (!list) - return (0); - list->name = name; - list->texture = content; - list->next = NULL; - return (list); -} diff --git a/libft/ft_lstlast.c b/libft/ft_lstlast.c index 22dfbc4..212be63 100644 --- a/libft/ft_lstlast.c +++ b/libft/ft_lstlast.c @@ -10,18 +10,6 @@ /* */ /* ************************************************************************** */ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstlast.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jbadaire +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/01/17 17:18:29 by jbadaire #+# #+# */ -/* Updated: 2023/01/17 17:18:42 by jbadaire ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - #include "libft.h" t_list *ft_lstlast(t_list *lst) diff --git a/location_utils.c b/location_utils.c index 52e42cf..1009021 100644 --- a/location_utils.c +++ b/location_utils.c @@ -6,38 +6,26 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/05 15:30:50 by jbadaire #+# #+# */ -/* Updated: 2023/10/05 15:36:04 by jbadaire ### ########.fr */ +/* Updated: 2023/10/10 17:06:20 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -t_location create_location(int x, int y) +t_location *create_location(int x, int y) { - t_location location; + t_location *location; - location.x = x; - location.y = y; - return (location); -} - -t_location clone_location(t_location location) -{ - t_location clone; + location = malloc(sizeof (t_location)); + if(!location) + return (NULL); - clone.x = location.x; - clone.y = location.y; - return (clone); -} - -t_location edit_location(t_location location, int x, int y) -{ - location.x += x; - location.y += y; + location->x = x; + location->y = y; return (location); } -t_boolean loc_equals(t_location loc_1, t_location loc_2) +t_boolean loc_equals(t_location *loc_1, t_location *loc_2) { - return loc_1.x == loc_2.x && loc_1.y == loc_2.y; + return loc_1->x == loc_2->x && loc_1->y == loc_2->y; } \ No newline at end of file diff --git a/main.c b/main.c index 88b3a68..c8ae1df 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/02 16:19:55 by jbadaire #+# #+# */ -/* Updated: 2023/10/05 17:08:40 by jbadaire ### ########.fr */ +/* Updated: 2023/10/10 19:06:42 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,26 +17,23 @@ int main(int argc, char *argv[]) { - (void) argc; - int fd; - if(argc < 2) - { - ft_putstr_fd( "Veuillez inclure le chemin vers une map dans votre commande.\n", 1); - return (-1); - } - fd = open(argv[1], O_RDONLY); - if (fd == -1) - { - ft_putstr_fd("Veuillez inclure le chemin vers une map dans votre commande.\n", 1); - return (-1); - } - t_world world = load_map(fd, argv[1]); + int value; + t_world *world; + + world = malloc(sizeof(t_world)); + if(!world) + return (0); + + value = handle_launch_error(argc, argv, world); + if(value == -1) + return (0); - t_location start_location = find_element(world, 'P'); - t_list *collectibles_list = load_collectibles(world); + t_location p_pos = find_element(world, 'P'); + t_location *start_location = create_location(p_pos.x, p_pos.y); + t_collectible *collectibles_list = load_collectibles(world); - t_player player = init_player(start_location, collectibles_list); - world.player = &player; + t_player *player = init_player(start_location, collectibles_list); + world->player = player; if(!is_valid_map(world)) { @@ -53,29 +50,55 @@ int main(int argc, char *argv[]) { } - void *window = mlx_new_window(mlx, 2500, 2000, "Hello world!"); + t_textures *textures; + textures = load_textures(mlx); - void *wall_texture = load_texture("./textures/wall.xpm", mlx); - void *grass_texture = load_texture("./textures/grass.xpm", mlx); - void *exit_texture = load_texture("./textures/exit.xpm", mlx); - void *player_texture = load_texture("./textures/player.xpm", mlx); - void *collectible_texture = load_texture("./textures/collectible.xpm", mlx); + if(textures == NULL) + return (0); - draw_type(mlx, window, wall_texture, world, '1'); - draw_type(mlx, window, grass_texture, world, '0'); - draw_type(mlx, window, exit_texture, world, 'E'); - draw_type(mlx, window, player_texture, world, 'P'); - draw_type(mlx, window, collectible_texture, world, 'C'); + t_game *game; - mlx_hook(window, 2, (1L<<0), &on_player_move, &world); - mlx_loop(mlx); - mlx_destroy_display(mlx); - mlx_clear_window(mlx, window); - mlx_destroy_window(mlx, window); - free(mlx); + game = malloc(sizeof (t_game)); + if(!game) + return (0); - free_map(world); + game->mlx = mlx; + game->world = world; + game->textures = textures; + printf("\nPOS Y %d\n", game->world->player->location->y); + printf("\nPOS X %d\n", game->world->player->location->x); + void *window = mlx_new_window(mlx, 128 * ft_strlen(world->map[0]), 128 * world->length_y, "Xe'Burger"); + game->window = window; + + draw_type(mlx, window, world, *textures->wall); + draw_type(mlx, window, world, *textures->grass); + draw_type(mlx, window, world, *textures->player); + draw_type(mlx, window, world, *textures->collectible); + draw_type(mlx, window, world, *textures->exit); + + + mlx_hook(window, 2, (1L<<0), on_player_move, game); + mlx_loop(mlx); return 0; } + + +int handle_launch_error(int argc, char *argv[], t_world *world) +{ + int fd; + if(argc < 2) + { + ft_putstr_fd( "You must include the abstract path to your map.\n", 1); + return (-1); + } + fd = open(argv[1], O_RDONLY); + if (fd == -1) + { + ft_putstr_fd("You must include the abstract path to your map.\n", 1); + return (-1); + } + load_map(fd, argv[1], world); + return (fd); +} diff --git a/maps_utils.c b/maps_utils.c index 2336a03..bc54ab1 100644 --- a/maps_utils.c +++ b/maps_utils.c @@ -6,40 +6,39 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/02 18:09:47 by jbadaire #+# #+# */ -/* Updated: 2023/10/05 15:22:01 by jbadaire ### ########.fr */ +/* Updated: 2023/10/10 17:23:02 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" #include -t_world load_map(int fd, char *path) +t_world *load_map(int fd, char *path, t_world *world) { int index; int y; char *s; - t_world world; index = 0; y = 0; while (get_next_line(fd) != NULL) index++; - world.map = malloc((index + 1) * sizeof(char *)); - if (!world.map) + world->map = malloc((index + 1) * sizeof(char *)); + if (!world->map) return (world); - world.map[index] = NULL; + world->map[index] = NULL; close(fd); fd = open(path, O_RDONLY); while (y <= index) { s = get_next_line(fd); if(s != NULL) - world.map[y] = ft_strtrim(s, "\n"); + world->map[y] = ft_strtrim(s, "\n"); y++; } - return (world.length_y = index, world); + return (world->length_y = index, world); } -t_boolean is_valid_map(t_world world) +t_boolean is_valid_map(t_world *world) { t_location start_loc; t_location exit_loc; @@ -51,18 +50,18 @@ t_boolean is_valid_map(t_world world) return (start_loc.x != -1 && exit_loc.x != -1); } -void is_solvable(t_world world, int x, int y) +void is_solvable(t_world *world, int x, int y) { char character; - if(world.map[y] == NULL || y > world.length_y) + if(world->map[y] == NULL || y > world->length_y) return; - character = world.map[y][x]; + character = world->map[y][x]; if (character == 'O') return ; if (character == '0' || character == 'C' || character == 'P' || character == 'E') { - world.map[y][x] = 'O'; + world->map[y][x] = 'O'; is_solvable(world, x - 1, y); is_solvable(world, x + 1, y); is_solvable(world, x, y + 1); diff --git a/minilibx-linux/.github/workflows/ci.yml b/minilibx-linux/.github/workflows/ci.yml deleted file mode 100644 index 8c880cb..0000000 --- a/minilibx-linux/.github/workflows/ci.yml +++ /dev/null @@ -1,88 +0,0 @@ -name: Build - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - - runs-on: ${{ matrix.os }} - env: - DISPLAY: ":99" - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - - timeout-minutes: 20 - steps: - - uses: actions/checkout@v2 - - name: Install mlx dependencies - run: | - set -x - if [ "$RUNNER_OS" == "Linux" ]; then - sudo apt-get update -qq - sudo apt-get install -y -qq gcc make xorg libxext-dev libbsd-dev - elif [ "$RUNNER_OS" == "macOS" ]; then - brew install xquartz - echo "/usr/X11/bin" >> $GITHUB_PATH - else - echo "$RUNNER_OS not supported" - exit 1 - fi - - name: Setup x11 headless testing environment - run: | - set -x - if [ "$RUNNER_OS" == "Linux" ]; then - sudo apt-get install xvfb xdotool valgrind - Xvfb $DISPLAY -screen 0 1280x1024x24 & - elif [ "$RUNNER_OS" == "macOS" ]; then - brew install xdotool - defaults write org.x.X11 enable_test_extensions -boolean true - sudo Xvfb $DISPLAY -screen 0 1280x1024x24 & - else - echo "$RUNNER_OS not supported" - exit 1 - fi - - name: Run ./configure - run: ./configure - - - name: make check Linux - if: matrix.os == 'ubuntu-latest' - run: make -f Makefile.gen check - - name: make check MacOS - continue-on-error: true - if: matrix.os == 'macos-latest' - run: make -f Makefile.gen check - # Didn't find a way to simulate inputs on Macos. libxdo seem to no longer work on macos. - # It can be partially fixed writing proper unit-tests, thus avoiding the need of libxdo. - - - name: Check leaks from binary "test/mlx-test" - run: | - cd test - if [ "$RUNNER_OS" == "Linux" ]; then - echo "Info: Still reachable doesn't matter. Valgrind will return success on thoses reports. - It is fine, we searching for lost pointers. Valgrind will return exit status 42 if any block is lost." - valgrind --leak-check=full --show-leak-kinds=definite,indirect,possible --errors-for-leak-kinds=definite,indirect,possible --error-exitcode=42 ./mlx-test > /dev/null & - PID=$! - sleep 30 - xdotool search --name Title3 windowfocus key Escape - xdotool search --name Title2 windowfocus key Escape - wait $PID - elif [ "$RUNNER_OS" == "macOS" ]; then - MallocStackLoggingNoCompact=1 - ./mlx-test & - sleep 30 - leaks mlx-test - pkill mlx-test - fi - - - name: Norminette, just for fun - continue-on-error: true - run: | - pip3 install Norminette - norminette *.c *.h - norminette --version diff --git a/player.c b/player.c index dbdc731..7567a6c 100644 --- a/player.c +++ b/player.c @@ -6,89 +6,127 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 10:22:53 by jbadaire #+# #+# */ -/* Updated: 2023/10/05 18:13:32 by jbadaire ### ########.fr */ +/* Updated: 2023/10/10 19:59:55 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" #include "stdio.h" +#include "minilibx-linux/mlx.h" -t_player init_player(t_location location, t_list *collectibles) +t_player *init_player(t_location *location, t_collectible *collectibles) { - t_player player; + t_player *player; - player.location = location; - player.collectibles = collectibles; + player = malloc(sizeof (t_player)); + if(!player) + return (NULL); + + location->x--; + location->y--; + player->location = location; + player->collectibles = collectibles; return (player); } -int on_player_move(int keycode, t_world *world) +t_boolean is_inside_world(int y, int x, t_world *world) +{ + if(y < 0 || y > world->length_y) + return (_false); + if(x < 0 || x > (int) ft_strlen(world->map[0])) + return (_false); + return (_true); +} + + +int on_player_move(int keycode, t_game *game) { //w = 119 //s = 115 //a = 97 //d = 100 - printf("\nPRINT: %d", keycode); - if(!can_move(keycode, world)) + if(!can_move(keycode, game->world)) return -1; + mlx_put_image_to_window(game->mlx, + game->window, + game->textures->grass->texture, + game->world->player->location->x * 128, + game->world->player->location->y * 128); + if(keycode == 119) - world->player->location.x += 1; + game->world->player->location->y -= 1; else if(keycode == 115) - world->player->location.x -= 1; + game->world->player->location->y += 1; else if(keycode == 97) - world->player->location.y -= 1; + game->world->player->location->x -= 1; else if(keycode == 100) - world->player->location.y += 1; + game->world->player->location->x += 1; + else { + mlx_put_image_to_window(game->mlx, + game->window, + game->textures->player->texture, + game->world->player->location->x * 128, + game->world->player->location->y * 128); + return (-1); + + } + + printf("\n-> KEYCODE: %d", keycode); + printf("\nPLAYER: \n -> POS Y: %d", game->world->player->location->y); + printf("\nPLAYER: \n -> POS X: %d\n ",game->world->player->location->x); + + mlx_put_image_to_window(game->mlx, + game->window, + game->textures->player->texture, + game->world->player->location->x * 128, + game->world->player->location->y * 128); - printf("\nPRINT2: %d", keycode); return 0; } + t_boolean can_move(int code, t_world *world) { char **map; - char c; int y; int x; - c = ' '; map = world->map; - y = world->player->location.y; - x = world->player->location.x; - if(code == 119 && map[y + 1]) - c = map[y + 1][x]; - else if(code == 115 && map[y - 1]) - c = map[y - 1][x]; - else if(code == 97 && map[y][x - 1]) - c = map[y][x - 1]; - else if(code == 100 && map[y][x + 1]) - c = map[y][x + 1]; - - return (is_solid(c, world->player)); + y = world->player->location->y; + x = world->player->location->x; + if(code == 119) + y--; + else if(code == 115) + y++; + else if(code == 97) + x--; + else if(code == 100) + x++; + if(!is_inside_world(y, x, world)) + return (_false); + + return (is_solid(map[y][x], world->player)); } t_boolean is_solid(char c, t_player *player) { - t_player copy; - copy = *player; - t_collectible collectible; + t_player *copy; + copy = player; if(!c) - return _false; - while (copy.collectibles && copy.collectibles->next) + return (_true); + while (copy->collectibles) { - - collectible = *((t_collectible *) ©.collectibles->content); - if(c == 'E' && !collectible.collected) - return (_false); - copy.collectibles = copy.collectibles->next; + if(c == 'E' && !copy->collectibles->collected) + return (_true); + copy->collectibles = copy->collectibles->next; } if(c != '1') - return (_false); - return _true; + return (_true); + return _false; } diff --git a/so_long.h b/so_long.h index 047cd5f..903ca11 100644 --- a/so_long.h +++ b/so_long.h @@ -6,7 +6,7 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/02 17:59:52 by jbadaire #+# #+# */ -/* Updated: 2023/10/05 19:29:00 by jbadaire ### ########.fr */ +/* Updated: 2023/10/10 17:30:30 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,14 +26,16 @@ typedef struct s_location { typedef struct s_collectible { - t_location location; - int collected; + t_location *location; + int collected; + struct s_collectible *next; } t_collectible; + typedef struct s_player { - t_location location; - t_list *collectibles; + t_location *location; + t_collectible *collectibles; } t_player; typedef struct s_world @@ -43,45 +45,70 @@ typedef struct s_world t_player *player; } t_world; -typedef struct s_texture_list +typedef struct s_texture { - void *name; + char character; void *texture; - struct s_texture_list *next; -} t_texture_list; +} t_texture; + + +typedef struct s_textures +{ + struct s_texture *wall; + struct s_texture *grass; + struct s_texture *collectible; + struct s_texture *player; + struct s_texture *exit; +} t_textures; + + +typedef struct s_game +{ + struct s_textures *textures; + struct s_world *world; + void *mlx; + void *window; + +} t_game; -t_world load_map(int fd, char *path); + +int handle_launch_error(int argc, char *argv[], t_world *world); + +t_world *load_map(int fd, char *path, t_world *world); t_boolean is_horizontal_rectangle(t_world world); t_boolean is_vertical_rectangle(t_world world); t_boolean is_rectangle(t_world world); t_boolean is_closed(t_world world); -t_boolean is_valid_map(t_world world); +t_boolean is_valid_map(t_world *world); +t_boolean is_inside_world(int y, int x, t_world *world); + + +t_player *init_player(t_location *location, t_collectible *collectibles); -t_player init_player(t_location location, t_list *collectibles); +t_collectible *load_collectibles(t_world *world); +t_collectible *create_collectible(t_location location); +int count_collectibles(t_collectible *collectibles, t_boolean o_uncollected, t_boolean o_collected); -t_list *load_collectibles(t_world world); -t_collectible create_collectible(t_location location); -int count_collectibles(t_list *collectibles, t_boolean o_uncollected, t_boolean o_collected); -void update_collectible(t_list **collectibles, t_location location, t_boolean collected); +void update_collectible(t_collectible *collectibles, t_location location, t_boolean collected); void free_collectibles(t_list **collectibles); t_boolean is_solid(char c, t_player *player); t_boolean can_move(int code, t_world *world); -void is_solvable(t_world world, int x, int y); +void is_solvable(t_world *world, int x, int y); t_world *clone(t_world world); int count_element(t_world world, char type); -void *load_texture(char *path, void *mlx); -void draw_type(void *mlx, void *mlx_window, void *texture, t_world world, char c); -int on_player_move(int keycode, t_world *world); -void free_map(t_world world); -t_location find_element(t_world world, char type); - -t_boolean loc_equals(t_location loc_1, t_location loc_2); -t_location edit_location(t_location location, int x, int y); -t_location clone_location(t_location location); -t_location create_location(int x, int y); - -t_collectible *get_collectible_at(t_world world, t_location location); -void ft_lstadd_texture(t_list **lst, t_list *new); +t_textures *load_textures(void *mlx); +t_texture *load_texture(void *mlx, char character, char *path); +void free_textures(t_textures *textures); + +void draw_type(void *mlx, void *mlx_window, t_world *world, t_texture texture); +int on_player_move(int keycode, t_game *world); +void free_map(t_world *world); +t_location find_element(t_world *world, char type); + +t_boolean loc_equals(t_location *loc_1, t_location *loc_2); +t_location *create_location(int x, int y); + +t_collectible *get_collectible_at(t_world *world, t_location *location); #endif diff --git a/utils.c b/utils.c index 4a55358..1926ca0 100644 --- a/utils.c +++ b/utils.c @@ -12,7 +12,7 @@ #include "so_long.h" -t_location find_element(t_world world, char type) { +t_location find_element(t_world *world, char type) { int pos_x; int pos_y; t_location location; @@ -21,9 +21,9 @@ t_location find_element(t_world world, char type) { pos_y = 0; location.x = -1; location.y = -1; - while (world.map[pos_y]) { - while (pos_x < (int) ft_strlen(world.map[0])) { - if (world.map[pos_y][pos_x] == type) { + while (world->map[pos_y]) { + while (pos_x < (int) ft_strlen(world->map[0])) { + if (world->map[pos_y][pos_x] == type) { location.x = ++pos_x; location.y = ++pos_y; return (location);