Browse Source

clx_sprite: Minor improvements from #7191

pull/7191/head
Gleb Mazovetskiy 2 years ago
parent
commit
78e87e9f09
  1. 92
      Source/engine/clx_sprite.hpp
  2. 12
      Source/utils/intrusive_optional.hpp

92
Source/engine/clx_sprite.hpp

@ -89,14 +89,10 @@ public:
private: private:
// For OptionalClxSprite. // For OptionalClxSprite.
constexpr ClxSprite() constexpr ClxSprite() = default;
: data_(nullptr)
, pixel_data_size_(0)
{
}
const uint8_t *data_; const uint8_t *data_ = nullptr;
uint32_t pixel_data_size_; uint32_t pixel_data_size_ = 0;
friend class OptionalClxSprite; friend class OptionalClxSprite;
}; };
@ -139,7 +135,7 @@ public:
} }
/** @brief The offset to the next sprite sheet, or file size if this is the last sprite sheet. */ /** @brief The offset to the next sprite sheet, or file size if this is the last sprite sheet. */
[[nodiscard]] constexpr uint32_t nextSpriteSheetOffsetOrFileSize() const [[nodiscard]] constexpr uint32_t dataSize() const
{ {
return LoadLE32(&data_[4 + numSprites() * 4]); return LoadLE32(&data_[4 + numSprites() * 4]);
} }
@ -154,12 +150,9 @@ public:
private: private:
// For OptionalClxSpriteList. // For OptionalClxSpriteList.
constexpr ClxSpriteList() constexpr ClxSpriteList() = default;
: data_(nullptr)
{
}
const uint8_t *data_; const uint8_t *data_ = nullptr;
friend class OptionalClxSpriteList; friend class OptionalClxSpriteList;
}; };
@ -264,16 +257,17 @@ public:
[[nodiscard]] constexpr ClxSpriteSheetIterator begin() const; [[nodiscard]] constexpr ClxSpriteSheetIterator begin() const;
[[nodiscard]] constexpr ClxSpriteSheetIterator end() const; [[nodiscard]] constexpr ClxSpriteSheetIterator end() const;
private: [[nodiscard]] size_t dataSize() const
// For OptionalClxSpriteSheet.
constexpr ClxSpriteSheet()
: data_(nullptr)
, num_lists_(0)
{ {
return static_cast<size_t>(&data_[sheetOffset(num_lists_ - 1)] + (*this)[num_lists_ - 1].dataSize() - &data_[0]);
} }
const uint8_t *data_; private:
uint16_t num_lists_; // For OptionalClxSpriteSheet.
constexpr ClxSpriteSheet() = default;
const uint8_t *data_ = nullptr;
uint16_t num_lists_ = 0;
friend class OptionalClxSpriteSheet; friend class OptionalClxSpriteSheet;
}; };
@ -367,6 +361,11 @@ public:
return ClxSpriteList { *this }.numSprites(); return ClxSpriteList { *this }.numSprites();
} }
[[nodiscard]] size_t dataSize() const
{
return ClxSpriteList { *this }.dataSize();
}
private: private:
// For OptionalOwnedClxSpriteList. // For OptionalOwnedClxSpriteList.
OwnedClxSpriteList() = default; OwnedClxSpriteList() = default;
@ -385,9 +384,9 @@ inline ClxSpriteList::ClxSpriteList(const OwnedClxSpriteList &owned)
inline OwnedClxSpriteList ClxSpriteList::clone() const inline OwnedClxSpriteList ClxSpriteList::clone() const
{ {
const size_t dataSize = nextSpriteSheetOffsetOrFileSize(); const size_t size = dataSize();
std::unique_ptr<uint8_t[]> data { new uint8_t[dataSize] }; std::unique_ptr<uint8_t[]> data { new uint8_t[size] };
memcpy(data.get(), data_, dataSize); memcpy(data.get(), data_, size);
return OwnedClxSpriteList { std::move(data) }; return OwnedClxSpriteList { std::move(data) };
} }
@ -422,16 +421,17 @@ public:
return ClxSpriteSheet { *this }.end(); return ClxSpriteSheet { *this }.end();
} }
private: [[nodiscard]] size_t dataSize() const
// For OptionalOwnedClxSpriteList.
OwnedClxSpriteSheet()
: data_(nullptr)
, num_lists_(0)
{ {
return ClxSpriteSheet { *this }.dataSize();
} }
private:
// For OptionalOwnedClxSpriteList.
OwnedClxSpriteSheet() = default;
std::unique_ptr<uint8_t[]> data_; std::unique_ptr<uint8_t[]> data_;
uint16_t num_lists_; uint16_t num_lists_ = 0;
friend class ClxSpriteSheet; // for implicit conversion. friend class ClxSpriteSheet; // for implicit conversion.
friend class OptionalOwnedClxSpriteSheet; friend class OptionalOwnedClxSpriteSheet;
@ -489,17 +489,18 @@ public:
return num_lists_ != 0; return num_lists_ != 0;
} }
private: [[nodiscard]] size_t dataSize() const
const uint8_t *data_;
uint16_t num_lists_;
// For OptionalClxSpriteListOrSheet.
constexpr ClxSpriteListOrSheet()
: data_(nullptr)
, num_lists_(0)
{ {
return isSheet() ? sheet().dataSize() : list().dataSize();
} }
private:
// For OptionalClxSpriteListOrSheet.
constexpr ClxSpriteListOrSheet() = default;
const uint8_t *data_ = nullptr;
uint16_t num_lists_ = 0;
friend class OptionalClxSpriteListOrSheet; friend class OptionalClxSpriteListOrSheet;
}; };
@ -563,17 +564,20 @@ public:
return num_lists_ != 0; return num_lists_ != 0;
} }
private: [[nodiscard]] uint16_t numLists() const { return num_lists_; }
std::unique_ptr<uint8_t[]> data_;
uint16_t num_lists_;
// For OptionalOwnedClxSpriteListOrSheet. [[nodiscard]] size_t dataSize() const
OwnedClxSpriteListOrSheet()
: data_(nullptr)
, num_lists_(0)
{ {
return ClxSpriteListOrSheet { *this }.dataSize();
} }
private:
// For OptionalOwnedClxSpriteListOrSheet.
OwnedClxSpriteListOrSheet() = default;
std::unique_ptr<uint8_t[]> data_;
uint16_t num_lists_ = 0;
friend class ClxSpriteListOrSheet; friend class ClxSpriteListOrSheet;
friend class OptionalOwnedClxSpriteListOrSheet; friend class OptionalOwnedClxSpriteListOrSheet;
}; };

12
Source/utils/intrusive_optional.hpp

@ -30,7 +30,10 @@ public:
} \ } \
\ \
template <class U = VALUE_CLASS> \ template <class U = VALUE_CLASS> \
CONSTEXPR OPTIONAL_CLASS &operator=(VALUE_CLASS &&value) \ CONSTEXPR std::enable_if_t< \
!std::is_same_v<OPTIONAL_CLASS, std::remove_cv_t<std::remove_reference_t<U>>>, \
OPTIONAL_CLASS> & \
operator=(U &&value) noexcept \
{ \ { \
value_ = std::forward<U>(value); \ value_ = std::forward<U>(value); \
return *this; \ return *this; \
@ -66,11 +69,16 @@ public:
return &value_; \ return &value_; \
} \ } \
\ \
CONSTEXPR operator bool() const \ [[nodiscard]] CONSTEXPR bool has_value() const \
{ \ { \
return value_.FIELD != NULL_VALUE; \ return value_.FIELD != NULL_VALUE; \
} \ } \
\ \
CONSTEXPR operator bool() const \
{ \
return has_value(); \
} \
\
private: \ private: \
VALUE_CLASS value_; VALUE_CLASS value_;

Loading…
Cancel
Save