refactor
This commit is contained in:
parent
d189bdc107
commit
e06350915e
@ -133,7 +133,7 @@ void SlotView::draw(const GfxContext* pctx, Assets* assets) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto batch = pctx->getBatch2D();
|
auto batch = pctx->getBatch2D();
|
||||||
batch->color = color;
|
batch->setColor(color);
|
||||||
if (color.a > 0.0) {
|
if (color.a > 0.0) {
|
||||||
batch->texture(nullptr);
|
batch->texture(nullptr);
|
||||||
if (highlighted) {
|
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 previews = frontend->getBlocksAtlas();
|
||||||
auto indices = content->getIndices();
|
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 x = coord.x+slotSize-text.length()*8;
|
||||||
int y = coord.y+slotSize-16;
|
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);
|
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);
|
font->draw(batch, text, x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -83,7 +83,7 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
|
|||||||
|
|
||||||
auto batch = pctx->getBatch2D();
|
auto batch = pctx->getBatch2D();
|
||||||
batch->texture(nullptr);
|
batch->texture(nullptr);
|
||||||
batch->render();
|
batch->flush();
|
||||||
{
|
{
|
||||||
GfxContext ctx = pctx->sub();
|
GfxContext ctx = pctx->sub();
|
||||||
ctx.scissors(glm::vec4(coord.x, coord.y, size.x, size.y));
|
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())
|
if (node->isVisible())
|
||||||
node->draw(pctx, assets);
|
node->draw(pctx, assets);
|
||||||
}
|
}
|
||||||
batch->render();
|
batch->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +102,7 @@ void Container::drawBackground(const GfxContext* pctx, Assets* assets) {
|
|||||||
|
|
||||||
auto batch = pctx->getBatch2D();
|
auto batch = pctx->getBatch2D();
|
||||||
batch->texture(nullptr);
|
batch->texture(nullptr);
|
||||||
batch->color = color;
|
batch->setColor(color);
|
||||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -85,7 +85,7 @@ void Label::draw(const GfxContext* pctx, Assets* assets) {
|
|||||||
auto batch = pctx->getBatch2D();
|
auto batch = pctx->getBatch2D();
|
||||||
auto font = assets->getFont(fontName);
|
auto font = assets->getFont(fontName);
|
||||||
|
|
||||||
batch->color = getColor();
|
batch->setColor(getColor());
|
||||||
|
|
||||||
uint lineHeight = font->getLineHeight();
|
uint lineHeight = font->getLineHeight();
|
||||||
glm::vec2 size = getSize();
|
glm::vec2 size = getSize();
|
||||||
@ -162,7 +162,7 @@ void Image::draw(const GfxContext* pctx, Assets* assets) {
|
|||||||
setSize(glm::vec2(texture->width, texture->height));
|
setSize(glm::vec2(texture->width, texture->height));
|
||||||
}
|
}
|
||||||
batch->texture(texture);
|
batch->texture(texture);
|
||||||
batch->color = color;
|
batch->setColor(color);
|
||||||
batch->rect(coord.x, coord.y, size.x, size.y,
|
batch->rect(coord.x, coord.y, size.x, size.y,
|
||||||
0, 0, 0, UVRegion(), false, true, color);
|
0, 0, 0, UVRegion(), false, true, color);
|
||||||
}
|
}
|
||||||
@ -254,7 +254,7 @@ void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
|
|||||||
glm::vec2 coord = calcCoord();
|
glm::vec2 coord = calcCoord();
|
||||||
auto batch = pctx->getBatch2D();
|
auto batch = pctx->getBatch2D();
|
||||||
batch->texture(nullptr);
|
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);
|
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();
|
glm::vec2 coord = calcCoord();
|
||||||
auto batch = pctx->getBatch2D();
|
auto batch = pctx->getBatch2D();
|
||||||
batch->texture(nullptr);
|
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);
|
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();
|
auto batch = pctx->getBatch2D();
|
||||||
batch->texture(nullptr);
|
batch->texture(nullptr);
|
||||||
if (int((Window::time() - caretLastMove) * 2) % 2 == 0) {
|
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);
|
int width = font->calcWidth(input, caret);
|
||||||
batch->rect(lcoord.x + width, lcoord.y+yoffset, 2, lineHeight);
|
batch->rect(lcoord.x + width, lcoord.y+yoffset, 2, lineHeight);
|
||||||
}
|
}
|
||||||
if (selectionStart != selectionEnd) {
|
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 start = font->calcWidth(input, selectionStart);
|
||||||
int end = font->calcWidth(input, selectionEnd);
|
int end = font->calcWidth(input, selectionEnd);
|
||||||
batch->rect(lcoord.x + start, lcoord.y+yoffset, end-start, lineHeight);
|
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 (valid) {
|
||||||
if (isFocused()) {
|
if (isFocused()) {
|
||||||
batch->color = focusedColor;
|
batch->setColor(focusedColor);
|
||||||
} else if (hover) {
|
} else if (hover) {
|
||||||
batch->color = hoverColor;
|
batch->setColor(hoverColor);
|
||||||
} else {
|
} else {
|
||||||
batch->color = color;
|
batch->setColor(color);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
batch->color = invalidColor;
|
batch->setColor(invalidColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
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();
|
glm::vec2 coord = calcCoord();
|
||||||
auto batch = pctx->getBatch2D();
|
auto batch = pctx->getBatch2D();
|
||||||
batch->texture(nullptr);
|
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);
|
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||||
label->setText(util::str2wstr_utf8(binding.text()));
|
label->setText(util::str2wstr_utf8(binding.text()));
|
||||||
}
|
}
|
||||||
@ -739,13 +739,13 @@ void TrackBar::draw(const GfxContext* pctx, Assets* assets) {
|
|||||||
glm::vec2 coord = calcCoord();
|
glm::vec2 coord = calcCoord();
|
||||||
auto batch = pctx->getBatch2D();
|
auto batch = pctx->getBatch2D();
|
||||||
batch->texture(nullptr);
|
batch->texture(nullptr);
|
||||||
batch->color = (hover ? hoverColor : color);
|
batch->setColor(hover ? hoverColor : color);
|
||||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||||
|
|
||||||
float width = size.x;
|
float width = size.x;
|
||||||
float t = (value - min) / (max-min+trackWidth*step);
|
float t = (value - min) / (max-min+trackWidth*step);
|
||||||
|
|
||||||
batch->color = trackColor;
|
batch->setColor(trackColor);
|
||||||
int actualWidth = size.x * (trackWidth / (max-min+trackWidth*step) * step);
|
int actualWidth = size.x * (trackWidth / (max-min+trackWidth*step) * step);
|
||||||
batch->rect(coord.x + width * t, coord.y, actualWidth, size.y);
|
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();
|
glm::vec2 coord = calcCoord();
|
||||||
auto batch = pctx->getBatch2D();
|
auto batch = pctx->getBatch2D();
|
||||||
batch->texture(nullptr);
|
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);
|
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -579,7 +579,7 @@ void Hud::draw(const GfxContext& ctx){
|
|||||||
(width-chsizex)/2, (height-chsizey)/2,
|
(width-chsizex)/2, (height-chsizey)/2,
|
||||||
chsizex, chsizey, 0,0, 1,1, 1,1,1,1
|
chsizex, chsizey, 0,0, 1,1, 1,1,1,1
|
||||||
);
|
);
|
||||||
batch->render();
|
batch->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delta-time visualizer
|
// Delta-time visualizer
|
||||||
@ -629,7 +629,7 @@ void Hud::draw(const GfxContext& ctx){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
grabbedItemView->setCoord(glm::vec2(Events::cursor));
|
grabbedItemView->setCoord(glm::vec2(Events::cursor));
|
||||||
batch->render();
|
batch->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Hud::isInventoryOpen() const {
|
bool Hud::isInventoryOpen() const {
|
||||||
|
|||||||
@ -76,11 +76,13 @@ void MenuScreen::draw(float delta) {
|
|||||||
|
|
||||||
batch->begin();
|
batch->begin();
|
||||||
batch->texture(engine->getAssets()->getTexture("gui/menubg"));
|
batch->texture(engine->getAssets()->getTexture("gui/menubg"));
|
||||||
batch->rect(0, 0,
|
batch->rect(
|
||||||
width, height, 0, 0, 0,
|
0, 0,
|
||||||
UVRegion(0, 0, width/64, height/64),
|
width, height, 0, 0, 0,
|
||||||
false, false, glm::vec4(1.0f));
|
UVRegion(0, 0, width/64, height/64),
|
||||||
batch->render();
|
false, false, glm::vec4(1.0f)
|
||||||
|
);
|
||||||
|
batch->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool backlight;
|
static bool backlight;
|
||||||
|
|||||||
@ -5,120 +5,125 @@
|
|||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
const uint B2D_VERTEX_SIZE = 8;
|
inline constexpr uint B2D_VERTEX_SIZE = 8;
|
||||||
|
|
||||||
using glm::vec2;
|
|
||||||
using glm::vec3;
|
|
||||||
using glm::vec4;
|
|
||||||
|
|
||||||
Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){
|
Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){
|
||||||
const vattr attrs[] = {
|
const vattr attrs[] = {
|
||||||
{2}, {2}, {4}, {0}
|
{2}, {2}, {4}, {0}
|
||||||
};
|
};
|
||||||
|
|
||||||
buffer = new float[capacity * B2D_VERTEX_SIZE];
|
buffer = new float[capacity * B2D_VERTEX_SIZE];
|
||||||
mesh = std::make_unique<Mesh>(buffer, 0, attrs);
|
mesh = std::make_unique<Mesh>(buffer, 0, attrs);
|
||||||
index = 0;
|
index = 0;
|
||||||
|
|
||||||
ubyte pixels[] = {
|
ubyte pixels[] = {
|
||||||
0xFF, 0xFF, 0xFF, 0xFF
|
0xFF, 0xFF, 0xFF, 0xFF
|
||||||
};
|
};
|
||||||
blank = new Texture(pixels, 1, 1, GL_RGBA);
|
blank = new Texture(pixels, 1, 1, GL_RGBA);
|
||||||
_texture = nullptr;
|
_texture = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Batch2D::~Batch2D(){
|
Batch2D::~Batch2D(){
|
||||||
delete blank;
|
delete blank;
|
||||||
delete[] buffer;
|
delete[] buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::begin(){
|
void Batch2D::begin(){
|
||||||
_texture = nullptr;
|
_texture = nullptr;
|
||||||
blank->bind();
|
blank->bind();
|
||||||
color = vec4(1.0f);
|
color = glm::vec4(1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::vertex(float x, float y,
|
void Batch2D::vertex(
|
||||||
float u, float v,
|
float x, float y,
|
||||||
float r, float g, float b, float a) {
|
float u, float v,
|
||||||
buffer[index++] = x;
|
float r, float g, float b, float a
|
||||||
buffer[index++] = y;
|
) {
|
||||||
buffer[index++] = u;
|
buffer[index++] = x;
|
||||||
buffer[index++] = v;
|
buffer[index++] = y;
|
||||||
buffer[index++] = r;
|
buffer[index++] = u;
|
||||||
buffer[index++] = g;
|
buffer[index++] = v;
|
||||||
buffer[index++] = b;
|
buffer[index++] = r;
|
||||||
buffer[index++] = a;
|
buffer[index++] = g;
|
||||||
|
buffer[index++] = b;
|
||||||
|
buffer[index++] = a;
|
||||||
}
|
}
|
||||||
void Batch2D::vertex(vec2 point,
|
void Batch2D::vertex(
|
||||||
vec2 uvpoint,
|
glm::vec2 point,
|
||||||
float r, float g, float b, float a) {
|
glm::vec2 uvpoint,
|
||||||
buffer[index++] = point.x;
|
float r, float g, float b, float a
|
||||||
buffer[index++] = point.y;
|
) {
|
||||||
buffer[index++] = uvpoint.x;
|
buffer[index++] = point.x;
|
||||||
buffer[index++] = uvpoint.y;
|
buffer[index++] = point.y;
|
||||||
buffer[index++] = r;
|
buffer[index++] = uvpoint.x;
|
||||||
buffer[index++] = g;
|
buffer[index++] = uvpoint.y;
|
||||||
buffer[index++] = b;
|
buffer[index++] = r;
|
||||||
buffer[index++] = a;
|
buffer[index++] = g;
|
||||||
|
buffer[index++] = b;
|
||||||
|
buffer[index++] = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::texture(Texture* new_texture){
|
void Batch2D::texture(Texture* new_texture){
|
||||||
if (_texture == new_texture)
|
if (_texture == new_texture)
|
||||||
return;
|
return;
|
||||||
render(GL_TRIANGLES);
|
flush(GL_TRIANGLES);
|
||||||
_texture = new_texture;
|
_texture = new_texture;
|
||||||
if (new_texture == nullptr)
|
if (new_texture == nullptr)
|
||||||
blank->bind();
|
blank->bind();
|
||||||
else
|
else
|
||||||
new_texture->bind();
|
new_texture->bind();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Batch2D::untexture() {
|
||||||
|
texture(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::point(float x, float y, float r, float g, float b, float a){
|
void Batch2D::point(float x, float y, float r, float g, float b, float a){
|
||||||
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
||||||
render(GL_TRIANGLES);
|
flush(GL_TRIANGLES);
|
||||||
|
|
||||||
vertex(x, y, 0, 0, r,g,b,a);
|
vertex(x, y, 0, 0, r,g,b,a);
|
||||||
render(GL_POINTS);
|
flush(GL_POINTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::line(float x1, float y1, float x2, float y2, float r, float g, float b, float a){
|
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)
|
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
||||||
render(GL_TRIANGLES);
|
flush(GL_TRIANGLES);
|
||||||
|
|
||||||
vertex(x1, y1, 0, 0, r,g,b,a);
|
vertex(x1, y1, 0, 0, r,g,b,a);
|
||||||
vertex(x2, y2, 1, 1, r,g,b,a);
|
vertex(x2, y2, 1, 1, r,g,b,a);
|
||||||
render(GL_LINES);
|
flush(GL_LINES);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::rect(float x, float y, float w, float h){
|
void Batch2D::rect(float x, float y, float w, float h){
|
||||||
const float r = color.r;
|
const float r = color.r;
|
||||||
const float g = color.g;
|
const float g = color.g;
|
||||||
const float b = color.b;
|
const float b = color.b;
|
||||||
const float a = color.a;
|
const float a = color.a;
|
||||||
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
||||||
render(GL_TRIANGLES);
|
flush(GL_TRIANGLES);
|
||||||
|
|
||||||
vertex(x, y, 0, 0, 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, y+h, 0, 1, r,g,b,a);
|
||||||
vertex(x+w, y+h, 1, 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, 0, 0, r,g,b,a);
|
||||||
vertex(x+w, y+h, 1, 1, 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+w, y, 1, 0, r,g,b,a);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::rect(
|
void Batch2D::rect(
|
||||||
float x, float y,
|
float x, float y,
|
||||||
float w, float h,
|
float w, float h,
|
||||||
float ox, float oy,
|
float ox, float oy,
|
||||||
float angle,
|
float angle,
|
||||||
UVRegion region,
|
UVRegion region,
|
||||||
bool flippedX,
|
bool flippedX,
|
||||||
bool flippedY,
|
bool flippedY,
|
||||||
vec4 tint) {
|
glm::vec4 tint
|
||||||
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
) {
|
||||||
render(GL_TRIANGLES);
|
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
||||||
|
flush(GL_TRIANGLES);
|
||||||
|
|
||||||
float centerX = w*ox;
|
float centerX = w*ox;
|
||||||
float centerY = h*oy;
|
float centerY = h*oy;
|
||||||
@ -202,108 +207,113 @@ void Batch2D::rect(
|
|||||||
vertex(x4, y4, u4, v4, tint.r, tint.g, tint.b, tint.a);
|
vertex(x4, y4, u4, v4, tint.r, tint.g, tint.b, tint.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::rect(float x, float y, float w, float h,
|
void Batch2D::rect(
|
||||||
float u, float v, float tx, float ty,
|
float x, float y, float w, float h,
|
||||||
float r, float g, float b, float a){
|
float u, float v, float tx, float ty,
|
||||||
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
float r, float g, float b, float a
|
||||||
render(GL_TRIANGLES);
|
){
|
||||||
vertex(x, y, u, v+ty, r,g,b,a);
|
if (index + 6*B2D_VERTEX_SIZE >= capacity)
|
||||||
vertex(x+w, y+h, u+tx, v, r,g,b,a);
|
flush(GL_TRIANGLES);
|
||||||
vertex(x, y+h, u, v, r,g,b,a);
|
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, y, u, v+ty, r,g,b,a);
|
||||||
vertex(x+w, y, u+tx, 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+w, y+h, u+tx, v, r,g,b,a);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::rect(float x, float y, float w, float h,
|
void Batch2D::rect(
|
||||||
float r0, float g0, float b0,
|
float x, float y, float w, float h,
|
||||||
float r1, float g1, float b1,
|
float r0, float g0, float b0,
|
||||||
float r2, float g2, float b2,
|
float r1, float g1, float b1,
|
||||||
float r3, float g3, float b3,
|
float r2, float g2, float b2,
|
||||||
float r4, float g4, float b4, int sh){
|
float r3, float g3, float b3,
|
||||||
if (index + 30*B2D_VERTEX_SIZE >= capacity)
|
float r4, float g4, float b4, int sh
|
||||||
render(GL_TRIANGLES);
|
){
|
||||||
vec2 v0 = vec2(x+h/2,y+h/2);
|
if (index + 30*B2D_VERTEX_SIZE >= capacity)
|
||||||
vec2 v1 = vec2(x+w-sh,y);
|
flush(GL_TRIANGLES);
|
||||||
vec2 v2 = vec2(x+sh,y);
|
glm::vec2 v0(x+h/2,y+h/2);
|
||||||
vec2 v3 = vec2(x,y+sh);
|
glm::vec2 v1(x+w-sh,y);
|
||||||
vec2 v4 = vec2(x,y+h-sh);
|
glm::vec2 v2(x+sh,y);
|
||||||
vec2 v5 = vec2(x+sh,y+h);
|
glm::vec2 v3(x,y+sh);
|
||||||
vec2 v6 = vec2(x+w-h/2,y+h/2);
|
glm::vec2 v4(x,y+h-sh);
|
||||||
vec2 v7 = vec2(x+w-sh,y+h);
|
glm::vec2 v5(x+sh,y+h);
|
||||||
vec2 v8 = vec2(x+w,y+h-sh);
|
glm::vec2 v6(x+w-h/2,y+h/2);
|
||||||
vec2 v9 = vec2(x+w,y+sh);
|
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(v0, glm::vec2(0, 0), r1,g1,b1,1.0f);
|
||||||
vertex(v6, vec2(0, 0), r1,g1,b1,1.0f);
|
vertex(v6, glm::vec2(0, 0), r1,g1,b1,1.0f);
|
||||||
vertex(v1, 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(v0, glm::vec2(0, 0), r1,g1,b1,1.0f);
|
||||||
vertex(v1, vec2(0, 0), r1,g1,b1,1.0f);
|
vertex(v1, glm::vec2(0, 0), r1,g1,b1,1.0f);
|
||||||
vertex(v2, 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(v0, glm::vec2(0, 0), r0,g0,b0,1.0f);
|
||||||
vertex(v2, vec2(0, 0), r0,g0,b0,1.0f);
|
vertex(v2, glm::vec2(0, 0), r0,g0,b0,1.0f);
|
||||||
vertex(v3, 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(v0, glm::vec2(0, 0), r1,g1,b1,1.0f);
|
||||||
vertex(v3, vec2(0, 0), r1,g1,b1,1.0f);
|
vertex(v3, glm::vec2(0, 0), r1,g1,b1,1.0f);
|
||||||
vertex(v4, 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(v0, glm::vec2(0, 0), r2,g2,b2,1.0f);
|
||||||
vertex(v4, vec2(0, 0), r2,g2,b2,1.0f);
|
vertex(v4, glm::vec2(0, 0), r2,g2,b2,1.0f);
|
||||||
vertex(v5, 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(v0, glm::vec2(0, 0), r3,g3,b3,1.0f);
|
||||||
vertex(v5, vec2(0, 0), r3,g3,b3,1.0f);
|
vertex(v5, glm::vec2(0, 0), r3,g3,b3,1.0f);
|
||||||
vertex(v6, 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(v6, glm::vec2(0, 0), r3,g3,b3,1.0f);
|
||||||
vertex(v5, vec2(0, 0), r3,g3,b3,1.0f);
|
vertex(v5, glm::vec2(0, 0), r3,g3,b3,1.0f);
|
||||||
vertex(v7, 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(v6, glm::vec2(0, 0), r4,g4,b4,1.0f);
|
||||||
vertex(v7, vec2(0, 0), r4,g4,b4,1.0f);
|
vertex(v7, glm::vec2(0, 0), r4,g4,b4,1.0f);
|
||||||
vertex(v8, 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(v6, glm::vec2(0, 0), r3,g3,b3,1.0f);
|
||||||
vertex(v8, vec2(0, 0), r3,g3,b3,1.0f);
|
vertex(v8, glm::vec2(0, 0), r3,g3,b3,1.0f);
|
||||||
vertex(v9, 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(v6, glm::vec2(0, 0), r2,g2,b2,1.0f);
|
||||||
vertex(v9, vec2(0, 0), r2,g2,b2,1.0f);
|
vertex(v9, glm::vec2(0, 0), r2,g2,b2,1.0f);
|
||||||
vertex(v1, vec2(0, 0), r2,g2,b2,1.0f);
|
vertex(v1, glm::vec2(0, 0), r2,g2,b2,1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::sprite(Sprite* sprite) {
|
void Batch2D::sprite(Sprite* sprite) {
|
||||||
vec2 position = sprite->position;
|
glm::vec2 position = sprite->position;
|
||||||
vec2 size = sprite->size;
|
glm::vec2 size = sprite->size;
|
||||||
vec2 origin = sprite->origin;
|
glm::vec2 origin = sprite->origin;
|
||||||
texture(sprite->texture);
|
texture(sprite->texture);
|
||||||
rect(
|
rect(
|
||||||
position.x, position.y,
|
position.x, position.y,
|
||||||
size.x, size.y,
|
size.x, size.y,
|
||||||
origin.x, origin.y,
|
origin.x, origin.y,
|
||||||
sprite->angle,
|
sprite->angle,
|
||||||
sprite->region,
|
sprite->region,
|
||||||
sprite->flippedX,
|
sprite->flippedX,
|
||||||
sprite->flippedY,
|
sprite->flippedY,
|
||||||
sprite->color);
|
sprite->color
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::sprite(float x, float y, float w, float h, const UVRegion& region, vec4 tint){
|
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);
|
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){
|
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 scale = 1.0f / (float)atlasRes;
|
||||||
float u = (index % atlasRes) * scale;
|
float u = (index % atlasRes) * scale;
|
||||||
float v = 1.0f - ((index / atlasRes) * scale) - 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);
|
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)
|
if (index == 0)
|
||||||
return;
|
return;
|
||||||
mesh->reload(buffer, index / B2D_VERTEX_SIZE);
|
mesh->reload(buffer, index / B2D_VERTEX_SIZE);
|
||||||
@ -311,10 +321,10 @@ void Batch2D::render(unsigned int gl_primitive) {
|
|||||||
index = 0;
|
index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::render() {
|
void Batch2D::flush() {
|
||||||
render(GL_TRIANGLES);
|
flush(GL_TRIANGLES);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::lineWidth(float width) {
|
void Batch2D::lineWidth(float width) {
|
||||||
glLineWidth(width);
|
glLineWidth(width);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,56 +12,82 @@ class Texture;
|
|||||||
class Sprite;
|
class Sprite;
|
||||||
|
|
||||||
class Batch2D {
|
class Batch2D {
|
||||||
float* buffer;
|
float* buffer;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
std::unique_ptr<Mesh> mesh;
|
std::unique_ptr<Mesh> mesh;
|
||||||
size_t index;
|
size_t index;
|
||||||
|
glm::vec4 color;
|
||||||
|
|
||||||
Texture* blank;
|
Texture* blank;
|
||||||
Texture* _texture;
|
Texture* _texture;
|
||||||
|
|
||||||
void vertex(float x, float y,
|
void vertex(
|
||||||
float u, float v,
|
float x, float y,
|
||||||
float r, float g, float b, float a);
|
float u, float v,
|
||||||
void vertex(glm::vec2 point,
|
float r, float g, float b, float a
|
||||||
glm::vec2 uvpoint,
|
);
|
||||||
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:
|
public:
|
||||||
glm::vec4 color;
|
Batch2D(size_t capacity);
|
||||||
|
~Batch2D();
|
||||||
|
|
||||||
Batch2D(size_t capacity);
|
void begin();
|
||||||
~Batch2D();
|
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 rect(
|
||||||
void texture(Texture* texture);
|
float x, float y,
|
||||||
void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint);
|
float w, float h,
|
||||||
void sprite(Sprite* sprite);
|
float ox, float oy,
|
||||||
void sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint);
|
float angle, UVRegion region,
|
||||||
void point(float x, float y, float r, float g, float b, float a);
|
bool flippedX, bool flippedY,
|
||||||
void line(float x1, float y1, float x2, float y2, float r, float g, float b, float a);
|
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);
|
||||||
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 r0, float g0, float b0,
|
float x, float y, float w, float h,
|
||||||
float r1, float g1, float b1,
|
float u, float v, float tx, float ty,
|
||||||
float r2, float g2, float b2,
|
float r, float g, float b, float a
|
||||||
float r3, float g3, float b3,
|
);
|
||||||
float r4, float g4, float b4, int sh);
|
|
||||||
void render(unsigned int gl_primitive);
|
|
||||||
void render();
|
|
||||||
|
|
||||||
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_ */
|
#endif /* SRC_GRAPHICS_BATCH2D_H_ */
|
||||||
|
|||||||
@ -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) {
|
static inline void drawGlyph(Batch2D* batch, int x, int y, uint c, FontStyle style) {
|
||||||
switch (style){
|
switch (style){
|
||||||
|
case FontStyle::none:
|
||||||
|
break;
|
||||||
case FontStyle::shadow:
|
case FontStyle::shadow:
|
||||||
batch->sprite(x+1, y+1, GLYPH_SIZE, GLYPH_SIZE, 16, c, SHADOW_TINT);
|
batch->sprite(x+1, y+1, GLYPH_SIZE, GLYPH_SIZE, 16, c, SHADOW_TINT);
|
||||||
break;
|
break;
|
||||||
@ -57,7 +59,7 @@ static inline void drawGlyph(Batch2D* batch, int x, int y, uint c, FontStyle sty
|
|||||||
}
|
}
|
||||||
break;
|
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) {
|
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;
|
x += 8;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int charpage = c >> 8;
|
uint charpage = c >> 8;
|
||||||
if (charpage == page){
|
if (charpage == page){
|
||||||
Texture* texture = pages[charpage].get();
|
Texture* texture = pages[charpage].get();
|
||||||
if (texture == nullptr){
|
if (texture == nullptr){
|
||||||
|
|||||||
@ -25,7 +25,15 @@ public:
|
|||||||
|
|
||||||
int getLineHeight() const;
|
int getLineHeight() const;
|
||||||
int getYOffset() 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);
|
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;
|
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);
|
||||||
void draw(Batch2D* batch, std::wstring text, int x, int y, FontStyle style);
|
void draw(Batch2D* batch, std::wstring text, int x, int y, FontStyle style);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user