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
|
|
|
*
|
2015-07-29 22:43:30 +03:00
|
|
|
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
|
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"
|
|
|
|
|
2016-06-02 02:26:44 +03:00
|
|
|
#include <stdlib.h> // Required for: malloc(), free()
|
|
|
|
#include <string.h> // Required for: strlen()
|
|
|
|
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
|
|
|
|
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fscanf(), feof(), rewind(), fgets()
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2016-06-02 02:26:44 +03:00
|
|
|
#include "utils.h" // Required for: GetExtension()
|
2014-04-19 18:36:49 +04:00
|
|
|
|
2015-04-06 15:02:29 +03:00
|
|
|
// Following libs are used on LoadTTF()
|
2015-01-18 12:58:04 +03:00
|
|
|
#define STB_TRUETYPE_IMPLEMENTATION
|
2016-06-06 15:34:43 +03:00
|
|
|
#include "external/stb_truetype.h" // Required for: stbtt_BakeFontBitmap()
|
2015-01-18 12:58:04 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
// Rectangle packing functions (not used at the moment)
|
|
|
|
//#define STB_RECT_PACK_IMPLEMENTATION
|
|
|
|
//#include "stb_rect_pack.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
|
2015-04-06 15:02:29 +03:00
|
|
|
#define MAX_FORMATTEXT_LENGTH 64
|
2015-12-30 15:33:26 +03:00
|
|
|
#define MAX_SUBTEXT_LENGTH 64
|
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
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-05-03 18:54:50 +03:00
|
|
|
static SpriteFont LoadImageFont(Image image, Color key, int firstChar); // Load a Image font file (XNA style)
|
2013-12-19 15:08:06 +04:00
|
|
|
static SpriteFont LoadRBMF(const char *fileName); // Load a rBMF font file (raylib BitMap Font)
|
2016-01-02 12:45:51 +03:00
|
|
|
static SpriteFont LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file)
|
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
|
|
|
{
|
2015-03-09 19:13:42 +03:00
|
|
|
// NOTE: Using UTF8 encoding table for Unicode U+0000..U+00FF Basic Latin + Latin-1 Supplement
|
|
|
|
// http://www.utf8-chartable.de/unicode-utf8-table.pl
|
2015-04-06 15:02:29 +03:00
|
|
|
|
2015-03-09 19:13:42 +03:00
|
|
|
defaultFont.numChars = 224; // Number of chars included in our default font
|
2014-03-25 15:40:35 +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
|
2015-03-09 19:13:42 +03:00
|
|
|
int defaultFontData[512] = {
|
2015-04-06 15:02:29 +03:00
|
|
|
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, 0x06000000, 0x24000000, 0x00000901, 0x00000000, 0x09108000,
|
|
|
|
0x24fa28a2, 0x00000901, 0x00000000, 0x013e0000, 0x2242252a, 0x00000952, 0x00000000, 0x038a8000, 0x2422222a, 0x00000929, 0x00000000, 0x010a8000,
|
|
|
|
0x2412252a, 0x00000901, 0x00000000, 0x010a8000, 0x24fbe8be, 0x00000901, 0x00000000, 0x0ebe8000, 0xac020000, 0x00000f01, 0x00000000, 0x00048000,
|
|
|
|
0x0003e000, 0x00000000, 0x00000000, 0x00008000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000038, 0x8443b80e, 0x00203a03,
|
|
|
|
0x02bea080, 0xf0000020, 0xc452208a, 0x04202b02, 0xf8029122, 0x07f0003b, 0xe44b388e, 0x02203a02, 0x081e8a1c, 0x0411e92a, 0xf4420be0, 0x01248202,
|
|
|
|
0xe8140414, 0x05d104ba, 0xe7c3b880, 0x00893a0a, 0x283c0e1c, 0x04500902, 0xc4400080, 0x00448002, 0xe8208422, 0x04500002, 0x80400000, 0x05200002,
|
|
|
|
0x083e8e00, 0x04100002, 0x804003e0, 0x07000042, 0xf8008400, 0x07f00003, 0x80400000, 0x04000022, 0x00000000, 0x00000000, 0x80400000, 0x04000002,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00800702, 0x1848a0c2, 0x84010000, 0x02920921, 0x01042642, 0x00005121, 0x42023f7f, 0x00291002,
|
|
|
|
0xefc01422, 0x7efdfbf7, 0xefdfa109, 0x03bbbbf7, 0x28440f12, 0x42850a14, 0x20408109, 0x01111010, 0x28440408, 0x42850a14, 0x2040817f, 0x01111010,
|
|
|
|
0xefc78204, 0x7efdfbf7, 0xe7cf8109, 0x011111f3, 0x2850a932, 0x42850a14, 0x2040a109, 0x01111010, 0x2850b840, 0x42850a14, 0xefdfbf79, 0x03bbbbf7,
|
|
|
|
0x001fa020, 0x00000000, 0x00001000, 0x00000000, 0x00002070, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x08022800, 0x00012283, 0x02430802, 0x01010001, 0x8404147c, 0x20000144, 0x80048404, 0x00823f08, 0xdfbf4284, 0x7e03f7ef, 0x142850a1, 0x0000210a,
|
|
|
|
0x50a14684, 0x528a1428, 0x142850a1, 0x03efa17a, 0x50a14a9e, 0x52521428, 0x142850a1, 0x02081f4a, 0x50a15284, 0x4a221428, 0xf42850a1, 0x03efa14b,
|
|
|
|
0x50a16284, 0x4a521428, 0x042850a1, 0x0228a17a, 0xdfbf427c, 0x7e8bf7ef, 0xf7efdfbf, 0x03efbd0b, 0x00000000, 0x04000000, 0x00000000, 0x00000008,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00200508, 0x00840400, 0x11458122, 0x00014210,
|
|
|
|
0x00514294, 0x51420800, 0x20a22a94, 0x0050a508, 0x00200000, 0x00000000, 0x00050000, 0x08000000, 0xfefbefbe, 0xfbefbefb, 0xfbeb9114, 0x00fbefbe,
|
|
|
|
0x20820820, 0x8a28a20a, 0x8a289114, 0x3e8a28a2, 0xfefbefbe, 0xfbefbe0b, 0x8a289114, 0x008a28a2, 0x228a28a2, 0x08208208, 0x8a289114, 0x088a28a2,
|
|
|
|
0xfefbefbe, 0xfbefbefb, 0xfa2f9114, 0x00fbefbe, 0x00000000, 0x00000040, 0x00000000, 0x00000000, 0x00000000, 0x00000020, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00210100, 0x00000004, 0x00000000, 0x00000000, 0x14508200, 0x00001402, 0x00000000, 0x00000000,
|
|
|
|
0x00000010, 0x00000020, 0x00000000, 0x00000000, 0xa28a28be, 0x00002228, 0x00000000, 0x00000000, 0xa28a28aa, 0x000022e8, 0x00000000, 0x00000000,
|
|
|
|
0xa28a28aa, 0x000022a8, 0x00000000, 0x00000000, 0xa28a28aa, 0x000022e8, 0x00000000, 0x00000000, 0xbefbefbe, 0x00003e2f, 0x00000000, 0x00000000,
|
|
|
|
0x00000004, 0x00002028, 0x00000000, 0x00000000, 0x80000000, 0x00003e0f, 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, 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 };
|
2013-11-23 16:30:54 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-03-09 19:13:42 +03:00
|
|
|
int charsWidth[224] = { 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,
|
2015-04-06 15:02:29 +03: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,
|
|
|
|
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,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 5, 5, 5, 7, 1, 5, 3, 7, 3, 5, 4, 1, 7, 4, 3, 5, 3, 3, 2, 5, 6, 1, 2, 2, 3, 5, 6, 6, 6, 6,
|
|
|
|
6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 6, 6, 3, 3, 3, 3, 7, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 4, 6,
|
|
|
|
5, 5, 5, 5, 5, 5, 9, 5, 5, 5, 5, 5, 2, 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 5 };
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
// Re-construct image from defaultFontData and generate OpenGL texture
|
|
|
|
//----------------------------------------------------------------------
|
2015-05-11 01:15:46 +03:00
|
|
|
int imWidth = 128;
|
|
|
|
int imHeight = 128;
|
|
|
|
|
|
|
|
Color *imagePixels = (Color *)malloc(imWidth*imHeight*sizeof(Color));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-05-11 01:15:46 +03:00
|
|
|
for (int i = 0; i < imWidth*imHeight; i++) imagePixels[i] = BLANK; // Initialize array
|
2014-03-25 15:40:35 +04:00
|
|
|
|
|
|
|
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!)
|
2015-05-11 01:15:46 +03:00
|
|
|
for (int i = 0; i < imWidth*imHeight; i += 32)
|
2014-03-25 15:40:35 +04:00
|
|
|
{
|
|
|
|
for (int j = 31; j >= 0; j--)
|
|
|
|
{
|
2015-05-05 00:46:31 +03:00
|
|
|
if (BIT_CHECK(defaultFontData[counter], j)) imagePixels[i+j] = WHITE;
|
2014-03-25 15:40:35 +04:00
|
|
|
}
|
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
|
|
|
|
2015-03-09 19:13:42 +03:00
|
|
|
if (counter > 512) counter = 0; // Security check...
|
2014-03-25 15:40:35 +04:00
|
|
|
}
|
2015-04-06 15:02:29 +03:00
|
|
|
|
2015-03-09 19:13:42 +03:00
|
|
|
//FILE *myimage = fopen("default_font.raw", "wb");
|
|
|
|
//fwrite(image.pixels, 1, 128*128*4, myimage);
|
|
|
|
//fclose(myimage);
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2015-07-05 19:21:01 +03:00
|
|
|
Image image = LoadImageEx(imagePixels, imWidth, imHeight);
|
2015-10-06 18:17:53 +03:00
|
|
|
ImageFormat(&image, UNCOMPRESSED_GRAY_ALPHA);
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
free(imagePixels);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-07-05 19:21:01 +03:00
|
|
|
defaultFont.texture = LoadTextureFromImage(image);
|
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
|
|
|
|
//------------------------------------------------------------------------------
|
2015-08-30 18:45:05 +03:00
|
|
|
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()
|
2016-01-02 12:45:51 +03:00
|
|
|
|
|
|
|
defaultFont.charOffsets = (Vector2 *)malloc(defaultFont.numChars*sizeof(Vector2));
|
|
|
|
defaultFont.charAdvanceX = (int *)malloc(defaultFont.numChars*sizeof(int));
|
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
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++)
|
|
|
|
{
|
2015-08-30 18:45:05 +03:00
|
|
|
defaultFont.charValues[i] = FONT_FIRST_CHAR + i; // First char is 32
|
|
|
|
|
|
|
|
defaultFont.charRecs[i].x = currentPosX;
|
|
|
|
defaultFont.charRecs[i].y = charsDivisor + currentLine * (charsHeight + charsDivisor);
|
|
|
|
defaultFont.charRecs[i].width = charsWidth[i];
|
|
|
|
defaultFont.charRecs[i].height = charsHeight;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
testPosX += (defaultFont.charRecs[i].width + charsDivisor);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
if (testPosX >= defaultFont.texture.width)
|
|
|
|
{
|
|
|
|
currentLine++;
|
2016-05-03 18:54:50 +03:00
|
|
|
currentPosX = 2*charsDivisor + charsWidth[i];
|
2013-11-23 16:30:54 +04:00
|
|
|
testPosX = currentPosX;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
defaultFont.charRecs[i].x = charsDivisor;
|
|
|
|
defaultFont.charRecs[i].y = charsDivisor + currentLine*(charsHeight + charsDivisor);
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
|
|
|
else currentPosX = testPosX;
|
2016-01-02 12:45:51 +03:00
|
|
|
|
|
|
|
// NOTE: On default font character offsets and xAdvance are not required
|
|
|
|
defaultFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f };
|
|
|
|
defaultFont.charAdvanceX[i] = 0;
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2015-08-30 18:45:05 +03:00
|
|
|
|
2015-10-06 18:17:53 +03:00
|
|
|
defaultFont.size = defaultFont.charRecs[0].height;
|
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
|
|
|
{
|
2015-04-06 15:02:29 +03:00
|
|
|
UnloadTexture(defaultFont.texture);
|
2015-08-30 18:45:05 +03:00
|
|
|
free(defaultFont.charValues);
|
|
|
|
free(defaultFont.charRecs);
|
2016-01-02 12:45:51 +03:00
|
|
|
free(defaultFont.charOffsets);
|
|
|
|
free(defaultFont.charAdvanceX);
|
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
|
|
|
{
|
2016-05-03 18:54:50 +03:00
|
|
|
SpriteFont spriteFont = { 0 };
|
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);
|
2016-01-02 12:45:51 +03:00
|
|
|
else if (strcmp(GetExtension(fileName),"ttf") == 0) spriteFont = LoadTTF(fileName, 32);
|
|
|
|
else if (strcmp(GetExtension(fileName),"fnt") == 0) spriteFont = LoadBMFont(fileName);
|
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);
|
2016-05-03 18:54:50 +03:00
|
|
|
if (image.data != NULL) spriteFont = LoadImageFont(image, MAGENTA, 32);
|
2014-03-25 15:40:35 +04:00
|
|
|
UnloadImage(image);
|
2013-12-19 15:08:06 +04:00
|
|
|
}
|
2016-01-02 12:45:51 +03:00
|
|
|
|
|
|
|
if (spriteFont.texture.id == 0)
|
|
|
|
{
|
|
|
|
TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName);
|
|
|
|
spriteFont = GetDefaultFont();
|
|
|
|
}
|
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)
|
|
|
|
{
|
2016-01-02 12:45:51 +03:00
|
|
|
// 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);
|
2016-01-18 14:04:54 +03:00
|
|
|
|
2016-05-03 18:54:50 +03:00
|
|
|
TraceLog(DEBUG, "Unloaded sprite font data");
|
2016-01-02 12:45:51 +03:00
|
|
|
}
|
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
|
|
|
|
if (fontSize < defaultFontSize) fontSize = defaultFontSize;
|
|
|
|
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
|
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);
|
2016-01-02 12:45:51 +03:00
|
|
|
int textOffsetX = 0;
|
|
|
|
int textOffsetY = 0; // Line break!
|
2013-11-23 16:30:54 +04:00
|
|
|
float scaleFactor;
|
2015-03-09 19:13:42 +03:00
|
|
|
unsigned char letter;
|
2015-04-06 15:02:29 +03:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
Rectangle rec;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
scaleFactor = (float)fontSize/spriteFont.size;
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2015-10-06 18:17:53 +03:00
|
|
|
// NOTE: Some ugly hacks are made to support Latin-1 Extended characters directly
|
|
|
|
// written in C code files (codified by default as UTF-8)
|
|
|
|
|
2015-04-06 15:02:29 +03:00
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
{
|
2016-02-12 14:22:56 +03:00
|
|
|
// TODO: Right now we are supposing characters that follow a continous order and start at FONT_FIRST_CHAR,
|
2015-10-06 18:17:53 +03:00
|
|
|
// 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!
|
2014-03-16 23:59:02 +04:00
|
|
|
{
|
2015-10-06 18:32:00 +03:00
|
|
|
// Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿)
|
2015-04-06 15:02:29 +03:00
|
|
|
letter = (unsigned char)text[i + 1];
|
2015-08-30 18:45:05 +03:00
|
|
|
rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR];
|
2015-04-06 15:02:29 +03:00
|
|
|
i++;
|
2014-03-16 23:59:02 +04:00
|
|
|
}
|
2015-10-06 18:17:53 +03:00
|
|
|
else if ((unsigned char)text[i] == 0xc3) // UTF-8 encoding identification HACK!
|
2015-04-06 15:02:29 +03:00
|
|
|
{
|
2015-10-06 18:32:00 +03:00
|
|
|
// Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ)
|
2015-04-06 15:02:29 +03:00
|
|
|
letter = (unsigned char)text[i + 1];
|
2015-08-30 18:45:05 +03:00
|
|
|
rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR + 64];
|
2015-04-06 15:02:29 +03:00
|
|
|
i++;
|
|
|
|
}
|
2015-10-21 19:24:44 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((unsigned char)text[i] == '\n')
|
|
|
|
{
|
2016-01-02 12:45:51 +03:00
|
|
|
// NOTE: Fixed line spacing of 1.5 lines
|
|
|
|
textOffsetY += ((spriteFont.size + spriteFont.size/2)*scaleFactor);
|
|
|
|
textOffsetX = 0;
|
2015-10-21 19:24:44 +03:00
|
|
|
rec.x = -1;
|
|
|
|
}
|
|
|
|
else rec = spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR];
|
|
|
|
}
|
2014-03-25 15:40:35 +04:00
|
|
|
|
2015-10-21 19:24:44 +03:00
|
|
|
if (rec.x > 0)
|
|
|
|
{
|
2016-01-02 12:45:51 +03:00
|
|
|
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);
|
2015-04-06 15:02:29 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
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);
|
2015-10-21 19:24:44 +03:00
|
|
|
}
|
2015-04-06 15:02:29 +03:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:33:26 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
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);
|
2015-12-09 22:57:50 +03:00
|
|
|
int tempLen = 0; // Used to count longer text line num chars
|
|
|
|
int lenCounter = 0;
|
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
int textWidth = 0;
|
2015-12-09 22:57:50 +03:00
|
|
|
int tempTextWidth = 0; // Used to count longer text line width
|
|
|
|
|
|
|
|
int textHeight = spriteFont.size;
|
2013-11-23 16:30:54 +04:00
|
|
|
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++)
|
|
|
|
{
|
2015-12-09 22:57:50 +03:00
|
|
|
lenCounter++;
|
|
|
|
|
|
|
|
if (text[i] != '\n')
|
|
|
|
{
|
2016-01-02 12:45:51 +03:00
|
|
|
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);
|
2015-12-09 22:57:50 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tempTextWidth < textWidth) tempTextWidth = textWidth;
|
|
|
|
lenCounter = 0;
|
|
|
|
textWidth = 0;
|
2016-01-02 12:45:51 +03:00
|
|
|
textHeight += (spriteFont.size + spriteFont.size/2); // NOTE: Fixed line spacing of 1.5 lines
|
2015-12-09 22:57:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tempLen < lenCounter) tempLen = lenCounter;
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2015-12-09 22:57:50 +03:00
|
|
|
|
|
|
|
if (tempTextWidth < textWidth) tempTextWidth = textWidth;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-12-09 22:57:50 +03:00
|
|
|
if (fontSize <= spriteFont.size) scaleFactor = 1.0f;
|
|
|
|
else scaleFactor = (float)fontSize/spriteFont.size;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
Vector2 vec;
|
2015-12-09 22:57:50 +03:00
|
|
|
vec.x = (float)tempTextWidth*scaleFactor + (tempLen - 1)*spacing; // Adds chars spacing to measure
|
|
|
|
vec.y = (float)textHeight*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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
2016-05-03 18:54:50 +03:00
|
|
|
// Load a Image font file (XNA style)
|
|
|
|
static SpriteFont LoadImageFont(Image image, Color key, int firstChar)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-05-03 18:54:50 +03:00
|
|
|
#define COLOR_EQUAL(col1, col2) ((col1.r == col2.r)&&(col1.g == col2.g)&&(col1.b == col2.b)&&(col1.a == col2.a))
|
|
|
|
|
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
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
// We allocate a temporal arrays for chars data measures,
|
|
|
|
// once we get the actual number of chars, we copy data to a sized arrays
|
|
|
|
int tempCharValues[MAX_FONTCHARS];
|
|
|
|
Rectangle tempCharRecs[MAX_FONTCHARS];
|
|
|
|
|
|
|
|
Color *pixels = GetImageData(image);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-05-03 18:54:50 +03:00
|
|
|
// Parse image data to get charSpacing and lineSpacing
|
2015-08-30 18:45:05 +03:00
|
|
|
for(y = 0; y < image.height; y++)
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2015-08-30 18:45:05 +03:00
|
|
|
for(x = 0; x < image.width; x++)
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2016-05-03 18:54:50 +03:00
|
|
|
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2016-05-03 18:54:50 +03:00
|
|
|
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
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
|
|
|
|
2016-05-03 18:54:50 +03:00
|
|
|
while(!COLOR_EQUAL(pixels[(lineSpacing + j)*image.width + charSpacing], key)) 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
|
|
|
|
2016-05-03 18:54:50 +03:00
|
|
|
// Parse image data to get rectangle sizes
|
2015-08-30 18:45:05 +03:00
|
|
|
while((lineSpacing + lineToRead * (charHeight + lineSpacing)) < image.height)
|
2013-11-23 16:30:54 +04:00
|
|
|
{
|
2015-08-30 18:45:05 +03:00
|
|
|
while((xPosToRead < image.width) &&
|
2016-05-03 18:54:50 +03:00
|
|
|
!COLOR_EQUAL((pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead]), key))
|
2013-11-23 16:30:54 +04:00
|
|
|
{
|
2016-05-03 18:54:50 +03:00
|
|
|
tempCharValues[index] = firstChar + index;
|
2015-08-30 18:45:05 +03:00
|
|
|
|
|
|
|
tempCharRecs[index].x = xPosToRead;
|
|
|
|
tempCharRecs[index].y = lineSpacing + lineToRead * (charHeight + lineSpacing);
|
|
|
|
tempCharRecs[index].height = 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
|
|
|
|
2016-05-03 18:54:50 +03:00
|
|
|
while(!COLOR_EQUAL(pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead + charWidth], key)) charWidth++;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
tempCharRecs[index].width = 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;
|
|
|
|
}
|
2015-08-30 18:45:05 +03:00
|
|
|
|
|
|
|
free(pixels);
|
2016-05-03 18:54:50 +03:00
|
|
|
|
|
|
|
TraceLog(DEBUG, "SpriteFont data parsed correctly from image");
|
|
|
|
|
|
|
|
// Create spritefont with all data parsed from image
|
|
|
|
SpriteFont spriteFont = { 0 };
|
|
|
|
|
|
|
|
spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture
|
|
|
|
spriteFont.numChars = index;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
// We got tempCharValues and tempCharsRecs populated with chars data
|
2016-05-03 18:54:50 +03:00
|
|
|
// Now we move temp data to sized charValues and charRecs arrays
|
|
|
|
spriteFont.charRecs = (Rectangle *)malloc(spriteFont.numChars*sizeof(Rectangle));
|
|
|
|
spriteFont.charValues = (int *)malloc(spriteFont.numChars*sizeof(int));
|
|
|
|
spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2));
|
|
|
|
spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int));
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2016-05-03 18:54:50 +03:00
|
|
|
for (int i = 0; i < spriteFont.numChars; i++)
|
2015-08-30 18:45:05 +03:00
|
|
|
{
|
2016-05-03 18:54:50 +03:00
|
|
|
spriteFont.charValues[i] = tempCharValues[i];
|
|
|
|
spriteFont.charRecs[i] = tempCharRecs[i];
|
|
|
|
|
|
|
|
// 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;
|
2015-08-30 18:45:05 +03:00
|
|
|
}
|
2016-05-03 18:54:50 +03:00
|
|
|
|
|
|
|
spriteFont.size = spriteFont.charRecs[0].height;
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2016-05-03 18:54:50 +03:00
|
|
|
return spriteFont;
|
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;
|
|
|
|
|
2016-01-18 14:04:54 +03:00
|
|
|
SpriteFont spriteFont = { 0 };
|
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)
|
|
|
|
{
|
2015-12-03 15:44:45 +03:00
|
|
|
TraceLog(WARNING, "[%s] rBMF font file could not be opened, using default font", fileName);
|
|
|
|
|
|
|
|
spriteFont = GetDefaultFont();
|
2014-12-31 20:03:32 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fread(&rbmfHeader, sizeof(rbmfInfoHeader), 1, rbmfFile);
|
2014-11-23 23:58:17 +03:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
TraceLog(DEBUG, "[%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
|
|
|
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
|
|
|
|
//-----------------------------------------
|
2015-05-11 01:15:46 +03:00
|
|
|
Color *imagePixels = (Color *)malloc(rbmfHeader.imgWidth*rbmfHeader.imgHeight*sizeof(Color));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-05-11 01:15:46 +03:00
|
|
|
for (int i = 0; i < rbmfHeader.imgWidth*rbmfHeader.imgHeight; i++) imagePixels[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!)
|
2015-05-11 01:15:46 +03:00
|
|
|
for (int i = 0; i < rbmfHeader.imgWidth*rbmfHeader.imgHeight; 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--)
|
|
|
|
{
|
2015-05-05 00:46:31 +03:00
|
|
|
if (BIT_CHECK(rbmfFileData[counter], j)) imagePixels[i+j] = WHITE;
|
2014-12-31 20:03:32 +03:00
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
counter++;
|
|
|
|
}
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2015-07-05 19:21:01 +03:00
|
|
|
Image image = LoadImageEx(imagePixels, rbmfHeader.imgWidth, rbmfHeader.imgHeight);
|
2015-10-06 18:17:53 +03:00
|
|
|
ImageFormat(&image, UNCOMPRESSED_GRAY_ALPHA);
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
free(imagePixels);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
TraceLog(DEBUG, "[%s] Image reconstructed correctly, now converting it to texture", fileName);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-05-03 18:54:50 +03:00
|
|
|
// Create spritefont with all data read from rbmf file
|
2015-07-05 19:21:01 +03:00
|
|
|
spriteFont.texture = LoadTextureFromImage(image);
|
2014-12-31 20:03:32 +03:00
|
|
|
UnloadImage(image); // Unload image data
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
//TraceLog(INFO, "[%s] Starting chars set reconstruction", fileName);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
// 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));
|
2016-01-18 14:04:54 +03:00
|
|
|
spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2));
|
|
|
|
spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int));
|
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++)
|
|
|
|
{
|
2015-08-30 18:45:05 +03:00
|
|
|
spriteFont.charValues[i] = (int)rbmfHeader.firstChar + i;
|
|
|
|
|
|
|
|
spriteFont.charRecs[i].x = currentPosX;
|
|
|
|
spriteFont.charRecs[i].y = charsDivisor + currentLine * ((int)rbmfHeader.charHeight + charsDivisor);
|
|
|
|
spriteFont.charRecs[i].width = (int)rbmfCharWidthData[i];
|
|
|
|
spriteFont.charRecs[i].height = (int)rbmfHeader.charHeight;
|
2016-01-18 14:04:54 +03:00
|
|
|
|
|
|
|
// 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;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
testPosX += (spriteFont.charRecs[i].width + 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
|
|
|
|
2015-08-30 18:45:05 +03:00
|
|
|
spriteFont.charRecs[i].x = charsDivisor;
|
|
|
|
spriteFont.charRecs[i].y = charsDivisor + currentLine * (rbmfHeader.charHeight + charsDivisor);
|
2014-12-31 20:03:32 +03:00
|
|
|
}
|
|
|
|
else currentPosX = testPosX;
|
2013-12-19 15:08:06 +04:00
|
|
|
}
|
2015-08-30 18:45:05 +03:00
|
|
|
|
|
|
|
spriteFont.size = spriteFont.charRecs[0].height;
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
// Load a BMFont file (AngelCode font file)
|
|
|
|
static SpriteFont LoadBMFont(const char *fileName)
|
2013-12-19 15:08:06 +04:00
|
|
|
{
|
2016-01-02 12:45:51 +03:00
|
|
|
#define MAX_BUFFER_SIZE 256
|
2015-08-28 15:14:29 +03:00
|
|
|
|
2015-11-05 14:28:45 +03:00
|
|
|
SpriteFont font = { 0 };
|
2016-01-02 12:45:51 +03:00
|
|
|
font.texture.id = 0;
|
|
|
|
|
|
|
|
char buffer[MAX_BUFFER_SIZE];
|
|
|
|
char *searchPoint = NULL;
|
|
|
|
|
|
|
|
int fontSize = 0;
|
|
|
|
int texWidth, texHeight;
|
|
|
|
char texFileName[128];
|
|
|
|
int numChars = 0;
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
int base; // Useless data
|
|
|
|
|
|
|
|
FILE *fntFile;
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
fntFile = fopen(fileName, "rt");
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
if (fntFile == NULL)
|
|
|
|
{
|
|
|
|
TraceLog(WARNING, "[%s] FNT file could not be opened", fileName);
|
|
|
|
return font;
|
|
|
|
}
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
// 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;
|
|
|
|
|
2016-07-06 21:02:15 +03:00
|
|
|
lastSlash = strrchr(fileName, '/');
|
|
|
|
|
|
|
|
// NOTE: We need some extra space to avoid memory corruption on next allocations!
|
|
|
|
texPath = malloc(strlen(fileName) - strlen(lastSlash) + strlen(texFileName) + 4);
|
|
|
|
|
|
|
|
// NOTE: strcat() and strncat() required a '\0' terminated string to work!
|
|
|
|
*texPath = '\0';
|
|
|
|
strncat(texPath, fileName, strlen(fileName) - strlen(lastSlash) + 1);
|
|
|
|
strncat(texPath, texFileName, strlen(texFileName));
|
2016-01-02 12:45:51 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-03-02 20:35:30 +03:00
|
|
|
bool unorderedChars = false;
|
|
|
|
int firstChar = 0;
|
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
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);
|
2016-03-02 20:35:30 +03:00
|
|
|
|
|
|
|
if (i == 0) firstChar = charId;
|
|
|
|
else if (i != (charId - firstChar)) unorderedChars = true;
|
2016-01-02 12:45:51 +03:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fntFile);
|
2016-03-02 20:35:30 +03:00
|
|
|
|
|
|
|
if (firstChar != FONT_FIRST_CHAR) TraceLog(WARNING, "BMFont not supported: expected SPACE(32) as first character, falling back to default font");
|
|
|
|
else if (unorderedChars) TraceLog(WARNING, "BMFont not supported: unordered chars data, falling back to default font");
|
|
|
|
|
|
|
|
// NOTE: Font data could be not ordered by charId: 32,33,34,35... raylib does not support unordered BMFonts
|
2016-07-06 21:02:15 +03:00
|
|
|
if ((firstChar != FONT_FIRST_CHAR) || (unorderedChars) || (font.texture.id == 0))
|
2016-03-02 20:35:30 +03:00
|
|
|
{
|
|
|
|
UnloadSpriteFont(font);
|
|
|
|
font = GetDefaultFont();
|
|
|
|
}
|
|
|
|
else TraceLog(INFO, "[%s] SpriteFont loaded successfully", fileName);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
return font;
|
|
|
|
}
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
// 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
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
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 };
|
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
FILE *ttfFile = fopen(fileName, "rb");
|
2016-01-02 12:45:51 +03:00
|
|
|
|
|
|
|
if (ttfFile == NULL)
|
|
|
|
{
|
|
|
|
TraceLog(WARNING, "[%s] FNT file could not be opened", fileName);
|
|
|
|
return font;
|
|
|
|
}
|
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
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
// 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);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
free(ttfBuffer);
|
2016-01-02 12:45:51 +03:00
|
|
|
|
|
|
|
// 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++)
|
2015-01-18 12:58:04 +03:00
|
|
|
{
|
2016-01-02 12:45:51 +03:00
|
|
|
dataGrayAlpha[k] = 255;
|
|
|
|
dataGrayAlpha[k + 1] = dataBitmap[i];
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
k += 2;
|
2015-01-18 12:58:04 +03:00
|
|
|
}
|
2016-01-02 12:45:51 +03:00
|
|
|
|
|
|
|
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;
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2015-07-05 19:21:01 +03:00
|
|
|
font.texture = LoadTextureFromImage(image);
|
2016-01-02 12:45:51 +03:00
|
|
|
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));
|
|
|
|
|
2015-01-18 12:58:04 +03:00
|
|
|
for (int i = 0; i < font.numChars; i++)
|
|
|
|
{
|
2016-01-02 12:45:51 +03:00
|
|
|
font.charValues[i] = i + FONT_FIRST_CHAR;
|
2015-01-18 12:58:04 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
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;
|
2015-01-18 12:58:04 +03:00
|
|
|
}
|
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
|
|
|
}
|