text_font_sdf.c 6.1 KB

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