summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonAki <konaki@anokonaki.org>2026-10-15 14:57:24 +0800
committerKonAki <konaki@anokonaki.org>2026-10-15 14:57:24 +0800
commit2211a7633fef48a0affcd27fe309ce1c26a06b04 (patch)
tree9c9fa6d68e1fa1e2c5e029f222ef0ec11fcd89e1
parenta971a6688ebdfc5fb4a21f2795546fd1cae8cc92 (diff)
add rmdraw: draw rpg maker maps
-rw-r--r--kon/konrmdraw.c118
-rw-r--r--kon/konrmdraw.h58
2 files changed, 176 insertions, 0 deletions
diff --git a/kon/konrmdraw.c b/kon/konrmdraw.c
new file mode 100644
index 0000000..37efef1
--- /dev/null
+++ b/kon/konrmdraw.c
@@ -0,0 +1,118 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * konrmdraw.c
+ *
+ * Copyright (c) 2026 KonAki
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "konrmdraw.h"
+
+#define SHADOW_COLOR 0x80000000u
+
+/*** helpers ***/
+
+static int floorDiv(int a, int b)
+{
+ int q = a / b;
+
+ return (a % b != 0 && (a < 0) != (b < 0)) ? q - 1 : q;
+}
+
+int kon_rmTileSize(const kon_rmSheets_t *sheets)
+{
+ int sheet, columns, rows;
+
+ if (!sheets) return 0;
+ for (sheet = 0; sheet < KON_RM_SHEETS; sheet++) {
+ const kon_image *image = sheets->image[sheet];
+
+ if (image && kon_rmSheetSize(sheet, &columns, &rows) && image->width >= columns) return image->width / columns * 2;
+ }
+ return 0;
+}
+
+static void drawTile(kon_framebuffer_t *fb, const kon_rmSheets_t *sheets, int tileId, int frame, int x, int y)
+{
+ kon_rmQuad_t quads[4];
+ int n = kon_rmTileQuads(tileId, frame, quads), i;
+
+ for (i = 0; i < n; i++) {
+ const kon_image *image = sheets->image[quads[i].sheet];
+ int columns, rows, half;
+ kon_rect_t src;
+
+ if (!image || !kon_rmSheetSize(quads[i].sheet, &columns, &rows)) continue;
+ half = image->width / columns;
+ if (half < 1) continue;
+ src.x = quads[i].sx * half;
+ src.y = quads[i].sy * half;
+ src.w = quads[i].w * half;
+ src.h = quads[i].h * half;
+ kon_drawImageRegion(fb, x + quads[i].dx * half, y + quads[i].dy * half, image, src);
+ }
+}
+
+static void drawShadow(kon_framebuffer_t *fb, int size, int bits, int x, int y)
+{
+ int i, half = size / 2;
+
+ for (i = 0; i < 4; i++) {
+ if (bits & (1 << i)) kon_fillRectangle(fb, x + (i % 2) * half, y + (i / 2) * half, half, half, SHADOW_COLOR);
+ }
+}
+
+/*** drawing ***/
+
+void kon_rmDrawTiles(kon_framebuffer_t *fb, const kon_rmMap_t *map, const kon_rmTileset_t *tileset, const kon_rmSheets_t *sheets, int frame, int cameraX, int cameraY, int pass)
+{
+ int size, firstColumn, lastColumn, firstRow, lastRow, cx, cy;
+
+ if (!fb || !map || !sheets || map->width < 1 || map->height < 1) return;
+ size = kon_rmTileSize(sheets);
+ if (size < 2) return;
+
+ firstColumn = floorDiv(cameraX, size);
+ lastColumn = floorDiv(cameraX + fb->width - 1, size);
+ firstRow = floorDiv(cameraY, size);
+ lastRow = floorDiv(cameraY + fb->height - 1, size);
+
+ for (cy = firstRow; cy <= lastRow; cy++) {
+ for (cx = firstColumn; cx <= lastColumn; cx++) {
+ int mx = kon_rmRoundX(map, cx), my = kon_rmRoundY(map, cy);
+ int x = cx * size - cameraX, y = cy * size - cameraY;
+ int tiles[4], layer;
+
+ if (mx < 0 || my < 0 || mx >= map->width || my >= map->height) continue;
+ for (layer = 0; layer < 4; layer++) tiles[layer] = kon_rmTile(map, layer, mx, my);
+
+ if (pass == 0) {
+ for (layer = 0; layer < 4; layer++) {
+ if (layer == 2) drawShadow(fb, size, kon_rmTile(map, 4, mx, my), x, y);
+ if (tiles[layer] > 0 && !kon_rmTileIsHigher(kon_rmFlags(tileset, tiles[layer]))) drawTile(fb, sheets, tiles[layer], frame, x, y);
+ }
+ } else {
+ for (layer = 0; layer < 4; layer++) {
+ if (tiles[layer] > 0 && kon_rmTileIsHigher(kon_rmFlags(tileset, tiles[layer]))) drawTile(fb, sheets, tiles[layer], frame, x, y);
+ }
+ }
+ }
+ }
+}
diff --git a/kon/konrmdraw.h b/kon/konrmdraw.h
new file mode 100644
index 0000000..dc3a304
--- /dev/null
+++ b/kon/konrmdraw.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * konrmdraw.h
+ *
+ * Copyright (c) 2026 KonAki
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef KONRMDRAW_H
+#define KONRMDRAW_H
+
+#include "konsofren.h"
+#include "konrmmap.h"
+#include "konrmtile.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*** definition ***/
+
+/* draws the tiles of an RPG Maker map the way RPG Maker lays them out: the ground layers, the shadows, the upper layers, and the
+ tiles marked with a star as a second pass for after the characters are drawn. you load the tileset's pictures yourself, in the
+ order of its names (see konrmtile.h), and keep them for as long as you draw. a picture that is NULL leaves its tiles out */
+typedef struct kon_rmSheets {
+ const kon_image *image[KON_RM_SHEETS];
+} kon_rmSheets_t;
+
+/* cameraX and cameraY are the map pixel at the top left of fb. tiles are drawn at the size they have in the pictures, which is 48
+ pixels for most games, so the camera counts in those too. a map that loops (its scroll type) is drawn around its edges.
+ pass 0 draws everything that goes under the characters, pass 1 what goes over them. frame changes the water and waterfalls,
+ count it up once every half second (30 frames of the game) */
+void kon_rmDrawTiles(kon_framebuffer_t *fb, const kon_rmMap_t *map, const kon_rmTileset_t *tileset, const kon_rmSheets_t *sheets, int frame, int cameraX, int cameraY, int pass);
+
+/* the size of a tile in the pictures, 48 for the usual ones, or 0 when none of the pictures is there */
+int kon_rmTileSize(const kon_rmSheets_t *sheets);
+
+#ifdef __cplusplus
+}
+#endif
+#endif