removed unnecessary factory pattern

This commit is contained in:
Lauchmelder 2021-12-23 04:55:19 +01:00
parent 711d18e718
commit 5875f1246f
9 changed files with 55 additions and 71 deletions

View file

@ -19,7 +19,7 @@ add_library(lol STATIC
"src/Shader.cpp"
"src/VertexArrayObject.cpp"
"vendor/glad/src/glad.c"
"include/lol/Texture.hpp" "src/Texture.cpp" "include/lol/util/Factory.hpp" )
"include/lol/Texture.hpp" "src/Texture.cpp" )
target_include_directories(lol PUBLIC
${GLM_INCLUDE_DIRS}

View file

@ -7,19 +7,23 @@
#include <glm/glm.hpp>
#include <lol/util/NonCopyable.hpp>
#include <lol/util/Factory.hpp>
#include <lol/util/ObjectManager.hpp>
namespace lol
{
class UniqueShader;
typedef std::shared_ptr<UniqueShader> Shader;
class AbstractShader : public NonCopyable
class UniqueShader : public NonCopyable
{
PRODUCT(AbstractShader);
public:
AbstractShader(const std::string& vertexShader, const std::string& fragmentShader);
~AbstractShader();
UniqueShader(const std::string& vertexShader, const std::string& fragmentShader);
~UniqueShader();
inline static Shader Share(const std::string& vertexShader, const std::string& fragmentShader)
{
return std::make_shared<UniqueShader>(vertexShader, fragmentShader);
}
inline bool Good() { return id != 0; }
void Use();
@ -33,7 +37,5 @@ namespace lol
bool recording = false;
};
typedef std::shared_ptr<AbstractShader> Shader;
typedef Factory<AbstractShader> ShaderFactory;
typedef ObjectManager<AbstractShader> ShaderManager;
typedef ObjectManager<Shader> ShaderManager;
}

View file

@ -1,15 +1,26 @@
#pragma once
#include <lol/util/NonCopyable.hpp>
#include <lol/util/ObjectManager.hpp>
namespace lol
{
class Texture2D : public NonCopyable
class UniqueTexture;
typedef std::shared_ptr<UniqueTexture> Texture;
class UniqueTexture : public NonCopyable
{
public:
Texture2D();
UniqueTexture();
inline static Texture Share()
{
return std::make_shared<UniqueTexture>();
}
private:
unsigned int textureID;
};
typedef ObjectManager<Texture> TextureManager;
}

View file

@ -4,7 +4,6 @@
#include <memory>
#include <lol/util/NonCopyable.hpp>
#include <lol/util/Factory.hpp>
#include <lol/util/ObjectManager.hpp>
namespace lol
@ -31,36 +30,33 @@ namespace lol
Static, Stream, Dynamic
};
// VAO structure that sets up the buffers and deletes them at the end of the lifecycle
class AbstractVertexArrayObject :
public NonCopyable
{
PRODUCT(AbstractVertexArrayObject);
class UniqueVertexArrayObject;
// You cannot actually create this VAO, you are forced to use a shared pointer
// so the buffers dont get accidentally deleted while another obejct is potentially still using it.
// I find this to be very important since VAOs are supposed to be shared between copies of objects
// if they have the same model.
typedef std::shared_ptr<UniqueVertexArrayObject> VertexArrayObject;
// VAO structure that sets up the buffers and deletes them at the end of the lifecycle
class UniqueVertexArrayObject : public NonCopyable
{
public:
AbstractVertexArrayObject() = delete;
~AbstractVertexArrayObject();
UniqueVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static);
~UniqueVertexArrayObject();
inline static VertexArrayObject Share(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static)
{
return std::make_shared<UniqueVertexArrayObject>(vertices, indices, layout, usage);
}
void Render(unsigned int mode = 4);
private:
AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static);
private:
unsigned int vao, vbo, ebo;
size_t indexCount;
};
// You cannot actually create this VAO, you are forced to use a shared pointer
// so the buffers dont get accidentally deleted while another obejct is potentially still using it.
// I find this to be very important since VAOs are supposed to be shared between copies of objects
// if they have the same model.
typedef std::shared_ptr<AbstractVertexArrayObject> VertexArrayObject;
// Factory for creating said shared pointers.
typedef Factory<AbstractVertexArrayObject> VAOFactory;
// Object manager for managing said shared pointers
typedef ObjectManager<AbstractVertexArrayObject> VAOManager;
typedef ObjectManager<VertexArrayObject> VAOManager;
}

