Skip to content

Commit

Permalink
clx_sprite: Minor improvements from #7191
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Aug 6, 2024
1 parent 90732aa commit 78e87e9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 46 deletions.
92 changes: 48 additions & 44 deletions Source/engine/clx_sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,10 @@ class ClxSprite {

private:
// For OptionalClxSprite.
constexpr ClxSprite()
: data_(nullptr)
, pixel_data_size_(0)
{
}
constexpr ClxSprite() = default;

const uint8_t *data_;
uint32_t pixel_data_size_;
const uint8_t *data_ = nullptr;
uint32_t pixel_data_size_ = 0;

friend class OptionalClxSprite;
};
Expand Down Expand Up @@ -139,7 +135,7 @@ class ClxSpriteList {
}

/** @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]);
}
Expand All @@ -154,12 +150,9 @@ class ClxSpriteList {

private:
// For OptionalClxSpriteList.
constexpr ClxSpriteList()
: data_(nullptr)
{
}
constexpr ClxSpriteList() = default;

const uint8_t *data_;
const uint8_t *data_ = nullptr;

friend class OptionalClxSpriteList;
};
Expand Down Expand Up @@ -264,16 +257,17 @@ class ClxSpriteSheet {
[[nodiscard]] constexpr ClxSpriteSheetIterator begin() const;
[[nodiscard]] constexpr ClxSpriteSheetIterator end() const;

private:
// For OptionalClxSpriteSheet.
constexpr ClxSpriteSheet()
: data_(nullptr)
, num_lists_(0)
[[nodiscard]] size_t dataSize() const
{
return static_cast<size_t>(&data_[sheetOffset(num_lists_ - 1)] + (*this)[num_lists_ - 1].dataSize() - &data_[0]);
}

const uint8_t *data_;
uint16_t num_lists_;
private:
// For OptionalClxSpriteSheet.
constexpr ClxSpriteSheet() = default;

const uint8_t *data_ = nullptr;
uint16_t num_lists_ = 0;

friend class OptionalClxSpriteSheet;
};
Expand Down Expand Up @@ -367,6 +361,11 @@ class OwnedClxSpriteList {
return ClxSpriteList { *this }.numSprites();
}

[[nodiscard]] size_t dataSize() const
{
return ClxSpriteList { *this }.dataSize();
}

private:
// For OptionalOwnedClxSpriteList.
OwnedClxSpriteList() = default;
Expand All @@ -385,9 +384,9 @@ inline ClxSpriteList::ClxSpriteList(const OwnedClxSpriteList &owned)

inline OwnedClxSpriteList ClxSpriteList::clone() const
{
const size_t dataSize = nextSpriteSheetOffsetOrFileSize();
std::unique_ptr<uint8_t[]> data { new uint8_t[dataSize] };
memcpy(data.get(), data_, dataSize);
const size_t size = dataSize();
std::unique_ptr<uint8_t[]> data { new uint8_t[size] };
memcpy(data.get(), data_, size);
return OwnedClxSpriteList { std::move(data) };
}

Expand Down Expand Up @@ -422,16 +421,17 @@ class OwnedClxSpriteSheet {
return ClxSpriteSheet { *this }.end();
}

private:
// For OptionalOwnedClxSpriteList.
OwnedClxSpriteSheet()
: data_(nullptr)
, num_lists_(0)
[[nodiscard]] size_t dataSize() const
{
return ClxSpriteSheet { *this }.dataSize();
}

private:
// For OptionalOwnedClxSpriteList.
OwnedClxSpriteSheet() = default;

std::unique_ptr<uint8_t[]> data_;
uint16_t num_lists_;
uint16_t num_lists_ = 0;

friend class ClxSpriteSheet; // for implicit conversion.
friend class OptionalOwnedClxSpriteSheet;
Expand Down Expand Up @@ -489,17 +489,18 @@ class ClxSpriteListOrSheet {
return num_lists_ != 0;
}

private:
const uint8_t *data_;
uint16_t num_lists_;

// For OptionalClxSpriteListOrSheet.
constexpr ClxSpriteListOrSheet()
: data_(nullptr)
, num_lists_(0)
[[nodiscard]] size_t dataSize() const
{
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;
};

Expand Down Expand Up @@ -563,17 +564,20 @@ class OwnedClxSpriteListOrSheet {
return num_lists_ != 0;
}

private:
std::unique_ptr<uint8_t[]> data_;
uint16_t num_lists_;
[[nodiscard]] uint16_t numLists() const { return num_lists_; }

// For OptionalOwnedClxSpriteListOrSheet.
OwnedClxSpriteListOrSheet()
: data_(nullptr)
, num_lists_(0)
[[nodiscard]] size_t dataSize() const
{
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 OptionalOwnedClxSpriteListOrSheet;
};
Expand Down
12 changes: 10 additions & 2 deletions Source/utils/intrusive_optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public:
} \
\
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); \
return *this; \
Expand Down Expand Up @@ -66,11 +69,16 @@ public:
return &value_; \
} \
\
CONSTEXPR operator bool() const \
[[nodiscard]] CONSTEXPR bool has_value() const \
{ \
return value_.FIELD != NULL_VALUE; \
} \
\
CONSTEXPR operator bool() const \
{ \
return has_value(); \
} \
\
private: \
VALUE_CLASS value_;

Expand Down

0 comments on commit 78e87e9

Please sign in to comment.