ADDED: SplitText() function
This commit is contained in:
parent
b5bcf2c934
commit
550dd40cb3
2 changed files with 93 additions and 54 deletions
|
@ -1088,10 +1088,13 @@ RLAPI void DrawTextEx(Font font, const char *text, Vector2 position, float fontS
|
||||||
// Text misc. functions
|
// Text misc. functions
|
||||||
RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font
|
RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font
|
||||||
RLAPI Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing); // Measure string size for Font
|
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(Font font, int character); // Get index position for a unicode character on font
|
RLAPI int GetGlyphIndex(Font font, int character); // Get index position for a unicode character on font
|
||||||
|
|
||||||
|
// Text string edition functions
|
||||||
|
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 char **SplitText(char *text, char delimiter, int *strCount); // Split text string into multiple strings (memory should be freed manually!)
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Basic 3d Shapes Drawing Functions (Module: models)
|
// Basic 3d Shapes Drawing Functions (Module: models)
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
|
|
140
src/text.c
140
src/text.c
|
@ -566,6 +566,28 @@ void UnloadFont(Font font)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shows current FPS on top-left corner
|
||||||
|
// NOTE: Uses default font
|
||||||
|
void DrawFPS(int posX, int posY)
|
||||||
|
{
|
||||||
|
// NOTE: We are rendering fps every second for better viewing on high framerates
|
||||||
|
|
||||||
|
static int fps = 0;
|
||||||
|
static int counter = 0;
|
||||||
|
static int refreshRate = 20;
|
||||||
|
|
||||||
|
if (counter < refreshRate) counter++;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fps = GetFPS();
|
||||||
|
refreshRate = fps;
|
||||||
|
counter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: We have rounding errors every frame, so it oscillates a lot
|
||||||
|
DrawText(FormatText("%2i FPS", fps), posX, posY, 20, LIME);
|
||||||
|
}
|
||||||
|
|
||||||
// Draw text (using default font)
|
// 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
|
// NOTE: fontSize work like in any drawing program but if fontSize is lower than font-base-size, then font-base-size is used
|
||||||
// NOTE: chars spacing is proportional to fontSize
|
// NOTE: chars spacing is proportional to fontSize
|
||||||
|
@ -642,44 +664,6 @@ void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formatting of text with variables to 'embed'
|
|
||||||
const char *FormatText(const char *text, ...)
|
|
||||||
{
|
|
||||||
static char buffer[MAX_FORMATTEXT_LENGTH];
|
|
||||||
|
|
||||||
va_list args;
|
|
||||||
va_start(args, text);
|
|
||||||
vsprintf(buffer, text, args);
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a piece of a text string
|
|
||||||
const char *SubText(const char *text, int position, int length)
|
|
||||||
{
|
|
||||||
static char buffer[MAX_SUBTEXT_LENGTH] = { 0 };
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Measure string width for default font
|
// Measure string width for default font
|
||||||
int MeasureText(const char *text, int fontSize)
|
int MeasureText(const char *text, int fontSize)
|
||||||
{
|
{
|
||||||
|
@ -764,26 +748,78 @@ int GetGlyphIndex(Font font, int character)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shows current FPS on top-left corner
|
// Formatting of text with variables to 'embed'
|
||||||
// NOTE: Uses default font
|
const char *FormatText(const char *text, ...)
|
||||||
void DrawFPS(int posX, int posY)
|
|
||||||
{
|
{
|
||||||
// NOTE: We are rendering fps every second for better viewing on high framerates
|
static char buffer[MAX_FORMATTEXT_LENGTH];
|
||||||
|
|
||||||
static int fps = 0;
|
va_list args;
|
||||||
static int counter = 0;
|
va_start(args, text);
|
||||||
static int refreshRate = 20;
|
vsprintf(buffer, text, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
if (counter < refreshRate) counter++;
|
return buffer;
|
||||||
else
|
}
|
||||||
|
|
||||||
|
// Get a piece of a text string
|
||||||
|
const char *SubText(const char *text, int position, int length)
|
||||||
|
{
|
||||||
|
static char buffer[MAX_SUBTEXT_LENGTH] = { 0 };
|
||||||
|
int textLength = strlen(text);
|
||||||
|
|
||||||
|
if (position >= textLength)
|
||||||
{
|
{
|
||||||
fps = GetFPS();
|
position = textLength - 1;
|
||||||
refreshRate = fps;
|
length = 0;
|
||||||
counter = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: We have rounding errors every frame, so it oscillates a lot
|
if (length >= textLength) length = textLength;
|
||||||
DrawText(FormatText("%2i FPS", fps), posX, posY, 20, LIME);
|
|
||||||
|
for (int c = 0 ; c < length ; c++)
|
||||||
|
{
|
||||||
|
*(buffer + c) = *(text + position);
|
||||||
|
text++;
|
||||||
|
}
|
||||||
|
|
||||||
|
*(buffer + length) = '\0';
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split string into multiple strings
|
||||||
|
// NOTE: Files count is returned by parameters pointer
|
||||||
|
// NOTE: Allocated memory should be manually freed
|
||||||
|
char **SplitText(char *text, char delimiter, int *strCount)
|
||||||
|
{
|
||||||
|
#define MAX_SUBSTRING_LENGTH 128
|
||||||
|
|
||||||
|
char **strings = NULL;
|
||||||
|
int len = strlen(text);
|
||||||
|
char *strDup = (char *)malloc(len + 1);
|
||||||
|
strcpy(strDup, text);
|
||||||
|
int counter = 1;
|
||||||
|
|
||||||
|
// Count how many substrings we have on string
|
||||||
|
for (int i = 0; i < len; i++) if (text[i] == delimiter) counter++;
|
||||||
|
|
||||||
|
// Memory allocation for substrings
|
||||||
|
strings = (char **)malloc(sizeof(char *)*counter);
|
||||||
|
for (int i = 0; i < counter; i++) strings[i] = (char *)malloc(sizeof(char)*MAX_SUBSTRING_LENGTH);
|
||||||
|
|
||||||
|
char *substrPtr = NULL;
|
||||||
|
char delimiters[1] = { delimiter }; // Only caring for one delimiter
|
||||||
|
substrPtr = strtok(strDup, delimiters);
|
||||||
|
|
||||||
|
for (int i = 0; (i < counter) && (substrPtr != NULL); i++)
|
||||||
|
{
|
||||||
|
strcpy(strings[i], substrPtr);
|
||||||
|
substrPtr = strtok(NULL, delimiters);
|
||||||
|
}
|
||||||
|
|
||||||
|
*strCount = counter;
|
||||||
|
free(strDup);
|
||||||
|
|
||||||
|
return strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue