2
0

text_codepoints_loading.c 6.3 KB

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