View file

@ -1,26 +0,0 @@
#pragma once
#include <lol/util/NonCopyable.hpp>
#define PRODUCT(type) friend class Factory<type>
namespace lol
{
template<typename Type>
class Factory : public NonCopyable
{
public:
~Factory() {}
template<typename... Args>
inline static std::shared_ptr<Type> Produce(Args&&... args)
{
return std::shared_ptr<Type>(new Type(args...));
}
private:
Factory() {}
};
}

View file

@ -30,7 +30,7 @@ namespace lol
/**
* Add new (existing) object to manager
*/
inline void Register(unsigned int id, std::shared_ptr<Type> obj)
inline void Register(unsigned int id, Type obj)
{
objects.insert(std::make_pair(id, obj));
}
@ -46,7 +46,7 @@ namespace lol
/**
* Retrieve object from manager
*/
inline std::shared_ptr<Type> Get(unsigned int id)
inline Type Get(unsigned int id)
{
auto it = objects.find(id);
@ -60,7 +60,7 @@ namespace lol
ObjectManager() {}
private:
std::map<unsigned int, std::shared_ptr<Type>> objects;
std::map<unsigned int, Type> objects;
};
}

View file

@ -11,7 +11,7 @@ inline
namespace lol
{
AbstractShader::AbstractShader(const std::string& vertexShader, const std::string& fragmentShader) :
UniqueShader::UniqueShader(const std::string& vertexShader, const std::string& fragmentShader) :
id(0)
{
GLint success;
@ -71,17 +71,17 @@ namespace lol
glDeleteShader(vertexShaderID);
}
AbstractShader::~AbstractShader()
UniqueShader::~UniqueShader()
{
glDeleteProgram(id);
}
void AbstractShader::Use()
void UniqueShader::Use()
{
glUseProgram(id);
}
void AbstractShader::SetUniform(const std::string& name, const glm::mat4& value)
void UniqueShader::SetUniform(const std::string& name, const glm::mat4& value)
{
GLint location = glGetUniformLocation(id, name.c_str());
if (location == -1)
@ -90,7 +90,7 @@ namespace lol
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value));
}
void AbstractShader::SetUniform(const std::string& name, const glm::vec4& value)
void UniqueShader::SetUniform(const std::string& name, const glm::vec4& value)
{
GLint location = glGetUniformLocation(id, name.c_str());
if (location == -1)

View file

@ -4,7 +4,8 @@
namespace lol
{
Texture2D::Texture2D()
UniqueTexture::UniqueTexture() :
textureID(0)
{
}

View file

@ -6,14 +6,14 @@
namespace lol
{
AbstractVertexArrayObject::~AbstractVertexArrayObject()
UniqueVertexArrayObject::~UniqueVertexArrayObject()
{
glDeleteBuffers(1, &ebo);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
}
void AbstractVertexArrayObject::Render(GLenum mode)
void UniqueVertexArrayObject::Render(GLenum mode)
{
assert(vao != 0);
@ -22,7 +22,7 @@ namespace lol
glDrawElements(mode, indexCount, GL_UNSIGNED_INT, 0);
}
AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) :
UniqueVertexArrayObject::UniqueVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) :
vao(0), vbo(0), ebo(0), indexCount(indices.size())
{
glGenVertexArrays(1, &vao);