summaryrefslogtreecommitdiff
path: root/map_draw.c
diff options
context:
space:
mode:
Diffstat (limited to 'map_draw.c')
-rw-r--r--map_draw.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/map_draw.c b/map_draw.c
new file mode 100644
index 0000000..3c43203
--- /dev/null
+++ b/map_draw.c
@@ -0,0 +1,70 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* map_draw.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2022/10/10 19:43:45 by kdx #+# #+# */
+/* Updated: 2022/10/12 07:09:49 by kdx ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "map.h"
+#include "sily.h"
+#include "game.h"
+
+static char map_get_grid(t_map *map, int x, int y)
+{
+ return (map_get(map, x * TSIZE, y * TSIZE));
+}
+
+static char wall_spr(t_map *map, int x, int y)
+{
+ char spr;
+
+ spr = 0;
+ spr |= (x <= 0 || map_get_grid(map, x - 1, y) == TILE_WALL)
+ << 0;
+ spr |= (y <= 0 || map_get_grid(map, x, y - 1) == TILE_WALL)
+ << 1;
+ spr |= (x + 1 >= (int)map->width || map_get_grid(map, x + 1, y)
+ == TILE_WALL)
+ << 2;
+ spr |= (y + 1 >= (int)map->height || map_get_grid(map, x, y + 1)
+ == TILE_WALL)
+ << 3;
+ return (spr + 'a');
+}
+
+void map_draw_tile(t_sily *sily, t_map *map, int x, int y)
+{
+ unsigned char tile;
+
+ x /= TSIZE;
+ y /= TSIZE;
+ tile = map_get_grid(map, x, y);
+ if (tile == TILE_WALL)
+ tile = wall_spr(map, x, y);
+ sprite_draw(sily, sily->game->s_tiles[tile], x * TSIZE, y * TSIZE);
+}
+
+void map_draw(t_sily *sily, t_map *map)
+{
+ size_t x;
+ size_t y;
+ unsigned char tile;
+
+ y = -1;
+ while (++y < map->height)
+ {
+ x = -1;
+ while (++x < map->width)
+ {
+ tile = map_get_grid(map, x, y);
+ if (tile == TILE_WALL)
+ tile = wall_spr(map, x, y);
+ sprite_draw(sily, sily->game->s_tiles[tile], x * TSIZE, y * TSIZE);
+ }
+ }
+}