Some functions review
This commit is contained in:
parent
cae209816c
commit
f69f930b51
31
src/raylib.h
31
src/raylib.h
@ -185,17 +185,20 @@
|
||||
#define GAMEPAD_PLAYER4 3 // Not supported
|
||||
|
||||
// Gamepad Buttons
|
||||
// NOTE: Adjusted for a PS3 USB Controller
|
||||
#define GAMEPAD_BUTTON_A 2
|
||||
#define GAMEPAD_BUTTON_B 1
|
||||
#define GAMEPAD_BUTTON_X 3
|
||||
#define GAMEPAD_BUTTON_Y 4
|
||||
#define GAMEPAD_BUTTON_R1 7
|
||||
#define GAMEPAD_BUTTON_R2 5
|
||||
#define GAMEPAD_BUTTON_L1 6
|
||||
#define GAMEPAD_BUTTON_L2 8
|
||||
#define GAMEPAD_BUTTON_SELECT 9
|
||||
#define GAMEPAD_BUTTON_START 10
|
||||
|
||||
// PS3 USB Controller
|
||||
#define GAMEPAD_PS3_BUTTON_A 2
|
||||
#define GAMEPAD_PS3_BUTTON_B 1
|
||||
#define GAMEPAD_PS3_BUTTON_X 3
|
||||
#define GAMEPAD_PS3_BUTTON_Y 4
|
||||
#define GAMEPAD_PS3_BUTTON_R1 7
|
||||
#define GAMEPAD_PS3_BUTTON_R2 5
|
||||
#define GAMEPAD_PS3_BUTTON_L1 6
|
||||
#define GAMEPAD_PS3_BUTTON_L2 8
|
||||
#define GAMEPAD_PS3_BUTTON_SELECT 9
|
||||
#define GAMEPAD_PS3_BUTTON_START 10
|
||||
|
||||
// TODO: Add PS3 d-pad axis
|
||||
|
||||
// Xbox360 USB Controller Buttons
|
||||
#define GAMEPAD_XBOX_BUTTON_A 0
|
||||
@ -782,10 +785,10 @@ void ImageCrop(Image *image, Rectangle crop);
|
||||
void ImageResize(Image *image, int newWidth, int newHeight); // Resize and image (bilinear filtering)
|
||||
void ImageResizeNN(Image *image,int newWidth,int newHeight); // Resize and image (Nearest-Neighbor scaling algorithm)
|
||||
Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font)
|
||||
Image ImageTextEx(SpriteFont font, const char *text, int fontSize, int spacing, Color tint); // Create an image from text (custom sprite font)
|
||||
Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing, Color tint); // Create an image from text (custom sprite font)
|
||||
void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec); // Draw a source image within a destination image
|
||||
void ImageDrawText(Image *dst, Vector2 position, const char *text, int fontSize, Color color); // Draw text (default font) within an image (destination)
|
||||
void ImageDrawTextEx(Image *dst, Vector2 position, SpriteFont font, const char *text, int fontSize, int spacing, Color color); // Draw text (custom sprite font) within an image (destination)
|
||||
void ImageDrawTextEx(Image *dst, Vector2 position, SpriteFont font, const char *text, float fontSize, int spacing, Color color); // Draw text (custom sprite font) within an image (destination)
|
||||
void ImageFlipVertical(Image *image); // Flip image vertically
|
||||
void ImageFlipHorizontal(Image *image); // Flip image horizontally
|
||||
void ImageColorTint(Image *image, Color color); // Modify image color: tint
|
||||
@ -812,7 +815,7 @@ void UnloadSpriteFont(SpriteFont spriteFont);
|
||||
|
||||
void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font)
|
||||
void DrawTextEx(SpriteFont spriteFont, const char* text, Vector2 position, // Draw text using SpriteFont and additional parameters
|
||||
int fontSize, int spacing, Color tint);
|
||||
float fontSize, int spacing, Color tint);
|
||||
int MeasureText(const char *text, int fontSize); // Measure string width for default font
|
||||
Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int spacing); // Measure string size for SpriteFont
|
||||
|
||||
|
38
src/text.c
38
src/text.c
@ -43,8 +43,7 @@
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#define FONT_FIRST_CHAR 32
|
||||
#define MAX_FONTCHARS 128
|
||||
#define FONT_FIRST_CHAR 32 // NOTE: Expected first char for a sprite font
|
||||
#define MAX_FORMATTEXT_LENGTH 64
|
||||
#define MAX_SUBTEXT_LENGTH 64
|
||||
|
||||
@ -72,7 +71,9 @@ static SpriteFont defaultFont; // Default font provided by raylib
|
||||
static SpriteFont LoadImageFont(Image image, Color key, int firstChar); // Load a Image font file (XNA style)
|
||||
static SpriteFont LoadRBMF(const char *fileName); // Load a rBMF font file (raylib BitMap Font)
|
||||
static SpriteFont LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file)
|
||||
static SpriteFont LoadTTF(const char *fileName, int fontSize); // Generate a sprite font image from TTF data (font size required)
|
||||
|
||||
// Generate a sprite font image from TTF data
|
||||
static SpriteFont LoadTTF(const char *fileName, int fontSize, int firstChar, int numChars);
|
||||
|
||||
extern void LoadDefaultFont(void);
|
||||
extern void UnloadDefaultFont(void);
|
||||
@ -245,16 +246,20 @@ SpriteFont GetDefaultFont()
|
||||
// Load a SpriteFont image into GPU memory
|
||||
SpriteFont LoadSpriteFont(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
|
||||
|
||||
SpriteFont spriteFont = { 0 };
|
||||
|
||||
// Check file extension
|
||||
if (strcmp(GetExtension(fileName),"rbmf") == 0) spriteFont = LoadRBMF(fileName);
|
||||
else if (strcmp(GetExtension(fileName),"ttf") == 0) spriteFont = LoadTTF(fileName, 32);
|
||||
else if (strcmp(GetExtension(fileName),"ttf") == 0) spriteFont = LoadTTF(fileName, DEFAULT_TTF_FONTSIZE, FONT_FIRST_CHAR, DEFAULT_TTF_NUMCHARS);
|
||||
else if (strcmp(GetExtension(fileName),"fnt") == 0) spriteFont = LoadBMFont(fileName);
|
||||
else
|
||||
{
|
||||
Image image = LoadImage(fileName);
|
||||
if (image.data != NULL) spriteFont = LoadImageFont(image, MAGENTA, 32);
|
||||
if (image.data != NULL) spriteFont = LoadImageFont(image, MAGENTA, FONT_FIRST_CHAR);
|
||||
UnloadImage(image);
|
||||
}
|
||||
|
||||
@ -294,12 +299,12 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
|
||||
if (fontSize < defaultFontSize) fontSize = defaultFontSize;
|
||||
int spacing = fontSize / defaultFontSize;
|
||||
|
||||
DrawTextEx(defaultFont, text, position, fontSize, spacing, color);
|
||||
DrawTextEx(defaultFont, text, position, (float)fontSize, spacing, color);
|
||||
}
|
||||
|
||||
// Draw text using SpriteFont
|
||||
// NOTE: chars spacing is NOT proportional to fontSize
|
||||
void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int fontSize, int spacing, Color tint)
|
||||
void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float fontSize, int spacing, Color tint)
|
||||
{
|
||||
int length = strlen(text);
|
||||
int textOffsetX = 0;
|
||||
@ -309,7 +314,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f
|
||||
|
||||
Rectangle rec;
|
||||
|
||||
scaleFactor = (float)fontSize/spriteFont.size;
|
||||
scaleFactor = fontSize/spriteFont.size;
|
||||
|
||||
// NOTE: Some ugly hacks are made to support Latin-1 Extended characters directly
|
||||
// written in C code files (codified by default as UTF-8)
|
||||
@ -498,6 +503,9 @@ static SpriteFont LoadImageFont(Image image, Color key, int firstChar)
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
// Default number of characters expected supported
|
||||
#define MAX_FONTCHARS 128
|
||||
|
||||
// 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];
|
||||
@ -634,7 +642,7 @@ static SpriteFont LoadRBMF(const char *fileName)
|
||||
|
||||
spriteFont.numChars = (int)rbmfHeader.numChars;
|
||||
|
||||
int numPixelBits = rbmfHeader.imgWidth * rbmfHeader.imgHeight / 32;
|
||||
int numPixelBits = rbmfHeader.imgWidth*rbmfHeader.imgHeight/32;
|
||||
|
||||
rbmfFileData = (unsigned int *)malloc(numPixelBits * sizeof(unsigned int));
|
||||
|
||||
@ -843,17 +851,15 @@ static SpriteFont LoadBMFont(const char *fileName)
|
||||
|
||||
// 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)
|
||||
static SpriteFont LoadTTF(const char *fileName, int fontSize, int firstChar, int numChars)
|
||||
{
|
||||
// NOTE: Generated font uses some hardcoded values
|
||||
#define FONT_TEXTURE_WIDTH 512 // Font texture width
|
||||
#define FONT_TEXTURE_HEIGHT 512 // Font texture height
|
||||
#define FONT_FIRST_CHAR 32 // Font first character (32 - space)
|
||||
#define FONT_NUM_CHARS 95 // ASCII 32..126 is 95 glyphs
|
||||
|
||||
unsigned char *ttfBuffer = (unsigned char *)malloc(1 << 25);
|
||||
unsigned char *dataBitmap = (unsigned char *)malloc(FONT_TEXTURE_WIDTH*FONT_TEXTURE_HEIGHT*sizeof(unsigned char)); // One channel bitmap returned!
|
||||
stbtt_bakedchar charData[FONT_NUM_CHARS]; // ASCII 32..126 is 95 glyphs
|
||||
stbtt_bakedchar charData[numChars];
|
||||
|
||||
SpriteFont font = { 0 };
|
||||
|
||||
@ -868,7 +874,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize)
|
||||
fread(ttfBuffer, 1, 1<<25, ttfFile);
|
||||
|
||||
// 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);
|
||||
stbtt_BakeFontBitmap(ttfBuffer,0, fontSize, dataBitmap, FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT, firstChar, numChars, charData);
|
||||
|
||||
free(ttfBuffer);
|
||||
|
||||
@ -898,7 +904,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize)
|
||||
UnloadImage(image); // Unloads dataGrayAlpha
|
||||
|
||||
font.size = fontSize;
|
||||
font.numChars = FONT_NUM_CHARS;
|
||||
font.numChars = numChars;
|
||||
font.charValues = (int *)malloc(font.numChars*sizeof(int));
|
||||
font.charRecs = (Rectangle *)malloc(font.numChars*sizeof(Rectangle));
|
||||
font.charOffsets = (Vector2 *)malloc(font.numChars*sizeof(Vector2));
|
||||
@ -906,7 +912,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize)
|
||||
|
||||
for (int i = 0; i < font.numChars; i++)
|
||||
{
|
||||
font.charValues[i] = i + FONT_FIRST_CHAR;
|
||||
font.charValues[i] = i + firstChar;
|
||||
|
||||
font.charRecs[i].x = (int)charData[i].x0;
|
||||
font.charRecs[i].y = (int)charData[i].y0;
|
||||
|
@ -1053,7 +1053,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, int fontSize, int spacing, Color tint)
|
||||
Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing, Color tint)
|
||||
{
|
||||
int length = strlen(text);
|
||||
int posX = 0;
|
||||
@ -1091,9 +1091,9 @@ Image ImageTextEx(SpriteFont font, const char *text, int fontSize, int spacing,
|
||||
Image imText = LoadImageEx(pixels, (int)imSize.x, (int)imSize.y);
|
||||
|
||||
// Scale image depending on text size
|
||||
if (fontSize > (int)imSize.y)
|
||||
if (fontSize > imSize.y)
|
||||
{
|
||||
float scaleFactor = (float)fontSize/imSize.y;
|
||||
float scaleFactor = fontSize/imSize.y;
|
||||
TraceLog(INFO, "Scalefactor: %f", scaleFactor);
|
||||
|
||||
// Using nearest-neighbor scaling algorithm for default font
|
||||
@ -1114,7 +1114,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, int fontSize, int spacing, Color color)
|
||||
void ImageDrawTextEx(Image *dst, Vector2 position, SpriteFont font, const char *text, float fontSize, int spacing, Color color)
|
||||
{
|
||||
Image imText = ImageTextEx(font, text, fontSize, spacing, color);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user