summaryrefslogtreecommitdiff
path: root/kon/konrmdraw.c
diff options
context:
space:
mode:
Diffstat (limited to 'kon/konrmdraw.c')
-rw-r--r--kon/konrmdraw.c118
1 files changed, 118 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);
+ }
+ }
+ }
+ }
+}