From 14218080b667e2572d20cd94b34ff0f409e4f978 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Mon, 21 Jun 2021 09:57:16 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=9A=20engine.h:=20Extract=20`CelGetFra?= =?UTF-8?q?me`=20and=20friends?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/engine.h | 42 ---------------------- Source/engine/cel_header.hpp | 55 +++++++++++++++++++++++++++++ Source/engine/render/cel_render.cpp | 1 + Source/engine/render/cl2_render.cpp | 1 + Source/missiles.cpp | 1 + Source/monster.cpp | 1 + Source/player.cpp | 1 + Source/towners.cpp | 1 + 8 files changed, 61 insertions(+), 42 deletions(-) create mode 100644 Source/engine/cel_header.hpp diff --git a/Source/engine.h b/Source/engine.h index cb48b5a28..d4009aa5d 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -37,7 +37,6 @@ #include "engine/point.hpp" #include "engine/size.hpp" #include "miniwin/miniwin.h" -#include "utils/endian.hpp" #include "utils/stdcompat/cstddef.hpp" #define TILE_WIDTH 64 @@ -45,47 +44,6 @@ namespace devilution { -inline byte *CelGetFrameStart(byte *pCelBuff, int nCel) -{ - const uint32_t *pFrameTable = reinterpret_cast(pCelBuff); - - return &pCelBuff[SDL_SwapLE32(pFrameTable[nCel])]; -} - -inline byte *CelGetFrame(byte *pCelBuff, int nCel, int *nDataSize) -{ - const uint32_t nCellStart = LoadLE32(&pCelBuff[nCel * sizeof(std::uint32_t)]); - *nDataSize = LoadLE32(&pCelBuff[(nCel + 1) * sizeof(std::uint32_t)]) - nCellStart; - return &pCelBuff[nCellStart]; -} - -inline const byte *CelGetFrame(const byte *pCelBuff, int nCel, int *nDataSize) -{ - const uint32_t nCellStart = LoadLE32(&pCelBuff[nCel * sizeof(std::uint32_t)]); - *nDataSize = LoadLE32(&pCelBuff[(nCel + 1) * sizeof(std::uint32_t)]) - nCellStart; - return &pCelBuff[nCellStart]; -} - -struct FrameHeader { - uint16_t row0; - uint16_t row32; - uint16_t row64; - uint16_t row96; - uint16_t row128; -}; - -inline const byte *CelGetFrameClipped(const byte *pCelBuff, int nCel, int *nDataSize) -{ - const byte *pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize); - - FrameHeader frameHeader; - memcpy(&frameHeader, pRLEBytes, sizeof(FrameHeader)); - - uint16_t nDataStart = SDL_SwapLE16(frameHeader.row0); - *nDataSize -= nDataStart; - - return &pRLEBytes[nDataStart]; -} struct CelOutputBuffer { // 8-bit palletized surface. diff --git a/Source/engine/cel_header.hpp b/Source/engine/cel_header.hpp new file mode 100644 index 000000000..9701655b4 --- /dev/null +++ b/Source/engine/cel_header.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include +#include + +#include + +#include "utils/endian.hpp" +#include "utils/stdcompat/cstddef.hpp" + +namespace devilution { + +inline byte *CelGetFrameStart(byte *pCelBuff, int nCel) +{ + const auto *pFrameTable = reinterpret_cast(pCelBuff); + + return &pCelBuff[SDL_SwapLE32(pFrameTable[nCel])]; +} + +inline byte *CelGetFrame(byte *pCelBuff, int nCel, int *nDataSize) +{ + const std::uint32_t nCellStart = LoadLE32(&pCelBuff[nCel * sizeof(std::uint32_t)]); + *nDataSize = static_cast(LoadLE32(&pCelBuff[(nCel + 1) * sizeof(std::uint32_t)]) - nCellStart); + return &pCelBuff[nCellStart]; +} + +inline const byte *CelGetFrame(const byte *pCelBuff, int nCel, int *nDataSize) +{ + const std::uint32_t nCellStart = LoadLE32(&pCelBuff[nCel * sizeof(std::uint32_t)]); + *nDataSize = static_cast(LoadLE32(&pCelBuff[(nCel + 1) * sizeof(std::uint32_t)]) - nCellStart); + return &pCelBuff[nCellStart]; +} + +struct FrameHeader { + uint16_t row0; + uint16_t row32; + uint16_t row64; + uint16_t row96; + uint16_t row128; +}; + +inline const byte *CelGetFrameClipped(const byte *pCelBuff, int nCel, int *nDataSize) +{ + const byte *pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize); + + FrameHeader frameHeader; + std::memcpy(&frameHeader, pRLEBytes, sizeof(FrameHeader)); + + std::uint16_t nDataStart = SDL_SwapLE16(frameHeader.row0); + *nDataSize -= nDataStart; + + return &pRLEBytes[nDataStart]; +} + +} // namespace devilution diff --git a/Source/engine/render/cel_render.cpp b/Source/engine/render/cel_render.cpp index f1292dcd4..24c22c9e4 100644 --- a/Source/engine/render/cel_render.cpp +++ b/Source/engine/render/cel_render.cpp @@ -8,6 +8,7 @@ #include #include +#include "engine/cel_header.hpp" #include "engine/render/common_impl.h" #include "options.h" #include "palette.h" diff --git a/Source/engine/render/cl2_render.cpp b/Source/engine/render/cl2_render.cpp index db7d67205..bcfc953b0 100644 --- a/Source/engine/render/cl2_render.cpp +++ b/Source/engine/render/cl2_render.cpp @@ -7,6 +7,7 @@ #include +#include "engine/cel_header.hpp" #include "engine/render/common_impl.h" #include "scrollrt.h" #include "utils/attributes.h" diff --git a/Source/missiles.cpp b/Source/missiles.cpp index 214d29cf8..05220c1bb 100644 --- a/Source/missiles.cpp +++ b/Source/missiles.cpp @@ -10,6 +10,7 @@ #include "control.h" #include "cursor.h" #include "dead.h" +#include "engine/cel_header.hpp" #include "engine/load_file.hpp" #include "init.h" #include "inv.h" diff --git a/Source/monster.cpp b/Source/monster.cpp index 0cb796901..90c8c933b 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -16,6 +16,7 @@ #include "dead.h" #include "drlg_l1.h" #include "drlg_l4.h" +#include "engine/cel_header.hpp" #include "engine/load_file.hpp" #include "engine/render/cl2_render.hpp" #include "init.h" diff --git a/Source/player.cpp b/Source/player.cpp index 7691aa8c6..098cf2329 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -9,6 +9,7 @@ #include "control.h" #include "cursor.h" #include "dead.h" +#include "engine/cel_header.hpp" #include "engine/load_file.hpp" #include "gamemenu.h" #include "init.h" diff --git a/Source/towners.cpp b/Source/towners.cpp index 7d82e0494..0c68f34be 100644 --- a/Source/towners.cpp +++ b/Source/towners.cpp @@ -1,6 +1,7 @@ #include "towners.h" #include "cursor.h" +#include "engine/cel_header.hpp" #include "engine/load_file.hpp" #include "inv.h" #include "minitext.h"