Review example formating

This commit is contained in:
Ray
2025-10-17 17:17:54 +02:00
parent ab4831911a
commit 085f933b17

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,15 +39,15 @@ 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 wrapWidth = screenWidth - 20;
@@ -99,7 +100,6 @@ int main(void)
.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
cam.target.y -= scroll*fontSize*1.5f; // Choosing an arbitrary speed for scroll
if(cam.target.y < 0) // Snapping to 0 if we go too far back
cam.target.y = 0;
if (cam.target.y < 0) cam.target.y = 0; // Snapping to 0 if we go too far back
if(cam.target.y > textHeight - screenHeight + textTop) // Ensuring that the camera does not scroll past all text
// 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.
// 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++)
{
// 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
//--------------------------------------------------------------------------------------