Compare commits
2 Commits
d01a4429ca
...
2793956be4
Author | SHA1 | Date |
---|---|---|
Riyyi | 2793956be4 | 6 months ago |
Riyyi | 51c5874318 | 6 months ago |
19 changed files with 405 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
|
@ -0,0 +1,63 @@
|
||||
The glad source code: |
||||
|
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2013-2022 David Herberth |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of |
||||
this software and associated documentation files (the "Software"), to deal in |
||||
the Software without restriction, including without limitation the rights to |
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
||||
the Software, and to permit persons to whom the Software is furnished to do so, |
||||
subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in all |
||||
copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||||
|
||||
|
||||
The Khronos Specifications: |
||||
|
||||
Copyright (c) 2013-2020 The Khronos Group Inc. |
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
you may not use this file except in compliance with the License. |
||||
You may obtain a copy of the License at |
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
||||
Unless required by applicable law or agreed to in writing, software |
||||
distributed under the License is distributed on an "AS IS" BASIS, |
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
See the License for the specific language governing permissions and |
||||
limitations under the License. |
||||
|
||||
|
||||
The EGL Specification and various headers: |
||||
|
||||
Copyright (c) 2007-2016 The Khronos Group Inc. |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and/or associated documentation files (the |
||||
"Materials"), to deal in the Materials without restriction, including |
||||
without limitation the rights to use, copy, modify, merge, publish, |
||||
distribute, sublicense, and/or sell copies of the Materials, and to |
||||
permit persons to whom the Materials are furnished to do so, subject to |
||||
the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Materials. |
||||
|
||||
THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
||||
MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
Loading…
Reference in new issue