|
|
@@ -3,6 +3,7 @@
|
|
|
#include "GlStateMachine.h"
|
|
|
#include "Texture.h"
|
|
|
#include "Logger.h"
|
|
|
+#include "UiFont.h"
|
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
@@ -11,19 +12,30 @@ namespace Ui {
|
|
|
//======================================================================================================================
|
|
|
// Constructor =
|
|
|
//======================================================================================================================
|
|
|
-Painter::Painter()
|
|
|
+Painter::Painter(uint deviceWidth, uint deviceHeight):
|
|
|
+ deviceWidth(deviceWidth),
|
|
|
+ deviceHeight(deviceHeight)
|
|
|
{
|
|
|
init();
|
|
|
}
|
|
|
|
|
|
|
|
|
+//======================================================================================================================
|
|
|
+// setFont =
|
|
|
+//======================================================================================================================
|
|
|
+void Painter::setFont(const char* fontFilename, uint nominalWidth, uint nominalHeight)
|
|
|
+{
|
|
|
+ font.reset(new Font(fontFilename, nominalWidth, nominalHeight));
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
//======================================================================================================================
|
|
|
// init =
|
|
|
//======================================================================================================================
|
|
|
void Painter::init()
|
|
|
{
|
|
|
// Map
|
|
|
- fontMap.loadRsrc("engine-rsrc/fontmap-big.png");
|
|
|
+ fontMap.loadRsrc("engine-rsrc/fontmap.png");
|
|
|
columns = 16;
|
|
|
rows = 8;
|
|
|
tabSize = 4;
|
|
|
@@ -32,7 +44,8 @@ void Painter::init()
|
|
|
sProg.loadRsrc("shaders/UiText.glsl");
|
|
|
|
|
|
// Geom
|
|
|
- float quadVertCoords[][2] = {{1.0, 0.0}, {0.0, 0.0}, {0.0, -1.0}, {1.0, -1.0}};
|
|
|
+ //float quadVertCoords[][2] = {{1.0, 0.0}, {0.0, 0.0}, {0.0, -1.0}, {1.0, -1.0}};
|
|
|
+ float quadVertCoords[][2] = {{1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}, {1.0, 0.0}};
|
|
|
qPositionsVbo.create(GL_ARRAY_BUFFER, sizeof(quadVertCoords), quadVertCoords, GL_STATIC_DRAW);
|
|
|
|
|
|
ushort quadVertIndeces[2][3] = {{0, 1, 3}, {1, 2, 3}}; // 2 triangles
|
|
|
@@ -51,74 +64,69 @@ void Painter::drawText(const char* text)
|
|
|
{
|
|
|
// Set GL
|
|
|
GlStateMachineSingleton::getInstance().enable(GL_BLEND);
|
|
|
+ //GlStateMachineSingleton::getInstance().disable(GL_BLEND);
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
GlStateMachineSingleton::getInstance().disable(GL_DEPTH_TEST);
|
|
|
|
|
|
// SProg (some)
|
|
|
sProg->bind();
|
|
|
|
|
|
- sProg->findUniVar("texture")->set(*fontMap, 0);
|
|
|
+ sProg->findUniVar("texture")->set(font->getMap(), 0);
|
|
|
sProg->findUniVar("color")->set(&col);
|
|
|
|
|
|
// Vao
|
|
|
qVao.bind();
|
|
|
|
|
|
// Iterate
|
|
|
- float horizontalMoving = fontSize.x() * 0.65;
|
|
|
- float verticalMoving = fontSize.y() /* * 0.7*/;
|
|
|
- Vec2 p = pos;
|
|
|
+ Vec2 p; // in NDC
|
|
|
+ p.x() = 2.0 * pos.x() - 1.0;
|
|
|
+ p.y() = 2.0 * -pos.y() - 1.0;
|
|
|
const char* c = text;
|
|
|
while(*c != '\0')
|
|
|
{
|
|
|
char cc = *c;
|
|
|
// Check if special
|
|
|
- if(cc == '\n')
|
|
|
+ if(cc == ' ')
|
|
|
{
|
|
|
- p.y() += verticalMoving;
|
|
|
+ // do nothing
|
|
|
+ }
|
|
|
+ else if(cc == '\n')
|
|
|
+ {
|
|
|
+ /*p.y() += verticalMoving;
|
|
|
p.x() = pos.x();
|
|
|
++c;
|
|
|
- continue;
|
|
|
+ continue;*/
|
|
|
}
|
|
|
- if(cc == '\t') // tab
|
|
|
+ else if(cc == '\t') // tab
|
|
|
{
|
|
|
- p.x() += horizontalMoving * (tabSize - 1);
|
|
|
+ p.x() += font->getGlyphWidth(' ') * (tabSize - 1);
|
|
|
cc = ' ';
|
|
|
}
|
|
|
- if(cc < ' ' || cc > '~') // out of range
|
|
|
+ else if(cc < ' ' || cc > '~') // out of range
|
|
|
{
|
|
|
cc = '~' + 1;
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Mat3 trfM = Mat3::getIdentity();
|
|
|
+ trfM(0, 0) = 2.0 * float(font->getGlyphWidth(cc)) / deviceWidth;
|
|
|
+ trfM(1, 1) = 2.0 * float(font->getGlyphHeight(cc)) / deviceHeight;
|
|
|
+ trfM(0, 2) = p.x() /*+ (font->getGlyphBearingX(cc) / float(deviceWidth))*/;
|
|
|
|
|
|
- // Pos trf
|
|
|
- Mat3 trf = Mat3::getIdentity();
|
|
|
- trf(0, 0) = fontSize.x() * 2.0;
|
|
|
- trf(1, 1) = fontSize.y() * 2.0;
|
|
|
- trf(0, 2) = p.x() * 2.0 - 1.0;
|
|
|
- trf(1, 2) = -p.y() * 2.0 + 1.0;
|
|
|
-
|
|
|
- sProg->findUniVar("transformation")->set(&trf);
|
|
|
-
|
|
|
- // Tex trf
|
|
|
- uint i = cc - 32;
|
|
|
- uint crntRow = i / columns;
|
|
|
- uint crntColumn = i % columns;
|
|
|
-
|
|
|
- //INFO(crntRow << " " << crntColumn);
|
|
|
+ trfM(1, 2) = 2 * (font->getGlyphBearingY(cc) - int(font->getGlyphHeight(cc))) / float(deviceHeight);
|
|
|
|
|
|
- Mat3 texTrf = Mat3::getIdentity();
|
|
|
- texTrf(0, 0) = 1.0 / columns;
|
|
|
- texTrf(1, 1) = 1.0 / rows;
|
|
|
- texTrf(0, 2) = crntColumn / float(columns);
|
|
|
- texTrf(1, 2) = 1.0 - crntRow / float(rows);
|
|
|
+ sProg->findUniVar("transformation")->set(&trfM);
|
|
|
+ sProg->findUniVar("textureTranformation")->set(&font->getGlyphTextureMatrix(cc));
|
|
|
|
|
|
- sProg->findUniVar("textureTranformation")->set(&texTrf);
|
|
|
|
|
|
- // Render
|
|
|
- glDrawElements(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_SHORT, 0);
|
|
|
+ // Render
|
|
|
+ glDrawElements(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_SHORT, 0);
|
|
|
+ }
|
|
|
|
|
|
// Inc
|
|
|
++c;
|
|
|
- p.x() += horizontalMoving;
|
|
|
+ //p.x() += 2 * font->getGlyphAdvance(cc) / float(deviceWidth);
|
|
|
+ p.x() += 2 * font->getGlyphWidth(cc) / float(deviceWidth);
|
|
|
}
|
|
|
}
|
|
|
|