From 55eb8aebec6e72f821e86b7eddda0c78149a69a0 Mon Sep 17 00:00:00 2001 From: Massinissa Mokhtari Date: Tue, 15 Nov 2016 20:55:52 +0100 Subject: [PATCH] Remove duplicates in IndexBuffer - Use a vector instead of an array to remove the array size from the attributes - Use a generic constructor for the constructor --- src/Engine2D/graphics/batchRenderer2d.cpp | 6 +++--- src/Engine2D/graphics/buffer/indexBuffer.cpp | 17 ----------------- src/Engine2D/graphics/buffer/indexBuffer.h | 14 +++++++++----- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/Engine2D/graphics/batchRenderer2d.cpp b/src/Engine2D/graphics/batchRenderer2d.cpp index 6df0b5e..5b94e86 100644 --- a/src/Engine2D/graphics/batchRenderer2d.cpp +++ b/src/Engine2D/graphics/batchRenderer2d.cpp @@ -21,8 +21,8 @@ namespace FlowEngine { namespace Graphics { mVAO->addBuffer(mVBO); - GLuint* indices = new GLuint[RENDERER_INDICES_SIZE]; - for(int i=0, offset=0; i < RENDERER_INDICES_SIZE; i+=6, offset+=4) + std::vector indices(RENDERER_INDICES_SIZE); + for(int i=0, offset=0; i < indices.size(); i+=6, offset+=4) { indices[i] = offset; indices[i+1] = offset + 1; @@ -32,7 +32,7 @@ namespace FlowEngine { namespace Graphics { indices[i+5] = offset; } - mIBO = new IndexBuffer(indices, RENDERER_INDICES_SIZE); + mIBO = new IndexBuffer(indices); mFramebufferShader->enable(); mFramebufferShader->uniform("tex", 0); diff --git a/src/Engine2D/graphics/buffer/indexBuffer.cpp b/src/Engine2D/graphics/buffer/indexBuffer.cpp index e29138b..bc4a399 100644 --- a/src/Engine2D/graphics/buffer/indexBuffer.cpp +++ b/src/Engine2D/graphics/buffer/indexBuffer.cpp @@ -1,24 +1,7 @@ #include "indexBuffer.h" -#include "../../api/API.h" namespace FlowEngine { namespace Graphics { - IndexBuffer::IndexBuffer(GLushort* data, GLsizei count) - : mCount(count), mBufferID(API::createBuffer()) - { - bind(); - API::setBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLushort), data, GL_STATIC_DRAW); - unbind(); - } - - IndexBuffer::IndexBuffer(GLuint *data, GLsizei count) - : mCount(count), mBufferID(API::createBuffer()) - { - bind(); - API::setBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLuint), data, GL_STATIC_DRAW); - unbind(); - } - void IndexBuffer::bind() const { API::bindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferID); diff --git a/src/Engine2D/graphics/buffer/indexBuffer.h b/src/Engine2D/graphics/buffer/indexBuffer.h index e60baff..e5ea35b 100644 --- a/src/Engine2D/graphics/buffer/indexBuffer.h +++ b/src/Engine2D/graphics/buffer/indexBuffer.h @@ -1,6 +1,8 @@ #pragma once #include +#include +#include "../../api/API.h" namespace FlowEngine { namespace Graphics { @@ -8,15 +10,17 @@ namespace FlowEngine { namespace Graphics { { private: GLuint mBufferID; - GLuint mCount; public: - IndexBuffer(GLushort* data, GLsizei count); - IndexBuffer(GLuint* data, GLsizei count); + template + IndexBuffer(const std::vector& indices) : mBufferID(API::createBuffer()) + { + bind(); + API::setBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(T), indices.data(), GL_STATIC_DRAW); + unbind(); + } void bind() const; void unbind() const; - - inline GLuint getCount() const { return mCount; } }; }}