您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

95 行
4.3 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-2023 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. bool useTtf = false;
  43. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. if (IsKeyDown(KEY_SPACE)) useTtf = true;
  51. else useTtf = false;
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. DrawText("Hold SPACE to use TTF generated font", 20, 20, 20, LIGHTGRAY);
  58. if (!useTtf)
  59. {
  60. DrawTextEx(fontBm, msg, (Vector2){ 20.0f, 100.0f }, (float)fontBm.baseSize, 2, MAROON);
  61. DrawText("Using BMFont (Angelcode) imported", 20, GetScreenHeight() - 30, 20, GRAY);
  62. }
  63. else
  64. {
  65. DrawTextEx(fontTtf, msg, (Vector2){ 20.0f, 100.0f }, (float)fontTtf.baseSize, 2, LIME);
  66. DrawText("Using TTF font generated", 20, GetScreenHeight() - 30, 20, GRAY);
  67. }
  68. EndDrawing();
  69. //----------------------------------------------------------------------------------
  70. }
  71. // De-Initialization
  72. //--------------------------------------------------------------------------------------
  73. UnloadFont(fontBm); // AngelCode Font unloading
  74. UnloadFont(fontTtf); // TTF Font unloading
  75. CloseWindow(); // Close window and OpenGL context
  76. //--------------------------------------------------------------------------------------
  77. return 0;
  78. }