text_font_sdf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - TTF loading and usage
  4. *
  5. * This example has been created using raylib 1.3.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [text] example - SDF fonts");
  19. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  20. const char msg[50] = "Signed Distance Fields";
  21. // Default font generation from TTF font
  22. Font fontDefault = { 0 };
  23. fontDefault.baseSize = 16;
  24. fontDefault.charsCount = 95;
  25. // Parameters > font size: 16, no chars array provided (0), chars count: 95 (autogenerate chars array)
  26. fontDefault.chars = LoadFontData("resources/AnonymousPro-Bold.ttf", 16, 0, 95, false);
  27. // Parameters > chars count: 95, font size: 16, chars padding in image: 4 px, pack method: 0 (default)
  28. Image atlas = GenImageFontAtlas(fontDefault.chars, 95, 16, 4, 0);
  29. fontDefault.texture = LoadTextureFromImage(atlas);
  30. UnloadImage(atlas);
  31. // SDF font generation from TTF font
  32. // NOTE: SDF chars data is generated with LoadFontData(), it's just a bool option
  33. Font fontSDF = { 0 };
  34. fontSDF.baseSize = 16;
  35. fontSDF.charsCount = 95;
  36. // Parameters > font size: 16, no chars array provided (0), chars count: 0 (defaults to 95)
  37. fontSDF.chars = LoadFontData("resources/AnonymousPro-Bold.ttf", 16, 0, 0, true);
  38. // Parameters > chars count: 95, font size: 16, chars padding in image: 0 px, pack method: 1 (Skyline algorythm)
  39. atlas = GenImageFontAtlas(fontSDF.chars, 95, 16, 0, 1);
  40. fontSDF.texture = LoadTextureFromImage(atlas);
  41. UnloadImage(atlas);
  42. // Load SDF required shader (we use default vertex shader)
  43. Shader shader = LoadShader(0, "resources/shaders/sdf.fs");
  44. SetTextureFilter(fontSDF.texture, FILTER_BILINEAR); // Required for SDF font
  45. Vector2 fontPosition = { 40, screenHeight/2 - 50 };
  46. Vector2 textSize = { 0.0f };
  47. float fontSize = 16.0f;
  48. int currentFont = 0; // 0 - fontDefault, 1 - fontSDF
  49. SetTargetFPS(60);
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. fontSize += GetMouseWheelMove()*8.0f;
  57. if (fontSize < 6) fontSize = 6;
  58. if (IsKeyDown(KEY_SPACE)) currentFont = 1;
  59. else currentFont = 0;
  60. if (currentFont == 0) textSize = MeasureTextEx(fontDefault, msg, fontSize, 0);
  61. else textSize = MeasureTextEx(fontSDF, msg, fontSize, 0);
  62. fontPosition.x = GetScreenWidth()/2 - textSize.x/2;
  63. fontPosition.y = GetScreenHeight()/2 - textSize.y/2 + 80;
  64. //----------------------------------------------------------------------------------
  65. // Draw
  66. //----------------------------------------------------------------------------------
  67. BeginDrawing();
  68. ClearBackground(RAYWHITE);
  69. if (currentFont == 1)
  70. {
  71. // NOTE: SDF fonts require a custom SDf shader to compute fragment color
  72. BeginShaderMode(shader); // Activate SDF font shader
  73. DrawTextEx(fontSDF, msg, fontPosition, fontSize, 0, BLACK);
  74. EndShaderMode(); // Activate our default shader for next drawings
  75. DrawTexture(fontSDF.texture, 10, 10, BLACK);
  76. }
  77. else
  78. {
  79. DrawTextEx(fontDefault, msg, fontPosition, fontSize, 0, BLACK);
  80. DrawTexture(fontDefault.texture, 10, 10, BLACK);
  81. }
  82. if (currentFont == 1) DrawText("SDF!", 320, 20, 80, RED);
  83. else DrawText("default font", 315, 40, 30, GRAY);
  84. DrawText("FONT SIZE: 16.0", GetScreenWidth() - 240, 20, 20, DARKGRAY);
  85. DrawText(FormatText("RENDER SIZE: %02.02f", fontSize), GetScreenWidth() - 240, 50, 20, DARKGRAY);
  86. DrawText("Use MOUSE WHEEL to SCALE TEXT!", GetScreenWidth() - 240, 90, 10, DARKGRAY);
  87. DrawText("PRESS SPACE to USE SDF FONT VERSION!", 340, GetScreenHeight() - 30, 20, MAROON);
  88. EndDrawing();
  89. //----------------------------------------------------------------------------------
  90. }
  91. // De-Initialization
  92. //--------------------------------------------------------------------------------------
  93. UnloadFont(fontDefault); // Default font unloading
  94. UnloadFont(fontSDF); // SDF font unloading
  95. UnloadShader(shader); // Unload SDF shader
  96. CloseWindow(); // Close window and OpenGL context
  97. //--------------------------------------------------------------------------------------
  98. return 0;
  99. }