summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c8
-rw-r--r--src/tilemap.c51
2 files changed, 56 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index cf517b6..53fb23d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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);
}
diff --git a/src/tilemap.c b/src/tilemap.c
index c0c0524..45d9fa5 100644
--- a/src/tilemap.c
+++ b/src/tilemap.c
@@ -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;