Skip to content

Commit

Permalink
add:(vSKAH): Fix player can move now
Browse files Browse the repository at this point in the history
  • Loading branch information
vSKAH committed Oct 10, 2023
1 parent 66a97f4 commit 1c822cf
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 341 deletions.
90 changes: 45 additions & 45 deletions collectible_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,106 +6,106 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}
36 changes: 31 additions & 5 deletions frees.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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) {
Expand All @@ -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);
}
78 changes: 32 additions & 46 deletions graphics_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,63 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

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++;
}
Expand All @@ -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);
}
12 changes: 0 additions & 12 deletions libft/ft_lstlast.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@
/* */
/* ************************************************************************** */

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
Expand Down
32 changes: 10 additions & 22 deletions location_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,26 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
Loading

0 comments on commit 1c822cf

Please sign in to comment.