Add OptionalOwnedCelSprite

This commit is contained in:
Gleb Mazovetskiy
2022-07-03 12:26:39 +01:00
parent 21dc7f553d
commit d152d2c0fb
25 changed files with 115 additions and 36 deletions
+5 -5
View File
@@ -85,7 +85,7 @@ Rectangle MainPanel;
Rectangle LeftPanel;
Rectangle RightPanel;
std::optional<OwnedSurface> pBtmBuff;
std::optional<OwnedCelSprite> pGBoxBuff;
OptionalOwnedCelSprite pGBoxBuff;
const Rectangle &GetMainPanel()
{
@@ -135,10 +135,10 @@ namespace {
std::optional<OwnedSurface> pLifeBuff;
std::optional<OwnedSurface> pManaBuff;
std::optional<OwnedCelSprite> talkButtons;
std::optional<OwnedCelSprite> pDurIcons;
std::optional<OwnedCelSprite> multiButtons;
std::optional<OwnedCelSprite> pPanelButtons;
OptionalOwnedCelSprite talkButtons;
OptionalOwnedCelSprite pDurIcons;
OptionalOwnedCelSprite multiButtons;
OptionalOwnedCelSprite pPanelButtons;
bool PanelButtons[8];
int PanelButtonIndex;
+1 -1
View File
@@ -52,7 +52,7 @@ const Rectangle &GetRightPanel();
bool IsLeftPanelOpen();
bool IsRightPanelOpen();
extern std::optional<OwnedSurface> pBtmBuff;
extern std::optional<OwnedCelSprite> pGBoxBuff;
extern OptionalOwnedCelSprite pGBoxBuff;
extern SDL_Rect PanBtnPos[8];
void CalculatePanelAreas();
+1 -1
View File
@@ -16,7 +16,7 @@
namespace devilution {
extern std::optional<OwnedCelSprite> pSBkIconCels;
extern OptionalOwnedCelSprite pSBkIconCels;
namespace {
+2 -2
View File
@@ -31,8 +31,8 @@
namespace devilution {
namespace {
/** Cursor images CEL */
std::optional<OwnedCelSprite> pCursCels;
std::optional<OwnedCelSprite> pCursCels2;
OptionalOwnedCelSprite pCursCels;
OptionalOwnedCelSprite pCursCels2;
/** Maps from objcurs.cel frame number to frame width. */
const uint16_t InvItemWidth1[] = {
+1 -1
View File
@@ -35,7 +35,7 @@
namespace devilution {
std::string TestMapPath;
std::optional<OwnedCelSprite> pSquareCel;
OptionalOwnedCelSprite pSquareCel;
bool DebugToggle = false;
bool DebugGodMode = false;
bool DebugVision = false;
+1 -1
View File
@@ -15,7 +15,7 @@
namespace devilution {
extern std::string TestMapPath;
extern std::optional<OwnedCelSprite> pSquareCel;
extern OptionalOwnedCelSprite pSquareCel;
extern bool DebugToggle;
extern bool DebugGodMode;
extern bool DebugVision;
+1 -1
View File
@@ -14,7 +14,7 @@
namespace devilution {
namespace {
std::optional<OwnedCelSprite> DoomCel;
OptionalOwnedCelSprite DoomCel;
} // namespace
bool DoomFlag;
+79
View File
@@ -142,6 +142,8 @@ private:
CelSprite sprite_;
};
class OptionalOwnedCelSprite;
/**
* Stores a CEL or CL2 sprite and its width(s).
* Owns the data.
@@ -169,10 +171,87 @@ public:
}
private:
// for OptionalOwnedCelSprite.
OwnedCelSprite()
: data_(nullptr)
, width_(nullptr)
{
}
std::unique_ptr<byte[]> data_;
PointerOrValue<uint16_t> width_;
friend class CelSprite;
friend class OptionalOwnedCelSprite;
};
/**
* @brief Equivalent to `std::optional<OwnedCelSprite>` but smaller.
*/
class OptionalOwnedCelSprite {
public:
OptionalOwnedCelSprite() = default;
OptionalOwnedCelSprite(OwnedCelSprite &&sprite)
: sprite_(std::move(sprite))
{
}
OptionalOwnedCelSprite(std::nullopt_t)
: OptionalOwnedCelSprite()
{
}
template <typename... Args>
OwnedCelSprite &emplace(Args &&...args)
{
sprite_ = OwnedCelSprite(std::forward<Args>(args)...);
return sprite_;
}
OptionalOwnedCelSprite &operator=(OwnedCelSprite &&sprite)
{
sprite_ = std::move(sprite);
return *this;
}
OptionalOwnedCelSprite &operator=(std::nullopt_t)
{
sprite_ = {};
return *this;
}
OwnedCelSprite &operator*()
{
assert(sprite_.data_ != nullptr);
return sprite_;
}
const OwnedCelSprite &operator*() const
{
assert(sprite_.data_ != nullptr);
return sprite_;
}
OwnedCelSprite *operator->()
{
assert(sprite_.data_ != nullptr);
return &sprite_;
}
const OwnedCelSprite *operator->() const
{
assert(sprite_.data_ != nullptr);
return &sprite_;
}
operator bool() const
{
return sprite_.data_ != nullptr;
}
private:
OwnedCelSprite sprite_;
};
inline CelSprite::CelSprite(const OwnedCelSprite &owned)
+1 -1
View File
@@ -31,7 +31,7 @@
namespace devilution {
std::optional<OwnedCelSprite> pSPentSpn2Cels;
OptionalOwnedCelSprite pSPentSpn2Cels;
namespace {
+1 -1
View File
@@ -123,7 +123,7 @@ private:
*
* Also used in the stores and the quest log.
*/
extern std::optional<OwnedCelSprite> pSPentSpn2Cels;
extern OptionalOwnedCelSprite pSPentSpn2Cels;
void LoadSmallSelectionSpinner();
+4 -4
View File
@@ -24,10 +24,10 @@ namespace devilution {
namespace {
std::optional<OwnedCelSprite> optbar_cel;
std::optional<OwnedCelSprite> PentSpin_cel;
std::optional<OwnedCelSprite> option_cel;
std::optional<OwnedCelSprite> sgpLogo;
OptionalOwnedCelSprite optbar_cel;
OptionalOwnedCelSprite PentSpin_cel;
OptionalOwnedCelSprite option_cel;
OptionalOwnedCelSprite sgpLogo;
bool mouseNavigation;
TMenuItem *sgpCurrItem;
int LogoAnim_tick;
+1 -1
View File
@@ -30,7 +30,7 @@ namespace devilution {
namespace {
std::optional<OwnedCelSprite> sgpBackCel;
OptionalOwnedCelSprite sgpBackCel;
bool IsProgress;
uint32_t sgdwProgress;
+1 -1
View File
@@ -137,7 +137,7 @@ const Point InvRect[] = {
namespace {
std::optional<OwnedCelSprite> pInvCels;
OptionalOwnedCelSprite pInvCels;
/**
* @brief Adds an item to a player's InvGrid array
+1 -1
View File
@@ -131,7 +131,7 @@ _sfx_id ItemInvSnds[] = {
namespace {
std::optional<OwnedCelSprite> itemanims[ITEMTYPES];
OptionalOwnedCelSprite itemanims[ITEMTYPES];
enum class PlayerArmorGraphic : uint8_t {
// clang-format off
+1 -1
View File
@@ -21,7 +21,7 @@ Bitset2d<DMAXX, DMAXY> Protected;
Rectangle SetPieceRoom;
Rectangle SetPiece;
std::unique_ptr<uint16_t[]> pSetPiece;
std::optional<OwnedCelSprite> pSpecialCels;
OptionalOwnedCelSprite pSpecialCels;
std::unique_ptr<MegaTile[]> pMegaTiles;
std::unique_ptr<byte[]> pDungeonCels;
std::array<TileProperties, MAXTILES> SOLData;
+1 -1
View File
@@ -148,7 +148,7 @@ extern Rectangle SetPieceRoom;
extern Rectangle SetPiece;
/** Contains the contents of the single player quest DUN file. */
extern std::unique_ptr<uint16_t[]> pSetPiece;
extern std::optional<OwnedCelSprite> pSpecialCels;
extern OptionalOwnedCelSprite pSpecialCels;
/** Specifies the tile definitions of the active dungeon type; (e.g. levels/l1data/l1.til). */
extern DVL_API_FOR_TEST std::unique_ptr<MegaTile[]> pMegaTiles;
extern std::unique_ptr<byte[]> pDungeonCels;
+1 -1
View File
@@ -30,7 +30,7 @@ int qtextSpd;
/** Start time of scrolling */
Uint32 ScrollStart;
/** Graphics for the window border */
std::optional<OwnedCelSprite> pTextBoxCels;
OptionalOwnedCelSprite pTextBoxCels;
/** Pixels for a line of text and the empty space under it. */
const int LineHeight = 38;
+1 -1
View File
@@ -17,7 +17,7 @@
namespace devilution {
std::optional<OwnedCelSprite> pChrButtons;
OptionalOwnedCelSprite pChrButtons;
/** Map of hero class names */
const char *const ClassStrTbl[] = {
+1 -1
View File
@@ -5,7 +5,7 @@
namespace devilution {
extern std::optional<OwnedCelSprite> pChrButtons;
extern OptionalOwnedCelSprite pChrButtons;
extern const char *const ClassStrTbl[];
void DrawChr(const Surface &);
+2 -2
View File
@@ -4,8 +4,8 @@
namespace devilution {
std::optional<OwnedCelSprite> pSTextBoxCels;
std::optional<OwnedCelSprite> pSTextSlidCels;
OptionalOwnedCelSprite pSTextBoxCels;
OptionalOwnedCelSprite pSTextSlidCels;
void InitInfoBoxGfx()
{
+2 -2
View File
@@ -9,14 +9,14 @@ namespace devilution {
*
* Used in stores, the quest log, the help window, and the unique item info window.
*/
extern std::optional<OwnedCelSprite> pSTextBoxCels;
extern OptionalOwnedCelSprite pSTextBoxCels;
/**
* @brief Info box scrollbar graphics.
*
* Used in stores and `DrawDiabloMsg`.
*/
extern std::optional<OwnedCelSprite> pSTextSlidCels;
extern OptionalOwnedCelSprite pSTextSlidCels;
void InitInfoBoxGfx();
void FreeInfoBoxGfx();
+3 -3
View File
@@ -21,12 +21,12 @@
namespace devilution {
std::optional<OwnedCelSprite> pSBkIconCels;
OptionalOwnedCelSprite pSBkIconCels;
namespace {
std::optional<OwnedCelSprite> pSBkBtnCel;
std::optional<OwnedCelSprite> pSpellBkCel;
OptionalOwnedCelSprite pSBkBtnCel;
OptionalOwnedCelSprite pSpellBkCel;
/** Maps from spellbook page number and position to spell_id. */
spell_id SpellPages[6][7] = {
+1 -1
View File
@@ -9,7 +9,7 @@
namespace devilution {
namespace {
std::optional<OwnedCelSprite> pSpellCels;
OptionalOwnedCelSprite pSpellCels;
uint8_t SplTransTbl[256];
} // namespace
+1 -1
View File
@@ -30,7 +30,7 @@
namespace devilution {
bool QuestLogIsOpen;
std::optional<OwnedCelSprite> pQLogCel;
OptionalOwnedCelSprite pQLogCel;
/** Contains the quests of the current game. */
Quest Quests[MAXQUESTS];
Point ReturnLvlPosition;
+1 -1
View File
@@ -73,7 +73,7 @@ struct QuestData {
};
extern bool QuestLogIsOpen;
extern std::optional<OwnedCelSprite> pQLogCel;
extern OptionalOwnedCelSprite pQLogCel;
extern DVL_API_FOR_TEST Quest Quests[MAXQUESTS];
extern Point ReturnLvlPosition;
extern dungeon_type ReturnLevelType;