CoriEngine
Loading...
Searching...
No Matches
SpriteAtlas.hpp
Go to the documentation of this file.
1#pragma once
2#include "Texture.hpp"
3#include "Image.hpp"
5
6namespace Cori {
7 namespace Graphics {
12 class SpriteAtlas : public Profiling::Trackable<SpriteAtlas> {
13 public:
17 class Descriptor {
18 public:
26 constexpr Descriptor(std::string name, std::filesystem::path imagePath, const glm::uvec2 spriteResolution) noexcept
27 : m_TexturePath(std::move(imagePath)),
28 m_SpriteResolution(spriteResolution),
29 m_Name(std::move(name)),
30 m_RuntimeID(s_NextRuntimeID.fetch_add(1, std::memory_order_relaxed))
31 { }
32
33 using AssetType = SpriteAtlas;
34
35 [[nodiscard]] uint32_t GetRuntimeID() const { return m_RuntimeID; }
36
37 constexpr bool operator==(const Descriptor& other) const noexcept {
38 return m_RuntimeID == other.m_RuntimeID;
39 }
40
41 struct Hasher {
42 std::size_t operator()(const Descriptor& descriptor) const noexcept {
43 return std::hash<uint32_t>{}(descriptor.m_RuntimeID);
44 }
45 };
46
47 const std::filesystem::path m_TexturePath;
48 const glm::uvec2 m_SpriteResolution;
49 const std::string m_Name;
50
51 private:
52 const uint32_t m_RuntimeID{ 0 };
53 inline static std::atomic<uint32_t> s_NextRuntimeID{ 1 };
54
55 };
56
65 [[nodiscard]] static std::shared_ptr<SpriteAtlas> Create(std::string name, const std::shared_ptr<Image>& image, const glm::u16vec2 spriteResolution);
66
72 [[nodiscard]] const UVs& GetSpriteUVsAtIndex(uint32_t index) const;
73
79 [[nodiscard]] const UVs& GetSpriteUVsAtPosition(glm::u16vec2 pos) const;
80
85 [[nodiscard]] bool GetSuccessStatus() const;
86
91 [[nodiscard]] std::string_view GetName() const;
92
97 [[nodiscard]] std::shared_ptr<Texture2D> GetTexture() const;
98
99 private:
100 explicit SpriteAtlas(std::string name, const std::shared_ptr<Image>& image, const glm::u16vec2 spriteResolution, const bool success);
101
102 friend AssetManager;
103 [[nodiscard]] static std::shared_ptr<SpriteAtlas> Create(const Descriptor& descriptor);
104 std::string m_Name;
105
106 uint32_t m_SpriteCount;
107 std::shared_ptr<Texture2D> m_Texture;
108 glm::u16vec2 m_GridDimensions;
109
110 std::vector<UVs> m_SpriteUVs;
111
112 bool m_Success;
113 };
114 }
115}
Used when you want to manually control the asset lifetime, loading, preloading, unloading.
constexpr bool operator==(const Descriptor &other) const noexcept
const std::filesystem::path m_TexturePath
constexpr Descriptor(std::string name, std::filesystem::path imagePath, const glm::uvec2 spriteResolution) noexcept
Constructs a descriptor.
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.
For InstanceMetrics to work with a type it should derive from this.
Definition Trackable.hpp:29
Almost everything connected to graphics is in this namespace.
Definition Window.hpp:7
Global engine namespace.
std::size_t operator()(const Descriptor &descriptor) const noexcept