From b4583ce18ce76d3bb4744a1e46ff292ecb21ca77 Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Sun, 26 Jan 2025 18:30:20 -0300 Subject: [PATCH] Added RGB and RGBA image support to BMFont loader --- src/rtext.c | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index d8d290053..a1b184310 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -2227,26 +2227,34 @@ static Font LoadBMFont(const char *fileName) { imFonts[i] = LoadImage(TextFormat("%s/%s", GetDirectoryPath(fileName), imFileName[i])); - if (imFonts[i].format == PIXELFORMAT_UNCOMPRESSED_GRAYSCALE) - { - // Convert image to GRAYSCALE + ALPHA, using the mask as the alpha channel - Image imFontAlpha = { - .data = RL_CALLOC(imFonts[i].width*imFonts[i].height, 2), - .width = imFonts[i].width, - .height = imFonts[i].height, - .mipmaps = 1, - .format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA - }; - - for (int p = 0, pi = 0; p < (imFonts[i].width*imFonts[i].height*2); p += 2, pi++) - { - ((unsigned char *)(imFontAlpha.data))[p] = 0xff; - ((unsigned char *)(imFontAlpha.data))[p + 1] = ((unsigned char *)imFonts[i].data)[pi]; - } - - UnloadImage(imFonts[i]); - imFonts[i] = imFontAlpha; - } + PixelFormat format = imFonts[i].format; + if (format != PIXELFORMAT_UNCOMPRESSED_GRAYSCALE && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8) + continue; + + // Convert image to GRAYSCALE + ALPHA, using the mask as the alpha channel + Image imFontAlpha = { + .data = RL_CALLOC(imFonts[i].width*imFonts[i].height, 2), + .width = imFonts[i].width, + .height = imFonts[i].height, + .mipmaps = 1, + .format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA + }; + + int stride = 1; + if (format == PIXELFORMAT_UNCOMPRESSED_R8G8B8){ + stride = 3; + }else if (format == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8){ + stride = 4; + } + + for (int p = 0, pi = 0; p < (imFonts[i].width*imFonts[i].height*2); p += 2, pi++) + { + ((unsigned char *)(imFontAlpha.data))[p] = 0xff; + ((unsigned char *)(imFontAlpha.data))[p + 1] = ((unsigned char *)imFonts[i].data)[pi * stride]; + } + + UnloadImage(imFonts[i]); + imFonts[i] = imFontAlpha; } Image fullFont = imFonts[0];