Riyyi
8 months ago
18 changed files with 342 additions and 395 deletions
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Riyyi |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#include <memory> // std::shared_ptr, std::static_pointer_cast |
||||
#include <string> |
||||
#include <string_view> |
||||
|
||||
#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> 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> asset) |
||||
{ |
||||
if (exists(asset->path())) { |
||||
m_assetList.erase(asset->path()); |
||||
} |
||||
} |
||||
|
||||
} // namespace Inferno
|
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Riyyi |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <memory> // std::shared_ptr |
||||
#include <string> |
||||
#include <string_view> |
||||
#include <unordered_map> |
||||
|
||||
#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<typename T> |
||||
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<typename T> |
||||
concept IsAsset = std::same_as<Asset, T> || std::derived_from<T, Asset>; |
||||
|
||||
// -----------------------------------------
|
||||
|
||||
class AssetManager : public ruc::Singleton<AssetManager> { |
||||
public: |
||||
AssetManager(s); |
||||
virtual ~AssetManager(); |
||||
|
||||
void add(std::string_view path, std::shared_ptr<Asset> asset); |
||||
bool exists(std::string_view path); |
||||
void remove(std::string_view path); |
||||
void remove(std::shared_ptr<Asset> asset); |
||||
|
||||
template<IsAsset T> |
||||
std::shared_ptr<T> get(std::string_view path) |
||||
{ |
||||
if (!exists(path)) { |
||||
return nullptr; |
||||
} |
||||
|
||||
auto asset = m_assetList.at(path.data()); |
||||
VERIFY(is<T>(asset.get()), "expected asset {}, got {}", typeid(T).name(), asset->className()); |
||||
return std::static_pointer_cast<T>(asset); |
||||
} |
||||
|
||||
template<IsAsset T> |
||||
std::shared_ptr<T> load(std::string_view path) |
||||
{ |
||||
if (exists(path)) { |
||||
return get<T>(path); |
||||
} |
||||
|
||||
auto asset = T::create(path); |
||||
add(path, asset); |
||||
return asset; |
||||
} |
||||
|
||||
private: |
||||
std::unordered_map<std::string, std::shared_ptr<Asset>> m_assetList; |
||||
}; |
||||
|
||||
} // namespace Inferno
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2022,2024 Riyyi |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#include <cstdint> // int32_t, uint32_t |
||||
#include <string_view> |
||||
|
||||
#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<Shader> 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<Shader>() const { return isShader(); } |
||||
// clang-format on
|
||||
|
||||
} // namespace Inferno
|
@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Riyyi |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <cstdint> // int32_t, uint32_t |
||||
#include <memory> // std::shared_ptr |
||||
#include <string> // std::string |
||||
#include <unordered_map> // 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<ShaderManager> { |
||||
public: |
||||
ShaderManager(s); |
||||
virtual ~ShaderManager(); |
||||
|
||||
void add(const std::string& name, std::shared_ptr<Shader> shader); |
||||
std::shared_ptr<Shader> load(const std::string& name); |
||||
std::shared_ptr<Shader> load(const std::string& vertexSource, |
||||
const std::string& fragmentSource); |
||||
std::shared_ptr<Shader> get(const std::string& name); |
||||
bool exists(const std::string& name); |
||||
|
||||
void remove(const std::string& name); |
||||
void remove(std::shared_ptr<Shader> shader); |
||||
|
||||
protected: |
||||
std::string computeName(const std::string& vertexSource, |
||||
const std::string& fragmentSource); |
||||
|
||||
private: |
||||
std::unordered_map<std::string, std::shared_ptr<Shader>> m_shaderList; |
||||
}; |
||||
|
||||
} // namespace Inferno
|
Loading…
Reference in new issue