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

@ -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;
};
}