1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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);
}
}
}
}
}
|