Skip to content

Commit 2f5eae7

Browse files
committed
DrawingTextOnPath in place
1 parent cde8acc commit 2f5eae7

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed
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(DrawingTextOnPath)
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 DrawingTextOnPath)
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.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include "Sketch.h"
2+
3+
#include "chr/path/SplinePath.h"
4+
5+
using namespace std;
6+
using namespace chr;
7+
using namespace gl;
8+
using namespace math;
9+
using namespace path;
10+
11+
const u16string TEXT = u"Some girls are bigger than others. Some girls mothers are bigger than other girls' mothers."; // Lyrics by the Smiths
12+
13+
void Sketch::setup()
14+
{
15+
font = fontManager.getFont(InputSource::resource("Georgia_Regular_64.fnt"), XFont::Properties2d());
16+
font->setShader(textureAlphaShader);
17+
font->setSize(20);
18+
font->setColor(0, 0, 0, 1);
19+
20+
lineBatch
21+
.setPrimitive(GL_LINES)
22+
.setShader(colorShader)
23+
.setShaderColor(0, 0, 0, 0.25f);
24+
25+
// ---
26+
27+
MatrixAffine matrix;
28+
matrix.scale(3);
29+
30+
SplinePath<glm::vec2> peanut;
31+
peanut
32+
.setType(SplinePath<glm::vec2>::TYPE_BSPLINE)
33+
.setSamplingTolerance(16)
34+
.add(-100, -100)
35+
.add( 0, -25)
36+
.add( 100, -100)
37+
.add( 200, 0)
38+
.add( 100, 100)
39+
.add( 0, 25)
40+
.add(-100, 100)
41+
.add(-200, 0)
42+
.close()
43+
.transformPoints(matrix);
44+
45+
path
46+
.setMode(FollowablePath2D::MODE_LOOP)
47+
.begin()
48+
.add(peanut.getPolyline())
49+
.end();
50+
51+
drawPolyline(path.getPoints());
52+
53+
// ---
54+
55+
glDisable(GL_DEPTH_TEST);
56+
glDepthMask(GL_FALSE);
57+
58+
glEnable(GL_BLEND);
59+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
60+
}
61+
62+
void Sketch::resize()
63+
{}
64+
65+
void Sketch::draw()
66+
{
67+
glClearColor(1, 1, 1, 1);
68+
glClear(GL_COLOR_BUFFER_BIT);
69+
70+
// ---
71+
72+
auto projectionMatrix = glm::ortho(0.0f, windowInfo.width, 0.0f, windowInfo.height);
73+
74+
Matrix modelViewMatrix;
75+
modelViewMatrix
76+
.translate(windowInfo.center())
77+
.scale(1, -1);
78+
79+
gl::State()
80+
.setShaderMatrix(modelViewMatrix * projectionMatrix)
81+
.apply();
82+
83+
// ---
84+
85+
lineBatch.flush();
86+
87+
font->beginSequence(sequence);
88+
drawTextOnPath(*font, TEXT, path, -clock()->getTime() * 75.0f, XFont::ALIGN_MIDDLE);
89+
font->endSequence();
90+
font->replaySequence(sequence);
91+
}
92+
93+
void Sketch::drawPolyline(const vector<FollowablePath2D::Point> &points)
94+
{
95+
auto size = points.size();
96+
if (size > 1)
97+
{
98+
for (auto i = 0; i < size - 1; i++)
99+
{
100+
lineBatch.addVertices(points[i].position, points[i + 1].position);
101+
}
102+
}
103+
}
104+
105+
void Sketch::drawTextOnPath(XFont &font, const u16string &text, const FollowablePath2D &path, float offset, XFont::Alignment alignY)
106+
{
107+
float offsetY = font.getOffsetY(alignY);
108+
Matrix matrix;
109+
110+
for (auto c : text)
111+
{
112+
auto glyphIndex = font.getGlyphIndex(c);
113+
float halfWidth = font.getGlyphAdvance(glyphIndex) / 2;
114+
offset += halfWidth;
115+
116+
if (glyphIndex >= 0)
117+
{
118+
path
119+
.offsetToValue(offset, halfWidth * 2)
120+
.applyToMatrix(matrix);
121+
122+
font.addGlyph(matrix, glyphIndex, -halfWidth, offsetY);
123+
}
124+
125+
offset += halfWidth;
126+
}
127+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
#include "chr/path/FollowablePath2D.h"
8+
9+
#include "common/xf/FontManager.h"
10+
11+
class Sketch : public chr::CrossSketch
12+
{
13+
public:
14+
void setup() final;
15+
void resize() final;
16+
void draw() final;
17+
18+
protected:
19+
chr::gl::VertexBatch<chr::gl::XYZ> lineBatch;
20+
chr::gl::shaders::ColorShader colorShader;
21+
22+
chr::xf::FontManager fontManager;
23+
std::shared_ptr<chr::XFont> font;
24+
chr::xf::FontSequence sequence;
25+
chr::gl::shaders::TextureAlphaShader textureAlphaShader;
26+
27+
chr::path::FollowablePath2D path;
28+
29+
void drawPolyline(const std::vector<chr::path::FollowablePath2D::Point> &points);
30+
static void drawTextOnPath(chr::XFont &font, const std::u16string &text, const chr::path::FollowablePath2D &path, float offset, chr::XFont::Alignment alignY);
31+
};
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)