From 018bccbafc553757d933898f8b83fc9f2a7dbc9f Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Mon, 20 Jun 2022 10:14:40 +0100 Subject: [PATCH] Reduce MicroTiles memory usage --- Source/engine/cel_sprite.hpp | 5 +++++ Source/gendung.cpp | 10 ++++++---- Source/gendung.h | 2 +- Source/scrollrt.cpp | 4 ++-- Source/utils/static_vector.hpp | 13 ++++++++++++- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Source/engine/cel_sprite.hpp b/Source/engine/cel_sprite.hpp index 6788bdf11..0bdec6bff 100644 --- a/Source/engine/cel_sprite.hpp +++ b/Source/engine/cel_sprite.hpp @@ -85,6 +85,11 @@ public: return data_.get(); } + [[nodiscard]] std::unique_ptr release() && + { + return std::move(data_); + } + private: std::unique_ptr data_; }; diff --git a/Source/gendung.cpp b/Source/gendung.cpp index 8a6c85682..0b4ee9dcc 100644 --- a/Source/gendung.cpp +++ b/Source/gendung.cpp @@ -33,7 +33,7 @@ std::optional pSpecialCels; std::unique_ptr pMegaTiles; std::unique_ptr pDungeonCels; std::array SOLData; -std::vector MicroTiles; +StaticVector, MAXTILES> MicroTiles; Point dminPosition; Point dmaxPosition; dungeon_type leveltype; @@ -520,9 +520,11 @@ void SetDungeonMicros() } targetBufferPosition.y += TILE_HEIGHT; } - Surface next { microTile.get() }; - - MicroTiles.emplace_back(SurfaceToCel(next.subregion(0, frameStartY, TILE_WIDTH, TILE_HEIGHT * blocks / 2 - frameStartY), 1, true, 225)); + MicroTiles.emplace_back( + SurfaceToCel( + Surface { microTile.get() }.subregion(0, frameStartY, TILE_WIDTH, TILE_HEIGHT * blocks / 2 - frameStartY), + /*numFrames=*/1, /*generateFrameHeaders=*/false, /*transparentColor=*/225) + .sprite.release()); } pDungeonCels = nullptr; } diff --git a/Source/gendung.h b/Source/gendung.h index 531b3ddce..0457b0cc2 100644 --- a/Source/gendung.h +++ b/Source/gendung.h @@ -157,7 +157,7 @@ extern std::unique_ptr pDungeonCels; * List tile properties */ extern DVL_API_FOR_TEST std::array SOLData; -extern std::vector MicroTiles; +extern StaticVector, MAXTILES> MicroTiles; /** Specifies the minimum X,Y-coordinates of the map. */ extern Point dminPosition; /** Specifies the maximum X,Y-coordinates of the map. */ diff --git a/Source/scrollrt.cpp b/Source/scrollrt.cpp index fa92376f7..da104be68 100644 --- a/Source/scrollrt.cpp +++ b/Source/scrollrt.cpp @@ -658,7 +658,7 @@ void DrawCell(const Surface &out, Point tilePosition, Point targetBufferPosition cel_transparency_active = TileHasAny(tileId, TileProperties::Transparent) && TransList[dTransVal[tilePosition.x][tilePosition.y]]; bool foliage = !TileHasAny(tileId, TileProperties::Solid); // TODO apply foliage and transparancy masks - CelClippedBlitLightTransTo(out, targetBufferPosition, MicroTiles[tileId].sprite, 0); + CelDrawLightTo(out, targetBufferPosition, CelSprite { MicroTiles[tileId].get(), TILE_WIDTH }, 0, nullptr); } /** @@ -672,7 +672,7 @@ void DrawFloor(const Surface &out, Point tilePosition, Point targetBufferPositio LightTableIndex = dLight[tilePosition.x][tilePosition.y]; int pn = dPiece[tilePosition.x][tilePosition.y]; // TODO Clip/mask to floor tile - CelClippedDrawLightTo(out, targetBufferPosition, MicroTiles[pn].sprite, 0); + CelDrawLightTo(out, targetBufferPosition, CelSprite { MicroTiles[pn].get(), TILE_WIDTH }, 0, nullptr); } /** diff --git a/Source/utils/static_vector.hpp b/Source/utils/static_vector.hpp index 80c98d469..480f75bdb 100644 --- a/Source/utils/static_vector.hpp +++ b/Source/utils/static_vector.hpp @@ -42,6 +42,18 @@ public: } ~StaticVector() + { + destroyAll(); + } + + void clear() + { + destroyAll(); + size_ = 0; + } + +private: + void destroyAll() { for (std::size_t pos = 0; pos < size_; ++pos) { #if __cplusplus >= 201703L @@ -52,7 +64,6 @@ public: } } -private: std::aligned_storage_t data_[N]; std::size_t size_ = 0; };