BREAKING CHANGE: Renamed SpriteFont type to Font

- Preparing MP3 files support
- Jumped version to raylib 2.0-dev (too many breaking changes...)
This commit is contained in:
Ray San
2018-05-04 16:59:48 +02:00
parent 6045062a05
commit ec33e7d705
34 changed files with 3027 additions and 179 deletions

View File

@@ -24,6 +24,7 @@
* #define SUPPORT_FILEFORMAT_XM
* #define SUPPORT_FILEFORMAT_MOD
* #define SUPPORT_FILEFORMAT_FLAC
* #define SUPPORT_FILEFORMAT_MP3
* Selected desired fileformats to be supported for loading. Some of those formats are
* supported by default, to remove support, just comment unrequired #define in this module
*
@@ -127,6 +128,11 @@
#include "external/dr_flac.h" // FLAC loading functions
#endif
#if defined(SUPPORT_FILEFORMAT_MP3)
#define DR_MP3_IMPLEMENTATION
#include "external/dr_mp3.h" // MP3 loading functions
#endif
#ifdef _MSC_VER
#undef bool
#endif

View File

@@ -25,7 +25,7 @@
*
**********************************************************************************************/
#define RAYLIB_VERSION "1.9.7-dev"
#define RAYLIB_VERSION "2.0-dev"
// Edit to control what features Makefile'd raylib is compiled with
#if defined(RAYLIB_CMAKE)
@@ -124,6 +124,7 @@
#define SUPPORT_FILEFORMAT_XM 1
#define SUPPORT_FILEFORMAT_MOD 1
//#define SUPPORT_FILEFORMAT_FLAC 1
//#define SUPPORT_FILEFORMAT_MP3 1
//------------------------------------------------------------------------------------

2841
src/external/dr_mp3.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -382,7 +382,7 @@ typedef struct RenderTexture2D {
Texture2D depth; // Depth buffer attachment texture
} RenderTexture2D;
// SpriteFont character info
// Font character info
typedef struct CharInfo {
int value; // Character value (Unicode)
Rectangle rec; // Character rectangle in sprite font
@@ -391,13 +391,15 @@ typedef struct CharInfo {
int advanceX; // Character advance position X
} CharInfo;
// SpriteFont type, includes texture and charSet array data
typedef struct SpriteFont {
// Font type, includes texture and charSet array data
typedef struct Font {
Texture2D texture; // Font texture
int baseSize; // Base size (default chars height)
int charsCount; // Number of characters
CharInfo *chars; // Characters info data
} SpriteFont;
} Font;
#define SpriteFont Font // SpriteFont type fallback, defaults to Font
// Camera type, defines a camera position/orientation in 3d space
typedef struct Camera3D {
@@ -915,11 +917,11 @@ RLAPI void ImageResizeNN(Image *image,int newWidth,int newHeight);
RLAPI void ImageMipmaps(Image *image); // Generate all mipmap levels for a provided image
RLAPI void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp); // Dither image data to 16bpp or lower (Floyd-Steinberg dithering)
RLAPI Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font)
RLAPI Image ImageTextEx(SpriteFont font, const char *text, float fontSize, float spacing, Color tint); // Create an image from text (custom sprite font)
RLAPI Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Color tint); // Create an image from text (custom sprite font)
RLAPI void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec); // Draw a source image within a destination image
RLAPI void ImageDrawRectangle(Image *dst, Vector2 position, Rectangle rec, Color color); // Draw rectangle within an image
RLAPI void ImageDrawText(Image *dst, Vector2 position, const char *text, int fontSize, Color color); // Draw text (default font) within an image (destination)
RLAPI void ImageDrawTextEx(Image *dst, Vector2 position, SpriteFont font, const char *text, float fontSize, float spacing, Color color); // Draw text (custom sprite font) within an image (destination)
RLAPI void ImageDrawTextEx(Image *dst, Vector2 position, Font font, const char *text, float fontSize, float spacing, Color color); // Draw text (custom sprite font) within an image (destination)
RLAPI void ImageFlipVertical(Image *image); // Flip image vertically
RLAPI void ImageFlipHorizontal(Image *image); // Flip image horizontally
RLAPI void ImageColorTint(Image *image, Color color); // Modify image color: tint
@@ -955,23 +957,23 @@ RLAPI void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle dest
// Font Loading and Text Drawing Functions (Module: text)
//------------------------------------------------------------------------------------
// SpriteFont loading/unloading functions
RLAPI SpriteFont GetDefaultFont(void); // Get the default SpriteFont
RLAPI SpriteFont LoadSpriteFont(const char *fileName); // Load SpriteFont from file into GPU memory (VRAM)
RLAPI SpriteFont LoadSpriteFontEx(const char *fileName, int fontSize, int charsCount, int *fontChars); // Load SpriteFont from file with extended parameters
RLAPI void UnloadSpriteFont(SpriteFont font); // Unload SpriteFont from GPU memory (VRAM)
// Font loading/unloading functions
RLAPI Font GetDefaultFont(void); // Get the default Font
RLAPI Font LoadFont(const char *fileName); // Load Font from file into GPU memory (VRAM)
RLAPI Font LoadFontEx(const char *fileName, int fontSize, int charsCount, int *fontChars); // Load Font from file with extended parameters
RLAPI void UnloadFont(Font font); // Unload Font from GPU memory (VRAM)
// Text drawing functions
RLAPI void DrawFPS(int posX, int posY); // Shows current FPS
RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font)
RLAPI void DrawTextEx(SpriteFont font, const char* text, Vector2 position, float fontSize, float spacing, Color tint); // Draw text using SpriteFont and additional parameters
RLAPI void DrawTextEx(Font font, const char* text, Vector2 position, float fontSize, float spacing, Color tint); // Draw text using Font and additional parameters
// Text misc. functions
RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font
RLAPI Vector2 MeasureTextEx(SpriteFont font, const char *text, float fontSize, float spacing); // Measure string size for SpriteFont
RLAPI Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing); // Measure string size for Font
RLAPI const char *FormatText(const char *text, ...); // Formatting of text with variables to 'embed'
RLAPI const char *SubText(const char *text, int position, int length); // Get a piece of a text string
RLAPI int GetGlyphIndex(SpriteFont font, int character); // Returns index position for a unicode character on sprite font
RLAPI int GetGlyphIndex(Font font, int character); // Returns index position for a unicode character on sprite font
//------------------------------------------------------------------------------------
// Basic 3d Shapes Drawing Functions (Module: models)

