text_font_loading.c 4.4 KB

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