text_ttf_loading.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_WEB)
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. //----------------------------------------------------------------------------------
  16. // Global Variables Definition
  17. //----------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. const char msg[50] = "TTF SpriteFont";
  21. SpriteFont font;
  22. float fontSize;
  23. Vector2 fontPosition = { 40, screenHeight/2 + 50 };
  24. Vector2 textSize;
  25. int currentFontFilter = 0; // FILTER_POINT
  26. #if !defined(PLATFORM_WEB)
  27. int count = 0;
  28. char **droppedFiles;
  29. #endif
  30. //----------------------------------------------------------------------------------
  31. // Module Functions Declaration
  32. //----------------------------------------------------------------------------------
  33. void UpdateDrawFrame(void); // Update and Draw one frame
  34. //----------------------------------------------------------------------------------
  35. // Main Enry Point
  36. //----------------------------------------------------------------------------------
  37. int main()
  38. {
  39. // Initialization
  40. //--------------------------------------------------------------------------------------
  41. InitWindow(screenWidth, screenHeight, "raylib [text] example - ttf loading");
  42. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  43. // TTF SpriteFont loading with custom generation parameters
  44. font = LoadSpriteFontEx("resources/KAISG.ttf", 96, 0, 0);
  45. // Generate mipmap levels to use trilinear filtering
  46. // NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR
  47. GenTextureMipmaps(&font.texture);
  48. fontSize = font.baseSize;
  49. SetTextureFilter(font.texture, FILTER_POINT);
  50. #if defined(PLATFORM_WEB)
  51. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  52. #else
  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. UpdateDrawFrame();
  59. }
  60. #endif
  61. // De-Initialization
  62. //--------------------------------------------------------------------------------------
  63. UnloadSpriteFont(font); // SpriteFont unloading
  64. #if !defined(PLATFORM_WEB)
  65. ClearDroppedFiles(); // Clear internal buffers
  66. #endif
  67. CloseWindow(); // Close window and OpenGL context
  68. //--------------------------------------------------------------------------------------
  69. return 0;
  70. }
  71. //----------------------------------------------------------------------------------
  72. // Module Functions Definition
  73. //----------------------------------------------------------------------------------
  74. void UpdateDrawFrame(void)
  75. {
  76. // Update
  77. //----------------------------------------------------------------------------------
  78. fontSize += GetMouseWheelMove()*4.0f;
  79. // Choose font texture filter method
  80. if (IsKeyPressed(KEY_ONE))
  81. {
  82. SetTextureFilter(font.texture, FILTER_POINT);
  83. currentFontFilter = 0;
  84. }
  85. else if (IsKeyPressed(KEY_TWO))
  86. {
  87. SetTextureFilter(font.texture, FILTER_BILINEAR);
  88. currentFontFilter = 1;
  89. }
  90. else if (IsKeyPressed(KEY_THREE))
  91. {
  92. // NOTE: Trilinear filter won't be noticed on 2D drawing
  93. SetTextureFilter(font.texture, FILTER_TRILINEAR);
  94. currentFontFilter = 2;
  95. }
  96. textSize = MeasureTextEx(font, msg, fontSize, 0);
  97. if (IsKeyDown(KEY_LEFT)) fontPosition.x -= 10;
  98. else if (IsKeyDown(KEY_RIGHT)) fontPosition.x += 10;
  99. #if !defined(PLATFORM_WEB)
  100. // Load a dropped TTF file dynamically (at current fontSize)
  101. if (IsFileDropped())
  102. {
  103. droppedFiles = GetDroppedFiles(&count);
  104. if (count == 1) // Only support one ttf file dropped
  105. {
  106. UnloadSpriteFont(font);
  107. font = LoadSpriteFontEx(droppedFiles[0], fontSize, 0, 0);
  108. ClearDroppedFiles();
  109. }
  110. }
  111. #endif
  112. //----------------------------------------------------------------------------------
  113. // Draw
  114. //----------------------------------------------------------------------------------
  115. BeginDrawing();
  116. ClearBackground(RAYWHITE);
  117. DrawText("Use mouse wheel to change font size", 20, 20, 10, GRAY);
  118. DrawText("Use KEY_RIGHT and KEY_LEFT to move text", 20, 40, 10, GRAY);
  119. DrawText("Use 1, 2, 3 to change texture filter", 20, 60, 10, GRAY);
  120. DrawText("Drop a new TTF font for dynamic loading", 20, 80, 10, DARKGRAY);
  121. DrawTextEx(font, msg, fontPosition, fontSize, 0, BLACK);
  122. // TODO: It seems texSize measurement is not accurate due to chars offsets...
  123. //DrawRectangleLines(fontPosition.x, fontPosition.y, textSize.x, textSize.y, RED);
  124. DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY);
  125. DrawText(FormatText("Font size: %02.02f", fontSize), 20, screenHeight - 50, 10, DARKGRAY);
  126. DrawText(FormatText("Text size: [%02.02f, %02.02f]", textSize.x, textSize.y), 20, screenHeight - 30, 10, DARKGRAY);
  127. DrawText("CURRENT TEXTURE FILTER:", 250, 400, 20, GRAY);
  128. if (currentFontFilter == 0) DrawText("POINT", 570, 400, 20, BLACK);
  129. else if (currentFontFilter == 1) DrawText("BILINEAR", 570, 400, 20, BLACK);
  130. else if (currentFontFilter == 2) DrawText("TRILINEAR", 570, 400, 20, BLACK);
  131. EndDrawing();
  132. //----------------------------------------------------------------------------------
  133. }