summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--include/so_long.h14
-rw-r--r--maps/hole_in_border.ber4
-rw-r--r--maps/no_collectibles.ber6
-rw-r--r--maps/no_exit.ber4
-rw-r--r--maps/no_path.ber4
-rw-r--r--maps/no_path2.ber4
-rw-r--r--maps/no_path3.ber10
-rw-r--r--maps/no_player.ber5
-rw-r--r--maps/no_walls.ber5
-rw-r--r--maps/two_exits.ber5
-rw-r--r--maps/two_players.ber5
-rw-r--r--maps/valid3.ber9
-rw-r--r--src/check_for_valid_path.c59
-rw-r--r--src/check_map.c88
-rw-r--r--src/collision.c16
-rw-r--r--src/draw.c69
-rw-r--r--src/init.c8
-rw-r--r--src/input.c56
-rw-r--r--src/loop.c4
-rw-r--r--src/main.c34
-rw-r--r--src/player_process.c32
22 files changed, 359 insertions, 84 deletions
diff --git a/Makefile b/Makefile
index 51de300..6bfaf36 100644
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ LIBS = -Llibft -lft -lm -LMLX42/build -lmlx42 -ldl -lglfw -pthread
VPATH := src
SRC = main.c init.c loop.c input.c draw.c tilemap.c player_process.c \
- collision.c map_utils.c
+ collision.c map_utils.c check_map.c check_for_valid_path.c
OBJ_DIR := obj
OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
diff --git a/include/so_long.h b/include/so_long.h
index 6f3800d..ea43bc7 100644
--- a/include/so_long.h
+++ b/include/so_long.h
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */
-/* Updated: 2024/05/20 20:07:16 by dkaiser ### ########.fr */
+/* Updated: 2024/06/10 16:38:06 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -62,6 +62,7 @@ typedef struct s_actor
t_vector direction;
t_vector velocity;
t_ivector size;
+ int steps;
mlx_image_t *img;
} t_actor;
@@ -72,7 +73,7 @@ typedef struct s_tilemap
char *tiles;
t_ivector player_start_tile;
t_ivector exit_tile;
- mlx_image_t *collectible_img;
+ mlx_image_t *collectible_img;
} t_tilemap;
typedef struct s_game
@@ -85,13 +86,14 @@ typedef struct s_game
} t_game;
int load_map_from_file(t_tilemap *tilemap, char *filename);
+int check_map(t_tilemap *map);
+int check_for_valid_path(t_tilemap *map);
int init(t_game *game);
void loop(void *params);
void player_process(t_game *game);
-int draw(t_game *game);
-int draw_walls(t_game *game);
-void draw_exit(t_game *game);
+void draw(t_game *game);
+void draw_map(t_game *game);
void on_key_input(mlx_key_data_t event, void *params);
t_vector grid_to_screen_pos(t_ivector grid_pos, t_ivector tile_size);
t_ivector screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size);
@@ -100,7 +102,7 @@ void set_tile(t_tilemap *map, int x, int y, enum e_tile type);
int check_collision(t_collider a, t_collider b);
int check_map_collision(t_collider collider, t_tilemap *map,
enum e_tile type);
-void move_and_slide(t_actor *actor, t_tilemap *map,
+int move_and_slide(t_actor *actor, t_tilemap *map,
double delta_time);
int is_on_floor(t_collider collider, t_tilemap *map);
diff --git a/maps/hole_in_border.ber b/maps/hole_in_border.ber
new file mode 100644
index 0000000..b58cb34
--- /dev/null
+++ b/maps/hole_in_border.ber
@@ -0,0 +1,4 @@
+11111111011111
+10000000C00001
+1E000P00000001
+11111111111111
diff --git a/maps/no_collectibles.ber b/maps/no_collectibles.ber
new file mode 100644
index 0000000..937209d
--- /dev/null
+++ b/maps/no_collectibles.ber
@@ -0,0 +1,6 @@
+111111111111
+10000000E001
+100000000001
+100000000001
+1000000P0001
+111111111111
diff --git a/maps/no_exit.ber b/maps/no_exit.ber
new file mode 100644
index 0000000..a6c831b
--- /dev/null
+++ b/maps/no_exit.ber
@@ -0,0 +1,4 @@
+11111111111111111
+100000000C0000001
+1000000P000000001
+11111111111111111
diff --git a/maps/no_path.ber b/maps/no_path.ber
new file mode 100644
index 0000000..7096539
--- /dev/null
+++ b/maps/no_path.ber
@@ -0,0 +1,4 @@
+11111111111111
+10000P100000E1
+1C00001000C001
+11111111111111
diff --git a/maps/no_path2.ber b/maps/no_path2.ber
new file mode 100644
index 0000000..a1b63aa
--- /dev/null
+++ b/maps/no_path2.ber
@@ -0,0 +1,4 @@
+1111111111111
+1PE00001000C1
+1000000111111
+1111111111111
diff --git a/maps/no_path3.ber b/maps/no_path3.ber
new file mode 100644
index 0000000..116b4af
--- /dev/null
+++ b/maps/no_path3.ber
@@ -0,0 +1,10 @@
+11111111111111
+11000000000001
+1010000E000001
+10010000000001
+10001000000001
+100001000C0001
+10000010000001
+100P0001000001
+10000000100001
+11111111111111
diff --git a/maps/no_player.ber b/maps/no_player.ber
new file mode 100644
index 0000000..0fb5036
--- /dev/null
+++ b/maps/no_player.ber
@@ -0,0 +1,5 @@
+1111111111
+10000C00C1
+1000000001
+1E00000001
+1111111111
diff --git a/maps/no_walls.ber b/maps/no_walls.ber
new file mode 100644
index 0000000..4285e0c
--- /dev/null
+++ b/maps/no_walls.ber
@@ -0,0 +1,5 @@
+0000000000
+0000000000
+00P00000E0
+00000C0000
+0000000000
diff --git a/maps/two_exits.ber b/maps/two_exits.ber
new file mode 100644
index 0000000..5fe4e75
--- /dev/null
+++ b/maps/two_exits.ber
@@ -0,0 +1,5 @@
+111111111
+1E00C00E1
+100000001
+1C000P001
+111111111
diff --git a/maps/two_players.ber b/maps/two_players.ber
new file mode 100644
index 0000000..185069f
--- /dev/null
+++ b/maps/two_players.ber
@@ -0,0 +1,5 @@
+1111111111111
+1000000P00001
+1P00000000C01
+100C0E0000C01
+1111111111111
diff --git a/maps/valid3.ber b/maps/valid3.ber
new file mode 100644
index 0000000..60f74ab
--- /dev/null
+++ b/maps/valid3.ber
@@ -0,0 +1,9 @@
+111111111111111
+1000000000000E1
+101111111111111
+1C1000C00000001
+101011111111101
+10100000000P101
+101111111111101
+10000000000C001
+111111111111111
diff --git a/src/check_for_valid_path.c b/src/check_for_valid_path.c
new file mode 100644
index 0000000..ef75bc2
--- /dev/null
+++ b/src/check_for_valid_path.c
@@ -0,0 +1,59 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* check_for_valid_path.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/29 15:54:52 by dkaiser #+# #+# */
+/* Updated: 2024/06/10 16:03:06 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include "so_long.h"
+
+static void floodfill(char *tiles, t_ivector size, t_ivector pos);
+static int check_tiles(char *tiles, int size);
+
+int check_for_valid_path(t_tilemap *map)
+{
+ char *tiles;
+
+ tiles = malloc(map->grid_size.x * map->grid_size.y);
+ if (!tiles)
+ {
+ ft_putstr_fd("Allocation error\n", 1);
+ return (1);
+ }
+ ft_strlcpy(tiles, map->tiles, map->grid_size.x * map->grid_size.y + 1);
+ floodfill(tiles, map->grid_size, map->player_start_tile);
+ return (check_tiles(tiles, map->grid_size.x * map->grid_size.y));
+}
+
+static void floodfill(char *tiles, t_ivector size, t_ivector pos)
+{
+ if (tiles[pos.y * size.x + pos.x] == WALL)
+ return ;
+ if (tiles[pos.y * size.x + pos.x] == 'X')
+ return ;
+ tiles[pos.y * size.x + pos.x] = 'X';
+ floodfill(tiles, size, (t_ivector){pos.x - 1, pos.y});
+ floodfill(tiles, size, (t_ivector){pos.x + 1, pos.y});
+ floodfill(tiles, size, (t_ivector){pos.x, pos.y - 1});
+ floodfill(tiles, size, (t_ivector){pos.x, pos.y + 1});
+}
+
+static int check_tiles(char *tiles, int size)
+{
+ int i;
+
+ i = 0;
+ while (i < size)
+ {
+ if (tiles[i] != WALL && tiles[i] != 'X')
+ return (1);
+ i++;
+ }
+ return (0);
+}
diff --git a/src/check_map.c b/src/check_map.c
new file mode 100644
index 0000000..af7a97d
--- /dev/null
+++ b/src/check_map.c
@@ -0,0 +1,88 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* check_map.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/29 13:23:01 by dkaiser #+# #+# */
+/* Updated: 2024/05/29 15:58:10 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "so_long.h"
+
+static int check_borders(t_tilemap *map);
+static int check_exactly_one(t_tilemap *map, char tile);
+static int check_collectibles(t_tilemap *map);
+
+int check_map(t_tilemap *map)
+{
+ if (check_borders(map))
+ return (1);
+ if (check_exactly_one(map, PLAYER_START))
+ return (1);
+ if (check_exactly_one(map, EXIT))
+ return (1);
+ if (check_collectibles(map))
+ return (1);
+ if (check_for_valid_path(map))
+ return (1);
+ return (0);
+}
+
+static int check_borders(t_tilemap *map)
+{
+ int i;
+
+ i = 0;
+ while (i < map->grid_size.x)
+ {
+ if (get_tile(map, i, 0) != WALL || get_tile(map, i, map->grid_size.y
+ - 1) != WALL)
+ return (1);
+ i++;
+ }
+ i = 0;
+ while (i < map->grid_size.y)
+ {
+ if (get_tile(map, 0, i) != WALL || get_tile(map, map->grid_size.x - 1,
+ i) != WALL)
+ return (1);
+ i++;
+ }
+ return (0);
+}
+
+static int check_exactly_one(t_tilemap *map, char tile)
+{
+ int i;
+ int found;
+
+ found = 0;
+ i = map->grid_size.x * map->grid_size.y - 1;
+ while (i >= 0)
+ {
+ if (map->tiles[i] == tile)
+ found++;
+ i--;
+ }
+ if (found != 1)
+ return (1);
+ return (0);
+}
+
+static int check_collectibles(t_tilemap *map)
+{
+ int i;
+
+ i = map->grid_size.x * map->grid_size.y - 1;
+ while (i >= 0)
+ {
+ if (map->tiles[i] == COLLECTIBLE)
+ return (0);
+ i--;
+ }
+ ft_printf("\nFound no collectibles\n");
+ return (1);
+}
diff --git a/src/collision.c b/src/collision.c
index 115b70a..c416184 100644
--- a/src/collision.c
+++ b/src/collision.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 11:48:59 by dkaiser #+# #+# */
-/* Updated: 2024/05/15 15:19:54 by dkaiser ### ########.fr */
+/* Updated: 2024/06/10 15:43:32 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -59,7 +59,8 @@ int check_map_collision(t_collider collider, t_tilemap *map, enum e_tile type)
{
check_tile.x = local_tile.x + x;
check_tile.y = local_tile.y + y;
- result |= check_map_collision_for_tile(collider, map, check_tile, type);
+ result |= check_map_collision_for_tile(collider, map, check_tile,
+ type);
y++;
}
x++;
@@ -82,23 +83,32 @@ static int check_map_collision_for_tile(t_collider collider, t_tilemap *map,
return (0);
}
-void move_and_slide(t_actor *actor, t_tilemap *map, double delta_time)
+int move_and_slide(t_actor *actor, t_tilemap *map, double delta_time)
{
t_collider c;
+ int is_moving;
+ is_moving = 0;
c.size = actor->size;
c.position.x = actor->position.x + (actor->velocity.x * delta_time);
c.position.y = actor->position.y;
if ((check_map_collision(c, map, WALL) & (RIGHT | LEFT)) == 0)
+ {
actor->position.x = c.position.x;
+ is_moving += (actor->direction.x != 0);
+ }
else
actor->velocity.x = 0;
c.position.x = actor->position.x;
c.position.y = actor->position.y + (actor->velocity.y * delta_time);
if ((check_map_collision(c, map, WALL) & (UP | DOWN)) == 0)
+ {
actor->position.y = c.position.y;
+ is_moving += (actor->direction.y != 0);
+ }
else
actor->velocity.y = 0;
+ return (is_moving != 0);
}
int is_on_floor(t_collider collider, t_tilemap *map)
diff --git a/src/draw.c b/src/draw.c
index 5284d74..2c525dc 100644
--- a/src/draw.c
+++ b/src/draw.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 17:58:23 by dkaiser #+# #+# */
-/* Updated: 2024/05/20 20:10:15 by dkaiser ### ########.fr */
+/* Updated: 2024/06/10 16:37:51 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,42 +14,29 @@
#include "libft.h"
#include "so_long.h"
-int draw(t_game *game)
+static void draw_walls_and_collectibles(t_game *game);
+static void setup_images(t_game *game, mlx_image_t **wall_image);
+static void draw_exit(t_game *game);
+
+void draw(t_game *game)
{
game->player.img->instances[0].x = game->player.position.x;
game->player.img->instances[0].y = game->player.position.y;
- return (0);
}
-void draw_exit(t_game *game)
+void draw_map(t_game *game)
{
- mlx_texture_t *exit_texture;
- mlx_image_t *exit_image;
- t_vector pos;
-
- exit_texture = mlx_load_png("textures/exit.png");
- exit_image = mlx_texture_to_image(game->mlx, exit_texture);
- mlx_resize_image(exit_image, game->map.tile_size.x, game->map.tile_size.y);
- pos = grid_to_screen_pos(game->map.exit_tile, game->map.tile_size);
- mlx_image_to_window(game->mlx, exit_image, pos.x, pos.y);
+ draw_walls_and_collectibles(game);
+ draw_exit(game);
}
-int draw_walls(t_game *game)
+static void draw_walls_and_collectibles(t_game *game)
{
- int x;
- int y;
- mlx_texture_t *wall_texture;
- mlx_image_t *wall_image;
- mlx_texture_t *collectible_texture;
+ int x;
+ int y;
+ mlx_image_t *wall_image;
- wall_texture = mlx_load_png("textures/wall.png");
- wall_image = mlx_texture_to_image(game->mlx, wall_texture);
- collectible_texture = mlx_load_png("textures/collectible.png");
- game->map.collectible_img = mlx_texture_to_image(game->mlx, collectible_texture);
- mlx_resize_image(wall_image, 48, 48);
- mlx_resize_image(game->map.collectible_img, 48, 48);
- game->map.tile_size.x = 48;
- game->map.tile_size.y = 48;
+ setup_images(game, &wall_image);
x = 0;
while (x < game->map.grid_size.x)
{
@@ -66,5 +53,31 @@ int draw_walls(t_game *game)
}
x++;
}
- return (0);
+}
+
+static void setup_images(t_game *game, mlx_image_t **wall_image)
+{
+ mlx_texture_t *wall_texture;
+ mlx_texture_t *collectible_texture;
+
+ wall_texture = mlx_load_png("textures/wall.png");
+ *wall_image = mlx_texture_to_image(game->mlx, wall_texture);
+ collectible_texture = mlx_load_png("textures/collectible.png");
+ game->map.collectible_img = mlx_texture_to_image(game->mlx,
+ collectible_texture);
+ mlx_resize_image(*wall_image, 48, 48);
+ mlx_resize_image(game->map.collectible_img, 48, 48);
+}
+
+static void draw_exit(t_game *game)
+{
+ mlx_texture_t *exit_texture;
+ mlx_image_t *exit_image;
+ t_vector pos;
+
+ exit_texture = mlx_load_png("textures/exit.png");
+ exit_image = mlx_texture_to_image(game->mlx, exit_texture);
+ mlx_resize_image(exit_image, game->map.tile_size.x, game->map.tile_size.y);
+ pos = grid_to_screen_pos(game->map.exit_tile, game->map.tile_size);
+ mlx_image_to_window(game->mlx, exit_image, pos.x, pos.y);
}
diff --git a/src/init.c b/src/init.c
index 40f15f1..c4600b0 100644
--- a/src/init.c
+++ b/src/init.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */
-/* Updated: 2024/05/20 18:31:33 by dkaiser ### ########.fr */
+/* Updated: 2024/06/10 15:55:28 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,10 +18,10 @@ static void init_player(t_game *game);
int init(t_game *game)
{
- game->mlx = mlx_init(1920, 1080, "so_long", false);
- // TODO: make size and title dynamic
game->input_direction = ZERO;
game->map.tile_size = (t_ivector){48, 48};
+ game->mlx = mlx_init(game->map.grid_size.x * game->map.tile_size.x,
+ game->map.grid_size.y * game->map.tile_size.y, "so_long", false);
init_hooks(game);
init_player(game);
return (0);
@@ -44,6 +44,8 @@ static void init_player(t_game *game)
game->map.tile_size);
player->velocity = (t_vector){0, 0};
player->size = (t_ivector){44, 44};
+ player->steps = 0;
+ ft_printf("Steps: %d\n", game->player.steps);
player->img = mlx_texture_to_image(game->mlx, texture);
mlx_resize_image(player->img, player->size.x, player->size.y);
mlx_image_to_window(game->mlx, player->img, player->position.x,
diff --git a/src/input.c b/src/input.c
index a6cb8d4..0bf54ec 100644
--- a/src/input.c
+++ b/src/input.c
@@ -6,37 +6,49 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:37:40 by dkaiser #+# #+# */
-/* Updated: 2024/05/10 12:13:22 by dkaiser ### ########.fr */
+/* Updated: 2024/06/10 15:58:43 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
+#include "MLX42/MLX42.h"
#include "so_long.h"
+static void on_key_press(t_game *game, mlx_key_data_t event);
+static void on_key_release(t_game *game, mlx_key_data_t event);
+
void on_key_input(mlx_key_data_t event, void *params)
{
t_game *game;
game = (t_game *)params;
- if (event.action == MLX_PRESS)
- {
- if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP)
- game->input_direction |= UP;
- else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT)
- game->input_direction |= LEFT;
- else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN)
- game->input_direction |= DOWN;
- else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT)
- game->input_direction |= RIGHT;
- }
+ if (event.key == MLX_KEY_ESCAPE && event.action == MLX_PRESS)
+ mlx_close_window(game->mlx);
+ else if (event.action == MLX_PRESS)
+ on_key_press(game, event);
else if (event.action == MLX_RELEASE)
- {
- if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP)
- game->input_direction &= ~UP;
- else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT)
- game->input_direction &= ~LEFT;
- else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN)
- game->input_direction &= ~DOWN;
- else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT)
- game->input_direction &= ~RIGHT;
- }
+ on_key_release(game, event);
+}
+
+static void on_key_press(t_game *game, mlx_key_data_t event)
+{
+ if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP)
+ game->input_direction |= UP;
+ else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT)
+ game->input_direction |= LEFT;
+ else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN)
+ game->input_direction |= DOWN;
+ else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT)
+ game->input_direction |= RIGHT;
+}
+
+static void on_key_release(t_game *game, mlx_key_data_t event)
+{
+ if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP)
+ game->input_direction &= ~UP;
+ else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT)
+ game->input_direction &= ~LEFT;
+ else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN)
+ game->input_direction &= ~DOWN;
+ else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT)
+ game->input_direction &= ~RIGHT;
}
diff --git a/src/loop.c b/src/loop.c
index c4f95b3..ec7f1b3 100644
--- a/src/loop.c
+++ b/src/loop.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:09:24 by dkaiser #+# #+# */
-/* Updated: 2024/05/14 12:45:26 by dkaiser ### ########.fr */
+/* Updated: 2024/06/10 15:43:17 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,5 +18,7 @@ void loop(void *params)
game = (t_game *)params;
player_process(game);
+ if (game->player.steps)
+ ft_printf("\e[1A\e[KSteps: %d\n", game->player.steps);
draw(game);
}
diff --git a/src/main.c b/src/main.c
index fca5cf0..10e68bc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,22 +6,46 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 14:14:13 by dkaiser #+# #+# */
-/* Updated: 2024/05/15 17:31:46 by dkaiser ### ########.fr */
+/* Updated: 2024/06/10 16:37:21 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
+#include "libft.h"
#include "so_long.h"
+static void cleanup(t_game *game);
+static int error(char *msg);
+
int main(int argc, char *argv[])
{
t_game game;
if (argc != 2)
- return (1);
+ return (error("Usage: ./so_long <map>"));
if (load_map_from_file(&game.map, argv[1]))
- return (1);
+ return (error("Not a valid map."));
+ if (check_map(&game.map))
+ {
+ free(game.map.tiles);
+ return (error("Not a valid map."));
+ }
init(&game);
- draw_walls(&game);
- draw_exit(&game);
+ draw_map(&game);
mlx_loop(game.mlx);
+ cleanup(&game);
+}
+
+static void cleanup(t_game *game)
+{
+ if (game->map.tiles)
+ free(game->map.tiles);
+ if (game->mlx)
+ mlx_terminate(game->mlx);
+}
+
+static int error(char *msg)
+{
+ ft_putendl_fd("Error", 2);
+ ft_putendl_fd(msg, 2);
+ return (1);
}
diff --git a/src/player_process.c b/src/player_process.c
index 7df0d53..5c6c6fb 100644
--- a/src/player_process.c
+++ b/src/player_process.c
@@ -6,17 +6,18 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */
-/* Updated: 2024/05/20 20:29:07 by dkaiser ### ########.fr */
+/* Updated: 2024/06/10 16:28:09 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
+#include "MLX42/MLX42.h"
#include "ft_printf.h"
#include "so_long.h"
static t_vector get_direction_from_input(t_game *game);
static void handle_collectible_collision(t_actor *player, t_tilemap *map);
static void collect_collectible(t_ivector pos, t_tilemap *map);
-static void handle_exit_collision(t_actor *player, t_tilemap *map);
+static void handle_exit_collision(t_game *game);
void player_process(t_game *game)
{
@@ -25,13 +26,11 @@ void player_process(t_game *game)
player = &game->player;
player->direction = get_direction_from_input(game);
player->velocity.x = player->direction.x * PLAYER_MOVE_SPEED;
- player->velocity.y += 50;
- if (player->direction.y == -1 && is_on_floor((t_collider){player->position,
- player->size}, &game->map))
- player->velocity.y = -1000;
+ player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED;
handle_collectible_collision(player, &game->map);
- handle_exit_collision(player, &game->map);
- move_and_slide(player, &game->map, game->mlx->delta_time);
+ handle_exit_collision(game);
+ if (move_and_slide(player, &game->map, game->mlx->delta_time))
+ player->steps++;
}
static void handle_collectible_collision(t_actor *player, t_tilemap *map)
@@ -64,11 +63,10 @@ static void handle_collectible_collision(t_actor *player, t_tilemap *map)
static void collect_collectible(t_ivector pos, t_tilemap *map)
{
- size_t i;
- t_vector collectible_pos;
- t_ivector collectible_tile;
+ size_t i;
+ t_vector collectible_pos;
+ t_ivector collectible_tile;
- ft_printf("Collected a collectile.\n");
set_tile(map, pos.x, pos.y, EMPTY);
i = 0;
while (i < map->collectible_img->count)
@@ -85,11 +83,15 @@ static void collect_collectible(t_ivector pos, t_tilemap *map)
}
}
-static void handle_exit_collision(t_actor *player, t_tilemap *map)
+static void handle_exit_collision(t_game *game)
{
t_collider player_collider;
- size_t i;
+ size_t i;
+ t_actor *player;
+ t_tilemap *map;
+ player = &game->player;
+ map = &game->map;
player_collider = (t_collider){player->position, player->size};
if (check_map_collision(player_collider, map, EXIT))
{
@@ -100,7 +102,7 @@ static void handle_exit_collision(t_actor *player, t_tilemap *map)
return ;
i++;
}
- exit(0);
+ mlx_close_window(game->mlx);
}
}