From 69765e686473bbd920221eda728b2cb83a6e516d Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 15:53:31 +0200 Subject: Add tilemap.c and read map size --- Makefile | 2 +- include/so_long.h | 31 ++++++++++++++++++++++--- src/tilemap.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/tilemap.c diff --git a/Makefile b/Makefile index 6f4e2c8..763c897 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ HEADERS = -Iinclude -Ilibft -IMLX42/include LIBS = -Llibft -lft -lm -LMLX42/build -lmlx42 -ldl -lglfw -pthread VPATH := src -SRC = main.c init.c loop.c input.c draw.c +SRC = main.c init.c loop.c input.c draw.c tilemap.c OBJ_DIR := obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/so_long.h b/include/so_long.h index d66e355..ce481ef 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,16 +6,28 @@ /* By: dkaiser grid_size = get_map_size_from_file(fd); + close(fd); + if (tilemap->grid_size.x < 0 || tilemap->grid_size.y < 0) + return (1); + return (0); +} + +static t_ivector get_map_size_from_file(int fd) +{ + t_ivector result; + char *next_line; + + result.x = 0; + result.y = 0; + next_line = get_next_line(fd); + while (next_line) + { + if (!result.x) + result.x = get_line_len(next_line); + else if (result.x != get_line_len(next_line)) + { + free(next_line); + result.x = -1; + result.y = -1; + return (result); + } + result.y++; + free(next_line); + next_line = get_next_line(fd); + } + return (result); +} + +static int get_line_len(char *line) +{ + int len; + + len = 0; + while (line[len] && line[len] != '\n') + len++; + return (len); +} -- cgit v1.2.3 From 81e3dc680b3895a8542d633d21b3698d4d41eaad Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 17:39:41 +0200 Subject: Add tilemap loading --- include/so_long.h | 9 ++++++--- src/main.c | 8 ++++++-- src/tilemap.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/include/so_long.h b/include/so_long.h index ce481ef..2333368 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser grid_size.x < 0 || tilemap->grid_size.y < 0) return (1); + fd = open(filename, O_RDONLY); + if (fd < 0) + return (1); + if (load_tiles_from_file(tilemap, fd)) + { + close(fd); + return (1); + } + close(fd); return (0); } @@ -56,6 +67,44 @@ static t_ivector get_map_size_from_file(int fd) return (result); } +static int load_tiles_from_file(t_tilemap *tilemap, int fd) +{ + int l; + char *next_line; + + tilemap->tiles = malloc(tilemap->grid_size.x * tilemap->grid_size.y + * sizeof(char)); + if (!tilemap->tiles) + return (1); + l = 0; + next_line = get_next_line(fd); + while (next_line) + { + if (load_tiles_from_line(tilemap, l, next_line)) + return (1); + next_line = get_next_line(fd); + l++; + } + return (0); +} + +static int load_tiles_from_line(t_tilemap *tilemap, int l, char *line) +{ + int x; + + x = 0; + while (line[x] && line[x] != '\n') + { + if (line[x] == EMPTY || line[x] == WALL || line[x] == COLLECTIBLE + || line[x] == PLAYER_START || line[x] == EXIT) + tilemap->tiles[l * tilemap->grid_size.x + x] = line[x]; + else + return (1); + line++; + } + return (0); +} + static int get_line_len(char *line) { int len; -- cgit v1.2.3 From 24a6825a00bf4ac319f57ed12e76afc8f1c9f401 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 17:39:55 +0200 Subject: Add some maps --- maps/different_line_sizes.ber | 4 ++++ maps/empty.ber | 0 maps/invalid_char.ber | 5 +++++ maps/valid.ber | 6 ++++++ 4 files changed, 15 insertions(+) create mode 100644 maps/different_line_sizes.ber create mode 100644 maps/empty.ber create mode 100644 maps/invalid_char.ber create mode 100644 maps/valid.ber diff --git a/maps/different_line_sizes.ber b/maps/different_line_sizes.ber new file mode 100644 index 0000000..9b35103 --- /dev/null +++ b/maps/different_line_sizes.ber @@ -0,0 +1,4 @@ +1111111111111111 +10000000000000011 +1P0C0000000000E1 +1111111111111111 diff --git a/maps/empty.ber b/maps/empty.ber new file mode 100644 index 0000000..e69de29 diff --git a/maps/invalid_char.ber b/maps/invalid_char.ber new file mode 100644 index 0000000..d4c02f9 --- /dev/null +++ b/maps/invalid_char.ber @@ -0,0 +1,5 @@ +111111111 +100 11111 +1aPE11111 +1C1111111 +111111111 diff --git a/maps/valid.ber b/maps/valid.ber new file mode 100644 index 0000000..8b9f951 --- /dev/null +++ b/maps/valid.ber @@ -0,0 +1,6 @@ +111111111111111 +100000000000001 +1C0000000000001 +110000000000001 +11100000P0000E1 +111111111111111 -- cgit v1.2.3 From f199b13df1350aa864a9104616f0d9b82606b4ed Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sat, 11 May 2024 16:26:51 +0200 Subject: Finish map input --- include/so_long.h | 2 ++ src/init.c | 28 +++++++++++++++------------- src/tilemap.c | 34 +++++++++++++++++++++++----------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/include/so_long.h b/include/so_long.h index 2333368..47443aa 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -63,6 +63,8 @@ typedef struct s_tilemap t_ivector grid_size; t_ivector tile_size; char *tiles; + t_ivector player_start_tile; + t_ivector exit_tile; } t_tilemap; typedef struct s_game diff --git a/src/init.c b/src/init.c index 3c15791..5277b5c 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: dkaiser mlx = mlx_init(1920, 1080, "so_long", false); // TODO: make size and title dynamic game->input_direction = ZERO; init_hooks(game); - init_player(&game->player, 960, 540); - // TODO: make player spawn point dynamic - texture = mlx_load_png("textures/player.png"); - game->player.img = mlx_texture_to_image(game->mlx, texture); - mlx_resize_image(game->player.img, 48, 48); - mlx_image_to_window(game->mlx, game->player.img, game->player.position.x, - game->player.position.y); + init_player(game); return (0); } @@ -40,10 +33,19 @@ static void init_hooks(t_game *game) mlx_key_hook(game->mlx, on_key_input, game); } -static void init_player(t_player *player, int x, int y) +static void init_player(t_game *game) { - player->position.x = x; - player->position.y = y; + mlx_texture_t *texture; + t_player *player; + + player = &game->player; + texture = mlx_load_png("textures/player.png"); + player->position.x = game->map.player_start_tile.x * 48; + player->position.y = game->map.player_start_tile.y * 48; player->velocity.x = 0; player->velocity.y = 0; + player->img = mlx_texture_to_image(game->mlx, texture); + mlx_resize_image(player->img, 48, 48); + mlx_image_to_window(game->mlx, player->img, player->position.x, + player->position.y); } diff --git a/src/tilemap.c b/src/tilemap.c index 45d9fa5..ef830c7 100644 --- a/src/tilemap.c +++ b/src/tilemap.c @@ -6,11 +6,12 @@ /* By: dkaiser tiles[l * tilemap->grid_size.x + x] = line[x]; - else + if (line[pos.x] != '0' && line[pos.x] != '1' && line[pos.x] != 'C' + && line[pos.x] != 'P' && line[pos.x] != 'E') return (1); - line++; + if (line[pos.x] == PLAYER_START && !found_player_start) + { + tilemap->player_start_tile = pos; + found_player_start = 1; + } + else if (line[pos.x] == EXIT && !found_exit) + { + tilemap->exit_tile = pos; + found_exit = 1; + } + tilemap->tiles[l * tilemap->grid_size.x + pos.x] = line[pos.x]; + pos.x++; } return (0); } -- cgit v1.2.3 From e597be4f58e7252333192cb1e4211d74522b6102 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sat, 11 May 2024 16:27:45 +0200 Subject: Add wall rendering --- include/so_long.h | 3 ++- src/draw.c | 30 +++++++++++++++++++++++++++++- src/main.c | 3 ++- textures/wall.png | Bin 0 -> 99 bytes 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 textures/wall.png diff --git a/include/so_long.h b/include/so_long.h index 47443aa..e9097d1 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser mlx, wall_texture); + mlx_resize_image(wall_image, 48, 48); + game->map.tile_size.x = 48; + game->map.tile_size.y = 48; + x = 0; + while (x < game->map.grid_size.x) + { + y = 0; + while (y < game->map.grid_size.y) + { + if (game->map.tiles[y * game->map.grid_size.x + x] == WALL) + mlx_image_to_window(game->mlx, wall_image, x * game->map.tile_size.x, y * game->map.tile_size.y); + y++; + } + x++; + } + return (0); +} + int draw(t_game *game) { game->player.img->instances[0].x = game->player.position.x; diff --git a/src/main.c b/src/main.c index 53fb23d..f7352f4 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: dkaiser