From fa155d4ffd7a348900e72bdc7ce8e000b22703db Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sat, 23 Sep 2023 12:22:25 +0100 Subject: [PATCH] Dungeon tiles: Port over some cleanups from #6636 --- Source/engine.h | 3 - Source/engine/render/dun_render.cpp | 11 +-- Source/engine/render/dun_render.hpp | 99 +------------------------ Source/levels/dun_tile.hpp | 107 ++++++++++++++++++++++++++++ Source/levels/gendung.cpp | 2 +- Source/levels/gendung.h | 4 +- 6 files changed, 119 insertions(+), 107 deletions(-) create mode 100644 Source/levels/dun_tile.hpp diff --git a/Source/engine.h b/Source/engine.h index b84dff4ed..5f4ffcfae 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -38,9 +38,6 @@ #include "engine/surface.hpp" #include "utils/attributes.h" -#define TILE_WIDTH 64 -#define TILE_HEIGHT 32 - namespace devilution { template diff --git a/Source/engine/render/dun_render.cpp b/Source/engine/render/dun_render.cpp index 56b50c1e9..e2c1b2936 100644 --- a/Source/engine/render/dun_render.cpp +++ b/Source/engine/render/dun_render.cpp @@ -19,6 +19,7 @@ #include #include "engine/render/blit_impl.hpp" +#include "levels/dun_tile.hpp" #include "lighting.h" #include "options.h" #include "utils/attributes.h" @@ -34,19 +35,19 @@ namespace devilution { namespace { /** Width of a tile rendering primitive. */ -constexpr int_fast16_t Width = TILE_WIDTH / 2; +constexpr int_fast16_t Width = DunFrameWidth; /** Height of a tile rendering primitive (except triangles). */ -constexpr int_fast16_t Height = TILE_HEIGHT; +constexpr int_fast16_t Height = DunFrameHeight; /** Height of the lower triangle of a triangular or a trapezoid tile. */ -constexpr int_fast16_t LowerHeight = TILE_HEIGHT / 2; +constexpr int_fast16_t LowerHeight = DunFrameHeight / 2; /** Height of the upper triangle of a triangular tile. */ -constexpr int_fast16_t TriangleUpperHeight = TILE_HEIGHT / 2 - 1; +constexpr int_fast16_t TriangleUpperHeight = DunFrameHeight / 2 - 1; /** Height of the upper rectangle of a trapezoid tile. */ -constexpr int_fast16_t TrapezoidUpperHeight = TILE_HEIGHT / 2; +constexpr int_fast16_t TrapezoidUpperHeight = DunFrameHeight / 2; constexpr int_fast16_t TriangleHeight = LowerHeight + TriangleUpperHeight; diff --git a/Source/engine/render/dun_render.hpp b/Source/engine/render/dun_render.hpp index d61c35145..88d4ed0c5 100644 --- a/Source/engine/render/dun_render.hpp +++ b/Source/engine/render/dun_render.hpp @@ -9,7 +9,9 @@ #include -#include "engine.h" +#include "engine/point.hpp" +#include "engine/surface.hpp" +#include "levels/dun_tile.hpp" // #define DUN_RENDER_STATS #ifdef DUN_RENDER_STATS @@ -18,72 +20,6 @@ namespace devilution { -/** - * Level tile type. - * - * The tile type determines data encoding and the shape. - * - * Each tile type has its own encoding but they all encode data in the order - * of bottom-to-top (bottom row first). - */ -enum class TileType : uint8_t { - /** - * 🮆 A 32x32 square. Stored as an array of pixels. - */ - Square, - - /** - * 🮆 A 32x32 square with transparency. RLE encoded. - * - * Each run starts with an int8_t value. - * If positive, it is followed by this many pixels. - * If negative, it indicates `-value` fully transparent pixels, which are omitted. - * - * Runs do not cross row boundaries. - */ - TransparentSquare, - - /** - *🭮 Left-pointing 32x31 triangle. Encoded as 31 varying-width rows with 2 padding bytes before every even row. - * - * The smallest rows (bottom and top) are 2px wide, the largest row is 16px wide (middle row). - * - * Encoding: - * for i in [0, 30]: - * - 2 unused bytes if i is even - * - row (only the pixels within the triangle) - */ - LeftTriangle, - - /** - * 🭬Right-pointing 32x31 triangle. Encoded as 31 varying-width rows with 2 padding bytes after every even row. - * - * The smallest rows (bottom and top) are 2px wide, the largest row is 16px wide (middle row). - * - * Encoding: - * for i in [0, 30]: - * - row (only the pixels within the triangle) - * - 2 unused bytes if i is even - */ - RightTriangle, - - /** - * 🭓 Left-pointing 32x32 trapezoid: a 32x16 rectangle and the 16x16 bottom part of `LeftTriangle`. - * - * Begins with triangle part, which uses the `LeftTriangle` encoding, - * and is followed by a flat array of pixels for the top rectangle part. - */ - LeftTrapezoid, - - /** - * 🭞 Right-pointing 32x32 trapezoid: 32x16 rectangle and the 16x16 bottom part of `RightTriangle`. - * - * Begins with the triangle part, which uses the `RightTriangle` encoding, - * and is followed by a flat array of pixels for the top rectangle part. - */ - RightTrapezoid, -}; - /** * @brief Specifies the mask to use for rendering. */ @@ -205,35 +141,6 @@ enum class MaskType : uint8_t { LeftFoliage, }; -/** - * Specifies the current MIN block of the level CEL file, as used during rendering of the level tiles. - */ -class LevelCelBlock { -public: - explicit LevelCelBlock(uint16_t data) - : data_(data) - { - } - - [[nodiscard]] bool hasValue() const - { - return data_ != 0; - } - - [[nodiscard]] TileType type() const - { - return static_cast((data_ & 0x7000) >> 12); - } - - [[nodiscard]] uint16_t frame() const - { - return data_ & 0xFFF; - } - -private: - uint16_t data_; -}; - #ifdef DUN_RENDER_STATS struct DunRenderType { TileType tileType; diff --git a/Source/levels/dun_tile.hpp b/Source/levels/dun_tile.hpp new file mode 100644 index 000000000..049830584 --- /dev/null +++ b/Source/levels/dun_tile.hpp @@ -0,0 +1,107 @@ +#pragma once + +#include + +#define TILE_WIDTH 64 +#define TILE_HEIGHT 32 + +namespace devilution { + +/** + * Level tile type. + * + * The tile type determines data encoding and the shape. + * + * Each tile type has its own encoding but they all encode data in the order + * of bottom-to-top (bottom row first). + */ +enum class TileType : uint8_t { + /** + * 🮆 A 32x32 square. Stored as an array of pixels. + */ + Square, + + /** + * 🮆 A 32x32 square with transparency. RLE encoded. + * + * Each run starts with an int8_t value. + * If positive, it is followed by this many pixels. + * If negative, it indicates `-value` fully transparent pixels, which are omitted. + * + * Runs do not cross row boundaries. + */ + TransparentSquare, + + /** + *🭮 Left-pointing 32x31 triangle. Encoded as 31 varying-width rows with 2 padding bytes before every even row. + * + * The smallest rows (bottom and top) are 2px wide, the largest row is 16px wide (middle row). + * + * Encoding: + * for i in [0, 30]: + * - 2 unused bytes if i is even + * - row (only the pixels within the triangle) + */ + LeftTriangle, + + /** + * 🭬Right-pointing 32x31 triangle. Encoded as 31 varying-width rows with 2 padding bytes after every even row. + * + * The smallest rows (bottom and top) are 2px wide, the largest row is 16px wide (middle row). + * + * Encoding: + * for i in [0, 30]: + * - row (only the pixels within the triangle) + * - 2 unused bytes if i is even + */ + RightTriangle, + + /** + * 🭓 Left-pointing 32x32 trapezoid: a 32x16 rectangle and the 16x16 bottom part of `LeftTriangle`. + * + * Begins with triangle part, which uses the `LeftTriangle` encoding, + * and is followed by a flat array of pixels for the top rectangle part. + */ + LeftTrapezoid, + + /** + * 🭞 Right-pointing 32x32 trapezoid: 32x16 rectangle and the 16x16 bottom part of `RightTriangle`. + * + * Begins with the triangle part, which uses the `RightTriangle` encoding, + * and is followed by a flat array of pixels for the top rectangle part. + */ + RightTrapezoid, +}; + +/** + * Specifies the current MIN block of the level CEL file, as used during rendering of the level tiles. + */ +struct LevelCelBlock { + uint16_t data; + + [[nodiscard]] bool hasValue() const + { + return data != 0; + } + + [[nodiscard]] TileType type() const + { + return static_cast((data & 0x7000) >> 12); + } + + /** + * @brief Returns the 1-based index of the frame in `pDungeonCels`. + */ + [[nodiscard]] uint16_t frame() const + { + return data & 0xFFF; + } +}; + +/** Width of a tile rendering primitive. */ +constexpr int_fast16_t DunFrameWidth = TILE_WIDTH / 2; + +/** Height of a tile rendering primitive (except triangles). */ +constexpr int_fast16_t DunFrameHeight = TILE_HEIGHT; + +} // namespace devilution diff --git a/Source/levels/gendung.cpp b/Source/levels/gendung.cpp index d781a31d7..a899572b1 100644 --- a/Source/levels/gendung.cpp +++ b/Source/levels/gendung.cpp @@ -494,7 +494,7 @@ void SetDungeonMicros() for (size_t i = 0; i < tileCount / blocks; i++) { uint16_t *pieces = &levelPieces[blocks * i]; for (size_t block = 0; block < blocks; block++) { - DPieceMicros[i].mt[block] = SDL_SwapLE16(pieces[blocks - 2 + (block & 1) - (block & 0xE)]); + DPieceMicros[i].mt[block] = LevelCelBlock { SDL_SwapLE16(pieces[blocks - 2 + (block & 1) - (block & 0xE)]) }; } } } diff --git a/Source/levels/gendung.h b/Source/levels/gendung.h index 4dd5d0dda..126036af0 100644 --- a/Source/levels/gendung.h +++ b/Source/levels/gendung.h @@ -9,12 +9,12 @@ #include #include -#include "engine.h" #include "engine/clx_sprite.hpp" #include "engine/point.hpp" #include "engine/rectangle.hpp" #include "engine/render/scrollrt.h" #include "engine/world_tile.hpp" +#include "levels/dun_tile.hpp" #include "utils/attributes.h" #include "utils/bitset2d.hpp" #include "utils/enum_traits.h" @@ -134,7 +134,7 @@ struct MegaTile { }; struct MICROS { - uint16_t mt[16]; + LevelCelBlock mt[16]; }; struct ShadowStruct {