refactor: Batch2D and Batch3D

This commit is contained in:
MihailRis
2024-06-06 11:24:16 +03:00
parent 18a3dcaabf
commit 7db76a0e6a
4 changed files with 57 additions and 42 deletions
+11 -10
View File
@@ -13,19 +13,18 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){
{2}, {2}, {4}, {0}
};
buffer = new float[capacity * B2D_VERTEX_SIZE];
mesh = std::make_unique<Mesh>(buffer, 0, attrs);
buffer = std::make_unique<float[]>(capacity * B2D_VERTEX_SIZE);
mesh = std::make_unique<Mesh>(buffer.get(), 0, attrs);
index = 0;
ubyte pixels[] = {
0xFF, 0xFF, 0xFF, 0xFF
};
blank = std::make_unique<Texture>(pixels, 1, 1, ImageFormat::rgba8888);
_texture = nullptr;
currentTexture = nullptr;
}
Batch2D::~Batch2D(){
delete[] buffer;
}
void Batch2D::setPrimitive(DrawPrimitive primitive) {
@@ -37,7 +36,7 @@ void Batch2D::setPrimitive(DrawPrimitive primitive) {
}
void Batch2D::begin(){
_texture = nullptr;
currentTexture = nullptr;
blank->bind();
color = glm::vec4(1.0f);
primitive = DrawPrimitive::triangle;
@@ -73,14 +72,16 @@ void Batch2D::vertex(
}
void Batch2D::texture(Texture* new_texture){
if (_texture == new_texture)
if (currentTexture == new_texture) {
return;
}
flush();
_texture = new_texture;
if (new_texture == nullptr)
currentTexture = new_texture;
if (new_texture == nullptr) {
blank->bind();
else
} else {
new_texture->bind();
}
}
void Batch2D::untexture() {
@@ -327,7 +328,7 @@ void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index
void Batch2D::flush() {
if (index == 0)
return;
mesh->reload(buffer, index / B2D_VERTEX_SIZE);
mesh->reload(buffer.get(), index / B2D_VERTEX_SIZE);
mesh->draw(gl::to_glenum(primitive));
index = 0;
}