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
|
/* SPDX-License-Identifier: MIT */
/*
* kontile.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 KONTILE_H
#define KONTILE_H
#include "konsofren.h"
#include "koncollide.h"
#ifdef __cplusplus
extern "C" {
#endif
/*** definition ***/
/* tiles holds columns * rows ids, 0 is empty and n draws tile n - 1 of the sheet, counted left to right then top to bottom */
typedef struct kon_tilemap {
int columns, rows;
int tileWidth, tileHeight;
uint16_t *tiles;
const kon_image *sheet;
int sheetColumns; /* 0 works it out from the sheet width */
} kon_tilemap_t;
/* 0 outside the map */
uint16_t kon_tilemapAt(const kon_tilemap_t *map, int column, int row);
/* does nothing outside the map */
void kon_tilemapSet(kon_tilemap_t *map, int column, int row, uint16_t tile);
/* works for negative positions too, returns 0 if the position is outside the map */
int kon_tilemapPixelToTile(const kon_tilemap_t *map, int x, int y, int *column, int *row);
/* solid is indexed by tile id, non-zero means solid, ids past solidCount are not solid. solid[0] decides what the empty tile
and everything outside the map counts as. returns 1 if any tile touching the box x, y, width, height (in map pixels) is solid */
int kon_tilemapBoxHits(const kon_tilemap_t *map, int x, int y, int width, int height, const uint8_t *solid, int solidCount);
/* which sides of the box ran into something in kon_tilemapMove */
#define KON_HIT_LEFT 1
#define KON_HIT_RIGHT 2
#define KON_HIT_UP 4
#define KON_HIT_DOWN 8
/* moves the box by dx, dy (in map pixels) and stops it against solid tiles, one axis after the other so it slides along walls.
solid works like in kon_tilemapBoxHits. returns the KON_HIT_ flags of what stopped it, down is the way y grows.
the box can start inside a solid tile, it is left alone on that axis. fast moves do not tunnel through tiles */
int kon_tilemapMove(const kon_tilemap_t *map, kon_box2_t *box, float dx, float dy, const uint8_t *solid, int solidCount);
/* cameraX, cameraY is the map pixel shown at the top left of fb */
void kon_drawTilemap(kon_framebuffer_t *fb, const kon_tilemap_t *map, int cameraX, int cameraY);
#ifdef __cplusplus
}
#endif
#endif
|