選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

145 行
6.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - Font SDF loading
  4. *
  5. * Example originally created with raylib 1.3, last time updated with raylib 4.0
  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) 2015-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #if defined(PLATFORM_DESKTOP)
  15. #define GLSL_VERSION 330
  16. #else // PLATFORM_ANDROID, PLATFORM_WEB
  17. #define GLSL_VERSION 100
  18. #endif
  19. #include <stdlib.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 [text] example - SDF fonts");
  30. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  31. const char msg[50] = "Signed Distance Fields";
  32. // Loading file to memory
  33. int fileSize = 0;
  34. unsigned char *fileData = LoadFileData("resources/anonymous_pro_bold.ttf", &fileSize);
  35. // Default font generation from TTF font
  36. Font fontDefault = { 0 };
  37. fontDefault.baseSize = 16;
  38. fontDefault.glyphCount = 95;
  39. // Loading font data from memory data
  40. // Parameters > font size: 16, no glyphs array provided (0), glyphs count: 95 (autogenerate chars array)
  41. fontDefault.glyphs = LoadFontData(fileData, fileSize, 16, 0, 95, FONT_DEFAULT);
  42. // Parameters > glyphs count: 95, font size: 16, glyphs padding in image: 4 px, pack method: 0 (default)
  43. Image atlas = GenImageFontAtlas(fontDefault.glyphs, &fontDefault.recs, 95, 16, 4, 0);
  44. fontDefault.texture = LoadTextureFromImage(atlas);
  45. UnloadImage(atlas);
  46. // SDF font generation from TTF font
  47. Font fontSDF = { 0 };
  48. fontSDF.baseSize = 16;
  49. fontSDF.glyphCount = 95;
  50. // Parameters > font size: 16, no glyphs array provided (0), glyphs count: 0 (defaults to 95)
  51. fontSDF.glyphs = LoadFontData(fileData, fileSize, 16, 0, 0, FONT_SDF);
  52. // Parameters > glyphs count: 95, font size: 16, glyphs padding in image: 0 px, pack method: 1 (Skyline algorythm)
  53. atlas = GenImageFontAtlas(fontSDF.glyphs, &fontSDF.recs, 95, 16, 0, 1);
  54. fontSDF.texture = LoadTextureFromImage(atlas);
  55. UnloadImage(atlas);
  56. UnloadFileData(fileData); // Free memory from loaded file
  57. // Load SDF required shader (we use default vertex shader)
  58. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/sdf.fs", GLSL_VERSION));
  59. SetTextureFilter(fontSDF.texture, TEXTURE_FILTER_BILINEAR); // Required for SDF font
  60. Vector2 fontPosition = { 40, screenHeight/2.0f - 50 };
  61. Vector2 textSize = { 0.0f, 0.0f };
  62. float fontSize = 16.0f;
  63. int currentFont = 0; // 0 - fontDefault, 1 - fontSDF
  64. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  65. //--------------------------------------------------------------------------------------
  66. // Main game loop
  67. while (!WindowShouldClose()) // Detect window close button or ESC key
  68. {
  69. // Update
  70. //----------------------------------------------------------------------------------
  71. fontSize += GetMouseWheelMove()*8.0f;
  72. if (fontSize < 6) fontSize = 6;
  73. if (IsKeyDown(KEY_SPACE)) currentFont = 1;
  74. else currentFont = 0;
  75. if (currentFont == 0) textSize = MeasureTextEx(fontDefault, msg, fontSize, 0);
  76. else textSize = MeasureTextEx(fontSDF, msg, fontSize, 0);
  77. fontPosition.x = GetScreenWidth()/2 - textSize.x/2;
  78. fontPosition.y = GetScreenHeight()/2 - textSize.y/2 + 80;
  79. //----------------------------------------------------------------------------------
  80. // Draw
  81. //----------------------------------------------------------------------------------
  82. BeginDrawing();
  83. ClearBackground(RAYWHITE);
  84. if (currentFont == 1)
  85. {
  86. // NOTE: SDF fonts require a custom SDf shader to compute fragment color
  87. BeginShaderMode(shader); // Activate SDF font shader
  88. DrawTextEx(fontSDF, msg, fontPosition, fontSize, 0, BLACK);
  89. EndShaderMode(); // Activate our default shader for next drawings
  90. DrawTexture(fontSDF.texture, 10, 10, BLACK);
  91. }
  92. else
  93. {
  94. DrawTextEx(fontDefault, msg, fontPosition, fontSize, 0, BLACK);
  95. DrawTexture(fontDefault.texture, 10, 10, BLACK);
  96. }
  97. if (currentFont == 1) DrawText("SDF!", 320, 20, 80, RED);
  98. else DrawText("default font", 315, 40, 30, GRAY);
  99. DrawText("FONT SIZE: 16.0", GetScreenWidth() - 240, 20, 20, DARKGRAY);
  100. DrawText(TextFormat("RENDER SIZE: %02.02f", fontSize), GetScreenWidth() - 240, 50, 20, DARKGRAY);
  101. DrawText("Use MOUSE WHEEL to SCALE TEXT!", GetScreenWidth() - 240, 90, 10, DARKGRAY);
  102. DrawText("HOLD SPACE to USE SDF FONT VERSION!", 340, GetScreenHeight() - 30, 20, MAROON);
  103. EndDrawing();
  104. //----------------------------------------------------------------------------------
  105. }
  106. // De-Initialization
  107. //--------------------------------------------------------------------------------------
  108. UnloadFont(fontDefault); // Default font unloading
  109. UnloadFont(fontSDF); // SDF font unloading
  110. UnloadShader(shader); // Unload SDF shader
  111. CloseWindow(); // Close window and OpenGL context
  112. //--------------------------------------------------------------------------------------
  113. return 0;
  114. }