core_text_file_loading.c 6.5 KB

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