Skip to content

Commit

Permalink
fix:(vSKAH): Fix all leaks reported by valgrind tool
Browse files Browse the repository at this point in the history
  • Loading branch information
vSKAH committed Oct 12, 2023
1 parent 1c822cf commit 4bfd655
Show file tree
Hide file tree
Showing 13 changed files with 602 additions and 556 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ OBJ_DIRECTORY = ./.obj/

CC = cc

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

INCLUDES = ./headers/so_long.h

Expand Down
60 changes: 26 additions & 34 deletions collectible_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/03 03:24:55 by jbadaire #+# #+# */
/* Updated: 2023/10/10 19:35:28 by jbadaire ### ########.fr */
/* Updated: 2023/10/12 08:57:08 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,20 +19,18 @@ t_collectible *load_collectibles(t_world *world) {

pos_x = 0;
pos_y = 0;
collectible = malloc(sizeof (t_collectible));
if(!collectible)
return (NULL);
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;
location.x = ++pos_x;
location.y = ++pos_y;
if(!collectible->location)
location.x = pos_x;
location.y = pos_y;
if(!collectible)
collectible = create_collectible(location);
else
collectible->next = create_collectible(location);
collectible->next = create_collectible(location);
}
pos_x++;
}
Expand All @@ -52,60 +50,54 @@ t_collectible *create_collectible(t_location location)
if(!collectible)
return (NULL);

collectible->location = create_location(location.x, location.y);
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_collectibles(t_collectible collectibles, t_boolean o_uncollected, t_boolean o_collected)
{
t_collectible *copy;
int count;

count = 0;
copy = collectibles;

if (!copy)
return (0);

while (copy)
while (collectibles.is_set)
{
if(o_uncollected && !copy->collected)
if(o_uncollected && !collectibles.collected)
count++;
if(o_collected && copy->collected)
if(o_collected && collectibles.collected)
count++;
copy = copy->next;
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)
{
t_collectible *copy;
copy = world->player->collectibles;
while (copy)
while (world.player.collectibles)
{
if(loc_equals(location, copy->location))
return copy;
copy = copy->next;
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)
{
t_collectible *copy;

copy = collectibles;
while (copy->next)
while (collectibles)
{
if(copy->location->x == location.x && copy->location->y == location.y) {
copy->collected = collected;
if(loc_equals(collectibles->location, location)) {
collectibles->collected = collected;
break;
}
copy = copy->next;
collectibles = collectibles->next;
}
}
72 changes: 27 additions & 45 deletions frees.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,47 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/03 10:21:10 by jbadaire #+# #+# */
/* Updated: 2023/10/10 14:58:36 by jbadaire ### ########.fr */
/* Updated: 2023/10/12 19:51:21 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "so_long.h"

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

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

void free_collectibles(t_list **collectibles) {

t_list *list;
t_list *copy;

list = *collectibles;
copy = list;
while (copy->next)
{
if(copy->next == NULL) {
free(list->content);
copy = list;
}
copy = copy->next;

}
free(list->content);
free(list);
void free_textures(t_game *game)
{
t_textures *textures = &game->textures;
if(textures->wall.is_set)
mlx_destroy_image(game->mlx, game->textures.wall.texture);
if(textures->player.is_set)
mlx_destroy_image(game->mlx, game->textures.player.texture);
if(textures->collectible.is_set)
mlx_destroy_image(game->mlx, game->textures.collectible.texture);
if(textures->grass.is_set)
mlx_destroy_image(game->mlx, game->textures.grass.texture);
if(textures->exit.is_set)
mlx_destroy_image(game->mlx, game->textures.exit.texture);
}

void free_textures(t_textures *textures)
void free_collectibles(t_collectible *collectible)
{
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)
t_collectible *copy;

while (collectible != NULL)
{
free(textures->grass->texture);
free(textures->grass);
copy = collectible;
collectible = collectible->next;
free(copy);
}
free(textures);
}
52 changes: 26 additions & 26 deletions graphics_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,35 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/03 12:56:12 by jbadaire #+# #+# */
/* Updated: 2023/10/10 15:20:11 by jbadaire ### ########.fr */
/* Updated: 2023/10/12 08:56:07 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "so_long.h"
#include "minilibx-linux/mlx.h"

t_textures *load_textures(void *mlx)
t_textures load_textures(void *mlx)
{
t_textures *textures;
t_textures textures;

textures = malloc(sizeof (t_textures));
if(!textures)
return (NULL);

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");

if (textures->wall == NULL || textures->grass == NULL || textures->exit == NULL)
return (NULL);
if (textures->collectible == NULL || textures->player == NULL)
return (NULL);
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 (textures);
}


t_texture *load_texture(void *mlx, char character, char *path)
t_texture load_texture(void *mlx, char character, char *path)
{
int img_width;
int img_height;
t_texture *texture;

texture = malloc(sizeof(t_texture));
if(!texture)
return (NULL);
t_texture texture;

texture->character = character;
texture->texture = mlx_xpm_file_to_image(mlx, path, &img_width, &img_height);
texture.character = character;
texture.texture = mlx_xpm_file_to_image(mlx, path, &img_width, &img_height);
texture.is_set = _true;
return (texture);
}

Expand All @@ -71,3 +58,16 @@ void draw_type(void *mlx, void *mlx_window, t_world *world, t_texture texture)
}
}

void draw_collectibles(t_game game)
{
t_location loc;
void *texture;

texture = game.textures.collectible.texture;
while (game.world.player.collectibles)
{
loc = game.world.player.collectibles->location;
mlx_put_image_to_window(game.mlx, game.window, texture, loc.x * 128, loc.y * 128);
game.world.player.collectibles = game.world.player.collectibles->next;
}
}
6 changes: 3 additions & 3 deletions libft/ft_strtrim.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <marvin@42.fr> +#+ +:+ +#+ */
/* By: jbadaire <[email protected].fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/11 10:08:19 by jbadaire #+# #+# */
/* Updated: 2023/01/17 17:50:54 by jbadaire ### ########lyon.fr */
/* Created: 2023/10/03 16:00:38 by jbadaire #+# #+# */
/* Updated: 2023/10/03 16:00:38 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
2 changes: 1 addition & 1 deletion libft/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# include "unistd.h"

# ifndef BUFFER_SIZE
# define BUFFER_SIZE 4
# define BUFFER_SIZE 50
# endif

int ft_isalpha(int c);
Expand Down
20 changes: 5 additions & 15 deletions location_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,10 @@

#include "so_long.h"

t_location *create_location(int x, int y)
t_boolean loc_equals(t_location loc_1, t_location loc_2)
{
t_location *location;

location = malloc(sizeof (t_location));
if(!location)
return (NULL);

location->x = x;
location->y = y;
return (location);
}

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;
if(loc_1.x == loc_2.x && loc_1.y == loc_2.y)
return (_true);
else
return (_false);
}
Loading

0 comments on commit 4bfd655

Please sign in to comment.