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.

130 lines
5.4 KiB

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