text_sprite_fonts.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - sprite fonts
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * NOTE: raylib is distributed with some free to use fonts (even for commercial pourposes!)
  8. * To view details and credits for those fonts, check raylib license file
  9. *
  10. * Example originally created with raylib 1.7, last time updated with raylib 3.7
  11. *
  12. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  13. * BSD-like license that allows static linking with closed source software
  14. *
  15. * Copyright (c) 2017-2025 Ramon Santamaria (@raysan5)
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. #define MAX_FONTS 8
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [text] example - sprite fonts");
  30. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  31. Font fonts[MAX_FONTS] = { 0 };
  32. fonts[0] = LoadFont("resources/sprite_fonts/alagard.png");
  33. fonts[1] = LoadFont("resources/sprite_fonts/pixelplay.png");
  34. fonts[2] = LoadFont("resources/sprite_fonts/mecha.png");
  35. fonts[3] = LoadFont("resources/sprite_fonts/setback.png");
  36. fonts[4] = LoadFont("resources/sprite_fonts/romulus.png");
  37. fonts[5] = LoadFont("resources/sprite_fonts/pixantiqua.png");
  38. fonts[6] = LoadFont("resources/sprite_fonts/alpha_beta.png");
  39. fonts[7] = LoadFont("resources/sprite_fonts/jupiter_crash.png");
  40. const char *messages[MAX_FONTS] = { "ALAGARD FONT designed by Hewett Tsoi",
  41. "PIXELPLAY FONT designed by Aleksander Shevchuk",
  42. "MECHA FONT designed by Captain Falcon",
  43. "SETBACK FONT designed by Brian Kent (AEnigma)",
  44. "ROMULUS FONT designed by Hewett Tsoi",
  45. "PIXANTIQUA FONT designed by Gerhard Grossmann",
  46. "ALPHA_BETA FONT designed by Brian Kent (AEnigma)",
  47. "JUPITER_CRASH FONT designed by Brian Kent (AEnigma)" };
  48. const int spacings[MAX_FONTS] = { 2, 4, 8, 4, 3, 4, 4, 1 };
  49. Vector2 positions[MAX_FONTS] = { 0 };
  50. for (int i = 0; i < MAX_FONTS; i++)
  51. {
  52. positions[i].x = screenWidth/2.0f - MeasureTextEx(fonts[i], messages[i], fonts[i].baseSize*2.0f, (float)spacings[i]).x/2.0f;
  53. positions[i].y = 60.0f + fonts[i].baseSize + 45.0f*i;
  54. }
  55. // Small Y position corrections
  56. positions[3].y += 8;
  57. positions[4].y += 2;
  58. positions[7].y -= 8;
  59. Color colors[MAX_FONTS] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED };
  60. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  61. //--------------------------------------------------------------------------------------
  62. // Main game loop
  63. while (!WindowShouldClose()) // Detect window close button or ESC key
  64. {
  65. // Update
  66. //----------------------------------------------------------------------------------
  67. // TODO: Update your variables here
  68. //----------------------------------------------------------------------------------
  69. // Draw
  70. //----------------------------------------------------------------------------------
  71. BeginDrawing();
  72. ClearBackground(RAYWHITE);
  73. DrawText("free sprite fonts included with raylib", 220, 20, 20, DARKGRAY);
  74. DrawLine(220, 50, 600, 50, DARKGRAY);
  75. for (int i = 0; i < MAX_FONTS; i++)
  76. {
  77. DrawTextEx(fonts[i], messages[i], positions[i], fonts[i].baseSize*2.0f, (float)spacings[i], colors[i]);
  78. }
  79. EndDrawing();
  80. //----------------------------------------------------------------------------------
  81. }
  82. // De-Initialization
  83. //--------------------------------------------------------------------------------------
  84. // Fonts unloading
  85. for (int i = 0; i < MAX_FONTS; i++) UnloadFont(fonts[i]);
  86. CloseWindow(); // Close window and OpenGL context
  87. //--------------------------------------------------------------------------------------
  88. return 0;
  89. }