mge-engine / mge

MIT License
0 stars 0 forks source link

#112 Support different index buffer usage #184

Closed github-actions[bot] closed 6 months ago

github-actions[bot] commented 6 months ago

https://github.com/mge-engine/mge/blob/b14e04e21e597f75ddfb167a739b474969d33cdd/src/modules/opengl/index_buffer.cpp#L18


// mge - Modern Game Engine
// Copyright (c) 2017-2023 by Alexander Schroeder
// All rights reserved.
#include "index_buffer.hpp"
#include "error.hpp"
#include "mge/core/checked_cast.hpp"
#include "render_context.hpp"
namespace mge::opengl {
    index_buffer::index_buffer(render_context& context,
                               mge::data_type  dt,
                               size_t          data_size,
                               void*           data)
        : mge::index_buffer(context, dt, data_size, data)
        , m_buffer(0)
    {
        glCreateBuffers(1, &m_buffer);
        CHECK_OPENGL_ERROR(glCreateBuffers);
        // TODO #112 Support different index buffer usage
        glNamedBufferData(m_buffer,
                          mge::checked_cast<GLsizeiptr>(size()),
                          data,
                          GL_STATIC_DRAW);
        CHECK_OPENGL_ERROR(glNamedBufferData);
    }

    index_buffer::~index_buffer()
    {
        if (m_buffer) {
            glDeleteBuffers(1, &m_buffer);
            TRACE_OPENGL_ERROR(glDeleteBuffers);
        }
    }

    void* index_buffer::on_map()
    {
        void* result = glMapNamedBuffer(m_buffer, GL_READ_WRITE);
        CHECK_OPENGL_ERROR(glMapNamedBuffer);
        return result;
    }

    void index_buffer::on_unmap()
    {
        glUnmapNamedBuffer(m_buffer);
        CHECK_OPENGL_ERROR(glUnmapNamedBuffer);
    }

} // namespace mge::opengl