diff options
| author | Dominik Kaiser | 2024-05-11 16:46:09 +0200 |
|---|---|---|
| committer | GitHub | 2024-05-11 16:46:09 +0200 |
| commit | 7b021c4426f21bf20fed61459ddedb5224e25bc4 (patch) | |
| tree | 7115ef51d858fe2275c7402a46716f66906fb2f4 | |
| parent | 18c61f3119e468e9a9418aef8aaeadcef090895c (diff) | |
| parent | 796108c1ebe5085da626ab82e5866ce5ec1383c2 (diff) | |
| download | so_long-7b021c4426f21bf20fed61459ddedb5224e25bc4.tar.gz so_long-7b021c4426f21bf20fed61459ddedb5224e25bc4.zip | |
Merge map-input into master
Read map input from file and display on screen
TODO: Display Collectibles and Exit
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | include/so_long.h | 37 | ||||
| -rw-r--r-- | maps/different_line_sizes.ber | 4 | ||||
| -rw-r--r-- | maps/empty.ber | 0 | ||||
| -rw-r--r-- | maps/invalid_char.ber | 5 | ||||
| -rw-r--r-- | maps/valid.ber | 6 | ||||
| -rw-r--r-- | maps/valid2.ber | 15 | ||||
| -rw-r--r-- | src/draw.c | 30 | ||||
| -rw-r--r-- | src/init.c | 28 | ||||
| -rw-r--r-- | src/main.c | 9 | ||||
| -rw-r--r-- | src/tilemap.c | 128 | ||||
| -rw-r--r-- | textures/wall.png | bin | 0 -> 99 bytes |
12 files changed, 244 insertions, 20 deletions
@@ -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..e9097d1 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,16 +6,28 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 12:24:12 by dkaiser ### ########.fr */ +/* Updated: 2024/05/11 16:10:26 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef SO_LONG_H # define SO_LONG_H -# define PLAYER_MOVE_SPEED 250 # include "MLX42/MLX42.h" +# include "fcntl.h" # include "libft.h" +# include "unistd.h" + +# define PLAYER_MOVE_SPEED 250 + +enum e_tile +{ + EMPTY = '0', + WALL = '1', + COLLECTIBLE = 'C', + EXIT = 'E', + PLAYER_START = 'P' +}; enum e_direction { @@ -32,6 +44,12 @@ typedef struct s_vector double y; } t_vector; +typedef struct s_ivector +{ + int x; + int y; +} t_ivector; + typedef struct s_player { t_vector position; @@ -40,17 +58,30 @@ typedef struct s_player mlx_image_t *img; } t_player; +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 { mlx_t *mlx; void *window; t_player player; int input_direction; + t_tilemap map; } t_game; +int load_map_from_file(t_tilemap *tilemap, char *filename); + int init(t_game *game); -void loop(void *game); +void loop(void *params); int draw(t_game *game); +int draw_map(t_game *game); void on_key_input(mlx_key_data_t event, void *params); #endif // SO_LONG_H 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 --- /dev/null +++ b/maps/empty.ber 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 diff --git a/maps/valid2.ber b/maps/valid2.ber new file mode 100644 index 0000000..853beae --- /dev/null +++ b/maps/valid2.ber @@ -0,0 +1,15 @@ +1111111111111111111111111111111 +1000000000000000000000000000001 +1000000000000000000000000000001 +10000000000000000000000C0000001 +1000000000000000000011111000001 +10000000000000000C0000000000001 +1000000000000011111000000000001 +1000000000000000000000000000001 +1000000011111000000000000000001 +10P0000000000000000000000000001 +1111100000000000000000000000001 +11111111000000000000C0000000001 +111111111111110000111110000E001 +1111111111111100001111111111111 +1111111111111111111111111111111 @@ -6,13 +6,41 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 17:58:23 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 11:58:52 by dkaiser ### ########.fr */ +/* Updated: 2024/05/11 16:00:17 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ +#include "MLX42/MLX42.h" #include "libft.h" #include "so_long.h" +int draw_map(t_game *game) +{ + int x; + int y; + mlx_texture_t *wall_texture; + mlx_image_t *wall_image; + + wall_texture = mlx_load_png("textures/wall.png"); + wall_image = mlx_texture_to_image(game->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; @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 13:05:13 by dkaiser ### ########.fr */ +/* Updated: 2024/05/11 16:23:54 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,23 +14,16 @@ #include "so_long.h" static void init_hooks(t_game *game); -static void init_player(t_player *player, int x, int y); +static void init_player(t_game *game); int init(t_game *game) { - mlx_texture_t *texture; game->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); } @@ -6,16 +6,21 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 14:14:13 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 10:53:37 by dkaiser ### ########.fr */ +/* Updated: 2024/05/11 15:58:31 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -int main(void) +int main(int argc, char *argv[]) { t_game game; + if (argc != 2) + return (1); + if (load_map_from_file(&game.map, argv[1])) + return (1); init(&game); + draw_map(&game); mlx_loop(game.mlx); } diff --git a/src/tilemap.c b/src/tilemap.c new file mode 100644 index 0000000..ef830c7 --- /dev/null +++ b/src/tilemap.c @@ -0,0 +1,128 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tilemap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 14:47:01 by dkaiser #+# #+# */ +/* Updated: 2024/05/11 16:19:50 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" +#include "libft.h" +#include "so_long.h" + +static t_ivector get_map_size_from_file(int fd); +static int load_tiles_from_file(t_tilemap *tilemap, int fd); +static int load_tiles_from_line(t_tilemap *tilemap, int l, char *line); +static int get_line_len(char *line); + +int load_map_from_file(t_tilemap *tilemap, char *filename) +{ + int fd; + + fd = open(filename, O_RDONLY); + if (fd < 0) + return (1); + tilemap->grid_size = get_map_size_from_file(fd); + close(fd); + if (tilemap->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); +} + +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 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); + } + return (0); +} + +static int load_tiles_from_line(t_tilemap *tilemap, int l, char *line) +{ + t_ivector pos; + static int found_player_start; + static int found_exit; + + pos.x = 0; + pos.y = l; + while (line[pos.x] && line[pos.x] != '\n') + { + if (line[pos.x] != '0' && line[pos.x] != '1' && line[pos.x] != 'C' + && line[pos.x] != 'P' && line[pos.x] != 'E') + return (1); + 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); +} + +static int get_line_len(char *line) +{ + int len; + + len = 0; + while (line[len] && line[len] != '\n') + len++; + return (len); +} diff --git a/textures/wall.png b/textures/wall.png Binary files differnew file mode 100644 index 0000000..358cdfb --- /dev/null +++ b/textures/wall.png |
