Browse Source

Review example formating

pull/5282/head
Ray 1 week ago
parent
commit
085f933b17
1 changed files with 28 additions and 28 deletions
  1. +28
    -28
      examples/core/core_text_file_loading.c

+ 28
- 28
examples/core/core_text_file_loading.c View File

@ -16,7 +16,8 @@
********************************************************************************************/
#include "raylib.h"
#include "raymath.h" // For Lerp
#include "raymath.h" // Required for: Lerp()
//------------------------------------------------------------------------------------
// Program main entry point
@ -38,35 +39,35 @@ int main(void)
.zoom = 1
};
// Loading file from resources/text_file.txt
// Loading text file from resources/text_file.txt
const char *fileName = "resources/text_file.txt";
char *fileData = LoadFileText(fileName);
char *text = LoadFileText(fileName);
// Loading all the lines
// Loading all the text lines
int lineCount = 0;
char **lines = LoadTextLines(fileData, &lineCount);
char **lines = LoadTextLines(text, &lineCount);
// Just sylistic choise
// Stylistic choises
int fontSize = 20;
int textTop = 25 + fontSize; // Top of the screen from where the text is rendered
int textTop = 25 + fontSize; // Top of the screen from where the text is rendered
int wrapWidth = screenWidth - 20;
// Wrap the lines as needed
for(int i = 0; i < lineCount; i++)
for (int i = 0; i < lineCount; i++)
{
int j = 0;
int lastSpace = 0; // Keeping track of last valid space to insert '\n'
int lastWrapStart = 0; // Keeping track of the start of this wrapped line.
while(lines[i][j] != '\0')
while (lines[i][j] != '\0')
{
if(lines[i][j] == ' ')
if (lines[i][j] == ' ')
{
// Making a C Style string by adding a '\0' at the required location so that we can use the MeasureText function
lines[i][j] = '\0';
// Checking if the text has crossed the wrapWidth, then going back and inserting a newline
if(MeasureText(lines[i] + lastWrapStart, fontSize) > wrapWidth)
if (MeasureText(lines[i] + lastWrapStart, fontSize) > wrapWidth)
{
lines[i][lastSpace] = '\n';
@ -85,7 +86,7 @@ int main(void)
// Calculating the total height so that we can show a scrollbar
int textHeight = 0;
for(int i = 0; i < lineCount; i++)
for (int i = 0; i < lineCount; i++)
{
Vector2 size = MeasureTextEx(GetFontDefault(), lines[i], fontSize, 2);
textHeight += size.y + 10;
@ -96,10 +97,9 @@ int main(void)
.x = screenWidth - 5,
.y = 0,
.width = 5,
.height = screenHeight * 100 / (textHeight - screenHeight) // Scrollbar height is just a percentage
.height = screenHeight*100/(textHeight - screenHeight) // Scrollbar height is just a percentage
};
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
@ -109,16 +109,17 @@ int main(void)
// Update
//----------------------------------------------------------------------------------
float scroll = GetMouseWheelMove();
cam.target.y -= scroll * fontSize * 1.5; // Choosing an arbitrary speed for scroll
if(cam.target.y < 0) // Snapping to 0 if we go too far back
cam.target.y = 0;
cam.target.y -= scroll*fontSize*1.5f; // Choosing an arbitrary speed for scroll
if(cam.target.y > textHeight - screenHeight + textTop) // Ensuring that the camera does not scroll past all text
if (cam.target.y < 0) cam.target.y = 0; // Snapping to 0 if we go too far back
// Ensuring that the camera does not scroll past all text
if (cam.target.y > textHeight - screenHeight + textTop)
cam.target.y = textHeight - screenHeight + textTop;
// Computing the position of the scrollBar depending on the percentage of text covered.
scrollBar.y = Lerp(textTop, screenHeight - scrollBar.height, (cam.target.y - textTop) / (textHeight - screenHeight));
// Computing the position of the scrollBar depending on the percentage of text covered
scrollBar.y = Lerp(textTop, screenHeight - scrollBar.height, (cam.target.y - textTop)/(textHeight - screenHeight));
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
@ -127,17 +128,16 @@ int main(void)
ClearBackground(RAYWHITE);
BeginMode2D(cam);
Font defaultFont = GetFontDefault();
// Going through all the read lines
for(int i = 0, t = textTop; i < lineCount; i++)
for (int i = 0, t = textTop; i < lineCount; i++)
{
// Each time we go through and calculate the height of the text to move the cursor appropriately
Vector2 size = MeasureTextEx(defaultFont, lines[i], fontSize, 2);
Vector2 size = MeasureTextEx(GetFontDefault(), lines[i], fontSize, 2);
DrawText(lines[i], 10, t, fontSize, RED);
// Inserting extra space for real newlines, wrapped lines are rendered closer together
// Inserting extra space for real newlines,
// wrapped lines are rendered closer together
t += size.y + 10;
}
EndMode2D();
@ -155,7 +155,7 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTextLines(lines, lineCount);
UnloadFileText(fileData);
UnloadFileText(text);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

Loading…
Cancel
Save