Skip to content

Commit 634edba

Browse files
committed
#157 Refactor SpriteFrame class (add load function)
1 parent 5e90fc5 commit 634edba

File tree

8 files changed

+280
-130
lines changed

8 files changed

+280
-130
lines changed

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

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace Skylicht
4040

4141
}
4242

43-
CGlyphFont::CGlyphFont(const char *fontName, float sizePt) :
43+
CGlyphFont::CGlyphFont(const char* fontName, float sizePt) :
4444
m_charPadding(0.0f),
4545
m_spacePadding(0.0f),
4646
m_fontName(fontName), // default font
@@ -54,21 +54,21 @@ namespace Skylicht
5454

5555
}
5656

57-
SImage* CGlyphFont::getImage(CAtlas *atlas)
57+
SImage* CGlyphFont::getImage(CAtlas* atlas)
5858
{
5959
// find atlas image
60-
for (SImage& img : m_images)
60+
for (SImage* img : m_images)
6161
{
62-
if (img.Texture == atlas->getTexture())
62+
if (img->Texture == atlas->getTexture())
6363
{
64-
return &img;
64+
return img;
6565
}
6666
}
6767

6868
// add new image
69-
m_images.push_back(SImage());
69+
m_images.push_back(new SImage());
7070

71-
SImage* img = &m_images.back();
71+
SImage* img = m_images.back();
7272
img->Texture = atlas->getTexture();
7373
img->Atlas = atlas;
7474

@@ -86,8 +86,8 @@ namespace Skylicht
8686

8787
float advance = 0.0f, x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f, offsetX = 0, offsetY = 0;
8888

89-
CGlyphFreetype *glyphFreetype = CGlyphFreetype::getInstance();
90-
CAtlas *atlas = glyphFreetype->getCharImage(
89+
CGlyphFreetype* glyphFreetype = CGlyphFreetype::getInstance();
90+
CAtlas* atlas = glyphFreetype->getCharImage(
9191
(u16)character,
9292
m_fontName.c_str(),
9393
fontSize,
@@ -99,59 +99,58 @@ namespace Skylicht
9999
{
100100
SImage* img = getImage(atlas);
101101

102-
m_frames.push_back(SFrame());
103-
SFrame &frame = m_frames.back();
102+
m_frames.push_back(new SFrame());
103+
SFrame* frame = m_frames.back();
104104

105-
frame.Image = img;
106-
frame.ID = character;
107-
frame.ModuleOffset.push_back(SModuleOffset());
105+
frame->Image = img;
106+
frame->ID = character;
107+
frame->ModuleOffset.push_back(SModuleOffset());
108108

109-
c = &frame.ModuleOffset.back();
109+
c = &frame->ModuleOffset.back();
110110
c->Character = character;
111111
c->XAdvance = advance;
112112
c->OffsetX = offsetX;
113113
c->OffsetY = offsetY;
114114

115115
core::dimension2du size = atlas->getImage()->getDimension();
116116

117-
m_moduleRect.push_back(SModuleRect());
118-
SModuleRect &module = m_moduleRect.back();
119-
module.X = x * size.Width;
120-
module.Y = y * size.Height;
121-
module.W = w * size.Width;
122-
module.H = h * size.Height;
117+
m_modules.push_back(new SModuleRect());
118+
SModuleRect* module = m_modules.back();
119+
module->X = x * size.Width;
120+
module->Y = y * size.Height;
121+
module->W = w * size.Width;
122+
module->H = h * size.Height;
123123

124-
c->Module = &module;
125-
c->Frame = &frame;
124+
c->Module = module;
125+
c->Frame = frame;
126126

127127
// bounding rect
128-
frame.BoudingRect.UpperLeftCorner.set(module.X, module.Y);
129-
frame.BoudingRect.LowerRightCorner.set(module.X + module.W, module.Y + module.H);
128+
frame->BoudingRect.UpperLeftCorner.set(module->X, module->Y);
129+
frame->BoudingRect.LowerRightCorner.set(module->X + module->W, module->Y + module->H);
130130

131131
m_moduleOffset[key] = c;
132-
m_frames.push_back(frame);
133132
}
134133

135134
return c;
136135
}
137136

138-
void CGlyphFont::getListModule(const wchar_t *string, std::vector<int>& format, std::vector<SModuleOffset*>& output, std::vector<int>& outputFormat)
137+
void CGlyphFont::getListModule(const wchar_t* string, std::vector<int>& format, std::vector<SModuleOffset*>& output, std::vector<int>& outputFormat)
139138
{
140139
IFont::getListModule(string, format, output, outputFormat);
141140

142-
for (SImage& img : m_images)
141+
for (SImage* img : m_images)
143142
{
144-
if (img.Atlas != NULL)
145-
img.Atlas->updateTexture();
143+
if (img->Atlas != NULL)
144+
img->Atlas->updateTexture();
146145
}
147146
}
148147

149148
void CGlyphFont::updateFontTexture()
150149
{
151-
for (SImage& img : m_images)
150+
for (SImage* img : m_images)
152151
{
153-
if (img.Atlas != NULL)
154-
img.Atlas->updateTexture();
152+
if (img->Atlas != NULL)
153+
img->Atlas->updateTexture();
155154
}
156155
}
157156
}

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,41 @@ This file is part of the "Skylicht Engine".
2929
#if defined(USE_FREETYPE)
3030

3131
#include "Graphics2D/Atlas/CAtlas.h"
32+
#include "CSpriteFrame.h"
3233

3334
namespace Skylicht
3435
{
35-
class CGlyphFont : public IFont
36+
class CGlyphFont :
37+
public CSpriteFrame,
38+
public IFont
3639
{
3740
protected:
3841
float m_charPadding;
3942
float m_spacePadding;
43+
float m_fontSizePt;
4044

4145
std::map<int, SModuleOffset*> m_moduleOffset;
4246

43-
std::list<SImage> m_images;
44-
std::list<SModuleRect> m_moduleRect;
45-
std::list<SFrame> m_frames;
46-
4747
std::string m_fontName;
48-
float m_fontSizePt;
4948

5049
protected:
5150

52-
SImage* getImage(CAtlas *atlas);
51+
SImage* getImage(CAtlas* atlas);
5352

5453
public:
5554
CGlyphFont();
5655

57-
CGlyphFont(const char *fontName, float sizePt);
56+
CGlyphFont(const char* fontName, float sizePt);
5857

5958
virtual ~CGlyphFont();
6059

61-
inline void setFont(const char *fontName, float sizePt)
60+
inline void setFont(const char* fontName, float sizePt)
6261
{
6362
m_fontName = fontName;
6463
m_fontSizePt = sizePt;
6564
}
6665

67-
inline const char *getFontName()
66+
inline const char* getFontName()
6867
{
6968
return m_fontName.c_str();
7069
}
@@ -92,11 +91,11 @@ namespace Skylicht
9291

9392
virtual SModuleOffset* getCharacterModule(int character);
9493

95-
virtual void getListModule(const wchar_t *string, std::vector<int>& format, std::vector<SModuleOffset*>& output, std::vector<int>& outputFormat);
94+
virtual void getListModule(const wchar_t* string, std::vector<int>& format, std::vector<SModuleOffset*>& output, std::vector<int>& outputFormat);
9695

9796
virtual void updateFontTexture();
9897

99-
std::list<SImage>& getImages()
98+
std::vector<SImage*>& getImages()
10099
{
101100
return m_images;
102101
}

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,15 @@ namespace Skylicht
3838

3939
CSpriteAtlas::~CSpriteAtlas()
4040
{
41-
for (SFrame *&f : m_frames)
42-
delete f;
43-
m_frames.clear();
4441

45-
for (SModuleRect *&m : m_modules)
46-
delete m;
47-
m_modules.clear();
48-
49-
for (SImage *&a : m_images)
50-
{
51-
delete a->Atlas;
52-
delete a;
53-
}
54-
m_images.clear();
55-
m_names.clear();
5642
}
5743

5844
SImage* CSpriteAtlas::createAtlasRect(int w, int h, core::recti& outRegion)
5945
{
60-
SImage *image = NULL;
46+
SImage* image = NULL;
6147
core::recti r;
6248

63-
for (SImage *&a : m_images)
49+
for (SImage*& a : m_images)
6450
{
6551
r = a->Atlas->createRect(w, h);
6652
if (r.getWidth() != 0 && r.getHeight() != 0)
@@ -84,10 +70,10 @@ namespace Skylicht
8470
return image;
8571
}
8672

87-
SFrame* CSpriteAtlas::addFrame(const char *name, const char *path)
73+
SFrame* CSpriteAtlas::addFrame(const char* name, const char* path)
8874
{
89-
IImage *img = getVideoDriver()->createImageFromFile(path);
90-
SFrame *frame = NULL;
75+
IImage* img = getVideoDriver()->createImageFromFile(path);
76+
SFrame* frame = NULL;
9177

9278
if (img != NULL)
9379
{
@@ -107,10 +93,10 @@ namespace Skylicht
10793
int atlasH = h + 2;
10894

10995
int imageID = 0;
110-
SImage *image = NULL;
96+
SImage* image = NULL;
11197
core::recti r;
11298

113-
for (SImage *&a : m_images)
99+
for (SImage*& a : m_images)
114100
{
115101
r = a->Atlas->createRect(atlasW, atlasH);
116102
if (r.getWidth() != 0 && r.getHeight() != 0)
@@ -145,7 +131,7 @@ namespace Skylicht
145131
frame->ID = (int)m_frames.size();
146132

147133
// create module
148-
SModuleRect *module = new SModuleRect();
134+
SModuleRect* module = new SModuleRect();
149135
module->X = (f32)r.UpperLeftCorner.X;
150136
module->Y = (f32)r.UpperLeftCorner.Y;
151137
module->W = (f32)w;
@@ -174,7 +160,7 @@ namespace Skylicht
174160
return frame;
175161
}
176162

177-
SFrame* CSpriteAtlas::getFrame(const char *name)
163+
SFrame* CSpriteAtlas::getFrame(const char* name)
178164
{
179165
std::map<std::string, SFrame*>::iterator it = m_names.find(name);
180166

@@ -186,7 +172,7 @@ namespace Skylicht
186172

187173
void CSpriteAtlas::updateTexture()
188174
{
189-
for (SImage *&a : m_images)
175+
for (SImage*& a : m_images)
190176
{
191177
a->Atlas->updateTexture();
192178
/*
@@ -200,7 +186,7 @@ namespace Skylicht
200186

201187
SImage* CSpriteAtlas::addEmptyAtlas()
202188
{
203-
SImage *image = new SImage();
189+
SImage* image = new SImage();
204190
image->Atlas = new CAtlas(m_fmt, m_width, m_height);
205191
image->Texture = image->Atlas->getTexture();
206192

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,9 @@ This file is part of the "Skylicht Engine".
2828

2929
namespace Skylicht
3030
{
31-
class CSpriteAtlas
31+
class CSpriteAtlas : public CSpriteFrame
3232
{
33-
protected:
34-
std::vector<SImage*> m_images;
35-
std::vector<SFrame*> m_frames;
36-
std::vector<SModuleRect*> m_modules;
37-
38-
std::map<std::string, SFrame*> m_names;
39-
33+
protected:
4034
int m_width;
4135
int m_height;
4236
ECOLOR_FORMAT m_fmt;

0 commit comments

Comments
 (0)