Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

132 rindas
5.5 KiB

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