Skip to content

Commit 14af968

Browse files
committed
#158 Enable default support Font Freetype on GUI and update load font on GUI Text
1 parent 2e30b23 commit 14af968

File tree

19 files changed

+136
-175
lines changed

19 files changed

+136
-175
lines changed

CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ option(BUILD_SDL "Build with Simple DirectMedia Layer is a cross-platform develo
108108

109109
option(BUILD_IMGUI "Build Ocornut Imgui" ON)
110110

111-
option(BUILD_FREETYPE "Build Font Freetype" ON)
112-
113111
option(BUILD_SKYLICHT_NETWORK "Build Http, Socket module" ON)
114112

115113
if (BUILD_EMSCRIPTEN)
@@ -134,10 +132,8 @@ else()
134132
set(BUILD_CURL OFF)
135133
endif()
136134

137-
if (BUILD_FREETYPE)
138-
add_definitions(-DUSE_FREETYPE)
139-
add_definitions(-DFT2_BUILD_LIBRARY)
140-
endif()
135+
# freetype font library
136+
add_definitions(-DFT2_BUILD_LIBRARY)
141137

142138
# skylicht debug leak memory
143139
if (BUILD_DEBUG_VLD AND MSVC)

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIText.cpp

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ namespace Skylicht
4040
m_charSpacePadding(0),
4141
m_linePadding(0),
4242
m_updateTextRender(true),
43-
m_font(font)
43+
m_font(NULL),
44+
m_customfont(font),
45+
m_fontData(NULL)
4446
{
4547

4648
}
@@ -55,7 +57,9 @@ namespace Skylicht
5557
m_charSpacePadding(0),
5658
m_linePadding(0),
5759
m_updateTextRender(true),
58-
m_font(font)
60+
m_font(NULL),
61+
m_customfont(font),
62+
m_fontData(NULL)
5963
{
6064
init();
6165
}
@@ -66,22 +70,6 @@ namespace Skylicht
6670
m_textHeight = 50;
6771
m_textSpaceWidth = 20;
6872

69-
if (m_font)
70-
{
71-
// get text height
72-
SModuleOffset* moduleCharA = m_font->getCharacterModule((int)'A');
73-
if (moduleCharA)
74-
{
75-
m_textHeight = (int)moduleCharA->OffsetY + (int)moduleCharA->Module->H;
76-
m_textOffsetY = (int)moduleCharA->OffsetY;
77-
}
78-
79-
// get space width
80-
SModuleOffset* moduleCharSpace = m_font->getCharacterModule((int)' ');
81-
if (moduleCharSpace)
82-
m_textSpaceWidth = (int)moduleCharSpace->XAdvance;
83-
}
84-
8573
for (int i = 0; i < MAX_FORMATCOLOR; i++)
8674
m_colorFormat[i].set(255, 255, 255, 255);
8775
}
@@ -91,6 +79,33 @@ namespace Skylicht
9179

9280
}
9381

