|
| 1 | +#include "Sketch.h" |
| 2 | + |
| 3 | +using namespace std; |
| 4 | +using namespace chr; |
| 5 | +using namespace gl; |
| 6 | + |
| 7 | +constexpr float PADDING = 20; |
| 8 | + |
| 9 | +void Sketch::setup() |
| 10 | +{ |
| 11 | + font1 = fontManager.getFont(InputSource::resource("Helvetica_Regular_64.fnt"), XFont::Properties2d()); |
| 12 | + font1->setShader(textureAlphaShader); |
| 13 | + font1->setSize(20); |
| 14 | + font1->setColor(0, 0, 0, 1); |
| 15 | + |
| 16 | + strokeBatch |
| 17 | + .setPrimitive(GL_LINES) |
| 18 | + .setShader(colorShader) |
| 19 | + .setShaderColor(0, 0, 0, 0.25f); |
| 20 | + |
| 21 | + // --- |
| 22 | + |
| 23 | + glDisable(GL_DEPTH_TEST); |
| 24 | + glDepthMask(GL_FALSE); |
| 25 | + |
| 26 | + glEnable(GL_BLEND); |
| 27 | + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 28 | +} |
| 29 | + |
| 30 | +void Sketch::resize() |
| 31 | +{ |
| 32 | + strokeBatch.clear(); |
| 33 | + drawGuides(); |
| 34 | + |
| 35 | + drawText1(); |
| 36 | +} |
| 37 | + |
| 38 | +void Sketch::draw() |
| 39 | +{ |
| 40 | + glClearColor(1, 1, 1, 1); |
| 41 | + glClear(GL_COLOR_BUFFER_BIT); |
| 42 | + |
| 43 | + // --- |
| 44 | + |
| 45 | + auto projectionMatrix = glm::ortho(0.0f, windowInfo.width, 0.0f, windowInfo.height); |
| 46 | + |
| 47 | + Matrix modelViewMatrix; |
| 48 | + modelViewMatrix |
| 49 | + .translate(0, windowInfo.height) |
| 50 | + .scale(1, -1); |
| 51 | + |
| 52 | + gl::State() |
| 53 | + .setShaderMatrix(modelViewMatrix * projectionMatrix) |
| 54 | + .apply(); |
| 55 | + |
| 56 | + // --- |
| 57 | + |
| 58 | + strokeBatch.flush(); |
| 59 | + font1->replaySequence(sequence1); |
| 60 | +} |
| 61 | + |
| 62 | +void Sketch::drawGuides() |
| 63 | +{ |
| 64 | + float width = windowInfo.width; |
| 65 | + float height = windowInfo.height; |
| 66 | + |
| 67 | + strokeBatch.addVertices(glm::vec2(PADDING, 0), glm::vec2(PADDING, height)); |
| 68 | + strokeBatch.addVertices(glm::vec2(width - PADDING, 0), glm::vec2(width - PADDING, height)); |
| 69 | + strokeBatch.addVertices(glm::vec2(0, PADDING), glm::vec2(width, PADDING)); |
| 70 | + strokeBatch.addVertices(glm::vec2(0, height - PADDING), glm::vec2(width, height - PADDING)); |
| 71 | +} |
| 72 | + |
| 73 | +void Sketch::drawText1() |
| 74 | +{ |
| 75 | + font1->beginSequence(sequence1); |
| 76 | + |
| 77 | + drawText(*font1, u"bottom-left", PADDING, windowInfo.height - PADDING); |
| 78 | + |
| 79 | + drawAlignedText(*font1, u"top-left", glm::vec2(PADDING, PADDING), XFont::ALIGN_LEFT, XFont::ALIGN_TOP); |
| 80 | + drawAlignedText(*font1, u"top-right", glm::vec2(windowInfo.width - PADDING, PADDING), XFont::ALIGN_RIGHT, XFont::ALIGN_TOP); |
| 81 | + drawAlignedText(*font1, u"bottom-right", glm::vec2(windowInfo.width - PADDING, windowInfo.height - PADDING), XFont::ALIGN_RIGHT, XFont::ALIGN_BASELINE); |
| 82 | + drawAlignedText(*font1, u"top-middle", glm::vec2(windowInfo.width / 2, PADDING), XFont::ALIGN_MIDDLE, XFont::ALIGN_TOP); |
| 83 | + drawAlignedText(*font1, u"bottom-middle", glm::vec2(windowInfo.width / 2, windowInfo.height - PADDING), XFont::ALIGN_MIDDLE, XFont::ALIGN_BASELINE); |
| 84 | + |
| 85 | + Matrix matrix; |
| 86 | + matrix |
| 87 | + .translate(PADDING, windowInfo.height / 2) |
| 88 | + .rotateZ(-HALF_PI); |
| 89 | + drawAlignedText(*font1, u"middle-left", matrix, XFont::ALIGN_MIDDLE, XFont::ALIGN_TOP); |
| 90 | + |
| 91 | + matrix |
| 92 | + .setTranslate(windowInfo.width - PADDING, windowInfo.height / 2) |
| 93 | + .rotateZ(HALF_PI); |
| 94 | + drawAlignedText(*font1, u"middle-right", matrix, XFont::ALIGN_MIDDLE, XFont::ALIGN_TOP); |
| 95 | + |
| 96 | + font1->endSequence(); |
| 97 | +} |
| 98 | + |
| 99 | +void Sketch::drawText(XFont &font, const u16string &text, float x, float y) |
| 100 | +{ |
| 101 | + for (auto c : text) |
| 102 | + { |
| 103 | + auto glyphIndex = font.getGlyphIndex(c); |
| 104 | + font.addGlyph(glyphIndex, x, y); |
| 105 | + x += font.getGlyphAdvance(glyphIndex); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void Sketch::drawAlignedText(XFont &font, const u16string &text, const glm::vec2 &position, XFont::Alignment alignX, XFont::Alignment alignY) |
| 110 | +{ |
| 111 | + auto center = position + font.getOffset(text, alignX, alignY); |
| 112 | + drawText(font, text, center.x, center.y); |
| 113 | +} |
| 114 | + |
| 115 | +void Sketch::drawAlignedText(XFont &font, const u16string &text, Matrix &matrix, XFont::Alignment alignX, XFont::Alignment alignY) |
| 116 | +{ |
| 117 | + auto offset = font.getOffset(text, alignX, alignY); |
| 118 | + |
| 119 | + for (auto c : text) |
| 120 | + { |
| 121 | + auto glyphIndex = font.getGlyphIndex(c); |
| 122 | + font.addGlyph(matrix, glyphIndex, offset.x, offset.y); |
| 123 | + offset.x += font.getGlyphAdvance(glyphIndex); |
| 124 | + } |
| 125 | +} |
0 commit comments