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.

128 regels
5.0 KiB

5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
6 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
7 jaren geleden
5 jaren geleden
5 jaren geleden
  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(void)
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. const int screenWidth = 800;
  17. const 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 = { 0.0f, 0.0f };
  29. // Setup texture scaling filter
  30. SetTextureFilter(font.texture, FILTER_POINT);
  31. int currentFontFilter = 0; // FILTER_POINT
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. fontSize += GetMouseWheelMove()*4.0f;
  40. // Choose font texture filter method
  41. if (IsKeyPressed(KEY_ONE))
  42. {
  43. SetTextureFilter(font.texture, FILTER_POINT);
  44. currentFontFilter = 0;
  45. }
  46. else if (IsKeyPressed(KEY_TWO))
  47. {
  48. SetTextureFilter(font.texture, FILTER_BILINEAR);
  49. currentFontFilter = 1;
  50. }
  51. else if (IsKeyPressed(KEY_THREE))
  52. {
  53. // NOTE: Trilinear filter won't be noticed on 2D drawing
  54. SetTextureFilter(font.texture, FILTER_TRILINEAR);
  55. currentFontFilter = 2;
  56. }
  57. textSize = MeasureTextEx(font, msg, fontSize, 0);
  58. if (IsKeyDown(KEY_LEFT)) fontPosition.x -= 10;
  59. else if (IsKeyDown(KEY_RIGHT)) fontPosition.x += 10;
  60. // Load a dropped TTF file dynamically (at current fontSize)
  61. if (IsFileDropped())
  62. {
  63. int count = 0;
  64. char **droppedFiles = GetDroppedFiles(&count);
  65. if (count == 1) // Only support one ttf file dropped
  66. {
  67. UnloadFont(font);
  68. font = LoadFontEx(droppedFiles[0], fontSize, 0, 0);
  69. ClearDroppedFiles();
  70. }
  71. }
  72. //----------------------------------------------------------------------------------
  73. // Draw
  74. //----------------------------------------------------------------------------------
  75. BeginDrawing();
  76. ClearBackground(RAYWHITE);
  77. DrawText("Use mouse wheel to change font size", 20, 20, 10, GRAY);
  78. DrawText("Use KEY_RIGHT and KEY_LEFT to move text", 20, 40, 10, GRAY);
  79. DrawText("Use 1, 2, 3 to change texture filter", 20, 60, 10, GRAY);
  80. DrawText("Drop a new TTF font for dynamic loading", 20, 80, 10, DARKGRAY);
  81. DrawTextEx(font, msg, fontPosition, fontSize, 0, BLACK);
  82. // TODO: It seems texSize measurement is not accurate due to chars offsets...
  83. //DrawRectangleLines(fontPosition.x, fontPosition.y, textSize.x, textSize.y, RED);
  84. DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY);
  85. DrawText(FormatText("Font size: %02.02f", fontSize), 20, screenHeight - 50, 10, DARKGRAY);
  86. DrawText(FormatText("Text size: [%02.02f, %02.02f]", textSize.x, textSize.y), 20, screenHeight - 30, 10, DARKGRAY);
  87. DrawText("CURRENT TEXTURE FILTER:", 250, 400, 20, GRAY);
  88. if (currentFontFilter == 0) DrawText("POINT", 570, 400, 20, BLACK);
  89. else if (currentFontFilter == 1) DrawText("BILINEAR", 570, 400, 20, BLACK);
  90. else if (currentFontFilter == 2) DrawText("TRILINEAR", 570, 400, 20, BLACK);
  91. EndDrawing();
  92. //----------------------------------------------------------------------------------
  93. }
  94. // De-Initialization
  95. //--------------------------------------------------------------------------------------
  96. ClearDroppedFiles(); // Clear internal buffers
  97. UnloadFont(font); // Font unloading
  98. CloseWindow(); // Close window and OpenGL context
  99. //--------------------------------------------------------------------------------------
  100. return 0;
  101. }