Skip to content

Commit d8da443

Browse files
authored
Fixed core_text_file_loading example in raylib examples, to account for blank lines in text file and text wrapping properly for the case when the last word goes out the display (#5339)
1 parent 4ff296b commit d8da443

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

examples/core/core_text_file_loading.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "raymath.h" // Required for: Lerp()
2121

22+
#include <string.h>
23+
2224
//------------------------------------------------------------------------------------
2325
// Program main entry point
2426
//------------------------------------------------------------------------------------
@@ -59,10 +61,11 @@ int main(void)
5961
int lastSpace = 0; // Keeping track of last valid space to insert '\n'
6062
int lastWrapStart = 0; // Keeping track of the start of this wrapped line.
6163

62-
while (lines[i][j] != '\0')
64+
while (j <= strlen(lines[i]))
6365
{
64-
if (lines[i][j] == ' ')
66+
if (lines[i][j] == ' ' || lines[i][j] == '\0')
6567
{
68+
char before = lines[i][j];
6669
// Making a C Style string by adding a '\0' at the required location so that we can use the MeasureText function
6770
lines[i][j] = '\0';
6871

@@ -75,7 +78,7 @@ int main(void)
7578
lastWrapStart = lastSpace + 1;
7679
}
7780

78-
lines[i][j] = ' '; // Resetting the space back
81+
if(before != '\0') lines[i][j] = ' '; // Resetting the space back
7982
lastSpace = j; // Since we encountered a new space we update our last encountered space location
8083
}
8184

@@ -92,7 +95,7 @@ int main(void)
9295
textHeight += (int)size.y + 10;
9396
}
9497

95-
// A simple scrollbar on the side to show how far we have red into the file
98+
// A simple scrollbar on the side to show how far we have read into the file
9699
Rectangle scrollBar = {
97100
.x = (float)screenWidth - 5,
98101
.y = 0,
@@ -132,7 +135,13 @@ int main(void)
132135
for (int i = 0, t = textTop; i < lineCount; i++)
133136
{
134137
// Each time we go through and calculate the height of the text to move the cursor appropriately
135-
Vector2 size = MeasureTextEx(GetFontDefault(), lines[i], (float)fontSize, 2);
138+
Vector2 size;
139+
if(strcmp(lines[i], "")){
140+
// Fix for empty line in the text file
141+
size = MeasureTextEx( GetFontDefault(), lines[i], (float)fontSize, 2);
142+
}else{
143+
size = MeasureTextEx( GetFontDefault(), " ", (float)fontSize, 2);
144+
}
136145

137146
DrawText(lines[i], 10, t, fontSize, RED);
138147

0 commit comments

Comments
 (0)