diff --git a/CMakeLists.txt b/CMakeLists.txt index daa6558..fdead73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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} diff --git a/include/lol/Shader.hpp b/include/lol/Shader.hpp index 872d218..e314c22 100644 --- a/include/lol/Shader.hpp +++ b/include/lol/Shader.hpp @@ -7,19 +7,23 @@ #include #include -#include #include namespace lol { + class UniqueShader; + typedef std::shared_ptr 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(vertexShader, fragmentShader); + } inline bool Good() { return id != 0; } void Use(); @@ -33,7 +37,5 @@ namespace lol bool recording = false; }; - typedef std::shared_ptr Shader; - typedef Factory ShaderFactory; - typedef ObjectManager ShaderManager; + typedef ObjectManager ShaderManager; } \ No newline at end of file diff --git a/include/lol/Texture.hpp b/include/lol/Texture.hpp index e486d2d..bf7e350 100644 --- a/include/lol/Texture.hpp +++ b/include/lol/Texture.hpp @@ -1,15 +1,26 @@ #pragma once #include +#include namespace lol { - class Texture2D : public NonCopyable + class UniqueTexture; + typedef std::shared_ptr Texture; + + class UniqueTexture : public NonCopyable { public: - Texture2D(); + UniqueTexture(); + + inline static Texture Share() + { + return std::make_shared(); + } private: unsigned int textureID; }; + + typedef ObjectManager TextureManager; } \ No newline at end of file diff --git a/include/lol/VertexArrayObject.hpp b/include/lol/VertexArrayObject.hpp index ded5277..ca17d8c 100644 --- a/include/lol/VertexArrayObject.hpp +++ b/include/lol/VertexArrayObject.hpp @@ -4,7 +4,6 @@ #include #include -#include #include 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 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(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 VertexArrayObject; - - // Factory for creating said shared pointers. - typedef Factory VAOFactory; - // Object manager for managing said shared pointers - typedef ObjectManager VAOManager; + typedef ObjectManager VAOManager; } \ No newline at end of file diff --git a/include/lol/util/Factory.hpp b/include/lol/util/Factory.hpp deleted file mode 100644 index 14f3ad6..0000000 --- a/include/lol/util/Factory.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include - -#define PRODUCT(type) friend class Factory - -namespace lol -{ - - template - class Factory : public NonCopyable - { - public: - ~Factory() {} - - template - inline static std::shared_ptr Produce(Args&&... args) - { - return std::shared_ptr(new Type(args...)); - } - - private: - Factory() {} - }; - -} \ No newline at end of file diff --git a/include/lol/util/ObjectManager.hpp b/include/lol/util/ObjectManager.hpp index 4de1438..fce0867 100644 --- a/include/lol/util/ObjectManager.hpp +++ b/include/lol/util/ObjectManager.hpp @@ -30,7 +30,7 @@ namespace lol /** * Add new (existing) object to manager */ - inline void Register(unsigned int id, std::shared_ptr 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 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> objects; + std::map objects; }; } \ No newline at end of file diff --git a/src/Shader.cpp b/src/Shader.cpp index b883c1a..74b8a53 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -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) diff --git a/src/Texture.cpp b/src/Texture.cpp index daa3bc9..045da3e 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -4,7 +4,8 @@ namespace lol { - Texture2D::Texture2D() + UniqueTexture::UniqueTexture() : + textureID(0) { } diff --git a/src/VertexArrayObject.cpp b/src/VertexArrayObject.cpp index 6fd8c49..d2b8ed9 100644 --- a/src/VertexArrayObject.cpp +++ b/src/VertexArrayObject.cpp @@ -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);