From 9af10686b28cb48de7cdd37625ae119e913d36b2 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 6 Oct 2015 17:17:53 +0200 Subject: Added comments and more... Corrected bug on defaultFont.size Renamed funcs: ImageConvertFormat() -> ImageFormat() ImageConvertToPOT() -> ImageToPOT() --- src/text.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 35a5f049..ebd5d44e 100644 --- a/src/text.c +++ b/src/text.c @@ -172,7 +172,7 @@ extern void LoadDefaultFont(void) //fclose(myimage); Image image = LoadImageEx(imagePixels, imWidth, imHeight); - ImageConvertFormat(&image, UNCOMPRESSED_GRAY_ALPHA); + ImageFormat(&image, UNCOMPRESSED_GRAY_ALPHA); free(imagePixels); @@ -211,7 +211,7 @@ extern void LoadDefaultFont(void) else currentPosX = testPosX; } - defaultFont.size = defaultFont.charRecs[0].y; + defaultFont.size = defaultFont.charRecs[0].height; TraceLog(INFO, "[TEX ID %i] Default font loaded successfully", defaultFont.texture.id); } @@ -242,10 +242,10 @@ SpriteFont LoadSpriteFont(const char *fileName) Image image = LoadImage(fileName); #if defined(PLATFORM_WEB) - ImageConvertToPOT(&image, MAGENTA); + ImageToPOT(&image, MAGENTA); #endif // Process bitmap font pixel data to get characters measures - // spriteFont.charSet data is filled inside the function and memory is allocated! + // spriteFont chars data is filled inside the function and memory is allocated! int numChars = ParseImageData(image, &spriteFont.charValues, &spriteFont.charRecs); TraceLog(DEBUG, "[%s] SpriteFont data parsed correctly", fileName); @@ -288,7 +288,6 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color) } // Draw text using SpriteFont -// NOTE: If font size is lower than base size, base size is used // NOTE: chars spacing is NOT proportional to fontSize void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int fontSize, int spacing, Color tint) { @@ -299,21 +298,27 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f Rectangle rec; - //if (fontSize <= spriteFont.charRecs[0].height) scaleFactor = 1.0f; - //else scaleFactor = (float)fontSize / spriteFont.charRecs[0].height; - scaleFactor = (float)fontSize/spriteFont.charRecs[0].height; + // NOTE: Some ugly hacks are made to support Latin-1 Extended characters directly + // written in C code files (codified by default as UTF-8) + for(int i = 0; i < length; i++) { - if ((unsigned char)text[i] == 0xc2) + // TODO: Right now we are supposing characters follow a continous order and start at FONT_FIRST_CHAR, + // this sytem can be improved to support any characters order and init value... + // An intermediate table could be created to link char values with predefined char position index in chars rectangle array + + if ((unsigned char)text[i] == 0xc2) // UTF-8 encoding identification HACK! { + // Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿) letter = (unsigned char)text[i + 1]; rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR]; i++; } - else if ((unsigned char)text[i] == 0xc3) + else if ((unsigned char)text[i] == 0xc3) // UTF-8 encoding identification HACK! { + // Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ) letter = (unsigned char)text[i + 1]; rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR + 64]; i++; @@ -569,7 +574,7 @@ static SpriteFont LoadRBMF(const char *fileName) } Image image = LoadImageEx(imagePixels, rbmfHeader.imgWidth, rbmfHeader.imgHeight); - ImageConvertFormat(&image, UNCOMPRESSED_GRAY_ALPHA); + ImageFormat(&image, UNCOMPRESSED_GRAY_ALPHA); free(imagePixels); -- cgit v1.2.3 From e9792ef7713d9b0066cabdc66227cb5431462797 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 6 Oct 2015 17:32:00 +0200 Subject: Convert to UTF-8 --- src/text.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index ebd5d44e..65650956 100644 --- a/src/text.c +++ b/src/text.c @@ -311,14 +311,14 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f if ((unsigned char)text[i] == 0xc2) // UTF-8 encoding identification HACK! { - // Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿) + // Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿) letter = (unsigned char)text[i + 1]; rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR]; i++; } else if ((unsigned char)text[i] == 0xc3) // UTF-8 encoding identification HACK! { - // Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ) + // Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ) letter = (unsigned char)text[i + 1]; rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR + 64]; i++; -- cgit v1.2.3 From f13b30d6060ce7541a7ef7f3074966c623128090 Mon Sep 17 00:00:00 2001 From: Ray San Date: Wed, 21 Oct 2015 18:24:44 +0200 Subject: Feature: Line-break support for text --- src/text.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 65650956..381aa052 100644 --- a/src/text.c +++ b/src/text.c @@ -293,6 +293,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f { int length = strlen(text); int offsetX = 0; + int offsetY = 0; // Line break! float scaleFactor; unsigned char letter; @@ -323,11 +324,23 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR + 64]; i++; } - else rec = spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR]; + else + { + if ((unsigned char)text[i] == '\n') + { + offsetY += ((spriteFont.size + spriteFont.size/2)*scaleFactor); + offsetX = 0; + rec.x = -1; + } + else rec = spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR]; + } - DrawTexturePro(spriteFont.texture, rec, (Rectangle){ position.x + offsetX, position.y, rec.width*scaleFactor, rec.height*scaleFactor} , (Vector2){ 0, 0 }, 0.0f, tint); + if (rec.x > 0) + { + DrawTexturePro(spriteFont.texture, rec, (Rectangle){ position.x + offsetX, position.y + offsetY, rec.width*scaleFactor, rec.height*scaleFactor} , (Vector2){ 0, 0 }, 0.0f, tint); - offsetX += (rec.width*scaleFactor + spacing); + offsetX += (rec.width*scaleFactor + spacing); + } } } -- cgit v1.2.3 From afdf357fbee7f01535bddefb4e62cb1308ed55b5 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 25 Oct 2015 01:50:15 +0200 Subject: Added some image manipulation functions Added (or completed functions): Image ImageText(const char *text, int fontSize, Color color); Image ImageTextEx(SpriteFont font, const char *text, int fontSize, int spacing, Color tint); void ImageFlipVertical(Image *image); void ImageFlipHorizontal(Image *image); void ImageColorTint(Image *image, Color color); void ImageColorInvert(Image *image); void ImageColorGrayscale(Image *image); void ImageColorContrast(Image *image, float contrast); void ImageColorBrightness(Image *image, int brightness); --- src/raylib.h | 21 ++--- src/text.c | 4 +- src/textures.c | 261 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 232 insertions(+), 54 deletions(-) (limited to 'src/text.c') diff --git a/src/raylib.h b/src/raylib.h index 74e6208f..9067c77d 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -547,20 +547,21 @@ void UnloadImage(Image image); void UnloadTexture(Texture2D texture); // Unload texture from GPU memory Color *GetImageData(Image image); // Get pixel data from image as a Color struct array Image GetTextureData(Texture2D texture); // Get pixel data from GPU texture and return an Image -void ImageToPOT(Image *image, Color fillColor); // Convert image to POT (power-of-two) -void ImageFormat(Image *image, int newFormat); // Convert image data to desired format +void ImageToPOT(Image *image, Color fillColor); // Convert image to POT (power-of-two) +void ImageFormat(Image *image, int newFormat); // Convert image data to desired format Image ImageCopy(Image image); // Create an image duplicate (useful for transformations) void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle void ImageResize(Image *image, int newWidth, int newHeight); // Resize and image (bilinear filtering) void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec); // Draw a source image within a destination image -void ImageDrawText(Image *dst, const char *text, Vector2 position, int size, Color color); // Draw text within an image -void ImageDrawTextEx(Image *dst, SpriteFont font, const char *text, Vector2 position, int size, Color color); -void ImageFlipVertical(Image *image); -void ImageFlipHorizontal(Image *image); -void ImageColorInvert(Image *image); -void ImageColorGrayscale(Image *image); -void ImageColorContrast(Image *image, float contrast); -void ImageColorBrightness(Image *image, int brightness); +Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font) +Image ImageTextEx(SpriteFont font, const char *text, int fontSize, int spacing, Color tint); // Create an image from text (custom sprite font) +void ImageFlipVertical(Image *image); // Flip image vertically +void ImageFlipHorizontal(Image *image); // Flip image horizontally +void ImageColorTint(Image *image, Color color); // Modify image color: tint +void ImageColorInvert(Image *image); // Modify image color: invert +void ImageColorGrayscale(Image *image); // Modify bimage color: grayscale +void ImageColorContrast(Image *image, float contrast); // Modify image color: contrast (-100 to 100) +void ImageColorBrightness(Image *image, int brightness); // Modify image color: brightness (-255 to 255) void GenTextureMipmaps(Texture2D texture); // Generate GPU mipmaps for a texture void DrawTexture(Texture2D texture, int posX, int posY, Color tint); // Draw a Texture2D diff --git a/src/text.c b/src/text.c index 381aa052..5e947187 100644 --- a/src/text.c +++ b/src/text.c @@ -279,9 +279,7 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color) Vector2 position = { (float)posX, (float)posY }; int defaultFontSize = 10; // Default Font chars height in pixel - if (fontSize < defaultFontSize) fontSize = defaultFontSize; - int spacing = fontSize / defaultFontSize; DrawTextEx(defaultFont, text, position, fontSize, spacing, color); @@ -380,7 +378,7 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int for (int i = 0; i < len; i++) { - textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; + if (text[i] != '\n') textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; } if (fontSize <= spriteFont.charRecs[0].height) scaleFactor = 1.0f; diff --git a/src/textures.c b/src/textures.c index 6307702f..66d2afc5 100644 --- a/src/textures.c +++ b/src/textures.c @@ -806,12 +806,9 @@ void ImageResize(Image *image, int newWidth, int newHeight) UnloadImage(*image); *image = LoadImageEx(output, newWidth, newHeight); - - free(output); - - // Reformat 32bit RGBA image to original format - ImageFormat(image, format); + ImageFormat(image, format); // Reformat 32bit RGBA image to original format + free(output); free(pixels); } @@ -877,104 +874,286 @@ void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec) } } - free(srcPixels); - - int format = dst->format; - - UnloadImage(*dst); + UnloadImage(*dst); // NOTE: Only dst->data is unloaded *dst = LoadImageEx(dstPixels, dst->width, dst->height); + ImageFormat(dst, dst->format); + free(srcPixels); free(dstPixels); - - ImageFormat(dst, format); } -// Draw a text within an image (destination) -// NOTE: Default font is used -void ImageDrawText(Image *dst, const char *text, Vector2 position, int size, Color color) +// Create an image from text (default font) +Image ImageText(const char *text, int fontSize, Color color) { - ImageDrawTextEx(dst, GetDefaultFont(), text, position, size, color); + int defaultFontSize = 10; // Default Font chars height in pixel + if (fontSize < defaultFontSize) fontSize = defaultFontSize; + int spacing = fontSize / defaultFontSize; + + Image imText = ImageTextEx(GetDefaultFont(), text, fontSize, spacing, color); + + return imText; } -// Draw a text within an image (destination) -// NOTE: Defined SpriteFont is used -void ImageDrawTextEx(Image *dst, SpriteFont font, const char *text, Vector2 position, int size, Color color) +// Create an image from text (custom sprite font) +Image ImageTextEx(SpriteFont font, const char *text, int fontSize, int spacing, Color tint) { + int length = strlen(text); + int posX = 0; + + Vector2 imSize = MeasureTextEx(font, text, font.size, spacing); + + // NOTE: GetTextureData() not available in OpenGL ES Image imFont = GetTextureData(font.texture); - int posX = (int)position.x; + ImageFormat(&imFont, UNCOMPRESSED_R8G8B8A8); // Required for color tint + ImageColorTint(&imFont, tint); // Apply color tint to font + + Color *fontPixels = GetImageData(imFont); - Rectangle srcRec = { 0, 0, 0, font.size }; - Rectangle dstRec = { posX, (int)position.y, 0, font.size }; - int length = strlen(text); + // Create image to store text + Color *pixels = (Color *)malloc(sizeof(Color)*(int)imSize.x*(int)imSize.y); for (int i = 0; i < length; i++) { - srcRec.x = font.charRecs[(int)text[i] - 32].x; - srcRec.y = font.charRecs[(int)text[i] - 32].y; - srcRec.width = font.charRecs[(int)text[i] - 32].width; - dstRec.width = font.charRecs[(int)text[i] - 32].width; - - printf("[%c] Source Rectangle: %i, %i, %i, %i\n", text[i], srcRec.x, srcRec.y, srcRec.width, srcRec.height); - printf("[%c] Destination Rectangle: %i, %i, %i, %i\n\n", text[i], dstRec.x, dstRec.y, dstRec.width, dstRec.height); + Rectangle letterRec = font.charRecs[(int)text[i] - 32]; - ImageDraw(dst, imFont, srcRec, dstRec); + for (int y = letterRec.y; y < (letterRec.y + letterRec.height); y++) + { + for (int x = posX; x < (posX + letterRec.width); x++) + { + pixels[(y - letterRec.y)*(int)imSize.x + x] = fontPixels[y*font.texture.width + (x - posX + letterRec.x)]; + } + } - dstRec.x += srcRec.width; + posX += letterRec.width + spacing; } UnloadImage(imFont); + + Image imText = LoadImageEx(pixels, (int)imSize.x, (int)imSize.y); + + // Scale image depending on text size + if (fontSize > (int)imSize.y) + { + float scaleFactor = (float)fontSize/imSize.y; + TraceLog(INFO, "Scalefactor: %f", scaleFactor); + + // TODO: Allow nearest-neighbor scaling algorithm + ImageResize(&imText, (int)(imSize.x*scaleFactor), (int)(imSize.y*scaleFactor)); + } + + free(pixels); + free(fontPixels); + + return imText; } +// Flip image vertically void ImageFlipVertical(Image *image) { - Image copy = ImageCopy(*image); - ImageFormat(©, UNCOMPRESSED_R8G8B8A8); - - Color *srcPixels = GetImageData(copy); // Get source image data as Color array - Color *dstPixels = GetImageData(copy); + Color *srcPixels = GetImageData(*image); + Color *dstPixels = (Color *)malloc(sizeof(Color)*image->width*image->height); for (int y = 0; y < image->height; y++) { for (int x = 0; x < image->width; x++) { - //dstPixels[y*image->width + x] = srcPixels[]; + dstPixels[y*image->width + x] = srcPixels[(image->height - 1 - y)*image->width + x]; } } + Image processed = LoadImageEx(dstPixels, image->width, image->height); + ImageFormat(&processed, image->format); + UnloadImage(*image); + free(srcPixels); free(dstPixels); - ImageFormat(©, image->format); + image->data = processed.data; +} + +// Flip image horizontally +void ImageFlipHorizontal(Image *image) +{ + Color *srcPixels = GetImageData(*image); + Color *dstPixels = (Color *)malloc(sizeof(Color)*image->width*image->height); + for (int y = 0; y < image->height; y++) + { + for (int x = 0; x < image->width; x++) + { + dstPixels[y*image->width + x] = srcPixels[y*image->width + (image->width - 1 - x)]; + } + } + + Image processed = LoadImageEx(dstPixels, image->width, image->height); + ImageFormat(&processed, image->format); UnloadImage(*image); - image = © + + free(srcPixels); + free(dstPixels); + + image->data = processed.data; } -void ImageFlipHorizontal(Image *image) +// Modify image color: tint +void ImageColorTint(Image *image, Color color) { + Color *pixels = GetImageData(*image); + + float cR = (float)color.r/255; + float cG = (float)color.g/255; + float cB = (float)color.b/255; + float cA = (float)color.a/255; + for (int y = 0; y < image->height; y++) + { + for (int x = 0; x < image->width; x++) + { + unsigned char r = 255*((float)pixels[y*image->width + x].r/255*cR); + unsigned char g = 255*((float)pixels[y*image->width + x].g/255*cG); + unsigned char b = 255*((float)pixels[y*image->width + x].b/255*cB); + unsigned char a = 255*((float)pixels[y*image->width + x].a/255*cA); + + pixels[y*image->width + x].r = r; + pixels[y*image->width + x].g = g; + pixels[y*image->width + x].b = b; + pixels[y*image->width + x].a = a; + } + } + + Image processed = LoadImageEx(pixels, image->width, image->height); + ImageFormat(&processed, image->format); + UnloadImage(*image); + free(pixels); + + TraceLog(INFO,"color tint applied"); + + image->data = processed.data; } +// Modify image color: invert void ImageColorInvert(Image *image) { + Color *pixels = GetImageData(*image); + for (int y = 0; y < image->height; y++) + { + for (int x = 0; x < image->width; x++) + { + pixels[y*image->width + x].r = 255 - pixels[y*image->width + x].r; + pixels[y*image->width + x].g = 255 - pixels[y*image->width + x].g; + pixels[y*image->width + x].b = 255 - pixels[y*image->width + x].b; + } + } + + Image processed = LoadImageEx(pixels, image->width, image->height); + ImageFormat(&processed, image->format); + UnloadImage(*image); + free(pixels); + + image->data = processed.data; } +// Modify image color: grayscale void ImageColorGrayscale(Image *image) { ImageFormat(image, UNCOMPRESSED_GRAYSCALE); } +// Modify image color: contrast +// NOTE: Contrast values between -100 and 100 void ImageColorContrast(Image *image, float contrast) { + if (contrast < -100) contrast = -100; + if (contrast > 100) contrast = 100; + + contrast = (100.0 + contrast)/100.0; + contrast *= contrast; + + Color *pixels = GetImageData(*image); + + for (int y = 0; y < image->height; y++) + { + for (int x = 0; x < image->width; x++) + { + float pR = (float)pixels[y*image->width + x].r/255.0; + pR -= 0.5; + pR *= contrast; + pR += 0.5; + pR *= 255; + if (pR < 0) pR = 0; + if (pR > 255) pR = 255; + + float pG = (float)pixels[y*image->width + x].g/255.0; + pG -= 0.5; + pG *= contrast; + pG += 0.5; + pG *= 255; + if (pG < 0) pG = 0; + if (pG > 255) pG = 255; + + float pB = (float)pixels[y*image->width + x].b/255.0; + pB -= 0.5; + pB *= contrast; + pB += 0.5; + pB *= 255; + if (pB < 0) pB = 0; + if (pB > 255) pB = 255; + + pixels[y*image->width + x].r = (unsigned char)pR; + pixels[y*image->width + x].g = (unsigned char)pG; + pixels[y*image->width + x].b = (unsigned char)pB; + } + } + + Image processed = LoadImageEx(pixels, image->width, image->height); + ImageFormat(&processed, image->format); + UnloadImage(*image); + free(pixels); + image->data = processed.data; } +// Modify image color: brightness +// NOTE: Brightness values between -255 and 255 void ImageColorBrightness(Image *image, int brightness) { + if (brightness < -255) brightness = -255; + if (brightness > 255) brightness = 255; + + Color *pixels = GetImageData(*image); + + for (int y = 0; y < image->height; y++) + { + for (int x = 0; x < image->width; x++) + { + int cR = pixels[y*image->width + x].r + brightness; + int cG = pixels[y*image->width + x].g + brightness; + int cB = pixels[y*image->width + x].b + brightness; + + if (cR < 0) cR = 1; + if (cR > 255) cR = 255; + + if (cG < 0) cG = 1; + if (cG > 255) cG = 255; + + if (cB < 0) cB = 1; + if (cB > 255) cB = 255; + + pixels[y*image->width + x].r = (unsigned char)cR; + pixels[y*image->width + x].g = (unsigned char)cG; + pixels[y*image->width + x].b = (unsigned char)cB; + } + } + + Image processed = LoadImageEx(pixels, image->width, image->height); + ImageFormat(&processed, image->format); + UnloadImage(*image); + free(pixels); + image->data = processed.data; } // Generate GPU mipmaps for a texture -- cgit v1.2.3 From 2fa7e00f169c87585a46b178e6282ed63889189c Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 5 Nov 2015 12:28:45 +0100 Subject: Variables initialization --- src/models.c | 7 +++---- src/text.c | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'src/text.c') diff --git a/src/models.c b/src/models.c index 4943aa5e..f7e39863 100644 --- a/src/models.c +++ b/src/models.c @@ -557,11 +557,10 @@ void DrawGizmo(Vector3 position) // Load a 3d model (from file) Model LoadModel(const char *fileName) { - VertexData vData; - Model model; + VertexData vData = { 0 }; - // TODO: Initialize default data for model in case loading fails, maybe a cube? + // NOTE: Initialize default data for model in case loading fails, maybe a cube? if (strcmp(GetExtension(fileName),"obj") == 0) vData = LoadOBJ(fileName); else TraceLog(WARNING, "[%s] Model extension not recognized, it can't be loaded", fileName); @@ -1589,7 +1588,7 @@ static float GetHeightValue(Color pixel) // Load OBJ mesh data static VertexData LoadOBJ(const char *fileName) { - VertexData vData; + VertexData vData = { 0 }; char dataType; char comments[200]; diff --git a/src/text.c b/src/text.c index 5e947187..7b9660dd 100644 --- a/src/text.c +++ b/src/text.c @@ -649,7 +649,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize) // 1) Generate sprite sheet image with characters from TTF // 2) Load image as SpriteFont - SpriteFont font; + SpriteFont font = { 0 }; Image image; image.width = 512; -- cgit v1.2.3 From 85908befeabecf34f04b983173d73a4f80fbf4b7 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 3 Dec 2015 13:44:45 +0100 Subject: Corrected bug on spritefont loading --- src/text.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 7b9660dd..c62704aa 100644 --- a/src/text.c +++ b/src/text.c @@ -240,20 +240,25 @@ SpriteFont LoadSpriteFont(const char *fileName) else { Image image = LoadImage(fileName); - -#if defined(PLATFORM_WEB) - ImageToPOT(&image, MAGENTA); -#endif - // Process bitmap font pixel data to get characters measures - // spriteFont chars data is filled inside the function and memory is allocated! - int numChars = ParseImageData(image, &spriteFont.charValues, &spriteFont.charRecs); - - TraceLog(DEBUG, "[%s] SpriteFont data parsed correctly", fileName); - TraceLog(DEBUG, "[%s] SpriteFont num chars detected: %i", fileName, numChars); - - spriteFont.numChars = numChars; - spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture - spriteFont.size = spriteFont.charRecs[0].height; + + if (image.data != NULL) + { + // Process bitmap font pixel data to get characters measures + // spriteFont chars data is filled inside the function and memory is allocated! + int numChars = ParseImageData(image, &spriteFont.charValues, &spriteFont.charRecs); + + TraceLog(DEBUG, "[%s] SpriteFont data parsed correctly", fileName); + TraceLog(DEBUG, "[%s] SpriteFont num chars detected: %i", fileName, numChars); + + spriteFont.numChars = numChars; + spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture + spriteFont.size = spriteFont.charRecs[0].height; + } + else + { + TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName, numChars); + spriteFont = GetDefaultFont(); + } UnloadImage(image); } @@ -545,7 +550,9 @@ static SpriteFont LoadRBMF(const char *fileName) if (rbmfFile == NULL) { - TraceLog(WARNING, "[%s] rBMF font file could not be opened", fileName); + TraceLog(WARNING, "[%s] rBMF font file could not be opened, using default font", fileName); + + spriteFont = GetDefaultFont(); } else { -- cgit v1.2.3 From b62f7c3057cdfefacb9dc46e3096ea377a7f05a6 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 9 Dec 2015 20:21:58 +0100 Subject: Corrected bug --- src/text.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index c62704aa..081a8491 100644 --- a/src/text.c +++ b/src/text.c @@ -256,7 +256,7 @@ SpriteFont LoadSpriteFont(const char *fileName) } else { - TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName, numChars); + TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName); spriteFont = GetDefaultFont(); } -- cgit v1.2.3 From f144b6bae43465a62a45f8e45ab2099f215664dc Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 9 Dec 2015 20:57:50 +0100 Subject: MeasureTextEx() - Added support for multi-line size measure --- src/text.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 081a8491..d24ff972 100644 --- a/src/text.c +++ b/src/text.c @@ -378,20 +378,42 @@ int MeasureText(const char *text, int fontSize) Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int spacing) { int len = strlen(text); + int tempLen = 0; // Used to count longer text line num chars + int lenCounter = 0; + int textWidth = 0; + int tempTextWidth = 0; // Used to count longer text line width + + int textHeight = spriteFont.size; float scaleFactor; for (int i = 0; i < len; i++) { - if (text[i] != '\n') textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; + lenCounter++; + + if (text[i] != '\n') + { + textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; + } + else + { + if (tempTextWidth < textWidth) tempTextWidth = textWidth; + lenCounter = 0; + textWidth = 0; + textHeight += (spriteFont.size + spriteFont.size/2); + } + + if (tempLen < lenCounter) tempLen = lenCounter; } + + if (tempTextWidth < textWidth) tempTextWidth = textWidth; - if (fontSize <= spriteFont.charRecs[0].height) scaleFactor = 1.0f; - else scaleFactor = (float)fontSize / spriteFont.charRecs[0].height; + if (fontSize <= spriteFont.size) scaleFactor = 1.0f; + else scaleFactor = (float)fontSize/spriteFont.size; Vector2 vec; - vec.x = (float)textWidth * scaleFactor + (len - 1) * spacing; // Adds chars spacing to measure - vec.y = (float)spriteFont.charRecs[0].height * scaleFactor; + vec.x = (float)tempTextWidth*scaleFactor + (tempLen - 1)*spacing; // Adds chars spacing to measure + vec.y = (float)textHeight*scaleFactor; return vec; } -- cgit v1.2.3 From da28cff0f62b5f14e1f557e17587e964ad0f79fd Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 30 Dec 2015 13:33:26 +0100 Subject: Added function: SubText() Useful to get a piece of text, could be used for text typing animations --- src/text.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index d24ff972..0930b779 100644 --- a/src/text.c +++ b/src/text.c @@ -44,6 +44,7 @@ #define FONT_FIRST_CHAR 32 #define MAX_FONTCHARS 128 #define MAX_FORMATTEXT_LENGTH 64 +#define MAX_SUBTEXT_LENGTH 64 #define BIT_CHECK(a,b) ((a) & (1<<(b))) @@ -360,6 +361,31 @@ const char *FormatText(const char *text, ...) return buffer; } +// Get a piece of a text string +const char *SubText(const char *text, int position, int length) +{ + static char buffer[MAX_SUBTEXT_LENGTH]; + int textLength = strlen(text); + + if (position >= textLength) + { + position = textLength - 1; + length = 0; + } + + if (length >= textLength) length = textLength; + + for (int c = 0 ; c < length ; c++) + { + *(buffer+c) = *(text+position); + text++; + } + + *(buffer+length) = '\0'; + + return buffer; +} + // Measure string width for default font int MeasureText(const char *text, int fontSize) { -- cgit v1.2.3 From fa057f512f6bcfb84a7f736d10c45da50793c8d9 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 2 Jan 2016 10:45:51 +0100 Subject: Improved fonts support Added LoadBMFont() to load AngelCode fonts (.fnt) Implemented LoadTTF() to load .ttf fonts (crappy packaging) --- src/raylib.h | 4 +- src/text.c | 306 ++++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 210 insertions(+), 100 deletions(-) (limited to 'src/text.c') diff --git a/src/raylib.h b/src/raylib.h index 785ebebb..1113958d 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1,6 +1,6 @@ /********************************************************************************************** * -* raylib 1.3.0 (www.raylib.com) +* raylib 1.4.0 (www.raylib.com) * * A simple and easy-to-use library to learn videogames programming * @@ -291,6 +291,8 @@ typedef struct SpriteFont { int numChars; // Number of characters int *charValues; // Characters values array Rectangle *charRecs; // Characters rectangles within the texture + Vector2 *charOffsets; // Characters offsets (on drawing) + int *charAdvanceX; // Characters x advance (on drawing) } SpriteFont; // Camera type, defines a camera position/orientation in 3d space diff --git a/src/text.c b/src/text.c index 0930b779..1a75b9e4 100644 --- a/src/text.c +++ b/src/text.c @@ -34,10 +34,12 @@ // Following libs are used on LoadTTF() #define STB_TRUETYPE_IMPLEMENTATION -#define STB_RECT_PACK_IMPLEMENTATION -#include "stb_rect_pack.h" #include "stb_truetype.h" +// Rectangle packing functions (not used at the moment) +//#define STB_RECT_PACK_IMPLEMENTATION +//#include "stb_rect_pack.h" + //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- @@ -70,6 +72,7 @@ static SpriteFont defaultFont; // Default font provided by raylib static bool PixelIsMagenta(Color p); // Check if a pixel is magenta static int ParseImageData(Image image, int **charValues, Rectangle **charSet); // Parse image pixel data to obtain characters data static SpriteFont LoadRBMF(const char *fileName); // Load a rBMF font file (raylib BitMap Font) +static SpriteFont LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file) static SpriteFont LoadTTF(const char *fileName, int fontSize); // Generate a sprite font image from TTF data (font size required) extern void LoadDefaultFont(void); @@ -185,6 +188,10 @@ extern void LoadDefaultFont(void) defaultFont.charValues = (int *)malloc(defaultFont.numChars*sizeof(int)); defaultFont.charRecs = (Rectangle *)malloc(defaultFont.numChars*sizeof(Rectangle)); // Allocate space for our character rectangle data // This memory should be freed at end! --> Done on CloseWindow() + + defaultFont.charOffsets = (Vector2 *)malloc(defaultFont.numChars*sizeof(Vector2)); + defaultFont.charAdvanceX = (int *)malloc(defaultFont.numChars*sizeof(int)); + int currentLine = 0; int currentPosX = charsDivisor; int testPosX = charsDivisor; @@ -210,6 +217,10 @@ extern void LoadDefaultFont(void) defaultFont.charRecs[i].y = charsDivisor + currentLine*(charsHeight + charsDivisor); } else currentPosX = testPosX; + + // NOTE: On default font character offsets and xAdvance are not required + defaultFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; + defaultFont.charAdvanceX[i] = 0; } defaultFont.size = defaultFont.charRecs[0].height; @@ -222,6 +233,8 @@ extern void UnloadDefaultFont(void) UnloadTexture(defaultFont.texture); free(defaultFont.charValues); free(defaultFont.charRecs); + free(defaultFont.charOffsets); + free(defaultFont.charAdvanceX); } // Get the default font, useful to be used with extended parameters @@ -237,7 +250,8 @@ SpriteFont LoadSpriteFont(const char *fileName) // Check file extension if (strcmp(GetExtension(fileName),"rbmf") == 0) spriteFont = LoadRBMF(fileName); - else if (strcmp(GetExtension(fileName),"ttf") == 0) spriteFont = LoadTTF(fileName, 20); + else if (strcmp(GetExtension(fileName),"ttf") == 0) spriteFont = LoadTTF(fileName, 32); + else if (strcmp(GetExtension(fileName),"fnt") == 0) spriteFont = LoadBMFont(fileName); else { Image image = LoadImage(fileName); @@ -254,6 +268,16 @@ SpriteFont LoadSpriteFont(const char *fileName) spriteFont.numChars = numChars; spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture spriteFont.size = spriteFont.charRecs[0].height; + + defaultFont.charOffsets = (Vector2 *)malloc(defaultFont.numChars*sizeof(Vector2)); + defaultFont.charAdvanceX = (int *)malloc(defaultFont.numChars*sizeof(int)); + + for (int i = 0; i < defaultFont.numChars; i++) + { + // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) + defaultFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; + defaultFont.charAdvanceX[i] = 0; + } } else { @@ -263,6 +287,12 @@ SpriteFont LoadSpriteFont(const char *fileName) UnloadImage(image); } + + if (spriteFont.texture.id == 0) + { + TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName); + spriteFont = GetDefaultFont(); + } return spriteFont; } @@ -270,11 +300,17 @@ SpriteFont LoadSpriteFont(const char *fileName) // Unload SpriteFont from GPU memory void UnloadSpriteFont(SpriteFont spriteFont) { - UnloadTexture(spriteFont.texture); - free(spriteFont.charValues); - free(spriteFont.charRecs); - - TraceLog(INFO, "Unloaded sprite font data"); + // NOTE: Make sure spriteFont is not default font (fallback) + if (spriteFont.texture.id != defaultFont.texture.id) + { + UnloadTexture(spriteFont.texture); + free(spriteFont.charValues); + free(spriteFont.charRecs); + free(spriteFont.charOffsets); + free(spriteFont.charAdvanceX); + + TraceLog(INFO, "Unloaded sprite font data"); + } } // Draw text (using default font) @@ -296,14 +332,14 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color) void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int fontSize, int spacing, Color tint) { int length = strlen(text); - int offsetX = 0; - int offsetY = 0; // Line break! + int textOffsetX = 0; + int textOffsetY = 0; // Line break! float scaleFactor; unsigned char letter; Rectangle rec; - scaleFactor = (float)fontSize/spriteFont.charRecs[0].height; + scaleFactor = (float)fontSize/spriteFont.size; // NOTE: Some ugly hacks are made to support Latin-1 Extended characters directly // written in C code files (codified by default as UTF-8) @@ -332,8 +368,9 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f { if ((unsigned char)text[i] == '\n') { - offsetY += ((spriteFont.size + spriteFont.size/2)*scaleFactor); - offsetX = 0; + // NOTE: Fixed line spacing of 1.5 lines + textOffsetY += ((spriteFont.size + spriteFont.size/2)*scaleFactor); + textOffsetX = 0; rec.x = -1; } else rec = spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR]; @@ -341,9 +378,12 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f if (rec.x > 0) { - DrawTexturePro(spriteFont.texture, rec, (Rectangle){ position.x + offsetX, position.y + offsetY, rec.width*scaleFactor, rec.height*scaleFactor} , (Vector2){ 0, 0 }, 0.0f, tint); + DrawTexturePro(spriteFont.texture, rec, (Rectangle){ position.x + textOffsetX + spriteFont.charOffsets[(int)text[i] - FONT_FIRST_CHAR].x*scaleFactor, + position.y + textOffsetY + spriteFont.charOffsets[(int)text[i] - FONT_FIRST_CHAR].y*scaleFactor, + rec.width*scaleFactor, rec.height*scaleFactor} , (Vector2){ 0, 0 }, 0.0f, tint); - offsetX += (rec.width*scaleFactor + spacing); + if (spriteFont.charAdvanceX[(int)text[i] - FONT_FIRST_CHAR] == 0) textOffsetX += (rec.width*scaleFactor + spacing); + else textOffsetX += (spriteFont.charAdvanceX[(int)text[i] - FONT_FIRST_CHAR]*scaleFactor + spacing); } } } @@ -419,14 +459,15 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int if (text[i] != '\n') { - textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; + if (spriteFont.charAdvanceX[(int)text[i] - FONT_FIRST_CHAR] != 0) textWidth += spriteFont.charAdvanceX[(int)text[i] - FONT_FIRST_CHAR]; + else textWidth += (spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width + spriteFont.charOffsets[(int)text[i] - FONT_FIRST_CHAR].x); } else { if (tempTextWidth < textWidth) tempTextWidth = textWidth; lenCounter = 0; textWidth = 0; - textHeight += (spriteFont.size + spriteFont.size/2); + textHeight += (spriteFont.size + spriteFont.size/2); // NOTE: Fixed line spacing of 1.5 lines } if (tempLen < lenCounter) tempLen = lenCounter; @@ -695,114 +736,181 @@ static SpriteFont LoadRBMF(const char *fileName) return spriteFont; } -// Generate a sprite font from TTF data (font size required) -// NOTE: This function is a mess, it should be completely redone! -static SpriteFont LoadTTF(const char *fileName, int fontSize) +// Load a BMFont file (AngelCode font file) +static SpriteFont LoadBMFont(const char *fileName) { - // Steps: - - // 1) Generate sprite sheet image with characters from TTF - // 2) Load image as SpriteFont + #define MAX_BUFFER_SIZE 256 SpriteFont font = { 0 }; + font.texture.id = 0; + + char buffer[MAX_BUFFER_SIZE]; + char *searchPoint = NULL; + + int fontSize = 0; + int texWidth, texHeight; + char texFileName[128]; + int numChars = 0; - Image image; - image.width = 512; - image.height = 512; - //image.pixels = (Color *)malloc(image.width*image.height*sizeof(Color)); + int base; // Useless data + + FILE *fntFile; - unsigned char *ttfBuffer = (unsigned char *)malloc(1 << 25); + fntFile = fopen(fileName, "rt"); - // TODO: Load TTF and generate bitmap font and chars data -> REVIEW! + if (fntFile == NULL) + { + TraceLog(WARNING, "[%s] FNT file could not be opened", fileName); + return font; + } - stbtt_packedchar chardata[128]; // Num characters: 128 (?) -> REVIEW! + // NOTE: We skip first line, it contains no useful information + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + //searchPoint = strstr(buffer, "size"); + //sscanf(searchPoint, "size=%i", &fontSize); + + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + searchPoint = strstr(buffer, "lineHeight"); + sscanf(searchPoint, "lineHeight=%i base=%i scaleW=%i scaleH=%i", &fontSize, &base, &texWidth, &texHeight); + + TraceLog(DEBUG, "[%s] Font size: %i", fileName, fontSize); + TraceLog(DEBUG, "[%s] Font texture scale: %ix%i", fileName, texWidth, texHeight); + + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + searchPoint = strstr(buffer, "file"); + sscanf(searchPoint, "file=\"%128[^\"]\"", texFileName); + + TraceLog(DEBUG, "[%s] Font texture filename: %s", fileName, texFileName); + + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + searchPoint = strstr(buffer, "count"); + sscanf(searchPoint, "count=%i", &numChars); + + TraceLog(DEBUG, "[%s] Font num chars: %i", fileName, numChars); + + // Compose correct path using route of .fnt file (fileName) and texFileName + char *texPath = NULL; + char *lastSlash = NULL; + + lastSlash = strrchr(fileName, '/'); // you need escape character + texPath = malloc(strlen(fileName) - strlen(lastSlash) + 1 + strlen(texFileName) + 1); + memcpy(texPath, fileName, strlen(fileName) - strlen(lastSlash)); + strcat(texPath, "/"); + strcat(texPath, texFileName); + strcat(texPath, "\0"); + + TraceLog(DEBUG, "[%s] Font texture loading path: %s", fileName, texPath); + + font.texture = LoadTexture(texPath); + font.size = fontSize; + font.numChars = numChars; + font.charValues = (int *)malloc(numChars*sizeof(int)); + font.charRecs = (Rectangle *)malloc(numChars*sizeof(Rectangle)); + font.charOffsets = (Vector2 *)malloc(numChars*sizeof(Vector2)); + font.charAdvanceX = (int *)malloc(numChars*sizeof(int)); + + free(texPath); + + int charId, charX, charY, charWidth, charHeight, charOffsetX, charOffsetY, charAdvanceX; + + for (int i = 0; i < numChars; i++) + { + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + sscanf(buffer, "char id=%i x=%i y=%i width=%i height=%i xoffset=%i yoffset=%i xadvance=%i", + &charId, &charX, &charY, &charWidth, &charHeight, &charOffsetX, &charOffsetY, &charAdvanceX); + + // Save data properly in sprite font + font.charValues[i] = charId; + font.charRecs[i] = (Rectangle){ charX, charY, charWidth, charHeight }; + font.charOffsets[i] = (Vector2){ (float)charOffsetX, (float)charOffsetY }; + font.charAdvanceX[i] = charAdvanceX; + } + + // TODO: Font data could be not ordered by charId: 32,33,34,35... review charValues and charRecs order + + fclose(fntFile); - unsigned char *tempBitmap = (unsigned char *)malloc(image.width*image.height*sizeof(unsigned char)); // One channel bitmap returned! + TraceLog(INFO, "[%s] SpriteFont loaded successfully", fileName); - // Reference struct -/* - typedef struct - { - unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap - float xoff,yoff,xadvance; - float xoff2,yoff2; - } stbtt_packedchar; -*/ + return font; + +} - stbtt_pack_context pc; +// Generate a sprite font from TTF file data (font size required) +// TODO: Review texture packing method and generation (use oversampling) +static SpriteFont LoadTTF(const char *fileName, int fontSize) +{ + // NOTE: Generated font uses some hardcoded values + #define FONT_TEXTURE_WIDTH 512 // Font texture width + #define FONT_TEXTURE_HEIGHT 512 // Font texture height + #define FONT_FIRST_CHAR 32 // Font first character (32 - space) + #define FONT_NUM_CHARS 95 // ASCII 32..126 is 95 glyphs + unsigned char *ttfBuffer = (unsigned char *)malloc(1 << 25); + unsigned char *dataBitmap = (unsigned char *)malloc(FONT_TEXTURE_WIDTH*FONT_TEXTURE_HEIGHT*sizeof(unsigned char)); // One channel bitmap returned! + stbtt_bakedchar charData[FONT_NUM_CHARS]; // ASCII 32..126 is 95 glyphs + + SpriteFont font = { 0 }; + FILE *ttfFile = fopen(fileName, "rb"); + + if (ttfFile == NULL) + { + TraceLog(WARNING, "[%s] FNT file could not be opened", fileName); + return font; + } fread(ttfBuffer, 1, 1<<25, ttfFile); - stbtt_PackBegin(&pc, tempBitmap, image.width, image.height, 0, 1, NULL); - - //stbtt_PackSetOversampling(&pc, 1, 1); - //stbtt_PackFontRange(&pc, ttfBuffer, 0, fontSize, 32, 95, chardata[0]+32); - stbtt_PackSetOversampling(&pc, 2, 2); // Better results - stbtt_PackFontRange(&pc, ttfBuffer, 0, fontSize, 32, 95, chardata + 32); - //stbtt_PackSetOversampling(&pc, 3, 1); - //stbtt_PackFontRange(&pc, ttfBuffer, 0, fontSize, 32, 95, chardata[2]+32); - - stbtt_PackEnd(&pc); + // NOTE: Using stb_truetype crappy packing method, no guarante the font fits the image... + stbtt_BakeFontBitmap(ttfBuffer,0, fontSize, dataBitmap, FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT, FONT_FIRST_CHAR, FONT_NUM_CHARS, charData); free(ttfBuffer); - - // Now we have image data in tempBitmap and chardata filled... -/* - for (int i = 0; i < 512*512; i++) + + // Convert image data from grayscale to to UNCOMPRESSED_GRAY_ALPHA + unsigned char *dataGrayAlpha = (unsigned char *)malloc(FONT_TEXTURE_WIDTH*FONT_TEXTURE_HEIGHT*sizeof(unsigned char)*2); // Two channels + int k = 0; + + for (int i = 0; i < FONT_TEXTURE_WIDTH*FONT_TEXTURE_HEIGHT; i++) { - image.pixels[i].r = tempBitmap[i]; - image.pixels[i].g = tempBitmap[i]; - image.pixels[i].b = tempBitmap[i]; - image.pixels[i].a = 255; - } -*/ - free(tempBitmap); - - // REFERENCE EXAMPLE -/* - //To draw, provide *text, posX, posY - //stbtt_aligned_quad letter; - //stbtt_GetPackedQuad(chardata[0], BITMAP_W, BITMAP_H, *text++, &posX, &posY, &letter, font ? 0 : integer_align); + dataGrayAlpha[k] = 255; + dataGrayAlpha[k + 1] = dataBitmap[i]; - void print(float x, float y, int fontNum, char *text) - { - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, font_tex); - glBegin(GL_QUADS); - while (*text) { - stbtt_aligned_quad q; - stbtt_GetPackedQuad(chardata[fontNum], BITMAP_W, BITMAP_H, *text++, &x, &y, &q, fontNum ? 0 : integer_align); - drawBoxTC(q.x0,q.y0,q.x1,q.y1, q.s0,q.t0,q.s1,q.t1); - } - glEnd(); + k += 2; } + + free(dataBitmap); + + // Sprite font generation from TTF extracted data + Image image; + image.width = FONT_TEXTURE_WIDTH; + image.height = FONT_TEXTURE_HEIGHT; + image.mipmaps = 1; + image.format = UNCOMPRESSED_GRAY_ALPHA; + image.data = dataGrayAlpha; - print(100,160, 0, "This is a test"); -*/ -/* - font.numChars = 95; - font.charValues (int *)malloc(font.numChars*sizeof(int)); - font.charRecs = (Character *)malloc(font.numChars*sizeof(Character)); font.texture = LoadTextureFromImage(image); - - //stbtt_aligned_quad letter; - //int x = 0, y = 0; - + UnloadImage(image); // Unloads dataGrayAlpha + + font.size = fontSize; + font.numChars = FONT_NUM_CHARS; + font.charValues = (int *)malloc(font.numChars*sizeof(int)); + font.charRecs = (Rectangle *)malloc(font.numChars*sizeof(Rectangle)); + font.charOffsets = (Vector2 *)malloc(font.numChars*sizeof(Vector2)); + font.charAdvanceX = (int *)malloc(font.numChars*sizeof(int)); + for (int i = 0; i < font.numChars; i++) { - font.charValues[i] = i + 32; - - //stbtt_GetPackedQuad(chardata[0], 512, 512, i, &x, &y, &letter, 0); + font.charValues[i] = i + FONT_FIRST_CHAR; - font.charRecs[i].x = chardata[i + 32].x0; - font.charRecs[i].y = chardata[i + 32].y0; - font.charRecs[i].width = chardata[i + 32].x1 - chardata[i + 32].x0; - font.charRecs[i].height = chardata[i + 32].y1 - chardata[i + 32].y0; + font.charRecs[i].x = (int)charData[i].x0; + font.charRecs[i].y = (int)charData[i].y0; + font.charRecs[i].width = (int)charData[i].x1 - (int)charData[i].x0; + font.charRecs[i].height = (int)charData[i].y1 - (int)charData[i].y0; + + font.charOffsets[i] = (Vector2){ charData[i].xoff, charData[i].yoff }; + font.charAdvanceX[i] = (int)charData[i].xadvance; } -*/ - UnloadImage(image); return font; } \ No newline at end of file -- cgit v1.2.3 From f98c4dc82b71041ca9ba8364c4d255c88c772501 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 18 Jan 2016 12:04:54 +0100 Subject: Corrected bug on fonts loading --- src/text.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 1a75b9e4..3755932d 100644 --- a/src/text.c +++ b/src/text.c @@ -269,14 +269,14 @@ SpriteFont LoadSpriteFont(const char *fileName) spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture spriteFont.size = spriteFont.charRecs[0].height; - defaultFont.charOffsets = (Vector2 *)malloc(defaultFont.numChars*sizeof(Vector2)); - defaultFont.charAdvanceX = (int *)malloc(defaultFont.numChars*sizeof(int)); + spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2)); + spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int)); - for (int i = 0; i < defaultFont.numChars; i++) + for (int i = 0; i < spriteFont.numChars; i++) { // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) - defaultFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; - defaultFont.charAdvanceX[i] = 0; + spriteFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; + spriteFont.charAdvanceX[i] = 0; } } else @@ -308,7 +308,7 @@ void UnloadSpriteFont(SpriteFont spriteFont) free(spriteFont.charRecs); free(spriteFont.charOffsets); free(spriteFont.charAdvanceX); - + TraceLog(INFO, "Unloaded sprite font data"); } } @@ -627,7 +627,7 @@ static SpriteFont LoadRBMF(const char *fileName) char charsDataType; // Char data type provided } rbmfInfoHeader; - SpriteFont spriteFont; + SpriteFont spriteFont = { 0 }; rbmfInfoHeader rbmfHeader; unsigned int *rbmfFileData = NULL; @@ -695,6 +695,8 @@ static SpriteFont LoadRBMF(const char *fileName) // Get characters data using rbmfCharWidthData, rbmfHeader.charHeight, charsDivisor, rbmfHeader.numChars spriteFont.charValues = (int *)malloc(spriteFont.numChars*sizeof(int)); spriteFont.charRecs = (Rectangle *)malloc(spriteFont.numChars*sizeof(Rectangle)); + spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2)); + spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int)); int currentLine = 0; int currentPosX = charsDivisor; @@ -708,6 +710,10 @@ static SpriteFont LoadRBMF(const char *fileName) spriteFont.charRecs[i].y = charsDivisor + currentLine * ((int)rbmfHeader.charHeight + charsDivisor); spriteFont.charRecs[i].width = (int)rbmfCharWidthData[i]; spriteFont.charRecs[i].height = (int)rbmfHeader.charHeight; + + // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) + spriteFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; + spriteFont.charAdvanceX[i] = 0; testPosX += (spriteFont.charRecs[i].width + charsDivisor); -- cgit v1.2.3 From 823abf666e09e96ccbf5230c96f2d3a61ff60895 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 12 Feb 2016 12:22:56 +0100 Subject: Reviewed code TODOs --- src/audio.c | 2 +- src/camera.c | 2 +- src/core.c | 17 +++++++---------- src/models.c | 1 - src/rlgl.c | 10 +++------- src/text.c | 2 +- src/textures.c | 10 ++++++---- 7 files changed, 19 insertions(+), 25 deletions(-) (limited to 'src/text.c') diff --git a/src/audio.c b/src/audio.c index e40fdd41..260f6778 100644 --- a/src/audio.c +++ b/src/audio.c @@ -280,9 +280,9 @@ Sound LoadSoundFromWave(Wave wave) } // Load sound to memory from rRES file (raylib Resource) +// TODO: Maybe rresName could be directly a char array with all the data? Sound LoadSoundFromRES(const char *rresName, int resId) { - // NOTE: rresName could be directly a char array with all the data!!! --> TODO Sound sound = { 0 }; #if defined(AUDIO_STANDALONE) diff --git a/src/camera.c b/src/camera.c index 6539da5f..517e4a2b 100644 --- a/src/camera.c +++ b/src/camera.c @@ -375,7 +375,7 @@ static void ProcessCamera(Camera *camera, Vector3 *playerPosition) } // Focus to center - // TODO: Move this function out of the module? + // TODO: Move this function out of this module? if (IsKeyDown('Z')) camera->target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera position update diff --git a/src/core.c b/src/core.c index 05ec0c0a..413006e4 100644 --- a/src/core.c +++ b/src/core.c @@ -359,7 +359,7 @@ void InitWindow(int width, int height, struct android_app *state) if (orientation == ACONFIGURATION_ORIENTATION_PORT) TraceLog(INFO, "PORTRAIT window orientation"); else if (orientation == ACONFIGURATION_ORIENTATION_LAND) TraceLog(INFO, "LANDSCAPE window orientation"); - // TODO: Review, it doesn't work... + // TODO: Automatic orientation doesn't seem to work if (width <= height) { AConfiguration_setOrientation(app->config, ACONFIGURATION_ORIENTATION_PORT); @@ -1246,7 +1246,7 @@ int GetTouchY(void) } // Returns touch position XY -// TODO: touch position should be scaled depending on display size and render size +// TODO: Touch position should be scaled depending on display size and render size Vector2 GetTouchPosition(int index) { Vector2 position = { -1.0f, -1.0f }; @@ -1257,7 +1257,7 @@ Vector2 GetTouchPosition(int index) if ((screenWidth > displayWidth) || (screenHeight > displayHeight)) { - // TODO: Seems to work ok but... review! + // TODO: Review touch position scaling for screenSize vs displaySize position.x = position.x*((float)screenWidth/(float)(displayWidth - renderOffsetX)) - renderOffsetX/2; position.y = position.y*((float)screenHeight/(float)(displayHeight - renderOffsetY)) - renderOffsetY/2; } @@ -1668,8 +1668,7 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i #endif else currentKeyState[key] = action; - // HACK for GuiTextBox, to deteck back key - // TODO: Review... + // TODO: Review (and remove) this HACK for GuiTextBox, to deteck back key if ((key == 259) && (action == GLFW_PRESS)) lastKeyPressed = 3; } @@ -1678,8 +1677,6 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int { currentMouseState[button] = action; - // TODO: Test mouse gestures - #define ENABLE_MOUSE_GESTURES #if defined(ENABLE_MOUSE_GESTURES) // Process mouse events as touches to be able to use mouse-gestures @@ -2046,7 +2043,7 @@ static bool GetKeyStatus(int key) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) return glfwGetKey(window, key); #elif defined(PLATFORM_ANDROID) - // TODO: Check virtual keyboard (?) + // TODO: Check for virtual keyboard return false; #elif defined(PLATFORM_RPI) // NOTE: Keys states are filled in PollInputEvents() @@ -2061,7 +2058,7 @@ static bool GetMouseButtonStatus(int button) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) return glfwGetMouseButton(window, button); #elif defined(PLATFORM_ANDROID) - // TODO: Check virtual keyboard (?) + // TODO: Check for virtual keyboard return false; #elif defined(PLATFORM_RPI) // NOTE: mouse buttons array is filled on PollInputEvents() @@ -2382,7 +2379,7 @@ static void RestoreKeyboard(void) // Init gamepad system static void InitGamepad(void) { - // TODO: Gamepad support + // TODO: Add Gamepad support if ((gamepadStream = open(DEFAULT_GAMEPAD_DEV, O_RDONLY|O_NONBLOCK)) < 0) TraceLog(WARNING, "Gamepad device could not be opened, no gamepad available"); else TraceLog(INFO, "Gamepad device initialized successfully"); } diff --git a/src/models.c b/src/models.c index 94e61d84..8a36c279 100644 --- a/src/models.c +++ b/src/models.c @@ -704,7 +704,6 @@ Model LoadHeightmap(Image heightmap, Vector3 size) // TODO: Calculate normals in an efficient way nCounter += 18; // 6 vertex, 18 floats - trisCounter += 2; } } diff --git a/src/rlgl.c b/src/rlgl.c index 2ea06e1c..5f8a5ea1 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -171,7 +171,7 @@ typedef struct { typedef struct { GLuint textureId; int vertexCount; - // TODO: DrawState state -> Blending mode, shader + // TODO: Store draw state -> blending mode, shader } DrawCall; // pixel type (same as Color type) @@ -1475,11 +1475,7 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro // Calculate model-view-projection matrix (MVP) Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates - // NOTE: Drawing in OpenGL 3.3+, matrices are passed to shader - // TODO: Reduce number of matrices passed to shaders, use only matMVP - //glUniformMatrix4fv(model.material.shader.modelLoc, 1, false, MatrixToFloat(matModel)); - //glUniformMatrix4fv(model.material.shader.viewLoc, 1, false, MatrixToFloat(matView)); - + // Send combined model-view-projection matrix to shader glUniformMatrix4fv(model.shader.mvpLoc, 1, false, MatrixToFloat(matMVP)); // Apply color tinting to model @@ -1900,7 +1896,7 @@ void rlglGenerateMipmaps(Texture2D texture) int mipmapCount = GenerateMipmaps(data, texture.width, texture.height); // TODO: Adjust mipmap size depending on texture format! - int size = texture.width*texture.height*4; + int size = texture.width*texture.height*4; // RGBA 32bit only int offset = size; int mipWidth = texture.width/2; diff --git a/src/text.c b/src/text.c index 3755932d..e4c7bbf3 100644 --- a/src/text.c +++ b/src/text.c @@ -346,7 +346,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f for(int i = 0; i < length; i++) { - // TODO: Right now we are supposing characters follow a continous order and start at FONT_FIRST_CHAR, + // TODO: Right now we are supposing characters that follow a continous order and start at FONT_FIRST_CHAR, // this sytem can be improved to support any characters order and init value... // An intermediate table could be created to link char values with predefined char position index in chars rectangle array diff --git a/src/textures.c b/src/textures.c index f03d2d9a..36819daf 100644 --- a/src/textures.c +++ b/src/textures.c @@ -712,7 +712,7 @@ void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp) { oldpixel = pixels[y*image->width + x]; - // TODO: New pixel obtained by bits truncate, it would be better to round values (check ImageFormat()) + // NOTE: New pixel obtained by bits truncate, it would be better to round values (check ImageFormat()) newpixel.r = oldpixel.r>>(8 - rBpp); // R bits newpixel.g = oldpixel.g>>(8 - gBpp); // G bits newpixel.b = oldpixel.b>>(8 - bBpp); // B bits @@ -769,7 +769,7 @@ void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp) } // Convert image to POT (power-of-two) -// NOTE: Requirement on OpenGL ES 2.0 (RPI, HTML5) +// NOTE: It could be useful on OpenGL ES 2.0 (RPI, HTML5) void ImageToPOT(Image *image, Color fillColor) { Color *pixels = GetImageData(*image); // Get pixels data @@ -784,7 +784,7 @@ void ImageToPOT(Image *image, Color fillColor) Color *pixelsPOT = NULL; // Generate POT array from NPOT data - pixelsPOT = (Color *)malloc(potWidth * potHeight * sizeof(Color)); + pixelsPOT = (Color *)malloc(potWidth*potHeight*sizeof(Color)); for (int j = 0; j < potHeight; j++) { @@ -896,7 +896,9 @@ void ImageCrop(Image *image, Rectangle crop) } // Resize and image to new size -// NOTE: Uses stb default scaling filter +// NOTE: Uses stb default scaling filters (both bicubic): +// STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM +// STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL (high-quality Catmull-Rom) void ImageResize(Image *image, int newWidth, int newHeight) { // Get data as Color pixels array to work with it -- cgit v1.2.3