core_text_file_loading.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - text file loading
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 5.5, last time updated with raylib 5.6
  8. *
  9. * Example contributed by Aanjishnu Bhattacharyya (@NimComPoo-04) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 0 Aanjishnu Bhattacharyya (@NimComPoo-04)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h" // For Lerp
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [core] example - text file loading");
  29. // Setting up the camera
  30. Camera2D cam = {
  31. .offset = {0, 0},
  32. .target = {0, 0},
  33. .rotation = 0,
  34. .zoom = 1
  35. };
  36. // Loading file from resources/text_file.txt
  37. const char *fileName = "resources/text_file.txt";
  38. char *fileData = LoadFileText(fileName);
  39. // Loading all the lines
  40. int lineCount = 0;
  41. char **lines = LoadTextLines(fileData, &lineCount);
  42. // Just sylistic choise
  43. int fontSize = 20;
  44. int textTop = 25 + fontSize; // Top of the screen from where the text is rendered
  45. int wrapWidth = screenWidth - 20;
  46. // Wrap the lines as needed
  47. for(int i = 0; i < lineCount; i++)
  48. {
  49. int j = 0;
  50. int lastSpace = 0; // Keeping track of last valid space to insert '\n'
  51. int lastWrapStart = 0; // Keeping track of the start of this wrapped line.
  52. while(lines[i][j] != '\0')
  53. {
  54. if(lines[i][j] == ' ')
  55. {
  56. // Making a C Style string by adding a '\0' at the required location so that we can use the MeasureText function
  57. lines[i][j] = '\0';
  58. // Checking if the text has crossed the wrapWidth, then going back and inserting a newline
  59. if(MeasureText(lines[i] + lastWrapStart, fontSize) > wrapWidth)
  60. {
  61. lines[i][lastSpace] = '\n';
  62. // Since we added a newline the place of wrap changed so we update our lastWrapStart
  63. lastWrapStart = lastSpace + 1;
  64. }
  65. lines[i][j] = ' '; // Resetting the space back
  66. lastSpace = j; // Since we encountered a new space we update our last encountered space location
  67. }
  68. j++;
  69. }
  70. }
  71. // Calculating the total height so that we can show a scrollbar
  72. int textHeight = 0;
  73. for(int i = 0; i < lineCount; i++)
  74. {
  75. Vector2 size = MeasureTextEx(GetFontDefault(), lines[i], fontSize, 2);
  76. textHeight += size.y + 10;
  77. }
  78. // A simple scrollbar on the side to show how far we have red into the file
  79. Rectangle scrollBar = {
  80. .x = screenWidth - 5,
  81. .y = 0,
  82. .width = 5,
  83. .height = screenHeight * 100 / (textHeight - screenHeight) // Scrollbar height is just a percentage
  84. };
  85. SetTargetFPS(60);
  86. //--------------------------------------------------------------------------------------
  87. // Main game loop
  88. while (!WindowShouldClose()) // Detect window close button or ESC key
  89. {
  90. // Update
  91. //----------------------------------------------------------------------------------
  92. float scroll = GetMouseWheelMove();
  93. cam.target.y -= scroll * fontSize * 1.5; // Choosing an arbitrary speed for scroll
  94. if(cam.target.y < 0) // Snapping to 0 if we go too far back
  95. cam.target.y = 0;
  96. if(cam.target.y > textHeight - screenHeight + textTop) // Ensuring that the camera does not scroll past all text
  97. cam.target.y = textHeight - screenHeight + textTop;
  98. // Computing the position of the scrollBar depending on the percentage of text covered.
  99. scrollBar.y = Lerp(textTop, screenHeight - scrollBar.height, (cam.target.y - textTop) / (textHeight - screenHeight));
  100. // Draw
  101. //----------------------------------------------------------------------------------
  102. BeginDrawing();
  103. ClearBackground(RAYWHITE);
  104. BeginMode2D(cam);
  105. Font defaultFont = GetFontDefault();
  106. // Going through all the read lines
  107. for(int i = 0, t = textTop; i < lineCount; i++)
  108. {
  109. // Each time we go through and calculate the height of the text to move the cursor appropriately
  110. Vector2 size = MeasureTextEx(defaultFont, lines[i], fontSize, 2);
  111. DrawText(lines[i], 10, t, fontSize, RED);
  112. // Inserting extra space for real newlines, wrapped lines are rendered closer together
  113. t += size.y + 10;
  114. }
  115. EndMode2D();
  116. // Header displaying which file is being read currently
  117. DrawRectangle(0, 0, screenWidth, textTop - 10, BEIGE);
  118. DrawText(TextFormat("File: %s", fileName), 10, 10, fontSize, MAROON);
  119. DrawRectangleRec(scrollBar, MAROON);
  120. EndDrawing();
  121. //----------------------------------------------------------------------------------
  122. }
  123. // De-Initialization
  124. //--------------------------------------------------------------------------------------
  125. UnloadTextLines(lines, lineCount);
  126. UnloadFileText(fileData);
  127. CloseWindow(); // Close window and OpenGL context
  128. //--------------------------------------------------------------------------------------
  129. return 0;
  130. }