Skip to content

Commit fb6a621

Browse files
committed
tidy up font loading a bit
1 parent ac2f469 commit fb6a621

1 file changed

Lines changed: 24 additions & 66 deletions

File tree

library/SDLConsole_impl.cpp

Lines changed: 24 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ class Font : public SignalEmitter {
12281228
return '?';
12291229
}
12301230

1231-
Font make_copy()
1231+
Font clone()
12321232
{
12331233
return *this;
12341234
}
@@ -1294,60 +1294,16 @@ class Font : public SignalEmitter {
12941294
};
12951295

12961296

1297-
using FontMap = std::map<std::pair<std::string, int>, Font>;
1298-
class FontLoader {
1299-
public:
1300-
FontLoader(SDL_Renderer* renderer)
1301-
: renderer_(renderer)
1302-
{
1303-
}
1304-
1305-
virtual ~FontLoader() = default;
1306-
1307-
virtual Font* open(const std::string& path, int size) = 0;
1308-
1309-
Font* base_font()
1310-
{
1311-
return &fmap_.begin()->second;
1312-
}
1297+
using FontMap = std::map<std::string, Font>;
13131298

1314-
Font* clone(std::string key, Font* font)
1315-
{
1316-
auto kp = std::make_pair(key, 0);
1317-
auto result = fmap_.emplace(kp, font->make_copy());
1318-
return &result.first->second;
1319-
}
1320-
1321-
FontLoader(const FontLoader&) = delete;
1322-
FontLoader& operator=(const FontLoader&) = delete;
1323-
1324-
FontLoader(FontLoader&& other) noexcept
1325-
: fmap_(std::move(other.fmap_))
1326-
, renderer_(other.renderer_)
1327-
, textures_(std::move(other.textures_))
1328-
{
1329-
}
1330-
1331-
FontLoader& operator=(FontLoader&& other) noexcept
1332-
{
1333-
if (this != &other) {
1334-
fmap_ = std::move(other.fmap_);
1335-
renderer_ = other.renderer_;
1336-
textures_ = std::move(other.textures_);
1337-
}
1338-
return *this;
1339-
}
1340-
1341-
protected:
1299+
class BMPFontLoader {
1300+
private:
13421301
FontMap fmap_;
13431302
SDL_Renderer* renderer_;
13441303
std::vector<SDL_Texture*> textures_;
1345-
};
1346-
1347-
class BMPFontLoader : public FontLoader {
13481304
public:
13491305
BMPFontLoader(SDL_Renderer* renderer)
1350-
: FontLoader(renderer)
1306+
: renderer_(renderer)
13511307
{
13521308
}
13531309

@@ -1360,15 +1316,19 @@ class BMPFontLoader : public FontLoader {
13601316
}*/
13611317
}
13621318

1363-
Font* open(const std::string& path, int size)
1319+
Font* base_font()
13641320
{
1365-
auto key = std::make_pair(path, 0);
1366-
auto it = fmap_.find(key);
1321+
return &fmap_.begin()->second;
1322+
}
13671323

1368-
if (it != fmap_.end()) {
1369-
return &it->second;
1370-
}
1324+
Font* clone(std::string key, Font* font)
1325+
{
1326+
auto it = fmap_.emplace(key, font->clone());
1327+
return &it.first->second;
1328+
}
13711329

1330+
Font* load(const std::string& path)
1331+
{
13721332
SDL_Surface* surface = DFSDL::DFIMG_Load(path.c_str());
13731333
if (surface == nullptr) {
13741334
return nullptr;
@@ -1382,19 +1342,20 @@ class BMPFontLoader : public FontLoader {
13821342
// Create a surface in ARGB8888 format, and replace the keyed color
13831343
// with fully transparant pixels. This step completely removes the color.
13841344
// NOTE: Do not use surface->pitch
1385-
13861345
SDL_Surface* conv_surface = sdl_console::SDL_CreateRGBSurfaceWithFormat(0, surface->w, surface->h, 32,
13871346
SDL_PixelFormatEnum::SDL_PIXELFORMAT_ARGB8888);
13881347
sdl_console::SDL_BlitSurface(surface, nullptr, conv_surface, nullptr);
13891348
sdl_console::SDL_FreeSurface(surface);
13901349
surface = conv_surface;
13911350

1392-
int width = surface->w;
1393-
int height = surface->h;
1351+
// FIXME: magic numbers
1352+
int cols = 16;
1353+
int rows = 16;
1354+
int glyph_w = surface->w / cols;
1355+
int glyph_h = surface->h / rows;
13941356

13951357
std::vector<Glyph> glyphs;
1396-
// FIXME: magic numbers
1397-
glyphs = build_glyph_rects(width, height, 16, 16);
1358+
glyphs = build_glyph_rects(surface->w, surface->h, cols, rows);
13981359

13991360
auto* texture = sdl_tsd.CreateTextureFromSurface(renderer_, surface);
14001361
if (!texture ) {
@@ -1405,12 +1366,9 @@ class BMPFontLoader : public FontLoader {
14051366
sdl_console::SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
14061367
textures_.push_back(texture);
14071368

1408-
assert(width > 0);
1409-
assert(height > 0);
1410-
14111369
// FIXME: magic numbers
1412-
auto result = fmap_.emplace(key, Font(renderer_, texture, glyphs, std::max(8, width/16), std::max(8, height/16)));
1413-
return &result.first->second;
1370+
auto it = fmap_.emplace(path, Font(renderer_, texture, glyphs, glyph_w, glyph_h));
1371+
return &it.first->second;
14141372
}
14151373

14161374
BMPFontLoader(const BMPFontLoader&) = delete;
@@ -1589,7 +1547,7 @@ class Widget : public SignalEmitter {
15891547
, context(window_context)
15901548

15911549
{
1592-
auto* bmpfont = context.font_loader.open("data/art/curses_640x300.png", 14);
1550+
auto* bmpfont = context.font_loader.load("data/art/curses_640x300.png");
15931551
if (!bmpfont) {
15941552
throw(std::runtime_error("Error loading font"));
15951553
}

0 commit comments

Comments
 (0)