Files
portersky 40ae94788e style: fix include ordering across all source files
- C++ std headers first, then third-party, then local
- Apply consistently in cbt/ and scenes/
2026-05-05 22:40:51 +02:00

73 lines
1.4 KiB
C++

#include "glad/glad.h"
#include "cbt/opengl/buffer.hpp"
namespace cbt::opengl {
buffer::buffer() {
glGenBuffers(1, &m_id);
}
buffer::buffer(buffer_type type)
: m_type(type) {
glGenBuffers(1, &m_id);
}
buffer::~buffer() {
if (m_id) {
glDeleteBuffers(1, &m_id);
}
}
buffer::buffer(buffer&& other) noexcept
: m_id(other.m_id)
, m_type(other.m_type) {
other.m_id = 0;
}
auto buffer::operator=(buffer&& other) noexcept -> buffer& {
if (this != &other) {
if (m_id) {
glDeleteBuffers(1, &m_id);
}
m_id = other.m_id;
m_type = other.m_type;
other.m_id = 0;
}
return *this;
}
auto buffer::bind() const -> void {
glBindBuffer(static_cast<GLenum>(m_type), m_id);
}
auto buffer::unbind() const -> void {
glBindBuffer(static_cast<GLenum>(m_type), 0);
}
auto buffer::upload(void const* data, size_t size) -> void {
bind();
glBufferData(static_cast<GLenum>(m_type), size, data, GL_STATIC_DRAW);
unbind();
}
auto buffer::upload(std::vector<uint8_t> data) -> void {
upload(data.data(), data.size());
}
auto buffer::upload_dynamic(void const* data, size_t size) -> void {
bind();
glBufferData(static_cast<GLenum>(m_type), size, data, GL_DYNAMIC_DRAW);
unbind();
}
auto buffer::id() const -> GLuint {
return m_id;
}
auto buffer::valid() const -> bool {
return m_id != 0;
}
}