2
0

text_codepoints_loading.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 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. // Free codepoints, atlas has already been generated
  45. free(codepointsNoDups);
  46. bool showFontAtlas = false;
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. if (IsKeyPressed(KEY_SPACE)) showFontAtlas = !showFontAtlas;
  55. //----------------------------------------------------------------------------------
  56. // Draw
  57. //----------------------------------------------------------------------------------
  58. BeginDrawing();
  59. ClearBackground(RAYWHITE);
  60. DrawRectangle(0, 0, GetScreenWidth(), 70, BLACK);
  61. DrawText(TextFormat("Total codepoints contained in provided text: %i", codepointCount), 10, 10, 20, GREEN);
  62. DrawText(TextFormat("Total codepoints required for font atlas (duplicates excluded): %i", codepointsNoDupsCount), 10, 40, 20, GREEN);
  63. if (showFontAtlas)
  64. {
  65. // Draw generated font texture atlas containing provided codepoints
  66. DrawTexture(font.texture, 150, 100, BLACK);
  67. DrawRectangleLines(150, 100, font.texture.width, font.texture.height, BLACK);
  68. }
  69. else
  70. {
  71. // Draw provided text with laoded font, containing all required codepoint glyphs
  72. DrawTextEx(font, text, (Vector2) { 160, 110 }, 48, 5, BLACK);
  73. }
  74. DrawText("Press SPACE to toggle font atlas view!", 10, GetScreenHeight() - 30, 20, GRAY);
  75. EndDrawing();
  76. //----------------------------------------------------------------------------------
  77. }
  78. // De-Initialization
  79. //--------------------------------------------------------------------------------------
  80. UnloadFont(font); // Unload font
  81. CloseWindow(); // Close window and OpenGL context
  82. //--------------------------------------------------------------------------------------
  83. return 0;
  84. }
  85. // Remove codepoint duplicates if requested
  86. // WARNING: This process could be a bit slow if there text to process is very long
  87. static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *codepointsResultCount)
  88. {
  89. int codepointsNoDupsCount = codepointCount;
  90. int *codepointsNoDups = (int *)calloc(codepointCount, sizeof(int));
  91. memcpy(codepointsNoDups, codepoints, codepointCount*sizeof(int));
  92. // Remove duplicates
  93. for (int i = 0; i < codepointsNoDupsCount; i++)
  94. {
  95. for (int j = i + 1; j < codepointsNoDupsCount; j++)
  96. {
  97. if (codepointsNoDups[i] == codepointsNoDups[j])
  98. {
  99. for (int k = j; k < codepointsNoDupsCount; k++) codepointsNoDups[k] = codepointsNoDups[k + 1];
  100. codepointsNoDupsCount--;
  101. j--;
  102. }
  103. }
  104. }
  105. // NOTE: The size of codepointsNoDups is the same as original array but
  106. // only required positions are filled (codepointsNoDupsCount)
  107. *codepointsResultCount = codepointsNoDupsCount;
  108. return codepointsNoDups;
  109. }