text_ttf_loading.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [text] example - TTF loading and usage
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. -- Initialization
  12. -------------------------------------------------------------------------------------------
  13. local screenWidth = 800;
  14. local screenHeight = 450;
  15. InitWindow(screenWidth, screenHeight, "raylib [text] example - ttf loading")
  16. local msg = "TTF SpriteFont"
  17. -- NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  18. -- TTF SpriteFont loading with custom generation parameters
  19. local font = LoadSpriteFontTTF("resources/fonts/KAISG.ttf", 96, 0, 0)
  20. -- Generate mipmap levels to use trilinear filtering
  21. -- NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR
  22. --font.texture = GenTextureMipmaps(font.texture) -- ISSUE: attempt to index a SpriteFont value (local 'font')
  23. local fontSize = font.size
  24. local fontPosition = Vector2(40, screenHeight/2 + 50)
  25. local textSize
  26. SetTextureFilter(font.texture, TextureFilter.POINT)
  27. local currentFontFilter = 0 -- Default: FILTER_POINT
  28. local count = 0
  29. local droppedFiles
  30. SetTargetFPS(60)
  31. -------------------------------------------------------------------------------------------
  32. -- Main game loop
  33. while not WindowShouldClose() do -- Detect window close button or ESC key
  34. -- Update
  35. ---------------------------------------------------------------------------------------
  36. fontSize = fontSize + GetMouseWheelMove()*4.0
  37. -- Choose font texture filter method
  38. if (IsKeyPressed(KEY.ONE)) then
  39. SetTextureFilter(font.texture, TextureFilter.POINT)
  40. currentFontFilter = 0
  41. elseif (IsKeyPressed(KEY.TWO)) then
  42. SetTextureFilter(font.texture, TextureFilter.BILINEAR)
  43. currentFontFilter = 1
  44. elseif (IsKeyPressed(KEY.THREE)) then
  45. -- NOTE: Trilinear filter won't be noticed on 2D drawing
  46. SetTextureFilter(font.texture, TextureFilter.TRILINEAR)
  47. currentFontFilter = 2
  48. end
  49. textSize = MeasureTextEx(font, msg, fontSize, 0)
  50. if (IsKeyDown(KEY.LEFT)) then fontPosition.x = fontPosition.x - 10
  51. elseif (IsKeyDown(KEY.RIGHT)) then fontPosition.x = fontPosition.x + 10
  52. end
  53. -- Load a dropped TTF file dynamically (at current fontSize)
  54. if (IsFileDropped()) then
  55. droppedFiles = GetDroppedFiles()
  56. count = #droppedFiles
  57. if (count == 1) then -- Only support one ttf file dropped
  58. UnloadSpriteFont(font)
  59. font = LoadSpriteFontTTF(droppedFiles[1], fontSize, 0, 0)
  60. ClearDroppedFiles()
  61. end
  62. end
  63. ---------------------------------------------------------------------------------------
  64. -- Draw
  65. ---------------------------------------------------------------------------------------
  66. BeginDrawing()
  67. ClearBackground(RAYWHITE)
  68. DrawText("Use mouse wheel to change font size", 20, 20, 10, GRAY)
  69. DrawText("Use KEY_RIGHT and KEY_LEFT to move text", 20, 40, 10, GRAY)
  70. DrawText("Use 1, 2, 3 to change texture filter", 20, 60, 10, GRAY)
  71. DrawText("Drop a new TTF font for dynamic loading", 20, 80, 10, DARKGRAY)
  72. DrawTextEx(font, msg, fontPosition, fontSize, 0, BLACK)
  73. -- TODO: It seems texSize measurement is not accurate due to chars offsets...
  74. --DrawRectangleLines(fontPosition.x, fontPosition.y, textSize.x, textSize.y, RED)
  75. DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY)
  76. DrawText(string.format("Font size: %02.02f", fontSize), 20, screenHeight - 50, 10, DARKGRAY)
  77. DrawText(string.format("Text size: [%02.02f, %02.02f]", textSize.x, textSize.y), 20, screenHeight - 30, 10, DARKGRAY)
  78. DrawText("CURRENT TEXTURE FILTER:", 250, 400, 20, GRAY)
  79. if (currentFontFilter == 0) then DrawText("POINT", 570, 400, 20, BLACK)
  80. elseif (currentFontFilter == 1) then DrawText("BILINEAR", 570, 400, 20, BLACK)
  81. elseif (currentFontFilter == 2) then DrawText("TRILINEAR", 570, 400, 20, BLACK)
  82. end
  83. EndDrawing()
  84. ---------------------------------------------------------------------------------------
  85. end
  86. -- De-Initialization
  87. -------------------------------------------------------------------------------------------
  88. UnloadSpriteFont(font) -- SpriteFont unloading
  89. ClearDroppedFiles() -- Clear internal buffers
  90. CloseWindow() -- Close window and OpenGL context
  91. -------------------------------------------------------------------------------------------