From 04af7fbd7891b4be883be8231d8fc0ae11a763e7 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 13 Jun 2023 23:29:50 +0100 Subject: Map renderer --- data/stage.json | 45 ++++++++++----------------------------------- data/tiles.png | Bin 1038 -> 7976 bytes src/Makefile | 8 +++++++- src/data.h | 2 ++ src/main.c | 4 +++- src/map.c | 33 +++++++++++++++++++++++++++++++++ src/map.h | 17 +++++++++++++++++ 7 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 src/map.c create mode 100644 src/map.h diff --git a/data/stage.json b/data/stage.json index 152712b..7e181c0 100644 --- a/data/stage.json +++ b/data/stage.json @@ -3,30 +3,18 @@ "infinite":false, "layers":[ { - "data":[2, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 3, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164], - "height":23, - "id":2, - "name":"BG", - "opacity":1, - "type":"tilelayer", - "visible":true, - "width":20, - "x":0, - "y":0 - }, - { - "data":[14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 15, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 0, 0, 0, 0, 0, 0, 168, 14, 15, 0, 168, 0, 0, 0, 0, 168, 0, 0, 0, 0, 0, 0, 168, 0, 0, 0, 0, 15, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 15, 0, 0, 0, 0, 168, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 14, 180, 180, 180, 180, 180, 0, 0, 0, 0, 0, 0, 0, 18, 18, 18, 18, 18, 18, 14, 15, 163, 163, 163, 163, 167, 0, 0, 0, 0, 0, 0, 0, 1, 167, 1, 1, 1, 1, 15, 14, 0, 0, 0, 0, 166, 0, 0, 0, 0, 168, 0, 0, 0, 166, 0, 168, 0, 0, 14, 15, 0, 0, 168, 0, 166, 180, 180, 180, 0, 0, 0, 0, 0, 166, 18, 18, 18, 18, 15, 14, 0, 0, 0, 0, 166, 163, 167, 163, 0, 0, 0, 0, 0, 166, 1, 167, 1, 1, 14, 15, 0, 0, 0, 0, 166, 0, 166, 0, 0, 0, 0, 0, 0, 166, 0, 166, 0, 0, 15, 14, 0, 168, 0, 180, 180, 180, 180, 0, 0, 0, 168, 0, 18, 18, 18, 18, 0, 168, 14, 1, 0, 0, 0, 163, 163, 167, 163, 168, 0, 0, 0, 0, 1, 167, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 166, 0, 0, 0, 0, 0, 0, 0, 166, 0, 0, 0, 0, 0, 179, 18, 18, 18, 18, 18, 18, 0, 0, 0, 0, 0, 0, 18, 18, 18, 18, 18, 18, 179, 14, 1, 1, 167, 1, 1, 167, 0, 0, 0, 0, 0, 0, 1, 1, 1, 167, 1, 1, 14, 15, 0, 0, 166, 0, 0, 166, 0, 0, 0, 168, 0, 0, 0, 0, 0, 166, 0, 0, 15, 14, 18, 18, 18, 18, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 18, 18, 18, 18, 14, 15, 1, 1, 167, 1, 1, 167, 0, 168, 0, 0, 0, 0, 168, 0, 1, 167, 1, 1, 15, 14, 168, 0, 166, 0, 0, 166, 0, 0, 0, 0, 0, 0, 0, 0, 0, 166, 168, 0, 14, 15, 179, 179, 179, 179, 179, 179, 179, 0, 0, 0, 0, 0, 179, 179, 179, 179, 179, 179, 15, 14, 14, 14, 14, 14, 14, 14, 14, 27, 27, 27, 27, 189, 14, 14, 14, 14, 14, 14, 14], + "data":[41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 41, 42, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 41, 42, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 41, 42, 61, 62, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 5, 6, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 5, 6, 24, 61, 62, 41, 42, 3, 4, 5, 6, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 41, 42, 61, 62, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 5, 6, 24, 23, 24, 23, 24, 23, 24, 61, 62, 41, 42, 3, 4, 3, 4, 3, 4, 3, 4, 3, 5, 6, 4, 3, 4, 3, 4, 45, 45, 45, 45, 45, 45, 45, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 41, 42, 61, 62, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 1, 1, 1, 1, 1, 2, 1, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 61, 62, 41, 42, 3, 4, 3, 4, 3, 4, 45, 45, 45, 45, 45, 45, 45, 4, 3, 4, 3, 4, 3, 4, 3, 22, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 41, 42, 61, 62, 23, 24, 5, 6, 23, 24, 1, 2, 1, 1, 1, 2, 1, 24, 23, 24, 23, 24, 23, 24, 23, 45, 45, 45, 45, 45, 23, 24, 45, 45, 45, 45, 45, 45, 23, 24, 61, 62, 41, 42, 3, 4, 3, 4, 3, 4, 3, 22, 3, 4, 3, 22, 3, 4, 3, 4, 3, 4, 3, 4, 3, 1, 1, 1, 1, 2, 3, 4, 2, 1, 1, 1, 1, 1, 3, 4, 41, 42, 61, 62, 23, 24, 23, 24, 23, 24, 23, 22, 23, 24, 23, 22, 23, 24, 23, 24, 23, 5, 6, 24, 23, 24, 23, 24, 23, 22, 23, 24, 22, 24, 23, 24, 23, 24, 23, 24, 61, 62, 41, 42, 3, 4, 3, 4, 3, 45, 45, 45, 45, 45, 45, 45, 45, 45, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 45, 45, 45, 45, 45, 3, 4, 5, 6, 3, 4, 41, 42, 1, 1, 23, 24, 23, 24, 23, 1, 1, 2, 1, 1, 1, 1, 1, 1, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 1, 1, 1, 2, 1, 23, 24, 23, 24, 23, 24, 1, 1, 3, 4, 3, 4, 3, 4, 3, 4, 3, 22, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 22, 4, 3, 4, 3, 4, 3, 4, 3, 4, 63, 64, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 23, 24, 23, 24, 23, 24, 23, 24, 23, 5, 6, 24, 23, 24, 23, 24, 23, 24, 45, 45, 45, 45, 45, 45, 23, 24, 63, 64, 41, 42, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 1, 1, 2, 1, 1, 1, 3, 4, 41, 42, 61, 62, 5, 6, 23, 22, 23, 24, 23, 22, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 22, 24, 23, 24, 23, 24, 61, 62, 41, 42, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 3, 4, 3, 5, 6, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 41, 42, 61, 62, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 24, 23, 5, 6, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 61, 62, 41, 42, 3, 4, 3, 22, 3, 4, 3, 22, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 5, 6, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 22, 4, 5, 6, 3, 4, 41, 42, 41, 42, 43, 44, 43, 44, 43, 44, 43, 44, 43, 44, 43, 44, 43, 3, 4, 3, 4, 3, 4, 3, 4, 3, 43, 44, 43, 44, 43, 44, 43, 44, 43, 44, 43, 44, 43, 44, 41, 42, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 101, 101, 101, 101, 101, 101, 101, 101, 101, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62, 61, 62], "height":23, "id":1, "name":"Map", "opacity":1, "type":"tilelayer", "visible":true, - "width":20, + "width":40, "x":0, "y":0 }], - "nextlayerid":3, + "nextlayerid":2, "nextobjectid":1, "orientation":"orthogonal", "renderorder":"right-down", @@ -34,33 +22,20 @@ "tileheight":8, "tilesets":[ { - "columns":13, + "columns":20, "firstgid":1, "image":"tiles.png", "imageheight":48, - "imagewidth":208, + "imagewidth":160, "margin":0, - "name":"Default", - "spacing":0, - "tilecount":162, - "tileheight":8, - "tilewidth":16 - }, - { - "columns":13, - "firstgid":163, - "image":"tiles.png", - "imageheight":48, - "imagewidth":208, - "margin":0, - "name":"Default", + "name":"Map", "spacing":0, - "tilecount":78, + "tilecount":156, "tileheight":8, - "tilewidth":16 + "tilewidth":8 }], - "tilewidth":16, + "tilewidth":8, "type":"map", "version":"1.6", - "width":20 + "width":40 } \ No newline at end of file diff --git a/data/tiles.png b/data/tiles.png index f7cc4f2..80d8d0c 100644 Binary files a/data/tiles.png and b/data/tiles.png differ diff --git a/src/Makefile b/src/Makefile index 4852b93..50b1179 100644 --- a/src/Makefile +++ b/src/Makefile @@ -6,8 +6,11 @@ LDFLAGS := -s IMGS := $(wildcard ../data/*.png) IMG_OBJS := $(IMGS:../data/%.png=%.o) +MAPS := ../data/stage.json +MAP_OBJS := $(MAPS:../data/%.json=%.o) + SRCS := $(wildcard *.c) -OBJS := $(SRCS:.c=.o) $(IMG_OBJS) palette.o +OBJS := $(SRCS:.c=.o) $(IMG_OBJS) palette.o $(MAP_OBJS) all: $(BIN) @@ -23,6 +26,9 @@ palette.o: ../data/sprites.png $(IMG_OBJS): %.o: ../data/%.png ../tools/pngpix.py $< $@ +$(MAP_OBJS): %.o: ../data/%.json + ../tools/map.py $< $@ + clean: rm -f $(BIN) *.o Makefile.deps diff --git a/src/data.h b/src/data.h index d7f2f4c..8d4b2e3 100644 --- a/src/data.h +++ b/src/data.h @@ -7,4 +7,6 @@ extern const uint8_t binary_sprites_start[]; extern const uint8_t binary_tiles_start[]; extern const uint8_t binary_font_start[]; +extern const uint8_t binary_stage_start[]; + #endif /* _DATA_H */ diff --git a/src/main.c b/src/main.c index be248d3..9985159 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,7 @@ #include "keyb.h" #include "vga.h" #include "text.h" +#include "map.h" #include "data.h" /* disable paging because our int handlers are written in C */ @@ -36,7 +37,8 @@ int main(int argc, char *argv[]) blit_erase(0); wait_vsync(); - put_text(10, 10, "TESTING! 1234567890* ()^_"); + map_init(binary_stage_start); + map_render(); blit_update(); diff --git a/src/map.c b/src/map.c new file mode 100644 index 0000000..55fc470 --- /dev/null +++ b/src/map.c @@ -0,0 +1,33 @@ +#include + +#include "vga.h" +#include "data.h" + +#include "map.h" + +static const uint8_t *cmap; + +void map_init(const uint8_t map[]) +{ + cmap = map; +} + +void map_render() +{ + const uint8_t *tiles = binary_tiles_start; + Rect src = { 0, 0, 160, 48 }; + Rect dst = { 0, 0, 8, 8 }; + + for (uint8_t y = 0; y < MAP_H; y++) + for (uint8_t x = 0; x < MAP_W; x++) + { + dst.x = x * MAP_TILE_W; + dst.y = y * MAP_TILE_H + MAP_OFFS_Y; + + uint8_t t = cmap[x + y * MAP_W]; + src.x = (t % MAP_TILESET_COLS) * MAP_TILE_W; + src.y = (t / MAP_TILESET_COLS) * MAP_TILE_H; + + blitrc(binary_tiles_start, &src, &dst); + } +} diff --git a/src/map.h b/src/map.h new file mode 100644 index 0000000..7c08fe5 --- /dev/null +++ b/src/map.h @@ -0,0 +1,17 @@ +#ifndef _MAP_H +#define _MAP_H + +#define MAP_TILE_W 8 +#define MAP_TILE_H 8 + +#define MAP_W 40 +#define MAP_H 23 + +#define MAP_OFFS_Y 16 + +#define MAP_TILESET_COLS 20 + +void map_init(const uint8_t map[]); +void map_render(); + +#endif /* _MAP_H */ -- cgit v1.2.3