diff options
Diffstat (limited to 'kon/kontile.h')
| -rw-r--r-- | kon/kontile.h | 80 |
1 files changed, 0 insertions, 80 deletions
diff --git a/kon/kontile.h b/kon/kontile.h index d416d34..f671da9 100644 --- a/kon/kontile.h +++ b/kon/kontile.h @@ -55,84 +55,4 @@ int kon_tilemapBoxHits(const kon_tilemap_t *map, int x, int y, int width, int he /* 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); -/*** implementation ***/ - -#ifdef KONTILE_IMPLEMENTATION - -static int kon_floorDiv_(int a, int b) { - int q = a / b; - return (a % b != 0 && (a < 0) != (b < 0)) ? q - 1 : q; -} - -uint16_t kon_tilemapAt(const kon_tilemap_t *map, int column, int row) { - if (!map || !map->tiles) return 0; - if (column < 0 || row < 0 || column >= map->columns || row >= map->rows) return 0; - return map->tiles[(size_t)row * (size_t)map->columns + (size_t)column]; -} - -void kon_tilemapSet(kon_tilemap_t *map, int column, int row, uint16_t tile) { - if (!map || !map->tiles) return; - if (column < 0 || row < 0 || column >= map->columns || row >= map->rows) return; - map->tiles[(size_t)row * (size_t)map->columns + (size_t)column] = tile; -} - -int kon_tilemapPixelToTile(const kon_tilemap_t *map, int x, int y, int *column, int *row) { - if (!map || map->tileWidth <= 0 || map->tileHeight <= 0) return 0; - - int c = kon_floorDiv_(x, map->tileWidth); - int r = kon_floorDiv_(y, map->tileHeight); - if (column) *column = c; - if (row) *row = r; - - return c >= 0 && r >= 0 && c < map->columns && r < map->rows; -} - -int kon_tilemapBoxHits(const kon_tilemap_t *map, int x, int y, int width, int height, const uint8_t *solid, int solidCount) { - if (!map || !solid || solidCount <= 0 || map->tileWidth <= 0 || map->tileHeight <= 0 || width <= 0 || height <= 0) return 0; - - int firstColumn = kon_floorDiv_(x, map->tileWidth); - int firstRow = kon_floorDiv_(y, map->tileHeight); - int lastColumn = kon_floorDiv_(x + width - 1, map->tileWidth); - int lastRow = kon_floorDiv_(y + height - 1, map->tileHeight); - - for (int row = firstRow; row <= lastRow; row++) { - for (int column = firstColumn; column <= lastColumn; column++) { - int tile = kon_tilemapAt(map, column, row); - if (tile < solidCount && solid[tile]) return 1; - } - } - return 0; -} - -void kon_drawTilemap(kon_framebuffer_t *fb, const kon_tilemap_t *map, int cameraX, int cameraY) { - if (!fb || !map || !map->sheet || !map->tiles) return; - if (map->tileWidth <= 0 || map->tileHeight <= 0) return; - - int sheetColumns = map->sheetColumns > 0 ? map->sheetColumns : map->sheet->width / map->tileWidth; - if (sheetColumns <= 0) return; - - int firstColumn = kon_floorDiv_(cameraX, map->tileWidth); - int firstRow = kon_floorDiv_(cameraY, map->tileHeight); - int lastColumn = kon_floorDiv_(cameraX + fb->width - 1, map->tileWidth); - int lastRow = kon_floorDiv_(cameraY + fb->height - 1, map->tileHeight); - - if (firstColumn < 0) firstColumn = 0; - if (firstRow < 0) firstRow = 0; - if (lastColumn > map->columns - 1) lastColumn = map->columns - 1; - if (lastRow > map->rows - 1) lastRow = map->rows - 1; - - for (int row = firstRow; row <= lastRow; row++) { - for (int column = firstColumn; column <= lastColumn; column++) { - int tile = map->tiles[(size_t)row * (size_t)map->columns + (size_t)column]; - if (tile == 0) continue; - tile--; - - kon_rect_t src = KON_RECT((tile % sheetColumns) * map->tileWidth, (tile / sheetColumns) * map->tileHeight, map->tileWidth, map->tileHeight); - kon_drawImageRegion(fb, column * map->tileWidth - cameraX, row * map->tileHeight - cameraY, map->sheet, src); - } - } -} - -#endif /* KONTILE_IMPLEMENTATION */ - #endif |
