text_codepoints_loading.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - Codepoints loading
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * Example originally created with raylib 4.2, last time updated with raylib 2.5
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2022-2024 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include <stdlib.h> // Required for: calloc(), realloc(), free()
  17. #include <string.h> // Required for: memcpy()
  18. // Text to be displayed, must be UTF-8 (save this code file as UTF-8)
  19. // NOTE: It can contain all the required text for the game,
  20. // this text will be scanned to get all the required codepoints
  21. static char *text = "いろはにほへと ちりぬるを\nわかよたれそ つねならむ\nうゐのおくやま けふこえて\nあさきゆめみし ゑひもせす";
  22. // Remove codepoint duplicates if requested
  23. static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *codepointResultCount);
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. InitWindow(screenWidth, screenHeight, "raylib [text] example - codepoints loading");
  34. // Get codepoints from text
  35. int codepointCount = 0;
  36. int *codepoints = LoadCodepoints(text, &codepointCount);
  37. // Removed duplicate codepoints to generate smaller font atlas
  38. int codepointsNoDupsCount = 0;
  39. int *codepointsNoDups = CodepointRemoveDuplicates(codepoints, codepointCount, &codepointsNoDupsCount);
  40. UnloadCodepoints(codepoints);
  41. // Load font containing all the provided codepoint glyphs
  42. // A texture font atlas is automatically generated
  43. Font font = LoadFontEx("resources/DotGothic16-Regular.ttf", 36, codepointsNoDups, codepointsNoDupsCount);
  44. // Set bilinear scale filter for better font scaling
  45. SetTextureFilter(font.texture, TEXTURE_FILTER_BILINEAR);
  46. SetTextLineSpacing(20); // Set line spacing for multiline text (when line breaks are included '\n')
  47. // Free codepoints, atlas has already been generated
  48. free(codepointsNoDups);
  49. bool showFontAtlas = false;
  50. int codepointSize = 0;
  51. char *ptr = text;
  52. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  53. //--------------------------------------------------------------------------------------
  54. // Main game loop
  55. while (!WindowShouldClose()) // Detect window close button or ESC key
  56. {
  57. // Update
  58. //----------------------------------------------------------------------------------
  59. if (IsKeyPressed(KEY_SPACE)) showFontAtlas = !showFontAtlas;
  60. // Testing code: getting next and previous codepoints on provided text
  61. if (IsKeyPressed(KEY_RIGHT))
  62. {
  63. // Get next codepoint in string and move pointer
  64. GetCodepointNext(ptr, &codepointSize);
  65. ptr += codepointSize;
  66. }
  67. else if (IsKeyPressed(KEY_LEFT))
  68. {
  69. // Get previous codepoint in string and move pointer
  70. GetCodepointPrevious(ptr, &codepointSize);
  71. ptr -= codepointSize;
  72. }
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. DrawRectangle(0, 0, GetScreenWidth(), 70, BLACK);
  79. DrawText(TextFormat("Total codepoints contained in provided text: %i", codepointCount), 10, 10, 20, GREEN);
  80. DrawText(TextFormat("Total codepoints required for font atlas (duplicates excluded): %i", codepointsNoDupsCount), 10, 40, 20, GREEN);
  81. if (showFontAtlas)
  82. {
  83. // Draw generated font texture atlas containing provided codepoints
  84. DrawTexture(font.texture, 150, 100, BLACK);
  85. DrawRectangleLines(150, 100, font.texture.width, font.texture.height, BLACK);
  86. }
  87. else
  88. {
  89. // Draw provided text with laoded font, containing all required codepoint glyphs
  90. DrawTextEx(font, text, (Vector2) { 160, 110 }, 48, 5, BLACK);
  91. }
  92. DrawText("Press SPACE to toggle font atlas view!", 10, GetScreenHeight() - 30, 20, GRAY);
  93. EndDrawing();
  94. //----------------------------------------------------------------------------------
  95. }
  96. // De-Initialization
  97. //--------------------------------------------------------------------------------------
  98. UnloadFont(font); // Unload font
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }
  103. // Remove codepoint duplicates if requested
  104. // WARNING: This process could be a bit slow if there text to process is very long
  105. static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *codepointsResultCount)
  106. {
  107. int codepointsNoDupsCount = codepointCount;
  108. int *codepointsNoDups = (int *)calloc(codepointCount, sizeof(int));
  109. memcpy(codepointsNoDups, codepoints, codepointCount*sizeof(int));
  110. // Remove duplicates
  111. for (int i = 0; i < codepointsNoDupsCount; i++)
  112. {
  113. for (int j = i + 1; j < codepointsNoDupsCount; j++)
  114. {
  115. if (codepointsNoDups[i] == codepointsNoDups[j])
  116. {
  117. for (int k = j; k < codepointsNoDupsCount; k++) codepointsNoDups[k] = codepointsNoDups[k + 1];
  118. codepointsNoDupsCount--;
  119. j--;
  120. }
  121. }
  122. }
  123. // NOTE: The size of codepointsNoDups is the same as original array but
  124. // only required positions are filled (codepointsNoDupsCount)
  125. *codepointsResultCount = codepointsNoDupsCount;
  126. return codepointsNoDups;
  127. }