aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2018-06-21 00:18:13 +0200
committerRay <raysan5@gmail.com>2018-06-21 00:18:13 +0200
commit0e135118fd0edd3ef613cb28cb573d12916dca98 (patch)
tree0604d9317e17998eb44073a851bafa76fcbba858 /src
parentd6f56c0d5c96153172e0cc579316cd43d5350427 (diff)
downloadraylib-0e135118fd0edd3ef613cb28cb573d12916dca98.tar.gz
raylib-0e135118fd0edd3ef613cb28cb573d12916dca98.zip
Improved GenImageFontAtlas()
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h2
-rw-r--r--src/text.c22
2 files changed, 8 insertions, 16 deletions
diff --git a/src/raylib.h b/src/raylib.h
index 49434d52..9a6e117f 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -965,7 +965,7 @@ RLAPI void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle dest
RLAPI Font GetDefaultFont(void); // Get the default Font
RLAPI Font LoadFont(const char *fileName); // Load font from file into GPU memory (VRAM)
RLAPI CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int charsCount, bool sdf); // Load font data for further use
-RLAPI Image GenImageFontAtlas(CharInfo *chars, int fontSize, int charsCount, int packing); // Generate image font atlas using chars info
+RLAPI Image GenImageFontAtlas(CharInfo *chars, int fontSize, int charsCount, int padding, int packMethod); // Generate image font atlas using chars info
RLAPI void UnloadFont(Font font); // Unload Font from GPU memory (VRAM)
// Text drawing functions
diff --git a/src/text.c b/src/text.c
index 3c3ff751..5e02a471 100644
--- a/src/text.c
+++ b/src/text.c
@@ -284,7 +284,7 @@ Font LoadFont(const char *fileName)
font.baseSize = DEFAULT_TTF_FONTSIZE;
font.charsCount = DEFAULT_TTF_NUMCHARS;
font.chars = LoadFontData(fileName, font.baseSize, NULL, font.charsCount, false);
- Image atlas = GenImageFontAtlas(font.chars, font.charsCount, font.baseSize, 0);
+ Image atlas = GenImageFontAtlas(font.chars, font.charsCount, font.baseSize, 4, 0);
font.texture = LoadTextureFromImage(atlas);
UnloadImage(atlas);
}
@@ -398,20 +398,12 @@ CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int c
// Generate image font atlas using chars info
// NOTE: Packing method: 0-Default, 1-Skyline
-Image GenImageFontAtlas(CharInfo *chars, int charsCount, int fontSize, int packing)
+Image GenImageFontAtlas(CharInfo *chars, int charsCount, int fontSize, int padding, int packMethod)
{
Image atlas = { 0 };
- int padding = 10;
-
- // Calculate atlas texture size based on fontSize
- // NOTE: Font texture size is predicted (being as much conservative as possible)
- // Predictive method consist of supposing same number of chars by line-column (sqrtf)
- // and a maximum character width of 3/4 of fontSize... it worked ok with all my tests...
- //float guessSize = ceilf((float)fontSize*3/4)*ceilf(sqrtf((float)charsCount));
- //int textureSize = (int)powf(2, ceilf(logf((float)guessSize)/logf(2))); // Calculate next POT
-
- // TODO: TEXTURE SIZE NOT GOOD ENOUGH! -> Calculate chars area -> guess texture size?
+ // Calculate texture size based on required pixel area
+ // NOTE: Texture is forced to be squared and POT
float requiredArea = 0;
for (int i = 0; i < charsCount; i++) requiredArea += ((chars[i].rec.width + 2*padding)*(chars[i].rec.height + 2*padding));
float guessSize = sqrtf(requiredArea)*1.25f;
@@ -423,7 +415,7 @@ Image GenImageFontAtlas(CharInfo *chars, int charsCount, int fontSize, int packi
atlas.format = UNCOMPRESSED_GRAYSCALE;
atlas.mipmaps = 1;
- if (packing == 0) // Use basic packing algorythm
+ if (packMethod == 0) // Use basic packing algorythm
{
int offsetX = padding;
int offsetY = padding;
@@ -455,7 +447,7 @@ Image GenImageFontAtlas(CharInfo *chars, int charsCount, int fontSize, int packi
}
}
}
- else if (packing == 1) // Use Skyline rect packing algorythm
+ else if (packMethod == 1) // Use Skyline rect packing algorythm (stb_pack_rect)
{
stbrp_context *context = (stbrp_context *)malloc(sizeof(*context));
stbrp_node *nodes = (stbrp_node *)malloc(charsCount*sizeof(*nodes));
@@ -498,7 +490,7 @@ Image GenImageFontAtlas(CharInfo *chars, int charsCount, int fontSize, int packi
}
// Convert image data from GRAYSCALE to GRAY_ALPHA
- //ImageAlphaMask(&atlas, atlas); // WARNING: Not working in this case, requires manual operation
+ // WARNING: ImageAlphaMask(&atlas, atlas) does not work in this case, requires manual operation
unsigned char *dataGrayAlpha = (unsigned char *)malloc(textureSize*textureSize*sizeof(unsigned char)*2); // Two channels
for (int i = 0, k = 0; i < atlas.width*atlas.height; i++, k += 2)