Brenta Engine 1.2
Loading...
Searching...
No Matches
buffer.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/renderer/opengl/buffer.hpp>
7#include <brenta/logger.hpp>
8
9using namespace brenta;
10
11int Buffer::tot_memory = 0;
12
13Buffer::Buffer(Buffer::Target target)
14{
15 this->init(target);
16 return;
17}
18
19Buffer::~Buffer()
20{
21 this->destroy();
22 return;
23}
24
25void Buffer::init(Buffer::Target target)
26{
27 this->target = target;
28 glGenBuffers(1, &id);
29
30 EVENT(Logger::Event::Lifetime, "buffer: initialized {}", this->id);
31 return;
32}
33
34void Buffer::destroy()
35{
36 if (this->id == 0) return;
37
38 glDeleteBuffers(1, &this->id);
39
40 // Update memory for profiling
41 Buffer::tot_memory -= this->memory;
42 this->memory = 0;
43
44 EVENT(Logger::Event::Lifetime, "buffer: destroyed {}", this->id);
45 this->id = 0;
46 return;
47}
48
49void Buffer::bind() const
50{
51 if (this->id == 0)
52 {
53 ERROR("Buffer::bind: not initialized");
54 return;
55 }
56 glBindBuffer(this->target, this->id);
57 return;
58}
59
60void Buffer::unbind() const
61{
62 glBindBuffer(this->target, 0);
63 return;
64}
65
66Buffer::Id Buffer::get_id() const
67{
68 return this->id;
69}
70
71Buffer::Target &Buffer::get_target()
72{
73 return this->target;
74}
75
76void Buffer::set_id(Buffer::Id id)
77{
78 this->id = id;
79}
80
81void Buffer::set_target(Buffer::Target target)
82{
83 this->target = target;
84}
85
86void Buffer::copy_data(const void *data, GLsizeiptr size,
87 Buffer::DataUsage usage)
88{
89 glBufferData(this->target, size, data, (GLenum) usage);
90
91 // Update memory for profiling
92 Buffer::tot_memory -= this->memory;
93 this->memory = size;
94 Buffer::tot_memory += size;
95 return;
96}