diff options
author | rtk0c <[email protected]> | 2022-04-17 20:08:57 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-04-17 20:08:57 -0700 |
commit | 5424a1d5434e3ddd911a504719918c2df027e2fd (patch) | |
tree | 6275aab13140d81dcc46c8290e73ac9a8bbb5605 /source/Texture.hpp | |
parent | afcac59c7d04f4337d6b04ebed8cac7e871ccc50 (diff) |
Changeset: 8 Initial work on sprites and texture system
Diffstat (limited to 'source/Texture.hpp')
-rw-r--r-- | source/Texture.hpp | 92 |
1 files changed, 55 insertions, 37 deletions
diff --git a/source/Texture.hpp b/source/Texture.hpp index c330bb3..ef4d136 100644 --- a/source/Texture.hpp +++ b/source/Texture.hpp @@ -1,18 +1,28 @@ #pragma once +#include "GraphicsTags.hpp" +#include "Image.hpp" +#include "Ires.hpp" #include "RcPtr.hpp" -#include <absl/container/flat_hash_map.h> #include <glad/glad.h> +#include <cstdint> #include <glm/glm.hpp> -#include <memory> +#include <span> // TODO abstract texture traits such as component sizes from OpenGL -class TextureInfo { -public: +struct Subregion { + float u0 = 0.0f; + float v0 = 0.0f; + float u1 = 0.0f; + float v1 = 0.0f; +}; + +struct TextureInfo { glm::ivec2 size; - bool isAtlas = false; + Tags::TexFilter minifyingFilter = Tags::TF_Linear; + Tags::TexFilter magnifyingFilter = Tags::TF_Linear; }; class Texture : public RefCounted { @@ -31,18 +41,41 @@ public: Texture(Texture&&) = default; Texture& operator=(Texture&&) = default; - enum Filtering { - LinearFilter, - NearestFilter, + enum ErrorCode { + EC_Success, + EC_AlreadyInitialized, + EC_FileIoFailed, + EC_InvalidImage, }; - struct TextureProperties { - Filtering minifyingFilter = LinearFilter; - Filtering magnifyingFilter = LinearFilter; + ErrorCode InitFromFile(const char* filePath); + ErrorCode InitFromImage(const Image& image); + + struct AtlasSource { + std::string name; + Image image; }; - bool InitFromFile(const char* filePath, const TextureProperties& props, bool flipVertically = false); - // bool InitFromImage(const Image& image, const TextureProperties& props, bool flipVertically = false); + struct AltasElement { + std::string name; + Subregion subregion; + glm::ivec2 subregionSize; + }; + + enum PackingMode { + PM_KeepSquare, + PM_VerticalExtension, + PM_HorizontalExtension, + }; + + struct AtlasInput { + std::span<AtlasSource> sources; + PackingMode packingMode; + }; + struct AtlasOutput { + std::vector<AltasElement> elements; + }; + ErrorCode InitAtlas(const AtlasInput& in, AtlasOutput* out = nullptr); const TextureInfo& GetInfo() const; GLuint GetHandle() const; @@ -50,32 +83,17 @@ public: bool IsValid() const; }; -/// A pure numerical subregion of a texture. u0/v0 are the UV coordinates of bottom left -/// corner, and u1/v1 are the top left corner. -struct Subregion { - /// Bottom left corner - float u0 = 0.0f; - float v0 = 0.0f; - /// Top right corner - float u1 = 0.0f; - float v1 = 0.0f; -}; - -/// A subregion of a specific texture. -struct TextureSubregion : public Subregion { - RcPtr<Texture> atlasTexture; -}; - -class TextureManager { +class IresTexture : public IresObject { public: - static inline TextureManager* instance = nullptr; - -private: - absl::flat_hash_map<std::string_view, RcPtr<Texture>> mTextures; + RcPtr<Texture> mInstance; public: - void DiscoverTextures(); + IresTexture() + : IresObject(KD_Texture) {} + + Texture* CreateInstance() const; + Texture* GetInstance(); - const auto& GetTextures() const { return mTextures; } - Texture* FindTexture(std::string_view name); + void Write(rapidjson::Value& value, rapidjson::Document& root) const override; + void Read(const rapidjson::Value& value) override; }; |