diff --git a/src/inferno/application.cpp b/src/inferno/application.cpp index 741fe8e..09b2615 100644 --- a/src/inferno/application.cpp +++ b/src/inferno/application.cpp @@ -17,16 +17,16 @@ #include "inferno/event/keyevent.h" #include "inferno/event/mouseevent.h" // #include "inferno/io/gltffile.h" +#include "inferno/asset/font.h" #include "inferno/io/input.h" #include "inferno/keycodes.h" #include "inferno/render/buffer.h" #include "inferno/render/context.h" -#include "inferno/render/font.h" // #include "inferno/render/gltf.h" +#include "inferno/asset/shader.h" +#include "inferno/asset/texture.h" #include "inferno/render/render-command.h" #include "inferno/render/renderer.h" -#include "inferno/render/shader.h" -#include "inferno/render/texture.h" #include "inferno/scene/scene.h" #include "inferno/settings.h" #include "inferno/time.h" @@ -78,13 +78,11 @@ Application::~Application() { m_scene->destroy(); - FontManager::destroy(); RendererFont::destroy(); Renderer2D::destroy(); RendererCubemap::destroy(); RenderCommand::destroy(); - TextureManager::destroy(); - ShaderManager::destroy(); + AssetManager::destroy(); // Input::destroy(); Settings::destroy(); diff --git a/src/inferno/asset/asset-manager.cpp b/src/inferno/asset/asset-manager.cpp new file mode 100644 index 0000000..a9a462a --- /dev/null +++ b/src/inferno/asset/asset-manager.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2024 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#include // std::shared_ptr, std::static_pointer_cast +#include +#include + +#include "ruc/format/log.h" + +#include "inferno/asset/asset-manager.h" +#include "inferno/asset/shader.h" + +namespace Inferno { + +// ----------------------------------------- + +AssetManager::AssetManager(s) +{ + ruc::info("AssetManager initialized"); +} + +AssetManager::~AssetManager() +{ +} + +void AssetManager::add(std::string_view path, std::shared_ptr asset) +{ + // Construct (key, value) pair and insert it into the unordered_map + auto stringPath = std::string(path.begin(), path.end()); + m_assetList.emplace(std::move(stringPath), std::move(asset)); +} + +bool AssetManager::exists(std::string_view path) +{ + return m_assetList.find(path.data()) != m_assetList.end(); +} + +void AssetManager::remove(std::string_view path) +{ + if (exists(path)) { + m_assetList.erase(path.data()); + } +} + +void AssetManager::remove(std::shared_ptr asset) +{ + if (exists(asset->path())) { + m_assetList.erase(asset->path()); + } +} + +} // namespace Inferno diff --git a/src/inferno/asset/asset-manager.h b/src/inferno/asset/asset-manager.h new file mode 100644 index 0000000..6f8a839 --- /dev/null +++ b/src/inferno/asset/asset-manager.h @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2024 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include // std::shared_ptr +#include +#include +#include + +#include "ruc/meta/assert.h" +#include "ruc/meta/types.h" +#include "ruc/singleton.h" + +namespace Inferno { + +class Asset { +public: + virtual ~Asset() = default; + + std::string path() const { return m_path; } + + // ------------------------------------- + + std::string className() const { return typeid(*this).name(); } + + template + bool fastIs() const = delete; + + virtual bool isFont() const { return false; } + virtual bool isShader() const { return false; } + virtual bool isTexture() const { return false; } + virtual bool isTexture2D() const { return false; } + virtual bool isTextureCubemap() const { return false; } + +protected: + Asset(std::string_view path) + : m_path(std::string(path.begin(), path.end())) + { + } + +protected: + std::string m_path; +}; + +// ----------------------------------------- + +template +concept IsAsset = std::same_as || std::derived_from; + +// ----------------------------------------- + +class AssetManager : public ruc::Singleton { +public: + AssetManager(s); + virtual ~AssetManager(); + + void add(std::string_view path, std::shared_ptr asset); + bool exists(std::string_view path); + void remove(std::string_view path); + void remove(std::shared_ptr asset); + + template + std::shared_ptr get(std::string_view path) + { + if (!exists(path)) { + return nullptr; + } + + auto asset = m_assetList.at(path.data()); + VERIFY(is(asset.get()), "expected asset {}, got {}", typeid(T).name(), asset->className()); + return std::static_pointer_cast(asset); + } + + template + std::shared_ptr load(std::string_view path) + { + if (exists(path)) { + return get(path); + } + + auto asset = T::create(path); + add(path, asset); + return asset; + } + +private: + std::unordered_map> m_assetList; +}; + +} // namespace Inferno diff --git a/src/inferno/render/font.cpp b/src/inferno/asset/font.cpp similarity index 74% rename from src/inferno/render/font.cpp rename to src/inferno/asset/font.cpp index a1f77af..f18b0c2 100644 --- a/src/inferno/render/font.cpp +++ b/src/inferno/asset/font.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Riyyi + * Copyright (C) 2022-2024 Riyyi * * SPDX-License-Identifier: MIT */ @@ -7,32 +7,32 @@ #include // std::array #include // std;:from_chars #include // int32_t, uint32_t -#include // std::numeric_limits #include // std::views::split #include // std::getline -#include // std::move #include "ruc/file.h" -#include "ruc/format/log.h" #include "ruc/meta/assert.h" #include "ruc/meta/concepts.h" -#include "inferno/render/font.h" -#include "inferno/render/texture.h" -#include "inferno/util/integer.h" +#include "inferno/asset/font.h" +#include "inferno/asset/texture.h" namespace Inferno { -Font::Font(const std::string& name) - : m_name(std::move(name)) +std::shared_ptr Font::create(std::string_view path) { - std::string path = name + ".fnt"; - std::string image = name + ".png"; + auto result = std::shared_ptr(new Font(path)); - std::string font = ruc::File(path).data(); - parseFont(font); + auto stringPath = std::string(path); + std::string file = stringPath + ".fnt"; + std::string image = stringPath + ".png"; - m_texture = Texture2D::create(image); + std::string font = ruc::File(file).data(); + result->parseFont(font); + + result->m_texture = Texture2D::create(image); + + return result; } // TODO: Move this to ruc @@ -167,58 +167,6 @@ std::string Font::findValue(const std::string& key, const std::vector font) -{ - // Construct (key, value) pair and insert it into the unordered_map - m_fontList.emplace(std::move(name), std::move(font)); -} - -std::shared_ptr FontManager::load(const std::string& name) -{ - if (exists(name)) { - return get(name); - } - - std::shared_ptr font = std::make_shared(name); - add(name, font); - return get(name); -} - -std::shared_ptr FontManager::get(const std::string& name) -{ - return exists(name) ? m_fontList.at(name) : nullptr; -} - -bool FontManager::exists(const std::string& name) -{ - return m_fontList.find(name) != m_fontList.end(); -} - -void FontManager::remove(const std::string& name) -{ - if (exists(name)) { - m_fontList.erase(name); - } -} - -void FontManager::remove(std::shared_ptr font) -{ - if (exists(font->name())) { - m_fontList.erase(font->name()); - } -} - } // namespace Inferno void ruc::format::Formatter::format(Builder& builder, glm::ivec2 value) const diff --git a/src/inferno/render/font.h b/src/inferno/asset/font.h similarity index 72% rename from src/inferno/render/font.h rename to src/inferno/asset/font.h index 0a3c4e0..f2aa0d2 100644 --- a/src/inferno/render/font.h +++ b/src/inferno/asset/font.h @@ -1,22 +1,24 @@ /* - * Copyright (C) 2022 Riyyi + * Copyright (C) 2022-2024 Riyyi * * SPDX-License-Identifier: MIT */ #pragma once -#include // std::array -#include // int32_t, uint32_t -#include // std::shared_ptr -#include // std::string +#include // std::array +#include // int32_t, uint32_t +#include // std::shared_ptr +#include // std::string +#include #include // std::unordered_map #include // std::vector #include "glm/ext/vector_int2.hpp" // glm::ivec2 #include "glm/ext/vector_uint2.hpp" // glm::uvec2 #include "ruc/format/format.h" -#include "ruc/singleton.h" + +#include "inferno/asset/asset-manager.h" #define PADDING 3 @@ -35,11 +37,13 @@ struct Symbol { // ------------------------------------- -class Font { +class Font final : public Asset { public: - Font(const std::string& name); virtual ~Font() {} + // Factory function + static std::shared_ptr create(std::string_view path); + enum Padding { Top = 0, Right, @@ -47,7 +51,6 @@ public: Left, }; - inline std::string name() const { return m_name; } inline uint32_t size() const { return m_size; } inline uint32_t lineSpacing() const { return m_lineSpacing; } inline std::shared_ptr texture() const { return m_texture; } @@ -56,12 +59,19 @@ public: inline std::shared_ptr operator[](unsigned char c) const { return m_symbolList.at(c); } private: + Font(std::string_view path) + : Asset(path) + { + } + void parseFont(const std::string& font); std::string findAction(const std::string& line) const; std::vector findColumns(const std::string& line) const; std::string findValue(const std::string& key, const std::vector& columns) const; - std::string m_name; + virtual bool isFont() const override { return true; } + +private: unsigned char m_size = { 0 }; uint32_t m_lineSpacing = { 0 }; std::array m_padding = { 0 }; @@ -69,24 +79,12 @@ private: std::unordered_map> m_symbolList; }; -// ------------------------------------- +// ----------------------------------------- -class FontManager final : public ruc::Singleton { -public: - FontManager(s); - virtual ~FontManager(); - - void add(const std::string& name, std::shared_ptr font); - std::shared_ptr load(const std::string& name); - std::shared_ptr get(const std::string& name); - bool exists(const std::string& name); - - void remove(const std::string& name); - void remove(std::shared_ptr font); - -private: - std::unordered_map> m_fontList; -}; +// clang-format off +template<> +inline bool Asset::fastIs() const { return isFont(); } +// clang-format on } // namespace Inferno diff --git a/src/inferno/render/shader.cpp b/src/inferno/asset/shader.cpp similarity index 51% rename from src/inferno/render/shader.cpp rename to src/inferno/asset/shader.cpp index 859cd89..d327e9e 100644 --- a/src/inferno/render/shader.cpp +++ b/src/inferno/asset/shader.cpp @@ -1,11 +1,10 @@ /* - * Copyright (C) 2022 Riyyi + * Copyright (C) 2022,2024 Riyyi * * SPDX-License-Identifier: MIT */ -#include // std::move -#include // std::vector +#include // std::vector #include "glad/glad.h" #include "glm/gtc/type_ptr.hpp" // glm::value_ptr @@ -13,32 +12,34 @@ #include "ruc/format/log.h" #include "ruc/meta/assert.h" -#include "inferno/core.h" -#include "inferno/render/shader.h" +#include "inferno/asset/shader.h" namespace Inferno { -Shader::Shader(const std::string& name) - : m_name(std::move(name)) - , m_id(0) +std::shared_ptr Shader::create(std::string_view path) { + auto result = std::shared_ptr(new Shader(path)); + // Get file contents - std::string vertexSrc = ruc::File(name + ".vert").data(); - std::string fragmentSrc = ruc::File(name + ".frag").data(); + auto stringPath = std::string(path); + std::string vertexSrc = ruc::File(stringPath + ".vert").data(); + std::string fragmentSrc = ruc::File(stringPath + ".frag").data(); // Compile shaders - uint32_t vertexID = compileShader(GL_VERTEX_SHADER, vertexSrc.c_str()); - uint32_t fragmentID = compileShader(GL_FRAGMENT_SHADER, fragmentSrc.c_str()); + uint32_t vertexID = result->compileShader(GL_VERTEX_SHADER, vertexSrc.c_str()); + uint32_t fragmentID = result->compileShader(GL_FRAGMENT_SHADER, fragmentSrc.c_str()); // Link shaders if (vertexID > 0 && fragmentID > 0) { - m_id = linkShader(vertexID, fragmentID); + result->m_id = result->linkShader(vertexID, fragmentID); } // Clear resources else if (vertexID > 0) glDeleteShader(vertexID); else if (fragmentID > 0) glDeleteShader(fragmentID); + + return result; } Shader::~Shader() @@ -49,62 +50,62 @@ Shader::~Shader() } } -int32_t Shader::findUniform(const std::string& name) const +int32_t Shader::findUniform(std::string_view name) const { - int32_t location = glGetUniformLocation(m_id, name.c_str()); + int32_t location = glGetUniformLocation(m_id, name.data()); VERIFY(location != -1, "Shader could not find uniform '{}'", name); return location; } -void Shader::setInt(const std::string& name, int value) +void Shader::setInt(std::string_view name, int value) { // Set uniform int glUniform1i(findUniform(name), value); } -void Shader::setInt(const std::string& name, int* values, uint32_t count) +void Shader::setInt(std::string_view name, int* values, uint32_t count) { // Set uniform int array glUniform1iv(findUniform(name), count, values); } -void Shader::setFloat(const std::string& name, float value) const +void Shader::setFloat(std::string_view name, float value) const { // Set uniform float glUniform1f(findUniform(name), value); } -void Shader::setFloat(const std::string& name, float v1, float v2, float v3, float v4) const +void Shader::setFloat(std::string_view name, float v1, float v2, float v3, float v4) const { // Set uniform vec4 data glUniform4f(findUniform(name), v1, v2, v3, v4); } -void Shader::setFloat(const std::string& name, glm::vec2 value) const +void Shader::setFloat(std::string_view name, glm::vec2 value) const { // Set uniform vec2 data glUniform2f(findUniform(name), value.x, value.y); } -void Shader::setFloat(const std::string& name, glm::vec3 value) const +void Shader::setFloat(std::string_view name, glm::vec3 value) const { // Set uniform vec3 data glUniform3f(findUniform(name), value.x, value.y, value.z); } -void Shader::setFloat(const std::string& name, glm::vec4 value) const +void Shader::setFloat(std::string_view name, glm::vec4 value) const { // Set uniform vec4 data glUniform4f(findUniform(name), value.x, value.y, value.z, value.w); } -void Shader::setFloat(const std::string& name, glm::mat3 matrix) const +void Shader::setFloat(std::string_view name, glm::mat3 matrix) const { // Set uniform mat3 data glUniformMatrix3fv(findUniform(name), 1, GL_FALSE, glm::value_ptr(matrix)); } -void Shader::setFloat(const std::string& name, glm::mat4 matrix) const +void Shader::setFloat(std::string_view name, glm::mat4 matrix) const { // Set uniform mat4 data glUniformMatrix4fv(findUniform(name), 1, GL_FALSE, glm::value_ptr(matrix)); @@ -199,80 +200,4 @@ int32_t Shader::checkStatus(uint32_t check, bool isProgram) const return success; } -// ----------------------------------------- - -ShaderManager::ShaderManager(s) -{ - ruc::info("ShaderManager initialized"); -} - -ShaderManager::~ShaderManager() -{ -} - -void ShaderManager::add(const std::string& name, std::shared_ptr shader) -{ - // Construct (key, value) pair and insert it into the unordered_map - m_shaderList.emplace(std::move(name), std::move(shader)); -} - -std::shared_ptr ShaderManager::load(const std::string& name) -{ - if (exists(name)) { - return get(name); - } - - std::shared_ptr shader = std::make_shared(name); - add(name, shader); - return get(name); -} - -std::shared_ptr ShaderManager::load(const std::string& vertexSource, - const std::string& fragmentSource) -{ - std::string name = computeName(vertexSource, fragmentSource); - return load(name); -} - -std::shared_ptr ShaderManager::get(const std::string& name) -{ - return exists(name) ? m_shaderList.at(name) : nullptr; -} - -bool ShaderManager::exists(const std::string& name) -{ - return m_shaderList.find(name) != m_shaderList.end(); -} - -void ShaderManager::remove(const std::string& name) -{ - if (exists(name)) { - m_shaderList.erase(name); - } -} - -void ShaderManager::remove(std::shared_ptr shader) -{ - if (exists(shader->name())) { - m_shaderList.erase(shader->name()); - } -} - -std::string ShaderManager::computeName(const std::string& vertexSource, - const std::string& fragmentSource) -{ - auto vertexPos = vertexSource.find_last_of('.'); - auto fragmentPos = fragmentSource.find_last_of('.'); - - VERIFY(vertexPos != std::string::npos, "Shader did not have file extension: '{}'", vertexSource); - VERIFY(fragmentPos != std::string::npos, "Shader did not have file extension: '{}'", fragmentSource); - - auto vertexName = vertexSource.substr(0, vertexPos); - auto fragmentName = vertexSource.substr(0, fragmentPos); - - VERIFY(vertexName == fragmentName, "Shader names did not match: {} {}", vertexSource, fragmentSource); - - return vertexName; -} - } // namespace Inferno diff --git a/src/inferno/asset/shader.h b/src/inferno/asset/shader.h new file mode 100644 index 0000000..f4458f0 --- /dev/null +++ b/src/inferno/asset/shader.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2022,2024 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#include // int32_t, uint32_t +#include + +#include "glm/fwd.hpp" // glm::mat3, glm::mat4, glm::vec2, glm::vec3 + +#include "inferno/asset/asset-manager.h" + +namespace Inferno { + +class Shader : public Asset { +public: + virtual ~Shader(); + + // Factory function + static std::shared_ptr create(std::string_view path); + + int32_t findUniform(std::string_view name) const; + + void setInt(std::string_view name, int value); + void setInt(std::string_view name, int* values, uint32_t count); + void setFloat(std::string_view name, float value) const; + void setFloat(std::string_view name, float v1, float v2, float v3, float v4) const; + void setFloat(std::string_view name, glm::vec2 value) const; + void setFloat(std::string_view name, glm::vec3 value) const; + void setFloat(std::string_view name, glm::vec4 value) const; + void setFloat(std::string_view name, glm::mat3 matrix) const; + void setFloat(std::string_view name, glm::mat4 matrix) const; + + void bind() const; + void unbind() const; + + uint32_t id() const { return m_id; } + +protected: + uint32_t compileShader(int32_t type, const char* shaderSource) const; + uint32_t linkShader(uint32_t vertex, uint32_t fragment) const; + int32_t checkStatus(uint32_t check, bool isProgram = false) const; + +private: + Shader(std::string_view path) + : Asset(path) + { + } + + virtual bool isShader() const override { return true; } + +private: + uint32_t m_id { 0 }; +}; + +// ----------------------------------------- + +// clang-format off +template<> +inline bool Asset::fastIs() const { return isShader(); } +// clang-format on + +} // namespace Inferno diff --git a/src/inferno/render/texture.cpp b/src/inferno/asset/texture.cpp similarity index 76% rename from src/inferno/render/texture.cpp rename to src/inferno/asset/texture.cpp index 861fdc7..0f7011c 100644 --- a/src/inferno/render/texture.cpp +++ b/src/inferno/asset/texture.cpp @@ -7,15 +7,13 @@ #include // UINT_MAX #include // uint8_t, uint32_t #include // std::shared_ptr -#include // std::move #include "glad/glad.h" -#include "ruc/format/log.h" #include "ruc/meta/assert.h" #define STB_IMAGE_IMPLEMENTATION #include "stb/stb_image.h" -#include "inferno/render/texture.h" +#include "inferno/asset/texture.h" namespace Inferno { @@ -35,10 +33,9 @@ void Texture::init(uint32_t width, uint32_t height, uint8_t channels) // ----------------------------------------- -std::shared_ptr Texture2D::create(const std::string& path) +std::shared_ptr Texture2D::create(std::string_view path) { - auto result = std::shared_ptr(new Texture2D); - result->m_path = path; + auto result = std::shared_ptr(new Texture2D(path)); int width = 0; int height = 0; @@ -47,7 +44,7 @@ std::shared_ptr Texture2D::create(const std::string& path) // Load image data stbi_set_flip_vertically_on_load(1); - data = stbi_load(path.c_str(), &width, &height, &channels, STBI_default); + data = stbi_load(path.data(), &width, &height, &channels, STBI_default); VERIFY(data, "failed to load image: '{}'", path); result->init(width, height, channels); @@ -115,10 +112,9 @@ void Texture2D::create(unsigned char* data) // ----------------------------------------- -std::shared_ptr TextureCubemap::create(const std::string& path) +std::shared_ptr TextureCubemap::create(std::string_view path) { - auto result = std::shared_ptr(new TextureCubemap); - result->m_path = path; + auto result = std::shared_ptr(new TextureCubemap(path)); result->create(); @@ -201,57 +197,7 @@ void TextureCubemap::create() glBindTexture(GL_TEXTURE_CUBE_MAP, 0); } -// ----------------------------------------- - -TextureManager::TextureManager(s) -{ - ruc::info("TextureManager initialized"); -} - -TextureManager::~TextureManager() -{ -} - -void TextureManager::add(const std::string& path, std::shared_ptr texture) -{ - // Construct (key, value) pair and insert it into the unordered_map - m_textureList.emplace(std::move(path), std::move(texture)); -} - -std::shared_ptr TextureManager::load(const std::string& path, Texture::Type type) -{ - if (exists(path)) { - return get(path); - } - - auto texture = (type == Texture::TwoDimensional) ? Texture2D::create(path) : TextureCubemap::create(path); - add(path, texture); - - return texture; -} - -std::shared_ptr TextureManager::get(const std::string& path) -{ - return exists(path) ? m_textureList.at(path) : nullptr; -} - -bool TextureManager::exists(const std::string& path) -{ - return m_textureList.find(path) != m_textureList.end(); -} - -void TextureManager::remove(const std::string& path) -{ - if (exists(path)) { - m_textureList.erase(path); - } -} - -void TextureManager::remove(std::shared_ptr texture) -{ - if (exists(texture->path())) { - m_textureList.erase(texture->path()); - } -} - } // namespace Inferno + +// FIXME +// auto texture = (type == Texture::TwoDimensional) ? Texture2D::create(path) : TextureCubemap::create(path); diff --git a/src/inferno/render/texture.h b/src/inferno/asset/texture.h similarity index 53% rename from src/inferno/render/texture.h rename to src/inferno/asset/texture.h index 0185cdb..298d5f8 100644 --- a/src/inferno/render/texture.h +++ b/src/inferno/asset/texture.h @@ -6,33 +6,26 @@ #pragma once -#include // uint8_t, uint32_t -#include // std::shared_ptr -#include // std::string -#include // std::unordered_map +#include // uint8_t, uint32_t +#include // std::shared_ptr +#include -#include "ruc/singleton.h" +#include "inferno/asset/asset-manager.h" namespace Inferno { class Texture2D; class TextureCubemap; -class Texture { +class Texture : public Asset { public: virtual ~Texture(); - enum Type : uint8_t { - TwoDimensional = 0, - Cubemap, - }; - void init(uint32_t width, uint32_t height, uint8_t channels); virtual void bind(uint32_t unit = 0) const = 0; virtual void unbind() const = 0; - std::string path() const { return m_path; } uint32_t width() const { return m_width; } uint32_t height() const { return m_height; } uint32_t id() const { return m_id; } @@ -40,21 +33,26 @@ public: uint32_t dataFormat() const { return m_dataFormat; } virtual bool is2D() const { return false; } - virtual bool isCubeMap() const { return false; } + virtual bool isCubemap() const { return false; } friend Texture2D; friend TextureCubemap; protected: - Texture() {} + Texture(std::string_view path) + : Asset(path) + { + } protected: - std::string m_path; uint32_t m_width { 0 }; uint32_t m_height { 0 }; uint32_t m_id { 0 }; uint32_t m_internalFormat { 0 }; uint32_t m_dataFormat { 0 }; + +private: + virtual bool isTexture() const override { return true; } }; // ------------------------------------- @@ -64,15 +62,18 @@ public: virtual ~Texture2D() = default; // Factory function - static std::shared_ptr create(const std::string& path); + static std::shared_ptr create(std::string_view path); virtual void bind(uint32_t unit = 0) const override; virtual void unbind() const override; private: - Texture2D() {} + Texture2D(std::string_view path) + : Texture(path) + { + } - virtual bool is2D() const override { return true; } + virtual bool isTexture2D() const override { return true; } private: void create(unsigned char* data); @@ -85,37 +86,34 @@ public: virtual ~TextureCubemap() = default; // Factory function - static std::shared_ptr create(const std::string& path); + static std::shared_ptr create(std::string_view path); virtual void bind(uint32_t unit = 0) const override; virtual void unbind() const override; private: - TextureCubemap() {}; + TextureCubemap(std::string_view path) + : Texture(path) + { + } - virtual bool isCubeMap() const override { return true; } + virtual bool isTextureCubemap() const override { return true; } private: void create(); }; -// ------------------------------------- - -class TextureManager final : public ruc::Singleton { -public: - TextureManager(s); - ~TextureManager(); +// ----------------------------------------- - void add(const std::string& path, std::shared_ptr texture); - std::shared_ptr load(const std::string& path, Texture::Type type = Texture::Type::TwoDimensional); - std::shared_ptr get(const std::string& path); - bool exists(const std::string& path); +// clang-format off +template<> +inline bool Asset::fastIs() const { return isTexture(); } - void remove(const std::string& path); - void remove(std::shared_ptr texture); +template<> +inline bool Asset::fastIs() const { return isTexture2D(); } -private: - std::unordered_map> m_textureList; -}; +template<> +inline bool Asset::fastIs() const { return isTextureCubemap(); } +// clang-format on } // namespace Inferno diff --git a/src/inferno/component/cubemap-component.cpp b/src/inferno/component/cubemap-component.cpp index 14fcbd4..245c5be 100644 --- a/src/inferno/component/cubemap-component.cpp +++ b/src/inferno/component/cubemap-component.cpp @@ -6,9 +6,9 @@ #include "ruc/json/json.h" +#include "inferno/asset/texture.h" #include "inferno/component/cubemap-component.h" #include "inferno/component/spritecomponent.h" // TODO: Move glm::x toJson/fromJson to separate file -#include "inferno/render/texture.h" namespace Inferno { @@ -20,7 +20,7 @@ void fromJson(const ruc::Json& json, CubemapComponent& value) json.at("color").getTo(value.color); } if (json.exists("texture") && json.at("texture").type() == ruc::Json::Type::String) { - value.texture = TextureManager::the().load(json.at("texture").asString(), Texture::Type::Cubemap); + value.texture = AssetManager::the().load(json.at("texture").asString()); } } diff --git a/src/inferno/component/cubemap-component.h b/src/inferno/component/cubemap-component.h index 4c68c6e..011836c 100644 --- a/src/inferno/component/cubemap-component.h +++ b/src/inferno/component/cubemap-component.h @@ -11,7 +11,7 @@ #include "glm/ext/vector_float4.hpp" // glm::vec4 #include "ruc/json/json.h" -#include "inferno/render/texture.h" +#include "inferno/asset/texture.h" namespace Inferno { diff --git a/src/inferno/component/spritecomponent.cpp b/src/inferno/component/spritecomponent.cpp index 405962e..a97a02d 100644 --- a/src/inferno/component/spritecomponent.cpp +++ b/src/inferno/component/spritecomponent.cpp @@ -5,7 +5,8 @@ */ #include "inferno/component/spritecomponent.h" -#include "inferno/render/texture.h" +#include "inferno/asset/asset-manager.h" +#include "inferno/asset/texture.h" namespace Inferno { @@ -17,7 +18,7 @@ void fromJson(const ruc::Json& json, SpriteComponent& value) json.at("color").getTo(value.color); } if (json.exists("texture") && json.at("texture").type() == ruc::Json::Type::String) { - value.texture = TextureManager::the().load(json.at("texture").asString()); + value.texture = AssetManager::the().load(json.at("texture").asString()); } } diff --git a/src/inferno/component/spritecomponent.h b/src/inferno/component/spritecomponent.h index 49a333f..939f9e5 100644 --- a/src/inferno/component/spritecomponent.h +++ b/src/inferno/component/spritecomponent.h @@ -11,7 +11,7 @@ #include "glm/ext/vector_float4.hpp" // glm::vec4 #include "ruc/json/json.h" -#include "inferno/render/texture.h" +#include "inferno/asset/texture.h" namespace Inferno { diff --git a/src/inferno/render/renderer.cpp b/src/inferno/render/renderer.cpp index 75bed66..991f782 100644 --- a/src/inferno/render/renderer.cpp +++ b/src/inferno/render/renderer.cpp @@ -10,12 +10,13 @@ #include "glad/glad.h" #include "ruc/format/log.h" +#include "inferno/asset/asset-manager.h" +#include "inferno/asset/shader.h" +#include "inferno/asset/texture.h" #include "inferno/component/transformcomponent.h" #include "inferno/render/buffer.h" #include "inferno/render/render-command.h" #include "inferno/render/renderer.h" -#include "inferno/render/shader.h" -#include "inferno/render/texture.h" namespace Inferno { @@ -271,7 +272,7 @@ void Renderer2D::drawQuad(const TransformComponent& transform, glm::mat4 color, void Renderer2D::loadShader() { - m_shader = ShaderManager::the().load("assets/glsl/batch-quad"); + m_shader = AssetManager::the().load("assets/glsl/batch-quad"); } // ----------------------------------------- @@ -384,7 +385,7 @@ void RendererCubemap::drawCubemap(const TransformComponent& transform, glm::mat4 void RendererCubemap::loadShader() { - m_shader = ShaderManager::the().load("assets/glsl/batch-cubemap"); + m_shader = AssetManager::the().load("assets/glsl/batch-cubemap"); } // ----------------------------------------- @@ -455,7 +456,7 @@ void RendererFont::drawSymbol(std::array& symbolQua void RendererFont::loadShader() { - m_shader = ShaderManager::the().load("assets/glsl/batch-font"); + m_shader = AssetManager::the().load("assets/glsl/batch-font"); } } // namespace Inferno diff --git a/src/inferno/render/shader.h b/src/inferno/render/shader.h deleted file mode 100644 index 41ad245..0000000 --- a/src/inferno/render/shader.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2022 Riyyi - * - * SPDX-License-Identifier: MIT - */ - -#pragma once - -#include // int32_t, uint32_t -#include // std::shared_ptr -#include // std::string -#include // std::unordered_map - -#include "glm/glm.hpp" -#include "ruc/singleton.h" - -namespace Inferno { - -class Shader { -public: - Shader(const std::string& name); - virtual ~Shader(); - - int32_t findUniform(const std::string& name) const; - - void setInt(const std::string& name, int value); - void setInt(const std::string& name, int* values, uint32_t count); - void setFloat(const std::string& name, float value) const; - void setFloat(const std::string& name, float v1, float v2, float v3, float v4) const; - void setFloat(const std::string& name, glm::vec2 value) const; - void setFloat(const std::string& name, glm::vec3 value) const; - void setFloat(const std::string& name, glm::vec4 value) const; - void setFloat(const std::string& name, glm::mat3 matrix) const; - void setFloat(const std::string& name, glm::mat4 matrix) const; - - void bind() const; - void unbind() const; - - inline std::string name() const { return m_name; } - inline uint32_t id() const { return m_id; } - -protected: - uint32_t compileShader(int32_t type, const char* shaderSource) const; - uint32_t linkShader(uint32_t vertex, uint32_t fragment) const; - int32_t checkStatus(uint32_t check, bool isProgram = false) const; - -private: - std::string m_name; - uint32_t m_id; -}; - -// ------------------------------------- - -class ShaderManager final : public ruc::Singleton { -public: - ShaderManager(s); - virtual ~ShaderManager(); - - void add(const std::string& name, std::shared_ptr shader); - std::shared_ptr load(const std::string& name); - std::shared_ptr load(const std::string& vertexSource, - const std::string& fragmentSource); - std::shared_ptr get(const std::string& name); - bool exists(const std::string& name); - - void remove(const std::string& name); - void remove(std::shared_ptr shader); - -protected: - std::string computeName(const std::string& vertexSource, - const std::string& fragmentSource); - -private: - std::unordered_map> m_shaderList; -}; - -} // namespace Inferno diff --git a/src/inferno/system/textareasystem.cpp b/src/inferno/system/textareasystem.cpp index 823be7d..75747db 100644 --- a/src/inferno/system/textareasystem.cpp +++ b/src/inferno/system/textareasystem.cpp @@ -4,17 +4,15 @@ * SPDX-License-Identifier: MIT */ -#include // std::max - #include "ruc/format/log.h" #include "ruc/meta/assert.h" #include "inferno/application.h" +#include "inferno/asset/font.h" +#include "inferno/asset/texture.h" #include "inferno/component/textareacomponent.h" #include "inferno/component/transformcomponent.h" -#include "inferno/render/font.h" #include "inferno/render/renderer.h" -#include "inferno/render/texture.h" #include "inferno/scene/scene.h" #include "inferno/system/textareasystem.h" #include "inferno/window.h" @@ -46,7 +44,7 @@ void TextAreaSystem::render() // Calculate symbol quad // Submit symbol quad for rendering - std::shared_ptr font = FontManager::the().load(textarea.font); + std::shared_ptr font = AssetManager::the().load(textarea.font); // glm::mat4 translate = transform.translate; m_symbols.clear(); diff --git a/src/inferno/system/textareasystem.h b/src/inferno/system/textareasystem.h index 5a71a00..5af29d3 100644 --- a/src/inferno/system/textareasystem.h +++ b/src/inferno/system/textareasystem.h @@ -6,15 +6,13 @@ #pragma once -#include // std::uint32_t #include // std::shared_ptr #include // std::optional #include // std::vector -#include "glm/ext/vector_float3.hpp" // glm::vec3 #include "ruc/singleton.h" -#include "inferno/render/font.h" +#include "inferno/asset/font.h" #include "inferno/render/renderer.h" namespace Inferno { diff --git a/vendor/ruc b/vendor/ruc index 07c9f99..8520952 160000 --- a/vendor/ruc +++ b/vendor/ruc @@ -1 +1 @@ -Subproject commit 07c9f9959d3ce46da8bc7b0777d803524f9d1ec0 +Subproject commit 85209522358c83dc28df2499b4d70c754121e74d