You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
5.3 KiB

  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, FONT_DEFAULT);
  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. Font fontSDF = { 0 };
  33. fontSDF.baseSize = 16;
  34. fontSDF.charsCount = 95;
  35. // Parameters > font size: 16, no chars array provided (0), chars count: 0 (defaults to 95)
  36. fontSDF.chars = LoadFontData("resources/AnonymousPro-Bold.ttf", 16, 0, 0, FONT_SDF);
  37. // Parameters > chars count: 95, font size: 16, chars padding in image: 0 px, pack method: 1 (Skyline algorythm)
  38. atlas = GenImageFontAtlas(fontSDF.chars, 95, 16, 0, 1);
  39. fontSDF.texture = LoadTextureFromImage(atlas);
  40. UnloadImage(atlas);
  41. // Load SDF required shader (we use default vertex shader)
  42. Shader shader = LoadShader(0, "resources/shaders/sdf.fs");
  43. SetTextureFilter(fontSDF.texture, FILTER_BILINEAR); // Required for SDF font
  44. Vector2 fontPosition = { 40, screenHeight/2 - 50 };
  45. Vector2 textSize = { 0.0f };
  46. float fontSize = 16.0f;
  47. int currentFont = 0; // 0 - fontDefault, 1 - fontSDF
  48. SetTargetFPS(60);
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. fontSize += GetMouseWheelMove()*8.0f;
  56. if (fontSize < 6) fontSize = 6;
  57. if (IsKeyDown(KEY_SPACE)) currentFont = 1;
  58. else currentFont = 0;
  59. if (currentFont == 0) textSize = MeasureTextEx(fontDefault, msg, fontSize, 0);
  60. else textSize = MeasureTextEx(fontSDF, msg, fontSize, 0);
  61. fontPosition.x = GetScreenWidth()/2 - textSize.x/2;
  62. fontPosition.y = GetScreenHeight()/2 - textSize.y/2 + 80;
  63. //----------------------------------------------------------------------------------
  64. // Draw
  65. //----------------------------------------------------------------------------------
  66. BeginDrawing();
  67. ClearBackground(RAYWHITE);
  68. if (currentFont == 1)
  69. {
  70. // NOTE: SDF fonts require a custom SDf shader to compute fragment color
  71. BeginShaderMode(shader); // Activate SDF font shader
  72. DrawTextEx(fontSDF, msg, fontPosition, fontSize, 0, BLACK);
  73. EndShaderMode(); // Activate our default shader for next drawings
  74. DrawTexture(fontSDF.texture, 10, 10, BLACK);
  75. }
  76. else
  77. {
  78. DrawTextEx(fontDefault, msg, fontPosition, fontSize, 0, BLACK);
  79. DrawTexture(fontDefault.texture, 10, 10, BLACK);
  80. }
  81. if (currentFont == 1) DrawText("SDF!", 320, 20, 80, RED);
  82. else DrawText("default font", 315, 40, 30, GRAY);
  83. DrawText("FONT SIZE: 16.0", GetScreenWidth() - 240, 20, 20, DARKGRAY);
  84. DrawText(FormatText("RENDER SIZE: %02.02f", fontSize), GetScreenWidth() - 240, 50, 20, DARKGRAY);
  85. DrawText("Use MOUSE WHEEL to SCALE TEXT!", GetScreenWidth() - 240, 90, 10, DARKGRAY);
  86. DrawText("PRESS SPACE to USE SDF FONT VERSION!", 340, GetScreenHeight() - 30, 20, MAROON);
  87. EndDrawing();
  88. //----------------------------------------------------------------------------------
  89. }
  90. // De-Initialization
  91. //--------------------------------------------------------------------------------------
  92. UnloadFont(fontDefault); // Default font unloading
  93. UnloadFont(fontSDF); // SDF font unloading
  94. UnloadShader(shader); // Unload SDF shader
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }