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.

97 lines
4.4 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - Font loading
  4. *
  5. * NOTE: raylib can load fonts from multiple input file formats:
  6. *
  7. * - TTF/OTF > Sprite font atlas is generated on loading, user can configure
  8. * some of the generation parameters (size, characters to include)
  9. * - BMFonts > Angel code font fileformat, sprite font image must be provided
  10. * together with the .fnt file, font generation cna not be configured
  11. * - XNA Spritefont > Sprite font image, following XNA Spritefont conventions,
  12. * Characters in image must follow some spacing and order rules
  13. *
  14. * Example originally created with raylib 1.4, last time updated with raylib 3.0
  15. *
  16. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  17. * BSD-like license that allows static linking with closed source software
  18. *
  19. * Copyright (c) 2016-2024 Ramon Santamaria (@raysan5)
  20. *
  21. ********************************************************************************************/
  22. #include "raylib.h"
  23. //------------------------------------------------------------------------------------
  24. // Program main entry point
  25. //------------------------------------------------------------------------------------
  26. int main(void)
  27. {
  28. // Initialization
  29. //--------------------------------------------------------------------------------------
  30. const int screenWidth = 800;
  31. const int screenHeight = 450;
  32. InitWindow(screenWidth, screenHeight, "raylib [text] example - font loading");
  33. // Define characters to draw
  34. // NOTE: raylib supports UTF-8 encoding, following list is actually codified as UTF8 internally
  35. const char msg[256] = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHI\nJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmn\nopqrstuvwxyz{|}~¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓ\nÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷\nøùúûüýþÿ";
  36. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  37. // BMFont (AngelCode) : Font data and image atlas have been generated using external program
  38. Font fontBm = LoadFont("resources/pixantiqua.fnt");
  39. // TTF font : Font data and atlas are generated directly from TTF
  40. // NOTE: We define a font base size of 32 pixels tall and up-to 250 characters
  41. Font fontTtf = LoadFontEx("resources/pixantiqua.ttf", 32, 0, 250);
  42. SetTextLineSpacing(16); // Set line spacing for multiline text (when line breaks are included '\n')
  43. bool useTtf = false;
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. if (IsKeyDown(KEY_SPACE)) useTtf = true;
  52. else useTtf = false;
  53. //----------------------------------------------------------------------------------
  54. // Draw
  55. //----------------------------------------------------------------------------------
  56. BeginDrawing();
  57. ClearBackground(RAYWHITE);
  58. DrawText("Hold SPACE to use TTF generated font", 20, 20, 20, LIGHTGRAY);
  59. if (!useTtf)
  60. {
  61. DrawTextEx(fontBm, msg, (Vector2){ 20.0f, 100.0f }, (float)fontBm.baseSize, 2, MAROON);
  62. DrawText("Using BMFont (Angelcode) imported", 20, GetScreenHeight() - 30, 20, GRAY);
  63. }
  64. else
  65. {
  66. DrawTextEx(fontTtf, msg, (Vector2){ 20.0f, 100.0f }, (float)fontTtf.baseSize, 2, LIME);
  67. DrawText("Using TTF font generated", 20, GetScreenHeight() - 30, 20, GRAY);
  68. }
  69. EndDrawing();
  70. //----------------------------------------------------------------------------------
  71. }
  72. // De-Initialization
  73. //--------------------------------------------------------------------------------------
  74. UnloadFont(fontBm); // AngelCode Font unloading
  75. UnloadFont(fontTtf); // TTF Font unloading
  76. CloseWindow(); // Close window and OpenGL context
  77. //--------------------------------------------------------------------------------------
  78. return 0;
  79. }