]> git.dkaiser.de - 42/so_long.git/commitdiff
Add tilemap loading
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Fri, 10 May 2024 15:39:41 +0000 (17:39 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Fri, 10 May 2024 15:39:41 +0000 (17:39 +0200)
include/so_long.h
src/main.c
src/tilemap.c

index ce481ef19aaab1107602ca4aac3f73a214858a1e..2333368c96ff4d25be038dde7588190c3666b537 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/08 14:14:02 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/10 14:55:46 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/10 17:28:23 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,9 +14,9 @@
 # define SO_LONG_H
 
 # include "MLX42/MLX42.h"
+# include "fcntl.h"
 # include "libft.h"
 # include "unistd.h"
-# include "fcntl.h"
 
 # define PLAYER_MOVE_SPEED 250
 
@@ -62,7 +62,7 @@ typedef struct s_tilemap
 {
        t_ivector       grid_size;
        t_ivector       tile_size;
-       char            **tiles;
+       char            *tiles;
 }                              t_tilemap;
 
 typedef struct s_game
@@ -71,8 +71,11 @@ typedef struct s_game
        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 *params);
 int                            draw(t_game *game);
index cf517b6c7a35da7d666650e81e5780040140fae4..53fb23df05114f53786fbf95f2c8219139525ddf 100644 (file)
@@ -6,16 +6,20 @@
 /*   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/10 17:30:38 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);
        mlx_loop(game.mlx);
 }
index c0c05248f3b0c293443bfddf7c9565080dc0ee7e..45d9fa545c7c038992a3074a2ae8f8fcc60015e1 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
+/*   Updated: 2024/05/10 17:26:17 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,6 +14,8 @@
 #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)
@@ -27,6 +29,15 @@ int  load_map_from_file(t_tilemap *tilemap, char *filename)
        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);
 }
 
@@ -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;