Skip to content

Commit

Permalink
Remove duplicates in IndexBuffer
Browse files Browse the repository at this point in the history
- Use a vector instead of an array to remove the array size from the attributes
- Use a generic constructor for the constructor
  • Loading branch information
massile committed Nov 15, 2016
1 parent c33bc2c commit 55eb8ae
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/Engine2D/graphics/batchRenderer2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GLuint> 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;
Expand All @@ -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);
Expand Down
17 changes: 0 additions & 17 deletions src/Engine2D/graphics/buffer/indexBuffer.cpp
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
14 changes: 9 additions & 5 deletions src/Engine2D/graphics/buffer/indexBuffer.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
#pragma once

#include <GL/glew.h>
#include <vector>
#include "../../api/API.h"

namespace FlowEngine { namespace Graphics {

class IndexBuffer
{
private:
GLuint mBufferID;
GLuint mCount;
public:
IndexBuffer(GLushort* data, GLsizei count);
IndexBuffer(GLuint* data, GLsizei count);
template <typename T>
IndexBuffer(const std::vector<T>& 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; }
};

}}

0 comments on commit 55eb8ae

Please sign in to comment.