From b4583ce18ce76d3bb4744a1e46ff292ecb21ca77 Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Sun, 26 Jan 2025 18:30:20 -0300 Subject: [PATCH 1/6] 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 d8d290053861..a1b184310db3 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]; From c7f7fa97a25db80edb701758ae4f574ac533b923 Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Sun, 26 Jan 2025 18:33:50 -0300 Subject: [PATCH 2/6] Formatting changes --- src/rtext.c | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index a1b184310db3..208e16d155b2 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -2228,32 +2228,32 @@ static Font LoadBMFont(const char *fileName) imFonts[i] = LoadImage(TextFormat("%s/%s", GetDirectoryPath(fileName), imFileName[i])); 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]); + 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; } From 8414d6e467c6dcd2ded6b68b1c0bf0ce337c5b18 Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Sun, 26 Jan 2025 18:48:09 -0300 Subject: [PATCH 3/6] Added unsupported pixel format log warning --- src/rtext.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rtext.c b/src/rtext.c index 208e16d155b2..9f0a083a6ff7 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -2228,8 +2228,10 @@ static Font LoadBMFont(const char *fileName) imFonts[i] = LoadImage(TextFormat("%s/%s", GetDirectoryPath(fileName), imFileName[i])); PixelFormat format = imFonts[i].format; - if (format != PIXELFORMAT_UNCOMPRESSED_GRAYSCALE && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8) + if (format != PIXELFORMAT_UNCOMPRESSED_GRAYSCALE && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8){ + TRACELOG(LOG_WARNING, "FONT: [%s] Page number %i used an unsupported pixel format", i, fileName); continue; + } // Convert image to GRAYSCALE + ALPHA, using the mask as the alpha channel Image imFontAlpha = { From 6e2c28c671450e72c18d722ad6e11748e01df03b Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Sun, 26 Jan 2025 18:49:57 -0300 Subject: [PATCH 4/6] Oops, got the order on the log warning print wrong. --- src/rtext.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rtext.c b/src/rtext.c index 9f0a083a6ff7..407b8e854777 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -2229,7 +2229,7 @@ static Font LoadBMFont(const char *fileName) PixelFormat format = imFonts[i].format; if (format != PIXELFORMAT_UNCOMPRESSED_GRAYSCALE && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8){ - TRACELOG(LOG_WARNING, "FONT: [%s] Page number %i used an unsupported pixel format", i, fileName); + TRACELOG(LOG_WARNING, "FONT: [%s] Page number %i used an unsupported pixel format", fileName, i); continue; } From 5e8a67eec01e44aa6a5be26b75bf769e0da5fd1e Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Mon, 27 Jan 2025 07:06:46 -0300 Subject: [PATCH 5/6] Formatting changes --- src/rtext.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index 407b8e854777..687c12e7cf2b 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -2227,15 +2227,18 @@ static Font LoadBMFont(const char *fileName) { imFonts[i] = LoadImage(TextFormat("%s/%s", GetDirectoryPath(fileName), imFileName[i])); - PixelFormat format = imFonts[i].format; - if (format != PIXELFORMAT_UNCOMPRESSED_GRAYSCALE && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 && format != PIXELFORMAT_UNCOMPRESSED_R8G8B8){ + int pageFormat = imFonts[i].format; + + if (pageFormat != PIXELFORMAT_UNCOMPRESSED_GRAYSCALE && pageFormat != PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 && pageFormat != PIXELFORMAT_UNCOMPRESSED_R8G8B8) + { TRACELOG(LOG_WARNING, "FONT: [%s] Page number %i used an unsupported pixel format", fileName, i); 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), + + Image imFont = { + .data = RL_CALLOC(imFonts[i].width * imFonts[i].height, 2), .width = imFonts[i].width, .height = imFonts[i].height, .mipmaps = 1, @@ -2243,13 +2246,17 @@ static Font LoadBMFont(const char *fileName) }; int stride = 1; - if (format == PIXELFORMAT_UNCOMPRESSED_R8G8B8){ + + if (pageFormat == PIXELFORMAT_UNCOMPRESSED_R8G8B8) + { stride = 3; - }else if (format == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8){ + } + else if (pageFormat == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8) + { stride = 4; } - for (int p = 0, pi = 0; p < (imFonts[i].width*imFonts[i].height*2); p += 2, pi++) + 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]; From 8856ec6b450e51419c8e9714f900e4e87904e811 Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Mon, 27 Jan 2025 07:09:57 -0300 Subject: [PATCH 6/6] Name change for RGBA case --- src/rtext.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index 687c12e7cf2b..61348de0adef 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -2258,12 +2258,12 @@ static Font LoadBMFont(const char *fileName) 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]; + ((unsigned char *)(imFont.data))[p] = 0xff; + ((unsigned char *)(imFont.data))[p + 1] = ((unsigned char *)imFonts[i].data)[pi * stride]; } UnloadImage(imFonts[i]); - imFonts[i] = imFontAlpha; + imFonts[i] = imFont; } Image fullFont = imFonts[0];