82+
void CGUIText::initFont(IFont* font)
83+
{
84+
// get text height
85+
SModuleOffset* moduleCharA = font->getCharacterModule((int)'A');
86+
if (moduleCharA)
87+
{
88+
m_textHeight = (int)moduleCharA->OffsetY + (int)moduleCharA->Module->H;
89+
m_textOffsetY = (int)moduleCharA->OffsetY;
90+
}
91+
92+
// get space width
93+
SModuleOffset* moduleCharSpace = font->getCharacterModule((int)' ');
94+
if (moduleCharSpace)
95+
m_textSpaceWidth = (int)moduleCharSpace->XAdvance;
96+
}
97+
98+
IFont* CGUIText::getCurrentFont()
99+
{
100+
if (m_customfont)
101+
return m_customfont;
102+
103+
if (m_fontData)
104+
return m_fontData->getFont();
105+
106+
return NULL;
107+
}
108+
94109
void CGUIText::setFormatText(const char* formatText)
95110
{
96111
int i = 0;
@@ -373,12 +388,20 @@ namespace Skylicht
373388

374389
void CGUIText::render(CCamera* camera)
375390
{
376-
if (!m_font)
391+
IFont* font = getCurrentFont();
392+
393+
if (!font)
377394
{
378395
CGUIElement::render(camera);
379396
return;
380397
}
381398

399+
if (font != m_font)
400+
{
401+
initFont(font);
402+
m_font = font;
403+
}
404+
382405
if (m_updateTextRender == true)
383406
{
384407
// encode string to list modules & format
@@ -703,9 +726,9 @@ namespace Skylicht
703726
m_fontSource = object->get<std::string>("Font", std::string(""));
704727
m_fontGUID = object->get<std::string>("Font", std::string(""));
705728

706-
CFontSource* font = CFontManager::getInstance()->loadFontSource(m_fontSource.c_str());
707-
if (font)
708-
m_font = font->initFont();
729+
m_fontData = CFontManager::getInstance()->loadFontSource(m_fontSource.c_str());
730+
if (m_fontData)
731+
m_fontData->initFont();
709732

710733
std::string value = object->get<std::string>("Text", std::string(""));
711734
setText(value.c_str());

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIText.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ This file is part of the "Skylicht Engine".
3131

3232
namespace Skylicht
3333
{
34+
class CFontSource;
35+
3436
class CGUIText : public CGUIElement
3537
{
3638
friend class CCanvas;
@@ -40,6 +42,7 @@ namespace Skylicht
4042

4143
protected:
4244
IFont* m_font;
45+
IFont* m_customfont;
4346

4447
int m_charPadding;
4548
int m_charSpacePadding;
@@ -69,6 +72,8 @@ namespace Skylicht
6972
std::string m_fontSource;
7073
std::string m_fontGUID;
7174

75+
CFontSource* m_fontData;
76+
7277
protected:
7378
CGUIText(CCanvas* canvas, CGUIElement* parent, IFont* font);
7479
CGUIText(CCanvas* canvas, CGUIElement* parent, const core::rectf& rect, IFont* font);
@@ -152,5 +157,11 @@ namespace Skylicht
152157
virtual CObjectSerializable* createSerializable();
153158

154159
virtual void loadSerializable(CObjectSerializable* object);
160+
161+
protected:
162+
163+
void initFont(IFont* font);
164+
165+
IFont* getCurrentFont();
155166
};
156167
}

Projects/Skylicht/Engine/Source/Graphics2D/Glyph/CGlyphFreetype.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ This file is part of the "Skylicht Engine".
2525
#include "pch.h"
2626
#include "CGlyphFreetype.h"
2727

28-
#if defined(USE_FREETYPE)
29-
3028
// From SDL_ttf: Handy routines for converting from fixed point
3129
#define FT_CEIL(X) (((X + 63) & -64) / 64)
3230

@@ -432,6 +430,4 @@ namespace Skylicht
432430
m_atlas.push_back(newAtlas);
433431
return newAtlas;
434432
}
435-
}
436-
437-
#endif
433+
}

Projects/Skylicht/Engine/Source/Graphics2D/Glyph/CGlyphFreetype.h

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ This file is part of the "Skylicht Engine".
2424

2525
#pragma once
2626

27-
#if defined(USE_FREETYPE)
2827
#include <ft2build.h>
2928
#include FT_GLYPH_H
3029

@@ -36,7 +35,7 @@ namespace Skylicht
3635
{
3736
struct SGlyphEntity
3837
{
39-
CAtlas *m_atlas;
38+
CAtlas* m_atlas;
4039
float m_advance;
4140
float m_uvX;
4241
float m_uvY;
@@ -49,11 +48,11 @@ namespace Skylicht
4948
struct SFaceEntity
5049
{
5150
FT_Face m_face;
52-
FT_Byte *m_data;
51+
FT_Byte* m_data;
5352

5453
std::map<u32, SGlyphEntity*> m_ge;
5554

56-
SFaceEntity(FT_Face face, FT_Byte *data) :
55+
SFaceEntity(FT_Face face, FT_Byte* data) :
5756
m_face(face),
5857
m_data(data)
5958
{
@@ -91,45 +90,43 @@ namespace Skylicht
9190

9291
virtual ~CGlyphFreetype();
9392

94-
bool initFont(const char *name, const char *path);
93+
bool initFont(const char* name, const char* path);
9594

9695
void clearAtlas();
9796

9897
static int sizePtToPx(float pt);
9998

10099
static float sizePxToPt(int px);
101100

102-
CAtlas *getCharImage(unsigned short code,
103-
const char *name,
101+
CAtlas* getCharImage(unsigned short code,
102+
const char* name,
104103
int fontSize,
105-
float *advance,
106-
float *uvX,
107-
float *uvY,
108-
float *uvW,
109-
float *uvH,
110-
float *offsetX, float *offsetY);
111-
112-
CAtlas *getCharImage(
113-
CSpriteAtlas *external,
104+
float* advance,
105+
float* uvX,
106+
float* uvY,
107+
float* uvW,
108+
float* uvH,
109+
float* offsetX, float* offsetY);
110+
111+
CAtlas* getCharImage(
112+
CSpriteAtlas* external,
114113
unsigned short code,
115-
const char *name,
114+
const char* name,
116115
int fontSize,
117-
float *advance,
118-
float *uvX,
119-
float *uvY,
120-
float *uvW,
121-
float *uvH,
122-
float *offsetX, float *offsetY);
116+
float* advance,
117+
float* uvX,
118+
float* uvY,
119+
float* uvW,
120+
float* uvH,
121+
float* offsetX, float* offsetY);
123122

124123
protected:
125124

126125
CAtlas* addEmptyAtlas(ECOLOR_FORMAT color, int w, int h);
127126

128-
int putGlyphToTexture(const FT_GlyphSlot &glyph, float *uvx, float *uvy, float *uvW, float *uvH);
127+
int putGlyphToTexture(const FT_GlyphSlot& glyph, float* uvx, float* uvy, float* uvW, float* uvH);
129128

130-
CAtlas* putGlyphToTexture(CSpriteAtlas *external, const FT_GlyphSlot &glyph, float *uvx, float *uvy, float *uvW, float *uvH);
129+
CAtlas* putGlyphToTexture(CSpriteAtlas* external, const FT_GlyphSlot& glyph, float* uvx, float* uvy, float* uvW, float* uvH);
131130

132131
};
133-
}
134-
135-
#endif
132+
}

Projects/Skylicht/Engine/Source/Graphics2D/SpriteFrame/CGlyphFont.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ This file is part of the "Skylicht Engine".
2424

2525
#include "pch.h"
2626

27-
#if defined(USE_FREETYPE)
28-
2927
#include "CGlyphFont.h"
3028
#include "Graphics2D/Glyph/CGlyphFreetype.h"
3129

@@ -149,6 +147,4 @@ namespace Skylicht
149147
img->Atlas->updateTexture();
150148
}
151149
}
152-
}
153-
154-
#endif
150+
}

