Skip to content

Commit 10b4c52

Browse files
committed
Drawing2dText: Surrounding text in place
1 parent af415c3 commit 10b4c52

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed

sketches/Drawing2dText/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cmake_minimum_required (VERSION 3.2.2)
2+
3+
project(Drawing2dText)
4+
5+
set(INCLUDE_DIRS
6+
$ENV{CODE_PATH}
7+
)
8+
9+
set(SRC_FILES
10+
"src/Sketch.cpp"
11+
"$ENV{CODE_PATH}/common/xf/Font.cpp"
12+
"$ENV{CODE_PATH}/common/xf/FontManager.cpp"
13+
"$ENV{CODE_PATH}/common/xf/FontSequence.cpp"
14+
"$ENV{CODE_PATH}/common/quad/QuadBatch.cpp"
15+
)
16+
17+
#
18+
# For Mojave and up
19+
#
20+
add_compile_definitions(
21+
GL_SILENCE_DEPRECATION
22+
)
23+
24+
include("$ENV{CROSS_PATH}/core/cmake/sketch.cmake")
25+
include("$ENV{CROSS_PATH}/core/cmake/test.cmake")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
set(CTEST_PROJECT_NAME Drawing2dText)
3+
set(CTEST_CONFIGURATION_TYPE Release)
4+
5+
if (PLATFORM MATCHES osx|linux)
6+
set(ARGS
7+
-DRUN=EXE
8+
)
9+
elseif (PLATFORM MATCHES emscripten)
10+
set(ARGS
11+
-DRUN=BROWSER
12+
)
13+
endif()
14+
15+
include("$ENV{CROSS_PATH}/core/cmake/platforms.cmake")
Binary file not shown.

sketches/Drawing2dText/src/Sketch.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
}

sketches/Drawing2dText/src/Sketch.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "chr/cross/Context.h"
4+
#include "chr/gl/Batch.h"
5+
#include "chr/gl/shaders/ColorShader.h"
6+
#include "chr/gl/shaders/TextureAlphaShader.h"
7+
8+
#include "common/xf/FontManager.h"
9+
10+
class Sketch : public chr::CrossSketch
11+
{
12+
public:
13+
void setup() final;
14+
void resize();
15+
void draw() final;
16+
17+
protected:
18+
chr::gl::VertexBatch<chr::gl::XYZ> strokeBatch;
19+
chr::gl::shaders::ColorShader colorShader;
20+
21+
chr::xf::FontManager fontManager;
22+
std::shared_ptr<chr::XFont> font1;
23+
chr::xf::FontSequence sequence1;
24+
chr::gl::shaders::TextureAlphaShader textureAlphaShader;
25+
26+
void drawGuides();
27+
void drawText1();
28+
29+
static void drawText(chr::XFont &font, const std::u16string &text, float x, float y);
30+
static void drawAlignedText(chr::XFont &font, const std::u16string &text, const glm::vec2 &position, chr::XFont::Alignment alignX, chr::XFont::Alignment alignY);
31+
static void drawAlignedText(chr::XFont &font, const std::u16string &text, chr::gl::Matrix &matrix, chr::XFont::Alignment alignX, chr::XFont::Alignment alignY);
32+
};

sketches/Drawing2dText/src/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#include "Sketch.h"
3+
4+
namespace chr
5+
{
6+
CrossSketch* createSketch()
7+
{
8+
return new Sketch();
9+
}
10+
}
11+
12+
#if defined(CHR_PLATFORM_DESKTOP)
13+
14+
int main(int argc, char** argv)
15+
{
16+
chr::CrossSketch::run(1200, 900, 4);
17+
return 0;
18+
}
19+
20+
#elif defined(CHR_PLATFORM_EMSCRIPTEN)
21+
22+
int main(int argc, char** argv)
23+
{
24+
chr::CrossSketch::run(4);
25+
return 0;
26+
}
27+
28+
#endif

0 commit comments

Comments
 (0)