6 SpriteAtlas::SpriteAtlas(std::string name,
const std::shared_ptr<Image>& image,
const glm::u16vec2 spriteResolution,
const bool success) : m_Name(std::move(name)), m_Success(success) {
8 const auto successPadding = image->AddPadding(spriteResolution);
13 CORI_CORE_WARN_TAGGED({ Logger::Tags::Graphics::Self, Logger::Tags::Graphics::SpriteAtlas },
"Failed to add padding to a sprite atlas, Texture bleeding might occur. Error: {}", successPadding.error().what());
16 m_Texture = Texture2D::Create(image);
17 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Graphics::Self, Logger::Tags::Graphics::SpriteAtlas },
"Creating SpriteAtlas: '{}', texture size: '{}', sprite size: '{}'", m_Name, glm::to_string(glm::uvec2(m_Texture->GetWidth(), m_Texture->GetHeight())), glm::to_string(spriteResolution));
19 const glm::uvec2 cellSize = { spriteResolution.x + padding * 2, spriteResolution.y + padding * 2 };
21 m_GridDimensions.x = m_Texture->GetWidth() / cellSize.x;
22 m_GridDimensions.y = m_Texture->GetHeight() / cellSize.y;
24 m_SpriteCount = m_GridDimensions.x * m_GridDimensions.y;
25 m_SpriteUVs.reserve(m_SpriteCount);
27 const glm::vec2 normalizedSpriteSize = {
static_cast<float>(spriteResolution.x) /
static_cast<float>(m_Texture->GetWidth()),
static_cast<float>(spriteResolution.y) /
static_cast<float>(m_Texture->GetHeight()) };
28 const glm::vec2 normalizedCellSize = {
static_cast<float>(cellSize.x) /
static_cast<float>(m_Texture->GetWidth()),
static_cast<float>(cellSize.y) /
static_cast<float>(m_Texture->GetHeight()) };
29 const glm::vec2 normalizedCellOffset = {
static_cast<float>(padding) /
static_cast<float>(m_Texture->GetWidth()),
static_cast<float>(padding) /
static_cast<float>(m_Texture->GetHeight()) };
31 for (int32_t row = 0; row < m_GridDimensions.y; row++) {
32 for (int32_t col = 0; col < m_GridDimensions.x; col++) {
33 glm::vec2 cellNormalizedPos = { normalizedCellSize.x *
static_cast<float>(col), 1.0f - normalizedCellSize.y *
static_cast<float>(row + 1) };
34 glm::vec2 spriteNormalizedPos = cellNormalizedPos + normalizedCellOffset;
35 m_SpriteUVs.emplace_back(UVs{ spriteNormalizedPos, spriteNormalizedPos + normalizedSpriteSize });
38 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Graphics::Self, Logger::Tags::Graphics::SpriteAtlas },
"Created SpriteAtlas: '{}', grid dimensions: '{}', total sprite count: '{}'", m_Name, glm::to_string(m_GridDimensions), m_SpriteCount);
42 if (
CORI_CORE_CHECK(index + 1 <= m_SpriteCount,
"Requested a sprite UVs from '{}' at invalid index: {} : returned data from index: 0", m_Name, index)) {
return m_SpriteUVs[0]; }
43 return m_SpriteUVs[index];
47 if (
CORI_CORE_CHECK(pos.x * pos.y <= m_SpriteCount,
"Requested a sprite UVs from '{}' at invalid position: ({}, {}) : returned data from position: (0, 0)", m_Name, pos.x, pos.y)) {
return m_SpriteUVs[0]; }
48 return m_SpriteUVs[pos.x + m_GridDimensions.x * pos.y];
63 std::shared_ptr<SpriteAtlas>
SpriteAtlas::Create(std::string name,
const std::shared_ptr<Image>& image,
const glm::u16vec2 spriteResolution) {
64 if (!(!(image->GetHeight() % spriteResolution.y) && !(image->GetWidth() % spriteResolution.x))) {
65 CORI_CORE_ERROR_TAGGED({
Logger::Tags::Graphics::Self,
Logger::Tags::Graphics::SpriteAtlas },
"Can't create SpriteAtlas '{}'. Invalid sprite resolution, sprite resolution on x and y should be divisible by texture resolution without remainder. Texture resolution: ({}, {}). Sprite resolution: ({}, {}). Sprite Atlas will be generated with a placeholder.", std::move(name), image->GetWidth(), image->GetHeight(), spriteResolution.x, spriteResolution.y);
67 return std::shared_ptr<SpriteAtlas>(
new SpriteAtlas(std::move(name), placeholder, glm::u16vec2(32),
false));
70 if (image->GetSuccessStatus()) {
71 return std::shared_ptr<SpriteAtlas>(
new SpriteAtlas(std::move(name), image, spriteResolution,
true));
74 return std::shared_ptr<SpriteAtlas>(
new SpriteAtlas(std::move(name), image, glm::u16vec2(32),
false));
79 return Create(descriptor.m_Name, image, descriptor.m_SpriteResolution);
#define CORI_CORE_DEBUG_TAGGED(...)
#define CORI_CORE_CHECK(x,...)
#define CORI_CORE_ERROR_TAGGED(...)
#define CORI_CORE_WARN_TAGGED(...)
static std::filesystem::path GetAliasedPath(const std::string &alias)
Retries the full aliased path defined in fsgame.json.
static std::shared_ptr< Image > Create(const std::filesystem::path &path)
Creates an Image from the picture at the specified path.
static std::shared_ptr< SpriteAtlas > Create(std::string name, const std::shared_ptr< Image > &image, const glm::u16vec2 spriteResolution)
Creates a SpriteAtlas.
bool GetSuccessStatus() const
Checks if the SpriteAtlas was created successfully.
const UVs & GetSpriteUVsAtPosition(glm::u16vec2 pos) const
Request the UVs for the spite at specific position.
std::shared_ptr< Texture2D > GetTexture() const
Retrieves the Texture2D stored in the SpriteAtlas.
std::string_view GetName() const
Retrieves the name of the SpriteAtlas.
const UVs & GetSpriteUVsAtIndex(uint32_t index) const
Request the UVs for the spite at specific index.
Almost everything connected to graphics is in this namespace.