Projects/Skylicht/Engine/Source/Graphics2D/SpriteFrame/CGlyphFont.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ This file is part of the "Skylicht Engine".
2626

2727
#include "IFont.h"
2828

29-
#if defined(USE_FREETYPE)
30-
3129
#include "Graphics2D/Atlas/CAtlas.h"
3230
#include "CSpriteFrame.h"
3331

@@ -77,6 +75,4 @@ namespace Skylicht
7775
return m_images;
7876
}
7977
};
80-
}
81-
82-
#endif
78+
}

Projects/Skylicht/Engine/Source/Skylicht.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ This file is part of the "Skylicht Engine".
5656
#include "Components/CDependentComponent.h"
5757
#include "Components/CComponentCategory.h"
5858

59-
#if defined(USE_FREETYPE)
6059
#include "Graphics2D/Glyph/CGlyphFreetype.h"
61-
#endif
6260

6361

6462
namespace Skylicht
@@ -80,9 +78,7 @@ namespace Skylicht
8078
CAccelerometer::createGetInstance();
8179
CJoystick::createGetInstance();
8280

83-
#if defined(USE_FREETYPE)
8481
CGlyphFreetype::createGetInstance();
85-
#endif
8682

8783
CShaderManager::createGetInstance();
8884
CGraphics2D::createGetInstance();
@@ -133,9 +129,7 @@ namespace Skylicht
133129
CGraphics2D::releaseInstance();
134130
CShaderManager::releaseInstance();
135131

136-
#if defined(USE_FREETYPE)
137132
CGlyphFreetype::releaseInstance();
138-
#endif
139133

140134
CTouchManager::releaseInstance();
141135
CAccelerometer::releaseInstance();

Projects/Template/Source/SkylichtEngine.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ This file is part of the "Skylicht Engine".
7272
#include "Graphics2D/CGraphics2D.h"
7373
#include "Graphics2D/SpriteFrame/CSpriteAtlas.h"
7474
#include "Graphics2D/SpriteFrame/CSpriteFont.h"
75+
#include "Graphics2D/SpriteFrame/CSpriteManager.h"
76+
#include "Graphics2D/SpriteFrame/CFontManager.h"
7577

7678
// Font - Freetype
77-
#if defined(USE_FREETYPE)
7879
#include "Graphics2D/Glyph/CGlyphFreetype.h"
7980
#include "Graphics2D/SpriteFrame/CGlyphFont.h"
80-
#endif
8181

8282
// ECS system
8383
#include "Entity/CEntityManager.h"
@@ -105,6 +105,8 @@ This file is part of the "Skylicht Engine".
105105

106106
// Serializable
107107
#include "Serializable/CValueProperty.h"
108+
#include "Serializable/CObjectSerializable.h"
109+
#include "Serializable/CSerializableLoader.h"
108110

109111
// Utils
110112
#include "Utils/CColor.h"

0 commit comments

Comments
 (0)