mirror of
https://github.com/ApfelTeeSaft/WV-Core.git
synced 2026-08-26 19:43:26 +00:00
Working AssetManager and Shader class
This commit is contained in:
@@ -28,7 +28,10 @@ add_library(WVCore STATIC
|
||||
|
||||
src/app/App.cpp
|
||||
|
||||
src/assets/AssetManager.cpp
|
||||
|
||||
src/rendering/Renderer.cpp
|
||||
src/rendering/Shader.cpp
|
||||
src/rendering/Window.cpp
|
||||
|
||||
${glad_SOURCE_DIR}/src/glad.c
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <wv/wvpch.h>
|
||||
|
||||
namespace WillowVox
|
||||
{
|
||||
template<typename T>
|
||||
struct AssetLoader
|
||||
{
|
||||
static std::shared_ptr<T> Load(const std::string& name);
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <wv/assets/IAssetProvider.h>
|
||||
#include <wv/assets/AssetProvider.h>
|
||||
#include <wv/wvpch.h>
|
||||
#include <typeindex>
|
||||
|
||||
@@ -12,23 +12,22 @@ namespace WillowVox
|
||||
static AssetManager& GetInstance();
|
||||
|
||||
template<typename T>
|
||||
void GetAsset(const std::string& assetName)
|
||||
std::shared_ptr<T> GetAsset(const std::string& name)
|
||||
{
|
||||
IAssetProvider* provider = nullptr;
|
||||
// Get the asset provider for the given type
|
||||
AssetProvider<T>* provider = nullptr;
|
||||
auto it = m_AssetTypes.find(typeid(T));
|
||||
if (it != m_AssetTypes.end())
|
||||
{
|
||||
provider = it->second.get();
|
||||
// Use provider to get the asset
|
||||
}
|
||||
provider = static_cast<AssetProvider<T>*>(it->second.get());
|
||||
else
|
||||
{
|
||||
// Create and register a new provider for this asset type
|
||||
m_AssetTypes[typeid(T)] = std::make_unique<IAssetProvider>();
|
||||
provider = m_AssetTypes[typeid(T)].get();
|
||||
m_AssetTypes[typeid(T)] = std::make_unique<AssetProvider<T>>();
|
||||
provider = static_cast<AssetProvider<T>*>(m_AssetTypes[typeid(T)].get());
|
||||
}
|
||||
|
||||
|
||||
// Get the asset from the provider
|
||||
return provider->GetAsset(name);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -1,31 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <wv/assets/IAssetProvider.h>
|
||||
#include <wv/assets/AssetLoader.h>
|
||||
#include <wv/wvpch.h>
|
||||
|
||||
namespace WillowVox
|
||||
{
|
||||
class IAssetProvider
|
||||
{
|
||||
public:
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class AssetProvider : public IAssetProvider
|
||||
{
|
||||
public:
|
||||
T GetAsset(const std::string& assetName)
|
||||
std::shared_ptr<T> GetAsset(const std::string& name)
|
||||
{
|
||||
auto it = m_Assets.find(assetName);
|
||||
if (it != m_Assets.end())
|
||||
{
|
||||
auto it = m_assets.find(name);
|
||||
if (it != m_assets.end())
|
||||
return it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
T asset = LoadAsset(assetName);
|
||||
m_Assets[assetName] = asset;
|
||||
std::shared_ptr<T> asset = AssetLoader<T>::Load(name);
|
||||
m_assets.emplace(name, asset);
|
||||
return asset;
|
||||
}
|
||||
}
|
||||
|
||||
T LoadAsset(const std::string& assetName);
|
||||
void AddAsset(const std::string& name, std::shared_ptr<T> asset)
|
||||
{
|
||||
m_assets.emplace(name, asset);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, T> m_Assets;
|
||||
std::unordered_map<std::string, std::shared_ptr<T>> m_assets;
|
||||
};
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace WillowVox
|
||||
{
|
||||
class IAssetProvider
|
||||
{
|
||||
public:
|
||||
};
|
||||
}
|
||||
+3
-1
@@ -4,4 +4,6 @@
|
||||
|
||||
#include <wv/Logger.h>
|
||||
|
||||
#include <wv/app/App.h>
|
||||
#include <wv/app/App.h>
|
||||
|
||||
#include <wv/assets/AssetManager.h>
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <wv/assets/AssetLoader.h>
|
||||
#include <wv/wvpch.h>
|
||||
|
||||
namespace WillowVox
|
||||
@@ -7,8 +8,11 @@ namespace WillowVox
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
Shader(const char* vertexShaderPath, const char* fragmentShaderPath);
|
||||
Shader(const char* vertexShaderCode, const char* fragmentShaderCode, bool codePassed);
|
||||
static std::shared_ptr<Shader> FromFiles(const char* vertexShaderPath, const char* fragmentShaderPath);
|
||||
static std::shared_ptr<Shader> FromFiles(const std::string& name);
|
||||
static std::shared_ptr<Shader> FromSource(const char* vertexShaderCode, const char* fragmentShaderCode);
|
||||
|
||||
Shader(unsigned int programId) : _programId(programId) {}
|
||||
~Shader();
|
||||
|
||||
void Bind();
|
||||
@@ -27,4 +31,12 @@ namespace WillowVox
|
||||
private:
|
||||
unsigned int _programId;
|
||||
};
|
||||
|
||||
template<> struct AssetLoader<Shader>
|
||||
{
|
||||
static std::shared_ptr<Shader> Load(const std::string& name)
|
||||
{
|
||||
return Shader::FromFiles(name);
|
||||
}
|
||||
};
|
||||
}
|
||||
+29
-16
@@ -9,8 +9,8 @@
|
||||
|
||||
namespace WillowVox
|
||||
{
|
||||
Shader::Shader(const char* vertexShaderPath, const char* fragmentShaderPath)
|
||||
{
|
||||
std::shared_ptr<Shader> Shader::FromFiles(const char* vertexShaderPath, const char* fragmentShaderPath)
|
||||
{
|
||||
// 1. retrieve the vertex/fragment source code from filePath
|
||||
std::string vertexCode;
|
||||
std::string fragmentCode;
|
||||
@@ -72,24 +72,34 @@ namespace WillowVox
|
||||
}
|
||||
|
||||
// shader program
|
||||
_programId = glCreateProgram();
|
||||
glAttachShader(_programId, vertex);
|
||||
glAttachShader(_programId, fragment);
|
||||
glLinkProgram(_programId);
|
||||
unsigned int programId = glCreateProgram();
|
||||
glAttachShader(programId, vertex);
|
||||
glAttachShader(programId, fragment);
|
||||
glLinkProgram(programId);
|
||||
// print linking errors if any
|
||||
glGetProgramiv(_programId, GL_LINK_STATUS, &success);
|
||||
glGetProgramiv(programId, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(_programId, 512, nullptr, infoLog);
|
||||
glGetProgramInfoLog(programId, 512, nullptr, infoLog);
|
||||
Logger::Error("Error linking shader program: %s", infoLog);
|
||||
}
|
||||
|
||||
// delete the shaders
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
}
|
||||
|
||||
Shader::Shader(const char* vertexShaderCode, const char* fragmentShaderCode, bool codePassed)
|
||||
std::shared_ptr<Shader> shader = std::make_shared<Shader>(programId);
|
||||
return shader;
|
||||
}
|
||||
|
||||
std::shared_ptr<Shader> Shader::FromFiles(const std::string& name)
|
||||
{
|
||||
std::string vertPath = "assets/shaders/" + name + ".vert";
|
||||
std::string fragPath = "assets/shaders/" + name + ".frag";
|
||||
return Shader::FromFiles(vertPath.c_str(), fragPath.c_str());
|
||||
}
|
||||
|
||||
std::shared_ptr<Shader> Shader::FromSource(const char* vertexShaderCode, const char* fragmentShaderCode)
|
||||
{
|
||||
// 2. compile shaders
|
||||
unsigned int vertex, fragment;
|
||||
@@ -121,21 +131,24 @@ namespace WillowVox
|
||||
}
|
||||
|
||||
// shader program
|
||||
_programId = glCreateProgram();
|
||||
glAttachShader(_programId, vertex);
|
||||
glAttachShader(_programId, fragment);
|
||||
glLinkProgram(_programId);
|
||||
unsigned int programId = glCreateProgram();
|
||||
glAttachShader(programId, vertex);
|
||||
glAttachShader(programId, fragment);
|
||||
glLinkProgram(programId);
|
||||
// print linking errors if any
|
||||
glGetProgramiv(_programId, GL_LINK_STATUS, &success);
|
||||
glGetProgramiv(programId, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(_programId, 512, nullptr, infoLog);
|
||||
glGetProgramInfoLog(programId, 512, nullptr, infoLog);
|
||||
Logger::Error("Error linking shader program: %s", infoLog);
|
||||
}
|
||||
|
||||
// delete the shaders
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
|
||||
std::shared_ptr<Shader> shader = std::make_shared<Shader>(programId);
|
||||
return shader;
|
||||
}
|
||||
|
||||
Shader::~Shader()
|
||||
|
||||
Reference in New Issue
Block a user