]> git.dkaiser.de - 42/so_long.git/commitdiff
Add tilemap.c and read map size
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Fri, 10 May 2024 13:53:31 +0000 (15:53 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Fri, 10 May 2024 13:53:31 +0000 (15:53 +0200)
Makefile
include/so_long.h
src/tilemap.c [new file with mode: 0644]

index 6f4e2c80fc33e48d99f8f2f35f7791e9a8e842f1..763c89798b5ff8ccc65a26c6098ab0695976b463 100644 (file)
--- 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))
index d66e3552c3b43d7c8ca0d0ba3bc232f9e957d326..ce481ef19aaab1107602ca4aac3f73a214858a1e 100644 (file)
@@ -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/10 14:55:46 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #ifndef SO_LONG_H
 # define SO_LONG_H
 
-# define PLAYER_MOVE_SPEED 250
 # include "MLX42/MLX42.h"
 # include "libft.h"
+# include "unistd.h"
+# include "fcntl.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,6 +58,13 @@ 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_tilemap;
+
 typedef struct s_game
 {
        mlx_t           *mlx;
@@ -49,7 +74,7 @@ typedef struct s_game
 }                              t_game;
 
 int                            init(t_game *game);
-void                   loop(void *game);
+void                   loop(void *params);
 int                            draw(t_game *game);
 void                   on_key_input(mlx_key_data_t event, void *params);
 
diff --git a/src/tilemap.c b/src/tilemap.c
new file mode 100644 (file)
index 0000000..c0c0524
--- /dev/null
@@ -0,0 +1,67 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   tilemap.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/05/10 14:47:01 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/05/10 15:51:50 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "get_next_line.h"
+#include "so_long.h"
+
+static t_ivector       get_map_size_from_file(int fd);
+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);
+       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);
+}