2525#include < set>
2626// #include <cuchar>
2727
28+ #include " SDL_pixels.h"
2829#include " modules/DFSDL.h"
2930#include " SDL_console.h"
3031
@@ -1384,38 +1385,41 @@ class BMPFontLoader : public FontLoader {
13841385 return &it->second ;
13851386 }
13861387
1387- // SDL_Surface* surface = IMG_Load(path.c_str());
13881388 SDL_Surface* surface = DFSDL::DFIMG_Load (path.c_str ());
13891389 if (surface == nullptr ) {
13901390 return nullptr ;
13911391 }
13921392
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+
13941405 // FIXME: hardcoded magenta
13951406 Uint32 bg_color = sdl_console::SDL_MapRGB (surface->format , 255 , 0 , 255 );
13961407 sdl_console::SDL_SetColorKey (surface, SDL_TRUE, bg_color);
13971408
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 ;
14031411 int height = surface->h ;
14041412
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 );
14121416
1413- auto texture = sdl_tsd.CreateTextureFromSurface (renderer_, conv_surface );
1417+ auto texture = sdl_tsd.CreateTextureFromSurface (renderer_, surface );
14141418 if (!texture) {
14151419 std::cerr << " SDL_CreateTextureFromSurface Error: " << sdl_console::SDL_GetError () << std::endl;
14161420 return nullptr ;
14171421 }
1418- sdl_console::SDL_FreeSurface (conv_surface );
1422+ sdl_console::SDL_FreeSurface (surface );
14191423 sdl_console::SDL_SetTextureBlendMode (texture, SDL_BLENDMODE_BLEND);
14201424 textures_.push_back (texture);
14211425
@@ -1442,12 +1446,13 @@ class BMPFontLoader : public FontLoader {
14421446 int total_glyphs = rows * columns;
14431447
14441448 std::vector<Glyph> glyphs;
1445- glyphs.reserve (rows * columns);
1449+ glyphs.reserve (total_glyphs);
1450+
14461451 for (int i = 0 ; i < total_glyphs; ++i) {
1447- int r = i / rows ;
1452+ int r = i / columns ;
14481453 int c = i % columns;
14491454 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
14511456 glyphs.push_back (glyph);
14521457 }
14531458 return glyphs;
0 commit comments