text_font_spritefont.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - font spritefont
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * NOTE: Sprite fonts should be generated following this conventions:
  8. *
  9. * - Characters must be ordered starting with character 32 (Space)
  10. * - Every character must be contained within the same Rectangle height
  11. * - Every character and every line must be separated by the same distance (margin/padding)
  12. * - Rectangles must be defined by a MAGENTA color background
  13. *
  14. * Following those constraints, a font can be provided just by an image,
  15. * this is quite handy to avoid additional font descriptor files (like BMFonts use)
  16. *
  17. * Example originally created with raylib 1.0, last time updated with raylib 1.0
  18. *
  19. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  20. * BSD-like license that allows static linking with closed source software
  21. *
  22. * Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
  23. *
  24. ********************************************************************************************/
  25. #include "raylib.h"
  26. //------------------------------------------------------------------------------------
  27. // Program main entry point
  28. //------------------------------------------------------------------------------------
  29. int main(void)
  30. {
  31. // Initialization
  32. //--------------------------------------------------------------------------------------
  33. const int screenWidth = 800;
  34. const int screenHeight = 450;
  35. InitWindow(screenWidth, screenHeight, "raylib [text] example - font spritefont");
  36. const char msg1[50] = "THIS IS A custom SPRITE FONT...";
  37. const char msg2[50] = "...and this is ANOTHER CUSTOM font...";
  38. const char msg3[50] = "...and a THIRD one! GREAT! :D";
  39. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  40. Font font1 = LoadFont("resources/custom_mecha.png"); // Font loading
  41. Font font2 = LoadFont("resources/custom_alagard.png"); // Font loading
  42. Font font3 = LoadFont("resources/custom_jupiter_crash.png"); // Font loading
  43. Vector2 fontPosition1 = { screenWidth/2.0f - MeasureTextEx(font1, msg1, (float)font1.baseSize, -3).x/2,
  44. screenHeight/2.0f - font1.baseSize/2.0f - 80.0f };
  45. Vector2 fontPosition2 = { screenWidth/2.0f - MeasureTextEx(font2, msg2, (float)font2.baseSize, -2.0f).x/2.0f,
  46. screenHeight/2.0f - font2.baseSize/2.0f - 10.0f };
  47. Vector2 fontPosition3 = { screenWidth/2.0f - MeasureTextEx(font3, msg3, (float)font3.baseSize, 2.0f).x/2.0f,
  48. screenHeight/2.0f - font3.baseSize/2.0f + 50.0f };
  49. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. // TODO: Update variables here...
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. DrawTextEx(font1, msg1, fontPosition1, (float)font1.baseSize, -3, WHITE);
  63. DrawTextEx(font2, msg2, fontPosition2, (float)font2.baseSize, -2, WHITE);
  64. DrawTextEx(font3, msg3, fontPosition3, (float)font3.baseSize, 2, WHITE);
  65. EndDrawing();
  66. //----------------------------------------------------------------------------------
  67. }
  68. // De-Initialization
  69. //--------------------------------------------------------------------------------------
  70. UnloadFont(font1); // Font unloading
  71. UnloadFont(font2); // Font unloading
  72. UnloadFont(font3); // Font unloading
  73. CloseWindow(); // Close window and OpenGL context
  74. //--------------------------------------------------------------------------------------
  75. return 0;
  76. }