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.

102 lines
4.2 KiB

11 years ago
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - raylib font loading and usage
  4. *
  5. * NOTE: raylib is distributed with some free to use fonts (even for commercial pourposes!)
  6. * To view details and credits for those fonts, check raylib license file
  7. *
  8. * This example has been created using raylib 1.7 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2017 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #define MAX_FONTS 8
  16. int main()
  17. {
  18. // Initialization
  19. //--------------------------------------------------------------------------------------
  20. int screenWidth = 800;
  21. int screenHeight = 450;
  22. InitWindow(screenWidth, screenHeight, "raylib [text] example - raylib fonts");
  23. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  24. SpriteFont fonts[MAX_FONTS];
  25. fonts[0] = LoadSpriteFont("resources/fonts/alagard.png");
  26. fonts[1] = LoadSpriteFont("resources/fonts/pixelplay.png");
  27. fonts[2] = LoadSpriteFont("resources/fonts/mecha.png");
  28. fonts[3] = LoadSpriteFont("resources/fonts/setback.png");
  29. fonts[4] = LoadSpriteFont("resources/fonts/romulus.png");
  30. fonts[5] = LoadSpriteFont("resources/fonts/pixantiqua.png");
  31. fonts[6] = LoadSpriteFont("resources/fonts/alpha_beta.png");
  32. fonts[7] = LoadSpriteFont("resources/fonts/jupiter_crash.png");
  33. const char *messages[MAX_FONTS] = { "ALAGARD FONT designed by Hewett Tsoi",
  34. "PIXELPLAY FONT designed by Aleksander Shevchuk",
  35. "MECHA FONT designed by Captain Falcon",
  36. "SETBACK FONT designed by Brian Kent (AEnigma)",
  37. "ROMULUS FONT designed by Hewett Tsoi",
  38. "PIXANTIQUA FONT designed by Gerhard Grossmann",
  39. "ALPHA_BETA FONT designed by Brian Kent (AEnigma)",
  40. "JUPITER_CRASH FONT designed by Brian Kent (AEnigma)" };
  41. const int spacings[MAX_FONTS] = { 2, 4, 8, 4, 3, 4, 4, 1 };
  42. Vector2 positions[MAX_FONTS];
  43. for (int i = 0; i < MAX_FONTS; i++)
  44. {
  45. positions[i].x = screenWidth/2 - MeasureTextEx(fonts[i], messages[i], fonts[i].baseSize*2, spacings[i]).x/2;
  46. positions[i].y = 60 + fonts[i].baseSize + 45*i;
  47. }
  48. // Small Y position corrections
  49. positions[3].y += 8;
  50. positions[4].y += 2;
  51. positions[7].y -= 8;
  52. Color colors[MAX_FONTS] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED };
  53. //--------------------------------------------------------------------------------------
  54. // Main game loop
  55. while (!WindowShouldClose()) // Detect window close button or ESC key
  56. {
  57. // Update
  58. //----------------------------------------------------------------------------------
  59. // TODO: Update your variables here
  60. //----------------------------------------------------------------------------------
  61. // Draw
  62. //----------------------------------------------------------------------------------
  63. BeginDrawing();
  64. ClearBackground(RAYWHITE);
  65. DrawText("free fonts included with raylib", 250, 20, 20, DARKGRAY);
  66. DrawLine(220, 50, 590, 50, DARKGRAY);
  67. for (int i = 0; i < MAX_FONTS; i++)
  68. {
  69. DrawTextEx(fonts[i], messages[i], positions[i], fonts[i].baseSize*2, spacings[i], colors[i]);
  70. }
  71. EndDrawing();
  72. //----------------------------------------------------------------------------------
  73. }
  74. // De-Initialization
  75. //--------------------------------------------------------------------------------------
  76. // SpriteFonts unloading
  77. for (int i = 0; i < MAX_FONTS; i++) UnloadSpriteFont(fonts[i]);
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }