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.

140 line
5.7 KiB

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