diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index a1bea08b..88181436 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -133,7 +133,7 @@ void SlotView::draw(const GfxContext* pctx, Assets* assets) { } auto batch = pctx->getBatch2D(); - batch->color = color; + batch->setColor(color); if (color.a > 0.0) { batch->texture(nullptr); if (highlighted) { @@ -143,7 +143,7 @@ void SlotView::draw(const GfxContext* pctx, Assets* assets) { } } - batch->color = glm::vec4(1.0f); + batch->setColor(glm::vec4(1.0f)); auto previews = frontend->getBlocksAtlas(); auto indices = content->getIndices(); @@ -190,9 +190,9 @@ void SlotView::draw(const GfxContext* pctx, Assets* assets) { int x = coord.x+slotSize-text.length()*8; int y = coord.y+slotSize-16; - batch->color = glm::vec4(0, 0, 0, 1.0f); + batch->setColor(glm::vec4(0, 0, 0, 1.0f)); font->draw(batch, text, x+1, y+1); - batch->color = glm::vec4(1.0f); + batch->setColor(glm::vec4(1.0f)); font->draw(batch, text, x, y); } } diff --git a/src/frontend/gui/containers.cpp b/src/frontend/gui/containers.cpp index bea33bec..10cb0468 100644 --- a/src/frontend/gui/containers.cpp +++ b/src/frontend/gui/containers.cpp @@ -83,7 +83,7 @@ void Container::draw(const GfxContext* pctx, Assets* assets) { auto batch = pctx->getBatch2D(); batch->texture(nullptr); - batch->render(); + batch->flush(); { GfxContext ctx = pctx->sub(); ctx.scissors(glm::vec4(coord.x, coord.y, size.x, size.y)); @@ -91,7 +91,7 @@ void Container::draw(const GfxContext* pctx, Assets* assets) { if (node->isVisible()) node->draw(pctx, assets); } - batch->render(); + batch->flush(); } } @@ -102,7 +102,7 @@ void Container::drawBackground(const GfxContext* pctx, Assets* assets) { auto batch = pctx->getBatch2D(); batch->texture(nullptr); - batch->color = color; + batch->setColor(color); batch->rect(coord.x, coord.y, size.x, size.y); } diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index f727e6d9..3a5a35db 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -85,7 +85,7 @@ void Label::draw(const GfxContext* pctx, Assets* assets) { auto batch = pctx->getBatch2D(); auto font = assets->getFont(fontName); - batch->color = getColor(); + batch->setColor(getColor()); uint lineHeight = font->getLineHeight(); glm::vec2 size = getSize(); @@ -162,7 +162,7 @@ void Image::draw(const GfxContext* pctx, Assets* assets) { setSize(glm::vec2(texture->width, texture->height)); } batch->texture(texture); - batch->color = color; + batch->setColor(color); batch->rect(coord.x, coord.y, size.x, size.y, 0, 0, 0, UVRegion(), false, true, color); } @@ -254,7 +254,7 @@ void Button::drawBackground(const GfxContext* pctx, Assets* assets) { glm::vec2 coord = calcCoord(); auto batch = pctx->getBatch2D(); batch->texture(nullptr); - batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color)); + batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color)); batch->rect(coord.x, coord.y, size.x, size.y); } @@ -309,7 +309,7 @@ void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) { glm::vec2 coord = calcCoord(); auto batch = pctx->getBatch2D(); batch->texture(nullptr); - batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color)); + batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color)); batch->rect(coord.x, coord.y, size.x, size.y); } @@ -340,13 +340,13 @@ void TextBox::draw(const GfxContext* pctx, Assets* assets) { auto batch = pctx->getBatch2D(); batch->texture(nullptr); if (int((Window::time() - caretLastMove) * 2) % 2 == 0) { - batch->color = glm::vec4(1.0f); + batch->setColor(glm::vec4(1.0f)); int width = font->calcWidth(input, caret); batch->rect(lcoord.x + width, lcoord.y+yoffset, 2, lineHeight); } if (selectionStart != selectionEnd) { - batch->color = glm::vec4(0.8f, 0.9f, 1.0f, 0.5f); + batch->setColor(glm::vec4(0.8f, 0.9f, 1.0f, 0.5f)); int start = font->calcWidth(input, selectionStart); int end = font->calcWidth(input, selectionEnd); batch->rect(lcoord.x + start, lcoord.y+yoffset, end-start, lineHeight); @@ -361,14 +361,14 @@ void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) { if (valid) { if (isFocused()) { - batch->color = focusedColor; + batch->setColor(focusedColor); } else if (hover) { - batch->color = hoverColor; + batch->setColor(hoverColor); } else { - batch->color = color; + batch->setColor(color); } } else { - batch->color = invalidColor; + batch->setColor(invalidColor); } batch->rect(coord.x, coord.y, size.x, size.y); @@ -699,7 +699,7 @@ void InputBindBox::drawBackground(const GfxContext* pctx, Assets* assets) { glm::vec2 coord = calcCoord(); auto batch = pctx->getBatch2D(); batch->texture(nullptr); - batch->color = (isFocused() ? focusedColor : (hover ? hoverColor : color)); + batch->setColor(isFocused() ? focusedColor : (hover ? hoverColor : color)); batch->rect(coord.x, coord.y, size.x, size.y); label->setText(util::str2wstr_utf8(binding.text())); } @@ -739,13 +739,13 @@ void TrackBar::draw(const GfxContext* pctx, Assets* assets) { glm::vec2 coord = calcCoord(); auto batch = pctx->getBatch2D(); batch->texture(nullptr); - batch->color = (hover ? hoverColor : color); + batch->setColor(hover ? hoverColor : color); batch->rect(coord.x, coord.y, size.x, size.y); float width = size.x; float t = (value - min) / (max-min+trackWidth*step); - batch->color = trackColor; + batch->setColor(trackColor); int actualWidth = size.x * (trackWidth / (max-min+trackWidth*step) * step); batch->rect(coord.x + width * t, coord.y, actualWidth, size.y); } @@ -833,7 +833,7 @@ void CheckBox::draw(const GfxContext* pctx, Assets* assets) { glm::vec2 coord = calcCoord(); auto batch = pctx->getBatch2D(); batch->texture(nullptr); - batch->color = checked ? checkColor : (hover ? hoverColor : color); + batch->setColor(checked ? checkColor : (hover ? hoverColor : color)); batch->rect(coord.x, coord.y, size.x, size.y); } diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 9bd2116c..60f1396a 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -579,7 +579,7 @@ void Hud::draw(const GfxContext& ctx){ (width-chsizex)/2, (height-chsizey)/2, chsizex, chsizey, 0,0, 1,1, 1,1,1,1 ); - batch->render(); + batch->flush(); } // Delta-time visualizer @@ -629,7 +629,7 @@ void Hud::draw(const GfxContext& ctx){ } } grabbedItemView->setCoord(glm::vec2(Events::cursor)); - batch->render(); + batch->flush(); } bool Hud::isInventoryOpen() const { diff --git a/src/frontend/screens.cpp b/src/frontend/screens.cpp index 0c5578f7..e54fe2ad 100644 --- a/src/frontend/screens.cpp +++ b/src/frontend/screens.cpp @@ -76,11 +76,13 @@ void MenuScreen::draw(float delta) { batch->begin(); batch->texture(engine->getAssets()->getTexture("gui/menubg")); - batch->rect(0, 0, - width, height, 0, 0, 0, - UVRegion(0, 0, width/64, height/64), - false, false, glm::vec4(1.0f)); - batch->render(); + batch->rect( + 0, 0, + width, height, 0, 0, 0, + UVRegion(0, 0, width/64, height/64), + false, false, glm::vec4(1.0f) + ); + batch->flush(); } static bool backlight; diff --git a/src/graphics/Batch2D.cpp b/src/graphics/Batch2D.cpp index ee295849..ce9fe3ac 100644 --- a/src/graphics/Batch2D.cpp +++ b/src/graphics/Batch2D.cpp @@ -5,120 +5,125 @@ #include -const uint B2D_VERTEX_SIZE = 8; - -using glm::vec2; -using glm::vec3; -using glm::vec4; +inline constexpr uint B2D_VERTEX_SIZE = 8; Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){ - const vattr attrs[] = { - {2}, {2}, {4}, {0} - }; + const vattr attrs[] = { + {2}, {2}, {4}, {0} + }; - buffer = new float[capacity * B2D_VERTEX_SIZE]; - mesh = std::make_unique(buffer, 0, attrs); - index = 0; + buffer = new float[capacity * B2D_VERTEX_SIZE]; + mesh = std::make_unique(buffer, 0, attrs); + index = 0; - ubyte pixels[] = { - 0xFF, 0xFF, 0xFF, 0xFF - }; - blank = new Texture(pixels, 1, 1, GL_RGBA); - _texture = nullptr; + ubyte pixels[] = { + 0xFF, 0xFF, 0xFF, 0xFF + }; + blank = new Texture(pixels, 1, 1, GL_RGBA); + _texture = nullptr; } Batch2D::~Batch2D(){ - delete blank; - delete[] buffer; + delete blank; + delete[] buffer; } void Batch2D::begin(){ - _texture = nullptr; - blank->bind(); - color = vec4(1.0f); + _texture = nullptr; + blank->bind(); + color = glm::vec4(1.0f); } -void Batch2D::vertex(float x, float y, - float u, float v, - float r, float g, float b, float a) { - buffer[index++] = x; - buffer[index++] = y; - buffer[index++] = u; - buffer[index++] = v; - buffer[index++] = r; - buffer[index++] = g; - buffer[index++] = b; - buffer[index++] = a; +void Batch2D::vertex( + float x, float y, + float u, float v, + float r, float g, float b, float a +) { + buffer[index++] = x; + buffer[index++] = y; + buffer[index++] = u; + buffer[index++] = v; + buffer[index++] = r; + buffer[index++] = g; + buffer[index++] = b; + buffer[index++] = a; } -void Batch2D::vertex(vec2 point, - vec2 uvpoint, - float r, float g, float b, float a) { - buffer[index++] = point.x; - buffer[index++] = point.y; - buffer[index++] = uvpoint.x; - buffer[index++] = uvpoint.y; - buffer[index++] = r; - buffer[index++] = g; - buffer[index++] = b; - buffer[index++] = a; +void Batch2D::vertex( + glm::vec2 point, + glm::vec2 uvpoint, + float r, float g, float b, float a +) { + buffer[index++] = point.x; + buffer[index++] = point.y; + buffer[index++] = uvpoint.x; + buffer[index++] = uvpoint.y; + buffer[index++] = r; + buffer[index++] = g; + buffer[index++] = b; + buffer[index++] = a; } void Batch2D::texture(Texture* new_texture){ - if (_texture == new_texture) - return; - render(GL_TRIANGLES); - _texture = new_texture; - if (new_texture == nullptr) - blank->bind(); - else - new_texture->bind(); + if (_texture == new_texture) + return; + flush(GL_TRIANGLES); + _texture = new_texture; + if (new_texture == nullptr) + blank->bind(); + else + new_texture->bind(); +} + +void Batch2D::untexture() { + texture(nullptr); } void Batch2D::point(float x, float y, float r, float g, float b, float a){ - if (index + 6*B2D_VERTEX_SIZE >= capacity) - render(GL_TRIANGLES); + if (index + 6*B2D_VERTEX_SIZE >= capacity) + flush(GL_TRIANGLES); - vertex(x, y, 0, 0, r,g,b,a); - render(GL_POINTS); + vertex(x, y, 0, 0, r,g,b,a); + flush(GL_POINTS); } void Batch2D::line(float x1, float y1, float x2, float y2, float r, float g, float b, float a){ - if (index + 6*B2D_VERTEX_SIZE >= capacity) - render(GL_TRIANGLES); + if (index + 6*B2D_VERTEX_SIZE >= capacity) + flush(GL_TRIANGLES); - vertex(x1, y1, 0, 0, r,g,b,a); - vertex(x2, y2, 1, 1, r,g,b,a); - render(GL_LINES); + vertex(x1, y1, 0, 0, r,g,b,a); + vertex(x2, y2, 1, 1, r,g,b,a); + flush(GL_LINES); } void Batch2D::rect(float x, float y, float w, float h){ - const float r = color.r; - const float g = color.g; - const float b = color.b; - const float a = color.a; - if (index + 6*B2D_VERTEX_SIZE >= capacity) - render(GL_TRIANGLES); + const float r = color.r; + const float g = color.g; + const float b = color.b; + const float a = color.a; + if (index + 6*B2D_VERTEX_SIZE >= capacity) + flush(GL_TRIANGLES); - vertex(x, y, 0, 0, r,g,b,a); - vertex(x, y+h, 0, 1, r,g,b,a); - vertex(x+w, y+h, 1, 1, r,g,b,a); + vertex(x, y, 0, 0, r,g,b,a); + vertex(x, y+h, 0, 1, r,g,b,a); + vertex(x+w, y+h, 1, 1, r,g,b,a); - vertex(x, y, 0, 0, r,g,b,a); - vertex(x+w, y+h, 1, 1, r,g,b,a); - vertex(x+w, y, 1, 0, r,g,b,a); + vertex(x, y, 0, 0, r,g,b,a); + vertex(x+w, y+h, 1, 1, r,g,b,a); + vertex(x+w, y, 1, 0, r,g,b,a); } void Batch2D::rect( - float x, float y, - float w, float h, - float ox, float oy, - float angle, - UVRegion region, - bool flippedX, - bool flippedY, - vec4 tint) { - if (index + 6*B2D_VERTEX_SIZE >= capacity) - render(GL_TRIANGLES); + float x, float y, + float w, float h, + float ox, float oy, + float angle, + UVRegion region, + bool flippedX, + bool flippedY, + glm::vec4 tint +) { + if (index + 6*B2D_VERTEX_SIZE >= capacity) + flush(GL_TRIANGLES); float centerX = w*ox; float centerY = h*oy; @@ -202,108 +207,113 @@ void Batch2D::rect( vertex(x4, y4, u4, v4, tint.r, tint.g, tint.b, tint.a); } -void Batch2D::rect(float x, float y, float w, float h, - float u, float v, float tx, float ty, - float r, float g, float b, float a){ - if (index + 6*B2D_VERTEX_SIZE >= capacity) - render(GL_TRIANGLES); - vertex(x, y, u, v+ty, r,g,b,a); - vertex(x+w, y+h, u+tx, v, r,g,b,a); - vertex(x, y+h, u, v, r,g,b,a); +void Batch2D::rect( + float x, float y, float w, float h, + float u, float v, float tx, float ty, + float r, float g, float b, float a +){ + if (index + 6*B2D_VERTEX_SIZE >= capacity) + flush(GL_TRIANGLES); + vertex(x, y, u, v+ty, r,g,b,a); + vertex(x+w, y+h, u+tx, v, r,g,b,a); + vertex(x, y+h, u, v, r,g,b,a); - vertex(x, y, u, v+ty, r,g,b,a); - vertex(x+w, y, u+tx, v+ty, r,g,b,a); - vertex(x+w, y+h, u+tx, v, r,g,b,a); + vertex(x, y, u, v+ty, r,g,b,a); + vertex(x+w, y, u+tx, v+ty, r,g,b,a); + vertex(x+w, y+h, u+tx, v, r,g,b,a); } -void Batch2D::rect(float x, float y, float w, float h, - float r0, float g0, float b0, - float r1, float g1, float b1, - float r2, float g2, float b2, - float r3, float g3, float b3, - float r4, float g4, float b4, int sh){ - if (index + 30*B2D_VERTEX_SIZE >= capacity) - render(GL_TRIANGLES); - vec2 v0 = vec2(x+h/2,y+h/2); - vec2 v1 = vec2(x+w-sh,y); - vec2 v2 = vec2(x+sh,y); - vec2 v3 = vec2(x,y+sh); - vec2 v4 = vec2(x,y+h-sh); - vec2 v5 = vec2(x+sh,y+h); - vec2 v6 = vec2(x+w-h/2,y+h/2); - vec2 v7 = vec2(x+w-sh,y+h); - vec2 v8 = vec2(x+w,y+h-sh); - vec2 v9 = vec2(x+w,y+sh); +void Batch2D::rect( + float x, float y, float w, float h, + float r0, float g0, float b0, + float r1, float g1, float b1, + float r2, float g2, float b2, + float r3, float g3, float b3, + float r4, float g4, float b4, int sh +){ + if (index + 30*B2D_VERTEX_SIZE >= capacity) + flush(GL_TRIANGLES); + glm::vec2 v0(x+h/2,y+h/2); + glm::vec2 v1(x+w-sh,y); + glm::vec2 v2(x+sh,y); + glm::vec2 v3(x,y+sh); + glm::vec2 v4(x,y+h-sh); + glm::vec2 v5(x+sh,y+h); + glm::vec2 v6(x+w-h/2,y+h/2); + glm::vec2 v7(x+w-sh,y+h); + glm::vec2 v8(x+w,y+h-sh); + glm::vec2 v9(x+w,y+sh); - vertex(v0, vec2(0, 0), r1,g1,b1,1.0f); - vertex(v6, vec2(0, 0), r1,g1,b1,1.0f); - vertex(v1, vec2(0, 0), r1,g1,b1,1.0f); + vertex(v0, glm::vec2(0, 0), r1,g1,b1,1.0f); + vertex(v6, glm::vec2(0, 0), r1,g1,b1,1.0f); + vertex(v1, glm::vec2(0, 0), r1,g1,b1,1.0f); - vertex(v0, vec2(0, 0), r1,g1,b1,1.0f); - vertex(v1, vec2(0, 0), r1,g1,b1,1.0f); - vertex(v2, vec2(0, 0), r1,g1,b1,1.0f); + vertex(v0, glm::vec2(0, 0), r1,g1,b1,1.0f); + vertex(v1, glm::vec2(0, 0), r1,g1,b1,1.0f); + vertex(v2, glm::vec2(0, 0), r1,g1,b1,1.0f); - vertex(v0, vec2(0, 0), r0,g0,b0,1.0f); - vertex(v2, vec2(0, 0), r0,g0,b0,1.0f); - vertex(v3, vec2(0, 0), r0,g0,b0,1.0f); + vertex(v0, glm::vec2(0, 0), r0,g0,b0,1.0f); + vertex(v2, glm::vec2(0, 0), r0,g0,b0,1.0f); + vertex(v3, glm::vec2(0, 0), r0,g0,b0,1.0f); - vertex(v0, vec2(0, 0), r1,g1,b1,1.0f); - vertex(v3, vec2(0, 0), r1,g1,b1,1.0f); - vertex(v4, vec2(0, 0), r1,g1,b1,1.0f); + vertex(v0, glm::vec2(0, 0), r1,g1,b1,1.0f); + vertex(v3, glm::vec2(0, 0), r1,g1,b1,1.0f); + vertex(v4, glm::vec2(0, 0), r1,g1,b1,1.0f); - vertex(v0, vec2(0, 0), r2,g2,b2,1.0f); - vertex(v4, vec2(0, 0), r2,g2,b2,1.0f); - vertex(v5, vec2(0, 0), r2,g2,b2,1.0f); + vertex(v0, glm::vec2(0, 0), r2,g2,b2,1.0f); + vertex(v4, glm::vec2(0, 0), r2,g2,b2,1.0f); + vertex(v5, glm::vec2(0, 0), r2,g2,b2,1.0f); - vertex(v0, vec2(0, 0), r3,g3,b3,1.0f); - vertex(v5, vec2(0, 0), r3,g3,b3,1.0f); - vertex(v6, vec2(0, 0), r3,g3,b3,1.0f); + vertex(v0, glm::vec2(0, 0), r3,g3,b3,1.0f); + vertex(v5, glm::vec2(0, 0), r3,g3,b3,1.0f); + vertex(v6, glm::vec2(0, 0), r3,g3,b3,1.0f); - vertex(v6, vec2(0, 0), r3,g3,b3,1.0f); - vertex(v5, vec2(0, 0), r3,g3,b3,1.0f); - vertex(v7, vec2(0, 0), r3,g3,b3,1.0f); + vertex(v6, glm::vec2(0, 0), r3,g3,b3,1.0f); + vertex(v5, glm::vec2(0, 0), r3,g3,b3,1.0f); + vertex(v7, glm::vec2(0, 0), r3,g3,b3,1.0f); - vertex(v6, vec2(0, 0), r4,g4,b4,1.0f); - vertex(v7, vec2(0, 0), r4,g4,b4,1.0f); - vertex(v8, vec2(0, 0), r4,g4,b4,1.0f); + vertex(v6, glm::vec2(0, 0), r4,g4,b4,1.0f); + vertex(v7, glm::vec2(0, 0), r4,g4,b4,1.0f); + vertex(v8, glm::vec2(0, 0), r4,g4,b4,1.0f); - vertex(v6, vec2(0, 0), r3,g3,b3,1.0f); - vertex(v8, vec2(0, 0), r3,g3,b3,1.0f); - vertex(v9, vec2(0, 0), r3,g3,b3,1.0f); + vertex(v6, glm::vec2(0, 0), r3,g3,b3,1.0f); + vertex(v8, glm::vec2(0, 0), r3,g3,b3,1.0f); + vertex(v9, glm::vec2(0, 0), r3,g3,b3,1.0f); - vertex(v6, vec2(0, 0), r2,g2,b2,1.0f); - vertex(v9, vec2(0, 0), r2,g2,b2,1.0f); - vertex(v1, vec2(0, 0), r2,g2,b2,1.0f); + vertex(v6, glm::vec2(0, 0), r2,g2,b2,1.0f); + vertex(v9, glm::vec2(0, 0), r2,g2,b2,1.0f); + vertex(v1, glm::vec2(0, 0), r2,g2,b2,1.0f); } void Batch2D::sprite(Sprite* sprite) { - vec2 position = sprite->position; - vec2 size = sprite->size; - vec2 origin = sprite->origin; - texture(sprite->texture); - rect( - position.x, position.y, - size.x, size.y, - origin.x, origin.y, - sprite->angle, - sprite->region, - sprite->flippedX, - sprite->flippedY, - sprite->color); + glm::vec2 position = sprite->position; + glm::vec2 size = sprite->size; + glm::vec2 origin = sprite->origin; + texture(sprite->texture); + rect( + position.x, position.y, + size.x, size.y, + origin.x, origin.y, + sprite->angle, + sprite->region, + sprite->flippedX, + sprite->flippedY, + sprite->color + ); } -void Batch2D::sprite(float x, float y, float w, float h, const UVRegion& region, vec4 tint){ - rect(x, y, w, h, region.u1, region.v1, region.u2-region.u1, region.v2-region.v1, tint.r, tint.g, tint.b, tint.a); +void Batch2D::sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint){ + rect(x, y, w, h, region.u1, region.v1, region.u2-region.u1, region.v2-region.v1, tint.r, tint.g, tint.b, tint.a); } -void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index, vec4 tint){ - float scale = 1.0f / (float)atlasRes; - float u = (index % atlasRes) * scale; - float v = 1.0f - ((index / atlasRes) * scale) - scale; - rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a); +void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint){ + float scale = 1.0f / (float)atlasRes; + float u = (index % atlasRes) * scale; + float v = 1.0f - ((index / atlasRes) * scale) - scale; + rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a); } -void Batch2D::render(unsigned int gl_primitive) { +void Batch2D::flush(unsigned int gl_primitive) { if (index == 0) return; mesh->reload(buffer, index / B2D_VERTEX_SIZE); @@ -311,10 +321,10 @@ void Batch2D::render(unsigned int gl_primitive) { index = 0; } -void Batch2D::render() { - render(GL_TRIANGLES); +void Batch2D::flush() { + flush(GL_TRIANGLES); } void Batch2D::lineWidth(float width) { - glLineWidth(width); + glLineWidth(width); } diff --git a/src/graphics/Batch2D.h b/src/graphics/Batch2D.h index e7487483..2c6e0e2a 100644 --- a/src/graphics/Batch2D.h +++ b/src/graphics/Batch2D.h @@ -12,56 +12,82 @@ class Texture; class Sprite; class Batch2D { - float* buffer; - size_t capacity; - std::unique_ptr mesh; - size_t index; + float* buffer; + size_t capacity; + std::unique_ptr mesh; + size_t index; + glm::vec4 color; - Texture* blank; - Texture* _texture; + Texture* blank; + Texture* _texture; - void vertex(float x, float y, - float u, float v, - float r, float g, float b, float a); - void vertex(glm::vec2 point, - glm::vec2 uvpoint, - float r, float g, float b, float a); + void vertex( + float x, float y, + float u, float v, + float r, float g, float b, float a + ); + + void vertex( + glm::vec2 point, + glm::vec2 uvpoint, + float r, float g, float b, float a + ); public: - glm::vec4 color; + Batch2D(size_t capacity); + ~Batch2D(); - Batch2D(size_t capacity); - ~Batch2D(); + void begin(); + void texture(Texture* texture); + void untexture(); + void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint); + void sprite(Sprite* sprite); + void sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint); + void point(float x, float y, float r, float g, float b, float a); + + inline void setColor(glm::vec4 color) { + this->color = color; + } + inline glm::vec4 getColor() const { + return color; + } + + void line( + float x1, float y1, + float x2, float y2, + float r, float g, float b, float a + ); - void begin(); - void texture(Texture* texture); - void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint); - void sprite(Sprite* sprite); - void sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint); - void point(float x, float y, float r, float g, float b, float a); - void line(float x1, float y1, float x2, float y2, float r, float g, float b, float a); - void rect(float x, float y, - float w, float h, - float ox, float oy, - float angle, UVRegion region, - bool flippedX, bool flippedY, - glm::vec4 tint); + void rect( + float x, float y, + float w, float h, + float ox, float oy, + float angle, UVRegion region, + bool flippedX, bool flippedY, + glm::vec4 tint + ); - void rect(float x, float y, float w, float h); - void rect(float x, float y, float w, float h, - float u, float v, float tx, float ty, - float r, float g, float b, float a); + void rect(float x, float y, float w, float h); - void rect(float x, float y, float w, float h, - float r0, float g0, float b0, - float r1, float g1, float b1, - float r2, float g2, float b2, - float r3, float g3, float b3, - float r4, float g4, float b4, int sh); - void render(unsigned int gl_primitive); - void render(); + void rect( + float x, float y, float w, float h, + float u, float v, float tx, float ty, + float r, float g, float b, float a + ); - void lineWidth(float width); + void rect( + float x, float y, float w, float h, + float r0, float g0, float b0, + float r1, float g1, float b1, + float r2, float g2, float b2, + float r3, float g3, float b3, + float r4, float g4, float b4, int sh + ); + + void flush(unsigned int gl_primitive); + void flush(); + + void lineWidth(float width); }; #endif /* SRC_GRAPHICS_BATCH2D_H_ */ diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 510249b4..bf9e67c1 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -44,6 +44,8 @@ void Font::draw(Batch2D* batch, std::wstring text, int x, int y) { static inline void drawGlyph(Batch2D* batch, int x, int y, uint c, FontStyle style) { switch (style){ + case FontStyle::none: + break; case FontStyle::shadow: batch->sprite(x+1, y+1, GLYPH_SIZE, GLYPH_SIZE, 16, c, SHADOW_TINT); break; @@ -57,7 +59,7 @@ static inline void drawGlyph(Batch2D* batch, int x, int y, uint c, FontStyle sty } break; } - batch->sprite(x, y, GLYPH_SIZE, GLYPH_SIZE, 16, c, batch->color); + batch->sprite(x, y, GLYPH_SIZE, GLYPH_SIZE, 16, c, batch->getColor()); } void Font::draw(Batch2D* batch, std::wstring text, int x, int y, FontStyle style) { @@ -74,7 +76,7 @@ void Font::draw(Batch2D* batch, std::wstring_view text, int x, int y, FontStyle x += 8; continue; } - int charpage = c >> 8; + uint charpage = c >> 8; if (charpage == page){ Texture* texture = pages[charpage].get(); if (texture == nullptr){ diff --git a/src/graphics/Font.h b/src/graphics/Font.h index 1ea7efc1..327e38cc 100644 --- a/src/graphics/Font.h +++ b/src/graphics/Font.h @@ -25,7 +25,15 @@ public: int getLineHeight() const; int getYOffset() const; + + /// @brief Calculate text width in pixels + /// @param text selected text + /// @param length max text chunk length (default: no limit) + /// @return pixel width of the text int calcWidth(std::wstring text, size_t length=-1); + + /// @brief Check if character is visible (non-whitespace) + /// @param codepoint character unicode codepoint bool isPrintableChar(uint codepoint) const; void draw(Batch2D* batch, std::wstring text, int x, int y); void draw(Batch2D* batch, std::wstring text, int x, int y, FontStyle style);