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.

90 lines
4.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - Font loading
  4. *
  5. * raylib can load fonts from multiple 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. * This example has been created using raylib 2.6 (www.raylib.com)
  15. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  16. *
  17. * Copyright (c) 2016-2019 Ramon Santamaria (@raysan5)
  18. *
  19. ********************************************************************************************/
  20. #include "raylib.h"
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [text] example - font loading");
  28. // Define characters to draw
  29. // NOTE: raylib supports UTF-8 encoding, following list is actually codified as UTF8 internally
  30. const char msg[256] = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHI\nJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmn\nopqrstuvwxyz{|}~¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓ\nÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷\nøùúûüýþÿ";
  31. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  32. // BMFont (AngelCode) : Font data and image atlas have been generated using external program
  33. Font fontBm = LoadFont("resources/pixantiqua.fnt");
  34. // TTF font : Font data and atlas are generated directly from TTF
  35. // NOTE: We define a font base size of 32 pixels tall and up-to 250 characters
  36. Font fontTtf = LoadFontEx("resources/pixantiqua.ttf", 32, 0, 250);
  37. bool useTtf = false;
  38. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. if (IsKeyDown(KEY_SPACE)) useTtf = true;
  46. else useTtf = false;
  47. //----------------------------------------------------------------------------------
  48. // Draw
  49. //----------------------------------------------------------------------------------
  50. BeginDrawing();
  51. ClearBackground(RAYWHITE);
  52. DrawText("Hold SPACE to use TTF generated font", 20, 20, 20, LIGHTGRAY);
  53. if (!useTtf)
  54. {
  55. DrawTextEx(fontBm, msg, (Vector2){ 20.0f, 100.0f }, (float)fontBm.baseSize, 2, MAROON);
  56. DrawText("Using BMFont (Angelcode) imported", 20, GetScreenHeight() - 30, 20, GRAY);
  57. }
  58. else
  59. {
  60. DrawTextEx(fontTtf, msg, (Vector2){ 20.0f, 100.0f }, (float)fontTtf.baseSize, 2, LIME);
  61. DrawText("Using TTF font generated", 20, GetScreenHeight() - 30, 20, GRAY);
  62. }
  63. EndDrawing();
  64. //----------------------------------------------------------------------------------
  65. }
  66. // De-Initialization
  67. //--------------------------------------------------------------------------------------
  68. UnloadFont(fontBm); // AngelCode Font unloading
  69. UnloadFont(fontTtf); // TTF Font unloading
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }