Skip to content

Commit e4b8829

Browse files
committed
Starting with DrawingLotsOf2dText
1 parent 10e4ce7 commit e4b8829

File tree

7 files changed

+196
-0
lines changed

7 files changed

+196
-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(DrawingLotsOf2dText)
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 DrawingLotsOf2dText)
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
From the ice-age to the dole-age
2+
There is but one concern
3+
I have just discovered
4+
Some girls are bigger than others
5+
Some girls are bigger than others
6+
Some girls mothers are bigger than other girls' mothers
7+
Some girls are bigger than others
8+
Some girls are bigger than others
9+
Some girls mothers are bigger than other girls' mothers
10+
As Antony said to Cleopatra
11+
As he opened a crate of ale
12+
Oh, I say
13+
Some girls are bigger than others
14+
Some girls are bigger than others
15+
Some girls mothers are bigger than other girls' mothers
16+
Some girls are bigger than others
17+
Some girls are bigger than others
18+
Some girls mothers are bigger than other girls' mothers
19+
And I'll send you mine
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "Sketch.h"
2+
3+
using namespace std;
4+
using namespace chr;
5+
using namespace gl;
6+
7+
void Sketch::setup()
8+
{
9+
auto lines = utils::readLines<u16string>(InputSource::resource("song.txt"));
10+
11+
u16string text;
12+
for (const auto &line : lines)
13+
{
14+
text += line + u" ";
15+
}
16+
17+
font = fontManager.getFont(InputSource::resource("Georgia_Regular_64.fnt"), XFont::Properties2d());
18+
font->setShader(textureAlphaShader);
19+
font->setSize(16);
20+
font->setColor(0, 0, 0, 1);
21+
22+
font->beginSequence(sequence);
23+
drawTextSpiral(*font, text, 30, 800, 33, 0, XFont::ALIGN_MIDDLE);
24+
font->endSequence();
25+
26+
// ---
27+
28+
glDisable(GL_DEPTH_TEST);
29+
glDepthMask(GL_FALSE);
30+
31+
glEnable(GL_BLEND);
32+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
33+
}
34+
35+
void Sketch::draw()
36+
{
37+
glClearColor(1, 1, 1, 1);
38+
glClear(GL_COLOR_BUFFER_BIT);
39+
40+
// ---
41+
42+
auto projectionMatrix = glm::ortho(0.0f, windowInfo.width, 0.0f, windowInfo.height);
43+
44+
Matrix modelViewMatrix;
45+
modelViewMatrix
46+
.translate(windowInfo.center())
47+
.scale(1, -1)
48+
.rotateZ(clock()->getTime() * 1.0f);
49+
50+
gl::State()
51+
.setShaderMatrix(modelViewMatrix * projectionMatrix)
52+
.apply();
53+
54+
// ---
55+
56+
font->replaySequence(sequence);
57+
}
58+
59+
void Sketch::drawTextSpiral(XFont &font, const u16string &text, float r1, float r2, float turns, float offset, chr::XFont::Alignment alignY)
60+
{
61+
float l = TWO_PI * turns;
62+
float dr = (r2 - r1) / l;
63+
64+
float offsetY = font.getOffsetY(alignY);
65+
Matrix matrix;
66+
67+
for (auto c : text)
68+
{
69+
auto glyphIndex = font.getGlyphIndex(c);
70+
float halfWidth = font.getGlyphAdvance(glyphIndex) / 2;
71+
offset += halfWidth;
72+
73+
if (glyphIndex >= 0)
74+
{
75+
float r = sqrtf(r1 * r1 + 2 * dr * offset);
76+
float d = (r - r1) / dr;
77+
78+
matrix
79+
.setTranslate(cosf(-d) * r, sinf(-d) * r)
80+
.rotateZ(-d - HALF_PI);
81+
82+
font.addGlyph(matrix, glyphIndex, -halfWidth, offsetY);
83+
}
84+
85+
offset += halfWidth;
86+
}
87+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include "chr/cross/Context.h"
4+
#include "chr/gl/Batch.h"
5+
#include "chr/gl/shaders/TextureAlphaShader.h"
6+
7+
#include "common/xf/FontManager.h"
8+
9+
class Sketch : public chr::CrossSketch
10+
{
11+
public:
12+
void setup() final;
13+
void draw() final;
14+
15+
protected:
16+
chr::xf::FontManager fontManager;
17+
std::shared_ptr<chr::XFont> font;
18+
chr::xf::FontSequence sequence;
19+
chr::gl::shaders::TextureAlphaShader textureAlphaShader;
20+
21+
static void drawTextSpiral(chr::XFont &font, const std::u16string &text, float r1, float r2, float turns, float offset, chr::XFont::Alignment alignY);
22+
};
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)