View File

@@ -1,6 +1,6 @@
/**********************************************************************************************
*
* raylib.text - Basic functions to load SpriteFonts and draw Text
* raylib.text - Basic functions to load Fonts and draw Text
*
* CONFIGURATION:
*
@@ -73,7 +73,7 @@
// Global variables
//----------------------------------------------------------------------------------
#if defined(SUPPORT_DEFAULT_FONT)
static SpriteFont defaultFont; // Default font provided by raylib
static Font defaultFont; // Default font provided by raylib
// NOTE: defaultFont is loaded on InitWindow and disposed on CloseWindow [module: core]
#endif
@@ -85,12 +85,12 @@ static SpriteFont defaultFont; // Default font provided by raylib
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
static SpriteFont LoadImageFont(Image image, Color key, int firstChar); // Load a Image font file (XNA style)
static Font LoadImageFont(Image image, Color key, int firstChar); // Load a Image font file (XNA style)
#if defined(SUPPORT_FILEFORMAT_FNT)
static SpriteFont LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file)
static Font LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file)
#endif
#if defined(SUPPORT_FILEFORMAT_TTF)
static SpriteFont LoadTTF(const char *fileName, int fontSize, int charsCount, int *fontChars); // Load spritefont from TTF data
static Font LoadTTF(const char *fileName, int fontSize, int charsCount, int *fontChars); // Load spritefont from TTF data
#endif
#if defined(SUPPORT_DEFAULT_FONT)
@@ -114,7 +114,7 @@ extern void LoadDefaultFont(void)
defaultFont.charsCount = 224; // Number of chars included in our default font
// Default font is directly defined here (data generated from a sprite font image)
// This way, we reconstruct SpriteFont without creating large global variables
// This way, we reconstruct Font without creating large global variables
// This data is automatically allocated to Stack and automatically deallocated at the end of this function
int defaultFontData[512] = {
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00200020, 0x0001b000, 0x00000000, 0x00000000, 0x8ef92520, 0x00020a00, 0x7dbe8000, 0x1f7df45f,
@@ -261,28 +261,28 @@ extern void UnloadDefaultFont(void)
#endif // SUPPORT_DEFAULT_FONT
// Get the default font, useful to be used with extended parameters
SpriteFont GetDefaultFont()
Font GetDefaultFont()
{
#if defined(SUPPORT_DEFAULT_FONT)
return defaultFont;
#else
SpriteFont font = { 0 };
Font font = { 0 };
return font;
#endif
}
// Load SpriteFont from file into GPU memory (VRAM)
SpriteFont LoadSpriteFont(const char *fileName)
// Load Font from file into GPU memory (VRAM)
Font LoadFont(const char *fileName)
{
// Default hardcoded values for ttf file loading
#define DEFAULT_TTF_FONTSIZE 32 // Font first character (32 - space)
#define DEFAULT_TTF_NUMCHARS 95 // ASCII 32..126 is 95 glyphs
#define DEFAULT_FIRST_CHAR 32 // Expected first char for image spritefont
SpriteFont spriteFont = { 0 };
Font spriteFont = { 0 };
#if defined(SUPPORT_FILEFORMAT_TTF)
if (IsFileExtension(fileName, ".ttf")) spriteFont = LoadSpriteFontEx(fileName, DEFAULT_TTF_FONTSIZE, 0, NULL);
if (IsFileExtension(fileName, ".ttf")) spriteFont = LoadFontEx(fileName, DEFAULT_TTF_FONTSIZE, 0, NULL);
else
#endif
#if defined(SUPPORT_FILEFORMAT_FNT)
@@ -297,7 +297,7 @@ SpriteFont LoadSpriteFont(const char *fileName)
if (spriteFont.texture.id == 0)
{
TraceLog(LOG_WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName);
TraceLog(LOG_WARNING, "[%s] Font could not be loaded, using default font", fileName);
spriteFont = GetDefaultFont();
}
else SetTextureFilter(spriteFont.texture, FILTER_POINT); // By default we set point filter (best performance)
@@ -305,12 +305,12 @@ SpriteFont LoadSpriteFont(const char *fileName)
return spriteFont;
}
// Load SpriteFont from TTF font file with generation parameters
// Load Font from TTF font file with generation parameters
// NOTE: You can pass an array with desired characters, those characters should be available in the font
// if array is NULL, default char set is selected 32..126
SpriteFont LoadSpriteFontEx(const char *fileName, int fontSize, int charsCount, int *fontChars)
Font LoadFontEx(const char *fileName, int fontSize, int charsCount, int *fontChars)
{
SpriteFont spriteFont = { 0 };
Font spriteFont = { 0 };
int totalChars = 95; // Default charset [32..126]
#if defined(SUPPORT_FILEFORMAT_TTF)
@@ -330,15 +330,15 @@ SpriteFont LoadSpriteFontEx(const char *fileName, int fontSize, int charsCount,
if (spriteFont.texture.id == 0)
{
TraceLog(LOG_WARNING, "[%s] SpriteFont could not be generated, using default font", fileName);
TraceLog(LOG_WARNING, "[%s] Font could not be generated, using default font", fileName);
spriteFont = GetDefaultFont();
}
return spriteFont;
}
// Unload SpriteFont from GPU memory (VRAM)
void UnloadSpriteFont(SpriteFont font)
// Unload Font from GPU memory (VRAM)
void UnloadFont(Font font)
{
// NOTE: Make sure spriteFont is not default font (fallback)
if (font.texture.id != GetDefaultFont().texture.id)
@@ -368,9 +368,9 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
}
}
// Draw text using SpriteFont
// Draw text using Font
// NOTE: chars spacing is NOT proportional to fontSize
void DrawTextEx(SpriteFont font, const char *text, Vector2 position, float fontSize, float spacing, Color tint)
void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint)
{
int length = strlen(text);
int textOffsetX = 0; // Offset between characters
@@ -482,8 +482,8 @@ int MeasureText(const char *text, int fontSize)
return (int)vec.x;
}
// Measure string size for SpriteFont
Vector2 MeasureTextEx(SpriteFont font, const char *text, float fontSize, float spacing)
// Measure string size for Font
Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing)
{
int len = strlen(text);
int tempLen = 0; // Used to count longer text line num chars
@@ -527,7 +527,7 @@ Vector2 MeasureTextEx(SpriteFont font, const char *text, float fontSize, float s
}
// Returns index position for a unicode character on spritefont
int GetGlyphIndex(SpriteFont font, int character)
int GetGlyphIndex(Font font, int character)
{
#define UNORDERED_CHARSET
#if defined(UNORDERED_CHARSET)
@@ -575,7 +575,7 @@ void DrawFPS(int posX, int posY)
//----------------------------------------------------------------------------------
// Load an Image font file (XNA style)
static SpriteFont LoadImageFont(Image image, Color key, int firstChar)
static Font LoadImageFont(Image image, Color key, int firstChar)
{
#define COLOR_EQUAL(col1, col2) ((col1.r == col2.r)&&(col1.g == col2.g)&&(col1.b == col2.b)&&(col1.a == col2.a))
@@ -648,7 +648,7 @@ static SpriteFont LoadImageFont(Image image, Color key, int firstChar)
xPosToRead = charSpacing;
}
TraceLog(LOG_DEBUG, "SpriteFont data parsed correctly from image");
TraceLog(LOG_DEBUG, "Font data parsed correctly from image");
// NOTE: We need to remove key color borders from image to avoid weird
// artifacts on texture scaling when using FILTER_BILINEAR or FILTER_TRILINEAR
@@ -660,7 +660,7 @@ static SpriteFont LoadImageFont(Image image, Color key, int firstChar)
free(pixels); // Free pixels array memory
// Create spritefont with all data parsed from image
SpriteFont spriteFont = { 0 };
Font spriteFont = { 0 };
spriteFont.texture = LoadTextureFromImage(fontClear); // Convert processed image to OpenGL texture
spriteFont.charsCount = index;
@@ -684,18 +684,18 @@ static SpriteFont LoadImageFont(Image image, Color key, int firstChar)
spriteFont.baseSize = spriteFont.chars[0].rec.height;
TraceLog(LOG_INFO, "Image file loaded correctly as SpriteFont");
TraceLog(LOG_INFO, "Image file loaded correctly as Font");
return spriteFont;
}
#if defined(SUPPORT_FILEFORMAT_FNT)
// Load a BMFont file (AngelCode font file)
static SpriteFont LoadBMFont(const char *fileName)
static Font LoadBMFont(const char *fileName)
{
#define MAX_BUFFER_SIZE 256
SpriteFont font = { 0 };
Font font = { 0 };
font.texture.id = 0;
char buffer[MAX_BUFFER_SIZE];
@@ -800,10 +800,10 @@ static SpriteFont LoadBMFont(const char *fileName)
if (font.texture.id == 0)
{
UnloadSpriteFont(font);
UnloadFont(font);
font = GetDefaultFont();
}
else TraceLog(LOG_INFO, "[%s] SpriteFont loaded successfully", fileName);
else TraceLog(LOG_INFO, "[%s] Font loaded successfully", fileName);
return font;
}
@@ -812,7 +812,7 @@ static SpriteFont LoadBMFont(const char *fileName)
#if defined(SUPPORT_FILEFORMAT_TTF)
// 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, int charsCount, int *fontChars)
static Font LoadTTF(const char *fileName, int fontSize, int charsCount, int *fontChars)
{
#define MAX_TTF_SIZE 16 // Maximum ttf file size in MB
@@ -830,7 +830,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize, int charsCount, in
unsigned char *dataBitmap = (unsigned char *)malloc(textureSize*textureSize*sizeof(unsigned char)); // One channel bitmap returned!
stbtt_bakedchar *charData = (stbtt_bakedchar *)malloc(sizeof(stbtt_bakedchar)*charsCount);
SpriteFont font = { 0 };
Font font = { 0 };
FILE *ttfFile = fopen(fileName, "rb");

View File

@@ -1371,7 +1371,7 @@ Image ImageText(const char *text, int fontSize, Color color)
}
// Create an image from text (custom sprite font)
Image ImageTextEx(SpriteFont font, const char *text, float fontSize, float spacing, Color tint)
Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Color tint)
{
int length = strlen(text);
int posX = 0;
@@ -1385,7 +1385,7 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, float spaci
// NOTE: glGetTexImage() not available in OpenGL ES
// TODO: This is horrible, retrieving font texture from GPU!!!
// Define ImageFont struct? or include Image spritefont in SpriteFont struct?
// Define ImageFont struct? or include Image spritefont in Font struct?
Image imFont = GetTextureData(font.texture);
ImageColorTint(&imFont, tint); // Apply color tint to font
@@ -1466,7 +1466,7 @@ void ImageDrawText(Image *dst, Vector2 position, const char *text, int fontSize,
}
// Draw text (custom sprite font) within an image (destination)
void ImageDrawTextEx(Image *dst, Vector2 position, SpriteFont font, const char *text, float fontSize, float spacing, Color color)
void ImageDrawTextEx(Image *dst, Vector2 position, Font font, const char *text, float fontSize, float spacing, Color color)
{
Image imText = ImageTextEx(font, text, fontSize, spacing, color);