Remove some [textures] function dependencies
- LoadFontDefault() -> Some code simplifications - LoadFontFromImage() -> Avoid LoadImageEx() - LoadFontData() -> Avoid GenImageColor(), ImageFormat() - LoadBMFont() -> Avoid ImageCopy(), ImageFormat()
This commit is contained in:
parent
ca6016cc71
commit
65b7047111
1 changed files with 40 additions and 27 deletions
67
src/text.c
67
src/text.c
|
@ -191,33 +191,31 @@ extern void LoadFontDefault(void)
|
||||||
|
|
||||||
// Re-construct image from defaultFontData and generate OpenGL texture
|
// Re-construct image from defaultFontData and generate OpenGL texture
|
||||||
//----------------------------------------------------------------------
|
//----------------------------------------------------------------------
|
||||||
int imWidth = 128;
|
Image imFont = {
|
||||||
int imHeight = 128;
|
.data = calloc(128*128, 2), // 2 bytes per pixel (gray + alpha)
|
||||||
|
.width = 128,
|
||||||
|
.height = 128,
|
||||||
|
.format = UNCOMPRESSED_GRAY_ALPHA,
|
||||||
|
.mipmaps = 1
|
||||||
|
};
|
||||||
|
|
||||||
Color *imagePixels = (Color *)RL_MALLOC(imWidth*imHeight*sizeof(Color));
|
// Fill image.data with defaultFontData (convert from bit to pixel!)
|
||||||
|
for (int i = 0, counter = 0; i < imFont.width*imFont.height; i += 32)
|
||||||
for (int i = 0; i < imWidth*imHeight; i++) imagePixels[i] = BLANK; // Initialize array
|
|
||||||
|
|
||||||
int counter = 0; // Font data elements counter
|
|
||||||
|
|
||||||
// Fill imgData with defaultFontData (convert from bit to pixel!)
|
|
||||||
for (int i = 0; i < imWidth*imHeight; i += 32)
|
|
||||||
{
|
{
|
||||||
for (int j = 31; j >= 0; j--)
|
for (int j = 31; j >= 0; j--)
|
||||||
{
|
{
|
||||||
if (BIT_CHECK(defaultFontData[counter], j)) imagePixels[i+j] = WHITE;
|
if (BIT_CHECK(defaultFontData[counter], j))
|
||||||
|
{
|
||||||
|
// NOTE: We are unreferencing data as short, so,
|
||||||
|
// we must consider data as little-endian order (alpha + gray)
|
||||||
|
((unsigned short *)imFont.data)[i + j] = 0xffff;
|
||||||
|
}
|
||||||
|
else ((unsigned short *)imFont.data)[i + j] = 0x00ff;
|
||||||
}
|
}
|
||||||
|
|
||||||
counter++;
|
counter++;
|
||||||
|
|
||||||
if (counter > 512) counter = 0; // Security check...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Image imFont = LoadImageEx(imagePixels, imWidth, imHeight);
|
|
||||||
ImageFormat(&imFont, UNCOMPRESSED_GRAY_ALPHA);
|
|
||||||
|
|
||||||
RL_FREE(imagePixels);
|
|
||||||
|
|
||||||
defaultFont.texture = LoadTextureFromImage(imFont);
|
defaultFont.texture = LoadTextureFromImage(imFont);
|
||||||
|
|
||||||
// Reconstruct charSet using charsWidth[], charsHeight, charsDivisor, charsCount
|
// Reconstruct charSet using charsWidth[], charsHeight, charsDivisor, charsCount
|
||||||
|
@ -445,9 +443,13 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
|
||||||
for (int i = 0; i < image.height*image.width; i++) if (COLOR_EQUAL(pixels[i], key)) pixels[i] = BLANK;
|
for (int i = 0; i < image.height*image.width; i++) if (COLOR_EQUAL(pixels[i], key)) pixels[i] = BLANK;
|
||||||
|
|
||||||
// Create a new image with the processed color data (key color replaced by BLANK)
|
// Create a new image with the processed color data (key color replaced by BLANK)
|
||||||
Image fontClear = LoadImageEx(pixels, image.width, image.height);
|
Image fontClear = {
|
||||||
|
.data = pixels,
|
||||||
RL_FREE(pixels); // Free pixels array memory
|
.width = image.width,
|
||||||
|
.height = image.height,
|
||||||
|
.format = UNCOMPRESSED_R8G8B8A8,
|
||||||
|
.mipmaps = 1
|
||||||
|
};
|
||||||
|
|
||||||
// Create spritefont with all data parsed from image
|
// Create spritefont with all data parsed from image
|
||||||
Font font = { 0 };
|
Font font = { 0 };
|
||||||
|
@ -571,8 +573,15 @@ CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int c
|
||||||
// NOTE: We create an empty image for space character, it could be further required for atlas packing
|
// NOTE: We create an empty image for space character, it could be further required for atlas packing
|
||||||
if (ch == 32)
|
if (ch == 32)
|
||||||
{
|
{
|
||||||
chars[i].image = GenImageColor(chars[i].advanceX, fontSize, BLANK);
|
Image imSpace = {
|
||||||
ImageFormat(&chars[i].image, UNCOMPRESSED_GRAYSCALE);
|
.data = calloc(chars[i].advanceX*fontSize, 2),
|
||||||
|
.width = chars[i].advanceX,
|
||||||
|
.height = fontSize,
|
||||||
|
.format = UNCOMPRESSED_GRAYSCALE,
|
||||||
|
.mipmaps = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
chars[i].image = imSpace;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == FONT_BITMAP)
|
if (type == FONT_BITMAP)
|
||||||
|
@ -727,7 +736,6 @@ Image GenImageFontAtlas(const CharInfo *chars, Rectangle **charRecs, int charsCo
|
||||||
// TODO: Crop image if required for smaller size
|
// TODO: Crop image if required for smaller size
|
||||||
|
|
||||||
// Convert image data from GRAYSCALE to GRAY_ALPHA
|
// Convert image data from GRAYSCALE to GRAY_ALPHA
|
||||||
// WARNING: ImageAlphaMask(&atlas, atlas) does not work in this case, requires manual operation
|
|
||||||
unsigned char *dataGrayAlpha = (unsigned char *)RL_MALLOC(atlas.width*atlas.height*sizeof(unsigned char)*2); // Two channels
|
unsigned char *dataGrayAlpha = (unsigned char *)RL_MALLOC(atlas.width*atlas.height*sizeof(unsigned char)*2); // Two channels
|
||||||
|
|
||||||
for (int i = 0, k = 0; i < atlas.width*atlas.height; i++, k += 2)
|
for (int i = 0, k = 0; i < atlas.width*atlas.height; i++, k += 2)
|
||||||
|
@ -1732,9 +1740,14 @@ static Font LoadBMFont(const char *fileName)
|
||||||
if (imFont.format == UNCOMPRESSED_GRAYSCALE)
|
if (imFont.format == UNCOMPRESSED_GRAYSCALE)
|
||||||
{
|
{
|
||||||
// Convert image to GRAYSCALE + ALPHA, using the mask as the alpha channel
|
// Convert image to GRAYSCALE + ALPHA, using the mask as the alpha channel
|
||||||
Image imFontAlpha = ImageCopy(imFont);
|
Image imFontAlpha = {
|
||||||
ImageFormat(&imFontAlpha, UNCOMPRESSED_GRAY_ALPHA);
|
.data = calloc(imFont.width*imFont.height, 2),
|
||||||
|
.width = imFont.width,
|
||||||
|
.height = imFont.height,
|
||||||
|
.format = UNCOMPRESSED_GRAY_ALPHA,
|
||||||
|
.mipmaps = 1
|
||||||
|
};
|
||||||
|
|
||||||
for (int p = 0, i = 0; p < (imFont.width*imFont.height*2); p += 2, i++)
|
for (int p = 0, i = 0; p < (imFont.width*imFont.height*2); p += 2, i++)
|
||||||
{
|
{
|
||||||
((unsigned char *)(imFontAlpha.data))[p] = 0xff;
|
((unsigned char *)(imFontAlpha.data))[p] = 0xff;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue