Skip to content

Commit

Permalink
fix:(vSKAH): Fix leaks
Browse files Browse the repository at this point in the history
fix:(vSKAH): Collectibles load errors
fix:(vSKAH): Exit solve

add:(vSKAH): Xpm find security
add:(vSKAH): Add parsing errors

TODO: print nb of movements
  • Loading branch information
vSKAH committed Oct 13, 2023
1 parent 4bfd655 commit 8be4f2c
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 398 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ SRCS = main.c \
rectangle_check.c \
utils.c \
graphics_utils.c \
location_utils.c
location_utils.c \
textures_utils.c \
errors_utils.c \

LIBFT_FLAGS = -L./libft -l:libft.a
OBJ_DIRECTORY = ./.obj/


CC = cc

FLAGS = -c -Wall -Wextra -Werror -g -no-pie
FLAGS = -c -Wall -Wextra -Werror -g

INCLUDES = ./headers/so_long.h

Expand Down
92 changes: 41 additions & 51 deletions collectible_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,92 +12,82 @@

#include "so_long.h"

t_collectible *load_collectibles(t_world *world) {
int pos_x;
int pos_y;
t_collectible *collectible;
t_collectible *load_collectibles(t_world *world)
{
int pos_x;
int pos_y;
t_collectible *collectible;
t_location location;

pos_x = 0;
pos_y = 0;
collectible = 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') {
t_location location;
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')
{
location.x = pos_x;
location.y = pos_y;
if(!collectible)
collectible = create_collectible(location);
else
collectible->next = create_collectible(location);
last(&collectible, create_collectible(location));
}
pos_x++;
}
pos_x = 0;
pos_y++;
}
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)
if (!collectible)
return (NULL);

collectible->location = location;
collectible->collected = 0;
collectible->is_set = _true;
collectible->next = NULL;
return (collectible);
}

int count_collectibles(t_collectible collectibles, t_boolean o_uncollected, t_boolean o_collected)
{
int count;

count = 0;

while (collectibles.is_set)
{
if(o_uncollected && !collectibles.collected)
count++;
if(o_collected && collectibles.collected)
count++;
if(collectibles.next != NULL)
collectibles = *collectibles.next;
else
break;
}
return (count);
}


t_collectible *get_collectible_at(t_world world, t_location location)
t_collectible *get_collectible_at(t_world world, t_location location)
{
while (world.player.collectibles)
{
if(loc_equals(location, world.player.collectibles->location))
if (loc_equals(location, world.player.collectibles->location))
return (world.player.collectibles);
world.player.collectibles = world.player.collectibles->next;
}
return (NULL);
}

#include "stdio.h"
void update_collectible(t_collectible *collectibles, t_location location, t_boolean collected)
void update_col(t_collectible *col, t_location loc, t_boolean bool)
{
while (collectibles)
while (col)
{
if(loc_equals(collectibles->location, location)) {
collectibles->collected = collected;
break;
if (loc_equals(col->location, loc))
{
col->collected = bool;
break ;
}
collectibles = collectibles->next;
col = col->next;
}
}

void last(t_collectible **lst, t_collectible *new)
{
t_collectible *tmp;

tmp = *lst;
if (tmp == NULL)
{
*lst = new;
return ;
}
}
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
63 changes: 63 additions & 0 deletions errors_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/13 17:50:08 by jbadaire #+# #+# */
/* Updated: 2023/10/13 17:50:08 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "so_long.h"
#include <fcntl.h>

int handle_file_error(char *argv[])
{
int fd;

if (!argv[1])
return (ft_putstr_fd("Error\n -> Invalid file path.\n", 1), -2);
fd = open(argv[1], O_RDONLY);
if (fd == -1)
return (ft_putstr_fd("Error\n -> Invalid file path.\n", 1), -2);
return (fd);
}

int handle_map_error(char *path, t_game game)
{
int elements_code;
t_world cloned;

if (has_illegal_character(game.world)){
free_map(&game.world);
free_collectibles(game.world.player.collectibles);
return (ft_putstr_fd("Error\n -> Invalid Map / illegal character", 1), -1);

}
if (!is_valid_shape(game.world))
{
free_map(&game.world);
free_collectibles(game.world.player.collectibles);
return (ft_putstr_fd("Error\n -> Invalid shape / not closed", 1), -1);
}
elements_code = valid_elements(game.world);
if (elements_code < 0)
{
ft_putstr_fd("Error\n -> Please, check your map !", 1);
ft_putstr_fd("\nThe map must contains 1 E, 1 P, minimum 1 C", 1);
return (-1);
}
load_map(open(path, O_RDONLY), path, &cloned);
is_solvable(cloned.map, game.world.player.location.x, game.world.player.location.y, game.world.length_y);
if (count_element(cloned, 'C') > 0 || count_element(cloned, 'E') > 0)
{
ft_putstr_fd("Error\n -> Sorry, your map is not resolvable", 1);
free_map(&game.world);
free_collectibles(game.world.player.collectibles);
return (free_map(&cloned), -2);
}
free_map(&cloned);
return (0);
}
58 changes: 43 additions & 15 deletions frees.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,75 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/03 10:21:10 by jbadaire #+# #+# */
/* Updated: 2023/10/12 19:51:21 by jbadaire ### ########.fr */
/* Updated: 2023/10/13 17:09:19 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "so_long.h"

void free_map(t_world *world) {
int index;
void free_map(t_world *world)
{
int index;

index = 0;
while (world->map && world->map[index]) {
while (world->map && world->map[index])
{
free(world->map[index]);
index++;
}
free((world->map));
}


void free_textures(t_game *game)
void free_textures(t_game *game)
{
t_textures *textures = &game->textures;
if(textures->wall.is_set)
t_textures *textures;

textures = &game->textures;
if (textures->wall.is_set && textures->wall.texture != NULL)
mlx_destroy_image(game->mlx, game->textures.wall.texture);
if(textures->player.is_set)
if (textures->player.is_set && textures->player.texture != NULL)
mlx_destroy_image(game->mlx, game->textures.player.texture);
if(textures->collectible.is_set)
if (textures->collectible.is_set && textures->collectible.texture != NULL)
mlx_destroy_image(game->mlx, game->textures.collectible.texture);
if(textures->grass.is_set)
if (textures->grass.is_set && textures->grass.texture != NULL)
mlx_destroy_image(game->mlx, game->textures.grass.texture);
if(textures->exit.is_set)
if (textures->exit.is_set && textures->exit.texture != NULL)
mlx_destroy_image(game->mlx, game->textures.exit.texture);
}

void free_collectibles(t_collectible *collectible)
void free_collectibles(t_collectible *collectible)
{
t_collectible *copy;
t_collectible *copy;

while (collectible != NULL)
{
copy = collectible;
collectible = collectible->next;
free(copy);
}
}
}

int free_unavailable_texture(t_game game)
{
if (has_unavailable_texture(game.textures))
{
ft_putstr_fd("Error\n -> Textures can't be loaded.", 1);
free_map(&game.world);
free_textures(&game);
free_collectibles(game.world.player.collectibles);
mlx_destroy_display(game.mlx);
free(game.mlx);
return (1);
}
return (0);
}

void destroy(t_game *game)
{
free_map(&game->world);
free_textures(game);
free_collectibles(game->world.player.collectibles);
mlx_destroy_window(game->mlx, game->window);
mlx_destroy_display(game->mlx);
free(game->mlx);
}
Loading

0 comments on commit 8be4f2c

Please sign in to comment.