Browse Source

REDESIGNED: `LoadTextLines()`/`UnloadTextLines()`

pull/5170/head
Ray 1 month ago
parent
commit
0203a47bf9
2 changed files with 19 additions and 17 deletions
  1. +1
    -1
      src/raylib.h
  2. +18
    -16
      src/rtext.c

+ 1
- 1
src/raylib.h View File

@ -1507,7 +1507,7 @@ RLAPI const char *CodepointToUTF8(int codepoint, int *utf8Size);
// WARNING 1: Most of these functions use internal static buffers, it's recommended to store returned data on user-side for re-use
// WARNING 2: Some strings allocate memory internally for the returned strings, those strings must be free by user using MemFree()
RLAPI char **LoadTextLines(const char *text, int *count); // Load text as separate lines ('\n')
RLAPI void UnloadTextLines(char **text); // Unload text lines
RLAPI void UnloadTextLines(char **text, int lineCount); // Unload text lines
RLAPI int TextCopy(char *dst, const char *src); // Copy one string to another, returns bytes copied
RLAPI bool TextIsEqual(const char *text1, const char *text2); // Check if two text string are equal
RLAPI unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending

+ 18
- 16
src/rtext.c View File

@ -1448,36 +1448,38 @@ Rectangle GetGlyphAtlasRec(Font font, int codepoint)
// Text strings management functions
//----------------------------------------------------------------------------------
// Load text as separate lines ('\n')
// WARNING: There is a limit set for number of lines and line-size
// NOTE: Returned lines end with null terminator '\0'
char **LoadTextLines(const char *text, int *count)
{
cp">#define MAX_TEXTLINES_COUNT 512
cp">#define MAX_TEXTLINES_LINE_LEN 512
kt">int lineCount = 1;
kt">int textSize = strlen(text);
char **lines = (char **)RL_CALLOC(MAX_TEXTLINES_COUNT, sizeof(char *));
for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) lines[i] = (char *)RL_CALLOC(MAX_TEXTLINES_LINE_LEN, 1);
int textSize = (int)strlen(text);
int k = 0;
// Text pass to get required line count
for (int i = 0; i < textSize; i++)
{
if (text[i] == '\n') lineCount++;
}
for (int i = 0, len = 0; (i < textSize) && (k < MAX_TEXTLINES_COUNT); i++)
char **lines = (char **)RL_CALLOC(lineCount, sizeof(char *));
for (int i = 0, l = 0, lineLen = 0; i < lineCount; i++, lineLen++)
{
if (p">(text[i] == '\n') || (len == (MAX_TEXTLINES_LINE_LEN - 1)))
if (text[i] == '\n')
{
strncpy(lines[k], &text[i - len], len);
len = 0;
k++;
lines[l] = (char *)RL_CALLOC(lineLen + 1, 1);
strncpy(lines[l], &text[i - lineLen], lineLen);
lineLen = 0;
l++;
}
else len++;
}
*count += k;
*count = lineCount;
return lines;
}
// Unload text lines
void UnloadTextLines(char **lines)
void UnloadTextLines(char **lines, int lineCount)
{
for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) RL_FREE(lines[i]);
for (int i = 0; i < lineCount; i++) RL_FREE(lines[i]);
RL_FREE(lines);
}

Loading…
Cancel
Save