25
25
#include < set>
26
26
// #include <cuchar>
27
27
28
+ #include " SDL_pixels.h"
28
29
#include " modules/DFSDL.h"
29
30
#include " SDL_console.h"
30
31
@@ -1384,38 +1385,41 @@ class BMPFontLoader : public FontLoader {
1384
1385
return &it->second ;
1385
1386
}
1386
1387
1387
- // SDL_Surface* surface = IMG_Load(path.c_str());
1388
1388
SDL_Surface* surface = DFSDL::DFIMG_Load (path.c_str ());
1389
1389
if (surface == nullptr ) {
1390
1390
return nullptr ;
1391
1391
}
1392
1392
1393
- // sdl_console::SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32, 0);
1393
+ if (surface->format ->format != SDL_PIXELFORMAT_ARGB8888) {
1394
+ SDL_Surface* conv_surface = SDL_ConvertSurfaceFormat (surface, SDL_PIXELFORMAT_ARGB8888, 0 );
1395
+ if (!conv_surface) {
1396
+ std::cerr << " Error converting surface format: " << SDL_GetError () << std::endl;
1397
+ SDL_FreeSurface (surface);
1398
+ return nullptr ;
1399
+ }
1400
+
1401
+ SDL_FreeSurface (surface);
1402
+ surface = conv_surface;
1403
+ }
1404
+
1394
1405
// FIXME: hardcoded magenta
1395
1406
Uint32 bg_color = sdl_console::SDL_MapRGB (surface->format , 255 , 0 , 255 );
1396
1407
sdl_console::SDL_SetColorKey (surface, SDL_TRUE, bg_color);
1397
1408
1398
- std::vector<Glyph> glyphs;
1399
- // FIXME: magic numbers
1400
- glyphs = build_glyph_rects (surface->pitch , surface->h , 16 , 16 );
1401
-
1402
- int width = surface->pitch ;
1409
+ // NOTE: Do not use surface->pitch
1410
+ int width = surface->w ;
1403
1411
int height = surface->h ;
1404
1412
1405
- SDL_Surface* conv_surface = sdl_console::SDL_CreateRGBSurface (0 , surface->pitch , surface->h , 32 ,
1406
- 0xFF000000 , 0x00FF0000 , 0x0000FF00 , 0x000000FF );
1407
- if (!conv_surface)
1408
- return nullptr ;
1409
-
1410
- sdl_console::SDL_BlitSurface (surface, NULL , conv_surface, NULL );
1411
- sdl_console::SDL_FreeSurface (surface);
1413
+ std::vector<Glyph> glyphs;
1414
+ // FIXME: magic numbers
1415
+ glyphs = build_glyph_rects (width, height, 16 , 16 );
1412
1416
1413
- auto texture = sdl_tsd.CreateTextureFromSurface (renderer_, conv_surface );
1417
+ auto texture = sdl_tsd.CreateTextureFromSurface (renderer_, surface );
1414
1418
if (!texture) {
1415
1419
std::cerr << " SDL_CreateTextureFromSurface Error: " << sdl_console::SDL_GetError () << std::endl;
1416
1420
return nullptr ;
1417
1421
}
1418
- sdl_console::SDL_FreeSurface (conv_surface );
1422
+ sdl_console::SDL_FreeSurface (surface );
1419
1423
sdl_console::SDL_SetTextureBlendMode (texture, SDL_BLENDMODE_BLEND);
1420
1424
textures_.push_back (texture);
1421
1425
@@ -1442,12 +1446,13 @@ class BMPFontLoader : public FontLoader {
1442
1446
int total_glyphs = rows * columns;
1443
1447
1444
1448
std::vector<Glyph> glyphs;
1445
- glyphs.reserve (rows * columns);
1449
+ glyphs.reserve (total_glyphs);
1450
+
1446
1451
for (int i = 0 ; i < total_glyphs; ++i) {
1447
- int r = i / rows ;
1452
+ int r = i / columns ;
1448
1453
int c = i % columns;
1449
1454
Glyph glyph;
1450
- glyph.rect = { tile_w * c, tile_h * r, tile_w, tile_h };
1455
+ glyph.rect = { tile_w * c, tile_h * r, tile_w, tile_h }; // Rectangle in pixel dimensions
1451
1456
glyphs.push_back (glyph);
1452
1457
}
1453
1458
return glyphs;
0 commit comments