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.

135 lines
5.2 KiB

6 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. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [text] example - ttf loading");
  19. const char msg[50] = "TTF Font";
  20. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  21. // TTF Font loading with custom generation parameters
  22. Font font = LoadFontEx("resources/KAISG.ttf", 96, 0, 0);
  23. // Generate mipmap levels to use trilinear filtering
  24. // NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR
  25. GenTextureMipmaps(&font.texture);
  26. float fontSize = font.baseSize;
  27. Vector2 fontPosition = { 40, screenHeight/2 - 80 };
  28. Vector2 textSize;
  29. SetTextureFilter(font.texture, FILTER_POINT);
  30. int currentFontFilter = 0; // FILTER_POINT
  31. // NOTE: Drag and drop support only available for desktop platforms: Windows, Linux, OSX
  32. #if defined(PLATFORM_DESKTOP)
  33. int count = 0;
  34. char **droppedFiles;
  35. #endif
  36. SetTargetFPS(60);
  37. //--------------------------------------------------------------------------------------
  38. // Main game loop
  39. while (!WindowShouldClose()) // Detect window close button or ESC key
  40. {
  41. // Update
  42. //----------------------------------------------------------------------------------
  43. fontSize += GetMouseWheelMove()*4.0f;
  44. // Choose font texture filter method
  45. if (IsKeyPressed(KEY_ONE))
  46. {
  47. SetTextureFilter(font.texture, FILTER_POINT);
  48. currentFontFilter = 0;
  49. }
  50. else if (IsKeyPressed(KEY_TWO))
  51. {
  52. SetTextureFilter(font.texture, FILTER_BILINEAR);
  53. currentFontFilter = 1;
  54. }
  55. else if (IsKeyPressed(KEY_THREE))
  56. {
  57. // NOTE: Trilinear filter won't be noticed on 2D drawing
  58. SetTextureFilter(font.texture, FILTER_TRILINEAR);
  59. currentFontFilter = 2;
  60. }
  61. textSize = MeasureTextEx(font, msg, fontSize, 0);
  62. if (IsKeyDown(KEY_LEFT)) fontPosition.x -= 10;
  63. else if (IsKeyDown(KEY_RIGHT)) fontPosition.x += 10;
  64. #if defined(PLATFORM_DESKTOP)
  65. // Load a dropped TTF file dynamically (at current fontSize)
  66. if (IsFileDropped())
  67. {
  68. droppedFiles = GetDroppedFiles(&count);
  69. if (count == 1) // Only support one ttf file dropped
  70. {
  71. UnloadFont(font);
  72. font = LoadFontEx(droppedFiles[0], fontSize, 0, 0);
  73. ClearDroppedFiles();
  74. }
  75. }
  76. #endif
  77. //----------------------------------------------------------------------------------
  78. // Draw
  79. //----------------------------------------------------------------------------------
  80. BeginDrawing();
  81. ClearBackground(RAYWHITE);
  82. DrawText("Use mouse wheel to change font size", 20, 20, 10, GRAY);
  83. DrawText("Use KEY_RIGHT and KEY_LEFT to move text", 20, 40, 10, GRAY);
  84. DrawText("Use 1, 2, 3 to change texture filter", 20, 60, 10, GRAY);
  85. DrawText("Drop a new TTF font for dynamic loading", 20, 80, 10, DARKGRAY);
  86. DrawTextEx(font, msg, fontPosition, fontSize, 0, BLACK);
  87. // TODO: It seems texSize measurement is not accurate due to chars offsets...
  88. //DrawRectangleLines(fontPosition.x, fontPosition.y, textSize.x, textSize.y, RED);
  89. DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY);
  90. DrawText(FormatText("Font size: %02.02f", fontSize), 20, screenHeight - 50, 10, DARKGRAY);
  91. DrawText(FormatText("Text size: [%02.02f, %02.02f]", textSize.x, textSize.y), 20, screenHeight - 30, 10, DARKGRAY);
  92. DrawText("CURRENT TEXTURE FILTER:", 250, 400, 20, GRAY);
  93. if (currentFontFilter == 0) DrawText("POINT", 570, 400, 20, BLACK);
  94. else if (currentFontFilter == 1) DrawText("BILINEAR", 570, 400, 20, BLACK);
  95. else if (currentFontFilter == 2) DrawText("TRILINEAR", 570, 400, 20, BLACK);
  96. EndDrawing();
  97. //----------------------------------------------------------------------------------
  98. }
  99. // De-Initialization
  100. //--------------------------------------------------------------------------------------
  101. #if defined(PLATFORM_DESKTOP)
  102. ClearDroppedFiles(); // Clear internal buffers
  103. #endif
  104. UnloadFont(font); // Font unloading
  105. CloseWindow(); // Close window and OpenGL context
  106. //--------------------------------------------------------------------------------------
  107. return 0;
  108. }