CoriEngine
Loading...
Searching...
No Matches
SpriteAtlas.cpp
Go to the documentation of this file.
1#include "SpriteAtlas.hpp"
3
4namespace Cori {
5 namespace Graphics {
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) {
7 int32_t padding = 0;
8 const auto successPadding = image->AddPadding(spriteResolution);
9 if (successPadding) {
10 padding = 1;
11 }
12 else {
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());
14 }
15
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));
18
19 const glm::uvec2 cellSize = { spriteResolution.x + padding * 2, spriteResolution.y + padding * 2 };
20
21 m_GridDimensions.x = m_Texture->GetWidth() / cellSize.x;
22 m_GridDimensions.y = m_Texture->GetHeight() / cellSize.y;
23
24 m_SpriteCount = m_GridDimensions.x * m_GridDimensions.y;
25 m_SpriteUVs.reserve(m_SpriteCount);
26
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()) };
30
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 });
36 }
37 }
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);
39 }
40
41 const UVs& SpriteAtlas::GetSpriteUVsAtIndex(uint32_t index) const {
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];
44 }
45
46 const UVs& SpriteAtlas::GetSpriteUVsAtPosition(glm::u16vec2 pos) const {
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];
49 }
50
52 return m_Success;
53 }
54
55 std::string_view SpriteAtlas::GetName() const {
56 return m_Name;
57 }
58
59 std::shared_ptr<Texture2D> SpriteAtlas::GetTexture() const {
60 return m_Texture;
61 }
62
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);
66 const auto placeholder = Image::Create(FileSystem::PathManager::GetAliasedPath("ENGINE_DATA") / "placeholders/missing_texture32.png");
67 return std::shared_ptr<SpriteAtlas>(new SpriteAtlas(std::move(name), placeholder, glm::u16vec2(32), false));
68 }
69
70 if (image->GetSuccessStatus()) {
71 return std::shared_ptr<SpriteAtlas>(new SpriteAtlas(std::move(name), image, spriteResolution, true));
72 }
73
74 return std::shared_ptr<SpriteAtlas>(new SpriteAtlas(std::move(name), image, glm::u16vec2(32), false));
75 }
76
77 std::shared_ptr<SpriteAtlas> SpriteAtlas::Create(const Descriptor& descriptor) {
78 const auto image = Image::Create(descriptor.m_TexturePath);
79 return Create(descriptor.m_Name, image, descriptor.m_SpriteResolution);
80 }
81 }
82}
#define CORI_CORE_DEBUG_TAGGED(...)
Definition Logger.hpp:1026
#define CORI_CORE_CHECK(x,...)
Definition Logger.hpp:1042
#define CORI_CORE_ERROR_TAGGED(...)
Definition Logger.hpp:1039
#define CORI_CORE_WARN_TAGGED(...)
Definition Logger.hpp:1038
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.
Definition Image.cpp:189
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.
Definition Window.hpp:7
Global engine namespace.
static constexpr char SpriteAtlas[]
Definition Logger.hpp:69
static constexpr char Self[]
Definition Logger.hpp:47