2014-09-17 00:51:31 +04:00
|
|
|
/**********************************************************************************************
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* raylib.text
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* Basic functions to load SpriteFonts and draw Text
|
2014-09-03 18:51:28 +04:00
|
|
|
*
|
2014-09-17 00:51:31 +04:00
|
|
|
* Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com)
|
2014-03-16 23:59:02 +04:00
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
2013-11-23 16:30:54 +04:00
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
2013-11-23 16:30:54 +04:00
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* 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
|
2013-11-23 16:30:54 +04:00
|
|
|
* in the product documentation would be appreciated but is not required.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
* as being the original software.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
#include <stdlib.h> // Declares malloc() and free() for memory management
|
|
|
|
#include <string.h> // String management functions (just strlen() is used)
|
|
|
|
#include <stdarg.h> // Used for functions with variable number of parameters (FormatText())
|
2014-09-17 00:51:31 +04:00
|
|
|
#include <stdio.h> // Standard input / output lib
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 3.3+ or ES2
|
2014-04-19 18:36:49 +04:00
|
|
|
#include "utils.h" // Required for function GetExtendion()
|
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
// Following libs will be used on LoadTTF()
|
|
|
|
#define STB_TRUETYPE_IMPLEMENTATION
|
|
|
|
#define STB_RECT_PACK_IMPLEMENTATION
|
|
|
|
#include "stb_rect_pack.h"
|
|
|
|
#include "stb_truetype.h"
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Defines and Macros
|
|
|
|
//----------------------------------------------------------------------------------
|
2014-03-16 23:59:02 +04:00
|
|
|
#define FONT_FIRST_CHAR 32
|
|
|
|
#define MAX_FONTCHARS 128
|
|
|
|
#define MAX_FORMATTEXT_LENGTH 50
|
2013-11-19 02:38:44 +04:00
|
|
|
|
|
|
|
#define BIT_CHECK(a,b) ((a) & (1<<(b)))
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2014-11-23 23:58:17 +03:00
|
|
|
// ...
|
2014-04-09 22:25:26 +04:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global variables
|
|
|
|
//----------------------------------------------------------------------------------
|
2013-11-23 16:30:54 +04:00
|
|
|
static SpriteFont defaultFont; // Default font provided by raylib
|
|
|
|
// NOTE: defaultFont is loaded on InitWindow and disposed on CloseWindow [module: core]
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Other Modules Functions Declaration (required by text)
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
//...
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
2013-12-19 15:08:06 +04:00
|
|
|
static bool PixelIsMagenta(Color p); // Check if a pixel is magenta
|
2013-11-23 16:30:54 +04:00
|
|
|
static int ParseImageData(Color *imgDataPixel, int imgWidth, int imgHeight, Character **charSet); // Parse image pixel data to obtain character set measures
|
2013-12-19 15:08:06 +04:00
|
|
|
static SpriteFont LoadRBMF(const char *fileName); // Load a rBMF font file (raylib BitMap Font)
|
2015-01-18 12:58:04 +03:00
|
|
|
static SpriteFont LoadTTF(const char *fileName, int fontSize); // Generate a sprite font image from TTF data (font size required)
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
extern void LoadDefaultFont(void);
|
|
|
|
extern void UnloadDefaultFont(void);
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2014-09-03 19:06:10 +04:00
|
|
|
extern void LoadDefaultFont(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
defaultFont.numChars = 96; // We know our default font has 94 chars
|
2014-03-25 15:40:35 +04:00
|
|
|
|
|
|
|
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
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
// 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
|
|
|
|
int defaultFontData[256] = {
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00200020, 0x0001b000, 0x00000000, 0x00000000, 0x8ef92520, 0x00020a00, 0x7dbe8000, 0x1f7df45f,
|
|
|
|
0x4a2bf2a0, 0x0852091e, 0x41224000, 0x10041450, 0x2e292020, 0x08220812, 0x41222000, 0x10041450, 0x10f92020, 0x3efa084c, 0x7d22103c, 0x107df7de,
|
|
|
|
0xe8a12020, 0x08220832, 0x05220800, 0x10450410, 0xa4a3f000, 0x08520832, 0x05220400, 0x10450410, 0xe2f92020, 0x0002085e, 0x7d3e0281, 0x107df41f,
|
|
|
|
0x00200000, 0x8001b000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xc0000fbe, 0xfbf7e00f, 0x5fbf7e7d, 0x0050bee8, 0x440808a2, 0x0a142fe8, 0x50810285, 0x0050a048,
|
|
|
|
0x49e428a2, 0x0a142828, 0x40810284, 0x0048a048, 0x10020fbe, 0x09f7ebaf, 0xd89f3e84, 0x0047a04f, 0x09e48822, 0x0a142aa1, 0x50810284, 0x0048a048,
|
|
|
|
0x04082822, 0x0a142fa0, 0x50810285, 0x0050a248, 0x00008fbe, 0xfbf42021, 0x5f817e7d, 0x07d09ce8, 0x00008000, 0x00000fe0, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000c0180,
|
|
|
|
0xdfbf4282, 0x0bfbf7ef, 0x42850505, 0x004804bf, 0x50a142c6, 0x08401428, 0x42852505, 0x00a808a0, 0x50a146aa, 0x08401428, 0x42852505, 0x00081090,
|
|
|
|
0x5fa14a92, 0x0843f7e8, 0x7e792505, 0x00082088, 0x40a15282, 0x08420128, 0x40852489, 0x00084084, 0x40a16282, 0x0842022a, 0x40852451, 0x00088082,
|
|
|
|
0xc0bf4282, 0xf843f42f, 0x7e85fc21, 0x3e0900bf, 0x00000000, 0x00000004, 0x00000000, 0x000c0180, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x04000402, 0x41482000, 0x00000000, 0x00000800,
|
|
|
|
0x04000404, 0x4100203c, 0x00000000, 0x00000800, 0xf7df7df0, 0x514bef85, 0xbefbefbe, 0x04513bef, 0x14414500, 0x494a2885, 0xa28a28aa, 0x04510820,
|
|
|
|
0xf44145f0, 0x474a289d, 0xa28a28aa, 0x04510be0, 0x14414510, 0x494a2884, 0xa28a28aa, 0x02910a00, 0xf7df7df0, 0xd14a2f85, 0xbefbe8aa, 0x011f7be0,
|
|
|
|
0x00000000, 0x00400804, 0x20080000, 0x00000000, 0x00000000, 0x00600f84, 0x20080000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0xac000000, 0x00000f01, 0x00000000, 0x00000000, 0x24000000, 0x00000901, 0x00000000, 0x00000000, 0x24000000, 0x00000901, 0x00000000, 0x00000000,
|
|
|
|
0x24fa28a2, 0x00000901, 0x00000000, 0x00000000, 0x2242252a, 0x00000952, 0x00000000, 0x00000000, 0x2422222a, 0x00000929, 0x00000000, 0x00000000,
|
|
|
|
0x2412252a, 0x00000901, 0x00000000, 0x00000000, 0x24fbe8be, 0x00000901, 0x00000000, 0x00000000, 0xac020000, 0x00000f01, 0x00000000, 0x00000000,
|
|
|
|
0x0003e000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000 };
|
|
|
|
|
|
|
|
int charsHeight = 10;
|
|
|
|
int charsDivisor = 1; // Every char is separated from the consecutive by a 1 pixel divisor, horizontally and vertically
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
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,
|
2014-09-03 18:51:28 +04:00
|
|
|
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,
|
2013-11-23 16:30:54 +04:00
|
|
|
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 };
|
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
// Re-construct image from defaultFontData and generate OpenGL texture
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
image.pixels = (Color *)malloc(image.width * image.height * sizeof(Color));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
for (int i = 0; i < image.width * image.height; i++) image.pixels[i] = BLANK; // Initialize array
|
|
|
|
|
|
|
|
int counter = 0; // Font data elements counter
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
// Fill imgData with defaultFontData (convert from bit to pixel!)
|
|
|
|
for (int i = 0; i < image.width * image.height; i += 32)
|
|
|
|
{
|
|
|
|
for (int j = 31; j >= 0; j--)
|
|
|
|
{
|
|
|
|
if (BIT_CHECK(defaultFontData[counter], j)) image.pixels[i+j] = WHITE;
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
counter++;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
if (counter > 256) counter = 0; // Security check...
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-15 03:08:30 +03:00
|
|
|
defaultFont.texture = LoadTextureFromImage(image, false); // Convert loaded image to OpenGL texture
|
2014-03-25 15:40:35 +04:00
|
|
|
UnloadImage(image);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
// Reconstruct charSet using charsWidth[], charsHeight, charsDivisor, numChars
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
defaultFont.charSet = (Character *)malloc(defaultFont.numChars * sizeof(Character)); // Allocate space for our character data
|
|
|
|
// This memory should be freed at end! --> Done on CloseWindow()
|
|
|
|
int currentLine = 0;
|
|
|
|
int currentPosX = charsDivisor;
|
|
|
|
int testPosX = charsDivisor;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
for (int i = 0; i < defaultFont.numChars; i++)
|
|
|
|
{
|
2014-03-16 23:59:02 +04:00
|
|
|
defaultFont.charSet[i].value = FONT_FIRST_CHAR + i; // First char is 32
|
2013-11-23 16:30:54 +04:00
|
|
|
defaultFont.charSet[i].x = currentPosX;
|
|
|
|
defaultFont.charSet[i].y = charsDivisor + currentLine * (charsHeight + charsDivisor);
|
|
|
|
defaultFont.charSet[i].w = charsWidth[i];
|
|
|
|
defaultFont.charSet[i].h = charsHeight;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
testPosX += (defaultFont.charSet[i].w + charsDivisor);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
if (testPosX >= defaultFont.texture.width)
|
|
|
|
{
|
|
|
|
currentLine++;
|
|
|
|
currentPosX = 2 * charsDivisor + charsWidth[i];
|
|
|
|
testPosX = currentPosX;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
defaultFont.charSet[i].x = charsDivisor;
|
|
|
|
defaultFont.charSet[i].y = charsDivisor + currentLine * (charsHeight + charsDivisor);
|
|
|
|
}
|
|
|
|
else currentPosX = testPosX;
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-15 03:08:30 +03:00
|
|
|
TraceLog(INFO, "[TEX ID %i] Default font loaded successfully", defaultFont.texture.id);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2014-09-03 19:06:10 +04:00
|
|
|
extern void UnloadDefaultFont(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2014-04-19 18:36:49 +04:00
|
|
|
rlDeleteTextures(defaultFont.texture.id);
|
2013-11-23 16:30:54 +04:00
|
|
|
free(defaultFont.charSet);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-11-29 23:24:11 +04:00
|
|
|
// Get the default font, useful to be used with extended parameters
|
2014-09-17 00:51:31 +04:00
|
|
|
SpriteFont GetDefaultFont()
|
2013-11-29 23:24:11 +04:00
|
|
|
{
|
|
|
|
return defaultFont;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// Load a SpriteFont image into GPU memory
|
2014-09-17 00:51:31 +04:00
|
|
|
SpriteFont LoadSpriteFont(const char *fileName)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
SpriteFont spriteFont;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
// Check file extension
|
|
|
|
if (strcmp(GetExtension(fileName),"rbmf") == 0) spriteFont = LoadRBMF(fileName);
|
2015-01-18 12:58:04 +03:00
|
|
|
else if (strcmp(GetExtension(fileName),"ttf") == 0) spriteFont = LoadTTF(fileName, 20);
|
2013-12-19 15:08:06 +04:00
|
|
|
else
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
Image image = LoadImage(fileName);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
// At this point we have a pixel array with all the data...
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
#if defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
|
|
|
|
ConvertToPOT(&image, MAGENTA);
|
|
|
|
#endif
|
2013-12-19 15:08:06 +04:00
|
|
|
// Process bitmap Font pixel data to get measures (Character array)
|
|
|
|
// spriteFont.charSet data is filled inside the function and memory is allocated!
|
2014-09-17 00:51:31 +04:00
|
|
|
int numChars = ParseImageData(image.pixels, image.width, image.height, &spriteFont.charSet);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
TraceLog(INFO, "[%s] SpriteFont data parsed correctly", fileName);
|
2014-07-23 02:06:24 +04:00
|
|
|
TraceLog(INFO, "[%s] SpriteFont num chars detected: %i", fileName, numChars);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
spriteFont.numChars = numChars;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-15 03:08:30 +03:00
|
|
|
spriteFont.texture = LoadTextureFromImage(image, false); // Convert loaded image to OpenGL texture
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
UnloadImage(image);
|
2013-12-19 15:08:06 +04:00
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
return spriteFont;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unload SpriteFont from GPU memory
|
|
|
|
void UnloadSpriteFont(SpriteFont spriteFont)
|
|
|
|
{
|
2014-04-19 18:36:49 +04:00
|
|
|
rlDeleteTextures(spriteFont.texture.id);
|
2013-11-23 16:30:54 +04:00
|
|
|
free(spriteFont.charSet);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw text (using default font)
|
|
|
|
// NOTE: fontSize work like in any drawing program but if fontSize is lower than font-base-size, then font-base-size is used
|
2013-12-19 15:08:06 +04:00
|
|
|
// NOTE: chars spacing is proportional to fontSize
|
2014-09-17 00:51:31 +04:00
|
|
|
void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
Vector2 position = { (float)posX, (float)posY };
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
int defaultFontSize = 10; // Default Font chars height in pixel
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
if (fontSize < defaultFontSize) fontSize = defaultFontSize;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
int spacing = fontSize / defaultFontSize;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
DrawTextEx(defaultFont, text, position, fontSize, spacing, color);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw text using SpriteFont
|
|
|
|
// NOTE: If font size is lower than base size, base size is used
|
2013-12-19 15:08:06 +04:00
|
|
|
// NOTE: chars spacing is NOT proportional to fontSize
|
2014-09-17 00:51:31 +04:00
|
|
|
void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int fontSize, int spacing, Color tint)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
int length = strlen(text);
|
|
|
|
int positionX = (int)position.x;
|
|
|
|
float scaleFactor;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
Character c;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
if (fontSize <= spriteFont.charSet[0].h) scaleFactor = 1.0f;
|
|
|
|
else scaleFactor = (float)fontSize / spriteFont.charSet[0].h;
|
|
|
|
|
2014-04-19 18:36:49 +04:00
|
|
|
rlEnableTexture(spriteFont.texture.id);
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
rlBegin(RL_QUADS);
|
2014-03-16 23:59:02 +04:00
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
c = spriteFont.charSet[(int)text[i] - FONT_FIRST_CHAR];
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
|
|
|
|
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer
|
2014-09-03 18:51:28 +04:00
|
|
|
|
|
|
|
rlTexCoord2f((float)c.x / spriteFont.texture.width, (float)c.y / spriteFont.texture.height);
|
2014-03-25 15:40:35 +04:00
|
|
|
rlVertex2f(positionX, position.y);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
|
|
|
rlTexCoord2f((float)c.x / spriteFont.texture.width, (float)(c.y + c.h) / spriteFont.texture.height);
|
2014-03-25 15:40:35 +04:00
|
|
|
rlVertex2f(positionX, position.y + (c.h) * scaleFactor);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
|
|
|
rlTexCoord2f((float)(c.x + c.w) / spriteFont.texture.width, (float)(c.y + c.h) / spriteFont.texture.height);
|
2014-03-25 15:40:35 +04:00
|
|
|
rlVertex2f(positionX + (c.w) * scaleFactor, position.y + (c.h) * scaleFactor);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
|
|
|
rlTexCoord2f((float)(c.x + c.w) / spriteFont.texture.width, (float)c.y / spriteFont.texture.height);
|
2014-03-25 15:40:35 +04:00
|
|
|
rlVertex2f(positionX + (c.w) * scaleFactor, position.y);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-16 23:59:02 +04:00
|
|
|
positionX += ((spriteFont.charSet[(int)text[i] - FONT_FIRST_CHAR].w) * scaleFactor + spacing);
|
|
|
|
}
|
2014-03-25 15:40:35 +04:00
|
|
|
rlEnd();
|
|
|
|
|
|
|
|
rlDisableTexture();
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
// Formatting of text with variables to 'embed'
|
|
|
|
const char *FormatText(const char *text, ...)
|
|
|
|
{
|
2014-03-16 23:59:02 +04:00
|
|
|
static char buffer[MAX_FORMATTEXT_LENGTH];
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
va_list args;
|
|
|
|
va_start(args, text);
|
2014-04-09 22:25:26 +04:00
|
|
|
vsprintf(buffer, text, args);
|
2013-12-19 15:08:06 +04:00
|
|
|
va_end(args);
|
2014-03-16 23:59:02 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// Measure string width for default font
|
2013-11-28 22:59:56 +04:00
|
|
|
int MeasureText(const char *text, int fontSize)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
Vector2 vec;
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2014-10-17 23:11:58 +04:00
|
|
|
int defaultFontSize = 10; // Default Font chars height in pixel
|
|
|
|
if (fontSize < defaultFontSize) fontSize = defaultFontSize;
|
|
|
|
int spacing = fontSize / defaultFontSize;
|
|
|
|
|
|
|
|
vec = MeasureTextEx(defaultFont, text, fontSize, spacing);
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
return (int)vec.x;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Measure string size for SpriteFont
|
|
|
|
Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int spacing)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
int len = strlen(text);
|
|
|
|
int textWidth = 0;
|
|
|
|
float scaleFactor;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
{
|
2014-03-16 23:59:02 +04:00
|
|
|
textWidth += spriteFont.charSet[(int)text[i] - FONT_FIRST_CHAR].w;
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
if (fontSize <= spriteFont.charSet[0].h) scaleFactor = 1.0f;
|
|
|
|
else scaleFactor = (float)fontSize / spriteFont.charSet[0].h;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
Vector2 vec;
|
2013-12-27 03:15:02 +04:00
|
|
|
vec.x = (float)textWidth * scaleFactor + (len - 1) * spacing; // Adds chars spacing to measure
|
2013-11-23 16:30:54 +04:00
|
|
|
vec.y = (float)spriteFont.charSet[0].h * scaleFactor;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
return vec;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the base size for a SpriteFont (chars height)
|
|
|
|
int GetFontBaseSize(SpriteFont spriteFont)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
return spriteFont.charSet[0].h;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shows current FPS on top-left corner
|
|
|
|
// NOTE: Uses default font
|
2013-11-28 22:59:56 +04:00
|
|
|
void DrawFPS(int posX, int posY)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
char buffer[20];
|
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
// NOTE: We are rendering fps every second for better viewing on high framerates
|
2014-09-17 00:51:31 +04:00
|
|
|
// TODO: Not working properly on ANDROID and RPI
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
static float fps = 0.0f;
|
|
|
|
static int counter = 0;
|
|
|
|
static int refreshRate = 20;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
if (counter < refreshRate)
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fps = GetFPS();
|
|
|
|
refreshRate = fps;
|
|
|
|
counter = 0;
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
sprintf(buffer, "%2.0f FPS", fps);
|
|
|
|
DrawText(buffer, posX, posY, 20, LIME);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Check if a pixel is magenta
|
|
|
|
static bool PixelIsMagenta(Color p)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
return ((p.r == 255) && (p.g == 0) && (p.b == 255) && (p.a == 255));
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse image pixel data to obtain character set measures
|
|
|
|
static int ParseImageData(Color *imgDataPixel, int imgWidth, int imgHeight, Character **charSet)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
int charSpacing = 0;
|
|
|
|
int lineSpacing = 0;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
Character tempCharSet[MAX_FONTCHARS]; // We allocate a temporal array for charData, once we get the actual charNumber we copy data to a sized array.
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
for(y = 0; y < imgHeight; y++)
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
for(x = 0; x < imgWidth; x++)
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
if (!PixelIsMagenta(imgDataPixel[y*imgWidth + x])) break;
|
|
|
|
}
|
|
|
|
if (!PixelIsMagenta(imgDataPixel[y*imgWidth + x])) break;
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
charSpacing = x;
|
|
|
|
lineSpacing = y;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
int charHeight = 0;
|
|
|
|
int j = 0;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
while(!PixelIsMagenta(imgDataPixel[(lineSpacing + j)*imgWidth + charSpacing])) j++;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
charHeight = j;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
// Check array values to get characters: value, x, y, w, h
|
|
|
|
int index = 0;
|
|
|
|
int lineToRead = 0;
|
|
|
|
int xPosToRead = charSpacing;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
while((lineSpacing + lineToRead * (charHeight + lineSpacing)) < imgHeight)
|
|
|
|
{
|
|
|
|
while((xPosToRead < imgWidth) &&
|
|
|
|
!PixelIsMagenta((imgDataPixel[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*imgWidth + xPosToRead])))
|
|
|
|
{
|
2014-03-16 23:59:02 +04:00
|
|
|
tempCharSet[index].value = FONT_FIRST_CHAR + index;
|
2013-11-23 16:30:54 +04:00
|
|
|
tempCharSet[index].x = xPosToRead;
|
|
|
|
tempCharSet[index].y = lineSpacing + lineToRead * (charHeight + lineSpacing);
|
|
|
|
tempCharSet[index].h = charHeight;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
int charWidth = 0;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
while(!PixelIsMagenta(imgDataPixel[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*imgWidth + xPosToRead + charWidth])) charWidth++;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
tempCharSet[index].w = charWidth;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
index++;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
xPosToRead += (charWidth + charSpacing);
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
lineToRead++;
|
|
|
|
xPosToRead = charSpacing;
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
// 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!
|
|
|
|
|
|
|
|
for (int i = 0; i < index; i++) (*charSet)[i] = tempCharSet[i];
|
|
|
|
|
|
|
|
return index;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
// Load a rBMF font file (raylib BitMap Font)
|
|
|
|
static SpriteFont LoadRBMF(const char *fileName)
|
|
|
|
{
|
|
|
|
// rBMF Info Header (16 bytes)
|
|
|
|
typedef struct {
|
|
|
|
char id[4]; // rBMF file identifier
|
|
|
|
char version; // rBMF file version
|
|
|
|
// 4 MSB --> main version
|
|
|
|
// 4 LSB --> subversion
|
|
|
|
char firstChar; // First character in the font
|
|
|
|
// NOTE: Depending on charDataType, it could be useless
|
|
|
|
short imgWidth; // Image width - always POT (power-of-two)
|
|
|
|
short imgHeight; // Image height - always POT (power-of-two)
|
|
|
|
short numChars; // Number of characters contained
|
|
|
|
short charHeight; // Characters height - the same for all characters
|
2014-09-03 18:51:28 +04:00
|
|
|
char compType; // Compression type:
|
2013-12-19 15:08:06 +04:00
|
|
|
// 4 MSB --> image data compression
|
|
|
|
// 4 LSB --> chars data compression
|
|
|
|
char charsDataType; // Char data type provided
|
|
|
|
} rbmfInfoHeader;
|
|
|
|
|
|
|
|
SpriteFont spriteFont;
|
|
|
|
Image image;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
rbmfInfoHeader rbmfHeader;
|
2014-12-31 20:03:32 +03:00
|
|
|
unsigned int *rbmfFileData = NULL;
|
|
|
|
unsigned char *rbmfCharWidthData = NULL;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
int charsDivisor = 1; // Every char is separated from the consecutive by a 1 pixel divisor, horizontally and vertically
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
FILE *rbmfFile = fopen(fileName, "rb"); // Define a pointer to bitmap file and open it in read-binary mode
|
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
if (rbmfFile == NULL)
|
|
|
|
{
|
|
|
|
TraceLog(WARNING, "[%s] rBMF font file could not be opened", fileName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fread(&rbmfHeader, sizeof(rbmfInfoHeader), 1, rbmfFile);
|
2014-11-23 23:58:17 +03:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
TraceLog(INFO, "[%s] Loading rBMF file, size: %ix%i, numChars: %i, charHeight: %i", fileName, rbmfHeader.imgWidth, rbmfHeader.imgHeight, rbmfHeader.numChars, rbmfHeader.charHeight);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
spriteFont.numChars = (int)rbmfHeader.numChars;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
image.width = (int)rbmfHeader.imgWidth;
|
|
|
|
image.height = (int)rbmfHeader.imgHeight;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
int numPixelBits = rbmfHeader.imgWidth * rbmfHeader.imgHeight / 32;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
rbmfFileData = (unsigned int *)malloc(numPixelBits * sizeof(unsigned int));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
for(int i = 0; i < numPixelBits; i++) fread(&rbmfFileData[i], sizeof(unsigned int), 1, rbmfFile);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
rbmfCharWidthData = (unsigned char *)malloc(spriteFont.numChars * sizeof(unsigned char));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
for(int i = 0; i < spriteFont.numChars; i++) fread(&rbmfCharWidthData[i], sizeof(unsigned char), 1, rbmfFile);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
// Re-construct image from rbmfFileData
|
|
|
|
//-----------------------------------------
|
|
|
|
image.pixels = (Color *)malloc(image.width * image.height * sizeof(Color));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
for (int i = 0; i < image.width * image.height; i++) image.pixels[i] = BLANK; // Initialize array
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
int counter = 0; // Font data elements counter
|
2013-12-19 15:08:06 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
// Fill image data (convert from bit to pixel!)
|
|
|
|
for (int i = 0; i < image.width * image.height; i += 32)
|
2013-12-19 15:08:06 +04:00
|
|
|
{
|
2014-12-31 20:03:32 +03:00
|
|
|
for (int j = 31; j >= 0; j--)
|
|
|
|
{
|
|
|
|
if (BIT_CHECK(rbmfFileData[counter], j)) image.pixels[i+j] = WHITE;
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
counter++;
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
TraceLog(INFO, "[%s] Image reconstructed correctly, now converting it to texture", fileName);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
spriteFont.texture = LoadTextureFromImage(image, false);
|
|
|
|
UnloadImage(image); // Unload image data
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
//TraceLog(INFO, "[%s] Starting charSet reconstruction", fileName);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
// Reconstruct charSet using rbmfCharWidthData, rbmfHeader.charHeight, charsDivisor, rbmfHeader.numChars
|
|
|
|
spriteFont.charSet = (Character *)malloc(spriteFont.numChars * sizeof(Character)); // Allocate space for our character data
|
2013-12-19 15:08:06 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
int currentLine = 0;
|
|
|
|
int currentPosX = charsDivisor;
|
|
|
|
int testPosX = charsDivisor;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
for (int i = 0; i < spriteFont.numChars; i++)
|
|
|
|
{
|
|
|
|
spriteFont.charSet[i].value = (int)rbmfHeader.firstChar + i;
|
|
|
|
spriteFont.charSet[i].x = currentPosX;
|
|
|
|
spriteFont.charSet[i].y = charsDivisor + currentLine * ((int)rbmfHeader.charHeight + charsDivisor);
|
|
|
|
spriteFont.charSet[i].w = (int)rbmfCharWidthData[i];
|
|
|
|
spriteFont.charSet[i].h = (int)rbmfHeader.charHeight;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
testPosX += (spriteFont.charSet[i].w + charsDivisor);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
if (testPosX > spriteFont.texture.width)
|
|
|
|
{
|
|
|
|
currentLine++;
|
|
|
|
currentPosX = 2 * charsDivisor + (int)rbmfCharWidthData[i];
|
|
|
|
testPosX = currentPosX;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
spriteFont.charSet[i].x = charsDivisor;
|
|
|
|
spriteFont.charSet[i].y = charsDivisor + currentLine * (rbmfHeader.charHeight + charsDivisor);
|
|
|
|
}
|
|
|
|
else currentPosX = testPosX;
|
2013-12-19 15:08:06 +04:00
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
TraceLog(INFO, "[%s] rBMF file loaded correctly as SpriteFont", fileName);
|
|
|
|
}
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
fclose(rbmfFile);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
free(rbmfFileData); // Now we can free loaded data from RAM memory
|
2014-09-03 18:51:28 +04:00
|
|
|
free(rbmfCharWidthData);
|
2013-12-19 15:08:06 +04:00
|
|
|
|
|
|
|
return spriteFont;
|
|
|
|
}
|
|
|
|
|
2014-04-19 18:36:49 +04:00
|
|
|
// Generate a sprite font from TTF data (font size required)
|
2015-01-18 12:58:04 +03:00
|
|
|
static SpriteFont LoadTTF(const char *fileName, int fontSize)
|
2013-12-19 15:08:06 +04:00
|
|
|
{
|
2014-04-19 18:36:49 +04:00
|
|
|
SpriteFont font;
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
Image image;
|
|
|
|
image.width = 512;
|
|
|
|
image.height = 512;
|
|
|
|
image.pixels = (Color *)malloc(image.width*image.height*sizeof(Color));
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
unsigned char *ttfBuffer = (unsigned char *)malloc(1 << 25);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
// TODO: Load TTF and generate bitmap font and chars data -> REVIEW!
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
stbtt_packedchar chardata[128]; // Num characters: 128 (?) -> REVIEW!
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
unsigned char *tempBitmap = (unsigned char *)malloc(image.width*image.height*sizeof(unsigned char)); // One channel bitmap returned!
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
// REFERENCE
|
|
|
|
/*
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap
|
|
|
|
float xoff,yoff,xadvance;
|
|
|
|
float xoff2,yoff2;
|
|
|
|
} stbtt_packedchar;
|
|
|
|
*/
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
stbtt_pack_context pc;
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
FILE *ttfFile = fopen(fileName, "rb");
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
fread(ttfBuffer, 1, 1<<25, ttfFile);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
stbtt_PackBegin(&pc, tempBitmap, image.width, image.height, 0, 1, NULL);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
//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);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
free(ttfBuffer);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
// Now we have image data in tempBitmap and chardata filled...
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
for (int i = 0; i < 512*512; i++)
|
|
|
|
{
|
|
|
|
image.pixels[i].r = tempBitmap[i];
|
|
|
|
image.pixels[i].g = tempBitmap[i];
|
|
|
|
image.pixels[i].b = tempBitmap[i];
|
|
|
|
image.pixels[i].a = 255;
|
|
|
|
}
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
free(tempBitmap);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
// 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);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
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();
|
|
|
|
}
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
print(100,160, 0, "This is a test");
|
|
|
|
*/
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
font.numChars = 95;
|
|
|
|
font.charSet = (Character *)malloc(font.numChars*sizeof(Character));
|
|
|
|
font.texture = LoadTextureFromImage(image, false);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
//stbtt_aligned_quad letter;
|
|
|
|
//int x = 0, y = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < font.numChars; i++)
|
|
|
|
{
|
|
|
|
font.charSet[i].value = i + 32;
|
|
|
|
|
|
|
|
//stbtt_GetPackedQuad(chardata[0], 512, 512, i, &x, &y, &letter, 0);
|
|
|
|
|
|
|
|
font.charSet[i].x = chardata[i + 32].x0;
|
|
|
|
font.charSet[i].y = chardata[i + 32].y0;
|
|
|
|
font.charSet[i].w = chardata[i + 32].x1 - chardata[i + 32].x0;
|
|
|
|
font.charSet[i].h = chardata[i + 32].y1 - chardata[i + 32].y0;
|
|
|
|
}
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
UnloadImage(image);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2014-04-19 18:36:49 +04:00
|
|
|
return font;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|