diff options
| author | raysan5 <raysan5@gmail.com> | 2014-09-03 16:51:28 +0200 |
|---|---|---|
| committer | raysan5 <raysan5@gmail.com> | 2014-09-03 16:51:28 +0200 |
| commit | d2b98fbb5ce8d82bb2c46f3d66ffda58d6ad5c6f (patch) | |
| tree | 164098ab2ecc481353ff43fdcc614ca80d2ad3ad /src/text.c | |
| parent | 222995c32e696f04c4dd2d77141758d6591e2fe9 (diff) | |
| download | raylib-d2b98fbb5ce8d82bb2c46f3d66ffda58d6ad5c6f.tar.gz raylib-d2b98fbb5ce8d82bb2c46f3d66ffda58d6ad5c6f.zip | |
Removed useless spaces
Diffstat (limited to 'src/text.c')
| -rw-r--r-- | src/text.c | 228 |
1 files changed, 114 insertions, 114 deletions
@@ -3,20 +3,20 @@ * raylib.text * * Basic functions to load SpriteFonts and draw Text -* +* * Uses external lib: * stb_image - Multiple formats image loading (JPEG, PNG, BMP, TGA, PSD, GIF, HDR, PIC) * * Copyright (c) 2013 Ramon Santamaria (Ray San - raysan@raysanweb.com) * -* This software is provided "as-is", without any express or implied warranty. In no event +* This software is provided "as-is", without any express or implied warranty. In no event * will the authors be held liable for any damages arising from the use of this software. * -* Permission is granted to anyone to use this software for any purpose, including commercial +* Permission is granted to anyone to use this software for any purpose, including commercial * applications, and to alter it and redistribute it freely, subject to the following restrictions: * -* 1. The origin of this software must not be misrepresented; you must not claim that you -* wrote the original software. If you use this software in a product, an acknowledgment +* 1. The origin of this software must not be misrepresented; you must not claim that you +* wrote the original software. If you use this software in a product, an acknowledgment * in the product documentation would be appreciated but is not required. * * 2. Altered source versions must be plainly marked as such, and must not be misrepresented @@ -82,14 +82,14 @@ static SpriteFont LoadRBMF(const char *fileName); // Load a rBMF font file (ra //---------------------------------------------------------------------------------- // Module Functions Definition //---------------------------------------------------------------------------------- -extern void LoadDefaultFont() +extern void LoadDefaultFont() { defaultFont.numChars = 96; // We know our default font has 94 chars Image image; image.width = 128; // We know our default font image is 128 pixels width image.height = 64; // We know our default font image is 64 pixels height - + // Default font is directly defined here (data generated from a sprite font image) // This way, we reconstruct SpriteFont without creating large global variables // This data is automatically allocated to Stack and automatically deallocated at the end of this function @@ -119,19 +119,19 @@ extern void LoadDefaultFont() int charsHeight = 10; int charsDivisor = 1; // Every char is separated from the consecutive by a 1 pixel divisor, horizontally and vertically - + int charsWidth[96] = { 3, 1, 4, 6, 5, 7, 6, 2, 3, 3, 5, 5, 2, 4, 1, 7, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 3, 4, 3, 6, - 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 5, 6, 5, 7, 6, 6, 6, 6, 6, 6, 7, 6, 7, 7, 6, 6, 6, 2, 7, 2, 3, 5, + 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 5, 6, 5, 7, 6, 6, 6, 6, 6, 6, 7, 6, 7, 7, 6, 6, 6, 2, 7, 2, 3, 5, 2, 5, 5, 5, 5, 5, 4, 5, 5, 1, 2, 5, 2, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 3, 1, 3, 4, 4 }; // Re-construct image from defaultFontData and generate OpenGL texture //---------------------------------------------------------------------- image.pixels = (Color *)malloc(image.width * image.height * sizeof(Color)); - + for (int i = 0; i < image.width * image.height; i++) image.pixels[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 < image.width * image.height; i += 32) { @@ -139,15 +139,15 @@ extern void LoadDefaultFont() { if (BIT_CHECK(defaultFontData[counter], j)) image.pixels[i+j] = WHITE; } - + counter++; - + if (counter > 256) counter = 0; // Security check... } - + defaultFont.texture = CreateTexture(image, false); // Convert loaded image to OpenGL texture UnloadImage(image); - + // Reconstruct charSet using charsWidth[], charsHeight, charsDivisor, numChars //------------------------------------------------------------------------------ defaultFont.charSet = (Character *)malloc(defaultFont.numChars * sizeof(Character)); // Allocate space for our character data @@ -155,7 +155,7 @@ extern void LoadDefaultFont() int currentLine = 0; int currentPosX = charsDivisor; int testPosX = charsDivisor; - + for (int i = 0; i < defaultFont.numChars; i++) { defaultFont.charSet[i].value = FONT_FIRST_CHAR + i; // First char is 32 @@ -163,21 +163,21 @@ extern void LoadDefaultFont() defaultFont.charSet[i].y = charsDivisor + currentLine * (charsHeight + charsDivisor); defaultFont.charSet[i].w = charsWidth[i]; defaultFont.charSet[i].h = charsHeight; - + testPosX += (defaultFont.charSet[i].w + charsDivisor); - + if (testPosX >= defaultFont.texture.width) { currentLine++; currentPosX = 2 * charsDivisor + charsWidth[i]; testPosX = currentPosX; - + defaultFont.charSet[i].x = charsDivisor; defaultFont.charSet[i].y = charsDivisor + currentLine * (charsHeight + charsDivisor); } else currentPosX = testPosX; } - + TraceLog(INFO, "Default font loaded successfully"); } @@ -194,29 +194,29 @@ SpriteFont GetDefaultFont() } // Load a SpriteFont image into GPU memory -SpriteFont LoadSpriteFont(const char* fileName) +SpriteFont LoadSpriteFont(const char* fileName) { SpriteFont spriteFont; - + Image image; - + // Check file extension if (strcmp(GetExtension(fileName),"rbmf") == 0) spriteFont = LoadRBMF(fileName); else - { + { // Use stb_image to load image data! int imgWidth; int imgHeight; int imgBpp; - + byte *imgData = stbi_load(fileName, &imgWidth, &imgHeight, &imgBpp, 4); // Force loading to 4 components (RGBA) - + // Convert array to pixel array for working convenience Color *imgDataPixel = (Color *)malloc(imgWidth * imgHeight * sizeof(Color)); Color *imgDataPixelPOT = NULL; - + int pix = 0; - + for (int i = 0; i < (imgWidth * imgHeight * 4); i += 4) { imgDataPixel[pix].r = imgData[i]; @@ -225,33 +225,33 @@ SpriteFont LoadSpriteFont(const char* fileName) imgDataPixel[pix].a = imgData[i+3]; pix++; } - + stbi_image_free(imgData); - + // At this point we have a pixel array with all the data... - + TraceLog(INFO, "[%s] SpriteFont image loaded: %i x %i", fileName, imgWidth, imgHeight); - + // Process bitmap Font pixel data to get measures (Character array) // spriteFont.charSet data is filled inside the function and memory is allocated! int numChars = ParseImageData(imgDataPixel, imgWidth, imgHeight, &spriteFont.charSet); - + TraceLog(INFO, "[%s] SpriteFont data parsed correctly", fileName); TraceLog(INFO, "[%s] SpriteFont num chars detected: %i", fileName, numChars); - + spriteFont.numChars = numChars; - + // Convert image font to POT image before conversion to texture // Just add the required amount of pixels at the right and bottom sides of image... int potWidth = GetNextPOT(imgWidth); int potHeight = GetNextPOT(imgHeight); - + // Check if POT texture generation is required (if texture is not already POT) if ((potWidth != imgWidth) || (potHeight != imgHeight)) { // Generate POT array from NPOT data imgDataPixelPOT = (Color *)malloc(potWidth * potHeight * sizeof(Color)); - + for (int j = 0; j < potHeight; j++) { for (int i = 0; i < potWidth; i++) @@ -260,20 +260,20 @@ SpriteFont LoadSpriteFont(const char* fileName) else imgDataPixelPOT[j*potWidth + i] = MAGENTA; } } - + TraceLog(WARNING, "SpriteFont texture converted to POT: %ix%i", potWidth, potHeight); } - + free(imgDataPixel); image.pixels = imgDataPixelPOT; image.width = potWidth; image.height = potHeight; - + spriteFont.texture = CreateTexture(image, false); // Convert loaded image to OpenGL texture UnloadImage(image); } - + return spriteFont; } @@ -290,13 +290,13 @@ void UnloadSpriteFont(SpriteFont spriteFont) 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); } @@ -308,9 +308,9 @@ void DrawTextEx(SpriteFont spriteFont, const char* text, Vector2 position, int f int length = strlen(text); int positionX = (int)position.x; float scaleFactor; - + Character c; - + if (fontSize <= spriteFont.charSet[0].h) scaleFactor = 1.0f; else scaleFactor = (float)fontSize / spriteFont.charSet[0].h; @@ -320,22 +320,22 @@ void DrawTextEx(SpriteFont spriteFont, const char* text, Vector2 position, int f for(int i = 0; i < length; i++) { c = spriteFont.charSet[(int)text[i] - FONT_FIRST_CHAR]; - + rlColor4ub(tint.r, tint.g, tint.b, tint.a); rlNormal3f(0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer - - rlTexCoord2f((float)c.x / spriteFont.texture.width, (float)c.y / spriteFont.texture.height); + + rlTexCoord2f((float)c.x / spriteFont.texture.width, (float)c.y / spriteFont.texture.height); rlVertex2f(positionX, position.y); - - rlTexCoord2f((float)c.x / spriteFont.texture.width, (float)(c.y + c.h) / spriteFont.texture.height); + + rlTexCoord2f((float)c.x / spriteFont.texture.width, (float)(c.y + c.h) / spriteFont.texture.height); rlVertex2f(positionX, position.y + (c.h) * scaleFactor); - - rlTexCoord2f((float)(c.x + c.w) / spriteFont.texture.width, (float)(c.y + c.h) / spriteFont.texture.height); + + rlTexCoord2f((float)(c.x + c.w) / spriteFont.texture.width, (float)(c.y + c.h) / spriteFont.texture.height); rlVertex2f(positionX + (c.w) * scaleFactor, position.y + (c.h) * scaleFactor); - - rlTexCoord2f((float)(c.x + c.w) / spriteFont.texture.width, (float)c.y / spriteFont.texture.height); + + rlTexCoord2f((float)(c.x + c.w) / spriteFont.texture.width, (float)c.y / spriteFont.texture.height); rlVertex2f(positionX + (c.w) * scaleFactor, position.y); - + positionX += ((spriteFont.charSet[(int)text[i] - FONT_FIRST_CHAR].w) * scaleFactor + spacing); } rlEnd(); @@ -347,7 +347,7 @@ void DrawTextEx(SpriteFont spriteFont, const char* text, Vector2 position, int f const char *FormatText(const char *text, ...) { static char buffer[MAX_FORMATTEXT_LENGTH]; - + va_list args; va_start(args, text); vsprintf(buffer, text, args); @@ -372,19 +372,19 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int int len = strlen(text); int textWidth = 0; float scaleFactor; - + for (int i = 0; i < len; i++) { textWidth += spriteFont.charSet[(int)text[i] - FONT_FIRST_CHAR].w; } - + if (fontSize <= spriteFont.charSet[0].h) scaleFactor = 1.0f; else scaleFactor = (float)fontSize / spriteFont.charSet[0].h; - + Vector2 vec; vec.x = (float)textWidth * scaleFactor + (len - 1) * spacing; // Adds chars spacing to measure vec.y = (float)spriteFont.charSet[0].h * scaleFactor; - + return vec; } @@ -402,11 +402,11 @@ void DrawFPS(int posX, int posY) static float fps; static int counter = 0; static int refreshRate = 0; - + char buffer[20]; - + if (counter < refreshRate) - { + { counter++; } else @@ -415,7 +415,7 @@ void DrawFPS(int posX, int posY) refreshRate = fps; counter = 0; } - + sprintf(buffer, "%2.0f FPS", fps); DrawText(buffer, posX, posY, 20, LIME); } @@ -435,36 +435,36 @@ static int ParseImageData(Color *imgDataPixel, int imgWidth, int imgHeight, Char { int charSpacing = 0; int lineSpacing = 0; - + int x = 0; int y = 0; - + Character tempCharSet[MAX_FONTCHARS]; // We allocate a temporal array for charData, once we get the actual charNumber we copy data to a sized array. - + for(y = 0; y < imgHeight; y++) - { + { for(x = 0; x < imgWidth; x++) - { + { if (!PixelIsMagenta(imgDataPixel[y*imgWidth + x])) break; } if (!PixelIsMagenta(imgDataPixel[y*imgWidth + x])) break; } - + charSpacing = x; lineSpacing = y; - + int charHeight = 0; int j = 0; - + while(!PixelIsMagenta(imgDataPixel[(lineSpacing + j)*imgWidth + charSpacing])) j++; - + charHeight = j; - + // Check array values to get characters: value, x, y, w, h int index = 0; int lineToRead = 0; int xPosToRead = charSpacing; - + while((lineSpacing + lineToRead * (charHeight + lineSpacing)) < imgHeight) { while((xPosToRead < imgWidth) && @@ -474,22 +474,22 @@ static int ParseImageData(Color *imgDataPixel, int imgWidth, int imgHeight, Char tempCharSet[index].x = xPosToRead; tempCharSet[index].y = lineSpacing + lineToRead * (charHeight + lineSpacing); tempCharSet[index].h = charHeight; - + int charWidth = 0; - + while(!PixelIsMagenta(imgDataPixel[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*imgWidth + xPosToRead + charWidth])) charWidth++; - + tempCharSet[index].w = charWidth; - + index++; - + xPosToRead += (charWidth + charSpacing); } - + lineToRead++; xPosToRead = charSpacing; } - + // We got tempCharSet populated with char data and the number of chars (index) // Now we move temp data to real charSet (passed as parameter to the function) (*charSet) = (Character *)malloc(index * sizeof(Character)); // BE CAREFUL! This memory should be freed! @@ -531,7 +531,7 @@ static SpriteFont LoadRBMF(const char *fileName) short imgHeight; // Image height - always POT (power-of-two) short numChars; // Number of characters contained short charHeight; // Characters height - the same for all characters - char compType; // Compression type: + char compType; // Compression type: // 4 MSB --> image data compression // 4 LSB --> chars data compression char charsDataType; // Char data type provided @@ -539,42 +539,42 @@ static SpriteFont LoadRBMF(const char *fileName) SpriteFont spriteFont; Image image; - + rbmfInfoHeader rbmfHeader; unsigned int *rbmfFileData; unsigned char *rbmfCharWidthData; - + int charsDivisor = 1; // Every char is separated from the consecutive by a 1 pixel divisor, horizontally and vertically - + FILE *rbmfFile = fopen(fileName, "rb"); // Define a pointer to bitmap file and open it in read-binary mode fread(&rbmfHeader, sizeof(rbmfInfoHeader), 1, rbmfFile); - + TraceLog(INFO, "[%s] Loading rBMF file, size: %ix%i, numChars: %i, charHeight: %i", fileName, rbmfHeader.imgWidth, rbmfHeader.imgHeight, rbmfHeader.numChars, rbmfHeader.charHeight); - + spriteFont.numChars = (int)rbmfHeader.numChars; - + image.width = (int)rbmfHeader.imgWidth; image.height = (int)rbmfHeader.imgHeight; - + int numPixelBits = rbmfHeader.imgWidth * rbmfHeader.imgHeight / 32; - + rbmfFileData = (unsigned int *)malloc(numPixelBits * sizeof(unsigned int)); - + for(int i = 0; i < numPixelBits; i++) fread(&rbmfFileData[i], sizeof(unsigned int), 1, rbmfFile); - + rbmfCharWidthData = (unsigned char *)malloc(spriteFont.numChars * sizeof(unsigned char)); - + for(int i = 0; i < spriteFont.numChars; i++) fread(&rbmfCharWidthData[i], sizeof(unsigned char), 1, rbmfFile); - + // Re-construct image from rbmfFileData //----------------------------------------- image.pixels = (Color *)malloc(image.width * image.height * sizeof(Color)); - + for (int i = 0; i < image.width * image.height; i++) image.pixels[i] = BLANK; // Initialize array int counter = 0; // Font data elements counter - + // Fill image data (convert from bit to pixel!) for (int i = 0; i < image.width * image.height; i += 32) { @@ -582,24 +582,24 @@ static SpriteFont LoadRBMF(const char *fileName) { if (BIT_CHECK(rbmfFileData[counter], j)) image.pixels[i+j] = WHITE; } - + counter++; } - + TraceLog(INFO, "[%s] Image reconstructed correctly, now converting it to texture", fileName); - + spriteFont.texture = CreateTexture(image, false); UnloadImage(image); // Unload image data - + TraceLog(INFO, "[%s] Starting charSet reconstruction", fileName); - + // Reconstruct charSet using rbmfCharWidthData, rbmfHeader.charHeight, charsDivisor, rbmfHeader.numChars spriteFont.charSet = (Character *)malloc(spriteFont.numChars * sizeof(Character)); // Allocate space for our character data int currentLine = 0; int currentPosX = charsDivisor; int testPosX = charsDivisor; - + for (int i = 0; i < spriteFont.numChars; i++) { spriteFont.charSet[i].value = (int)rbmfHeader.firstChar + i; @@ -607,27 +607,27 @@ static SpriteFont LoadRBMF(const char *fileName) spriteFont.charSet[i].y = charsDivisor + currentLine * ((int)rbmfHeader.charHeight + charsDivisor); spriteFont.charSet[i].w = (int)rbmfCharWidthData[i]; spriteFont.charSet[i].h = (int)rbmfHeader.charHeight; - + testPosX += (spriteFont.charSet[i].w + charsDivisor); - + if (testPosX > spriteFont.texture.width) { currentLine++; currentPosX = 2 * charsDivisor + (int)rbmfCharWidthData[i]; testPosX = currentPosX; - + spriteFont.charSet[i].x = charsDivisor; spriteFont.charSet[i].y = charsDivisor + currentLine * (rbmfHeader.charHeight + charsDivisor); } else currentPosX = testPosX; } - + TraceLog(INFO, "[%s] rBMF file loaded correctly as SpriteFont", fileName); - + fclose(rbmfFile); - + free(rbmfFileData); // Now we can free loaded data from RAM memory - free(rbmfCharWidthData); + free(rbmfCharWidthData); return spriteFont; } @@ -636,8 +636,8 @@ static SpriteFont LoadRBMF(const char *fileName) static SpriteFont GenerateFromTTF(const char *fileName, int fontSize) { SpriteFont font; - + // TODO: Load TTF and generate bitmap font and chars data - + return font; }
\ No newline at end of file |
