2
0

text_raylib_fonts.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - raylib bitmap font (rbmf) loading and usage (adapted for HTML5 platform)
  4. *
  5. * This example has been created using raylib 1.3 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #if defined(PLATFORM_WEB)
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. #define MAX_FONTS 8
  16. //----------------------------------------------------------------------------------
  17. // Global Variables Definition
  18. //----------------------------------------------------------------------------------
  19. int screenWidth = 800;
  20. int screenHeight = 450;
  21. SpriteFont fonts[MAX_FONTS];
  22. const int spacings[MAX_FONTS] = { 2, 4, 8, 4, 3, 4, 4, 1 };
  23. Vector2 positions[MAX_FONTS];
  24. Color *colors;
  25. const char (*messages)[64];
  26. //----------------------------------------------------------------------------------
  27. // Module Functions Declaration
  28. //----------------------------------------------------------------------------------
  29. void UpdateDrawFrame(void); // Update and Draw one frame
  30. //----------------------------------------------------------------------------------
  31. // Main Enry Point
  32. //----------------------------------------------------------------------------------
  33. int main()
  34. {
  35. // Initialization
  36. //--------------------------------------------------------------------------------------
  37. InitWindow(screenWidth, screenHeight, "raylib [text] example - raylib fonts");
  38. Color tempColors[MAX_FONTS] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED };
  39. colors = tempColors;
  40. const char tempMessages[MAX_FONTS][64] = { "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. messages = tempMessages;
  49. fonts[0] = LoadSpriteFont("resources/fonts/alagard.png");
  50. fonts[1] = LoadSpriteFont("resources/fonts/pixelplay.png");
  51. fonts[2] = LoadSpriteFont("resources/fonts/mecha.png");
  52. fonts[3] = LoadSpriteFont("resources/fonts/setback.png");
  53. fonts[4] = LoadSpriteFont("resources/fonts/romulus.png");
  54. fonts[5] = LoadSpriteFont("resources/fonts/pixantiqua.png");
  55. fonts[6] = LoadSpriteFont("resources/fonts/alpha_beta.png");
  56. fonts[7] = LoadSpriteFont("resources/fonts/jupiter_crash.png");
  57. for (int i = 0; i < MAX_FONTS; i++)
  58. {
  59. positions[i].x = screenWidth/2 - MeasureTextEx(fonts[i], messages[i], fonts[i].baseSize*2, spacings[i]).x/2;
  60. positions[i].y = 60 + fonts[i].baseSize + 45*i;
  61. }
  62. // Small Y position corrections
  63. positions[3].y += 8;
  64. positions[4].y += 2;
  65. positions[7].y -= 8;
  66. #if defined(PLATFORM_WEB)
  67. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  68. #else
  69. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  70. //--------------------------------------------------------------------------------------
  71. // Main game loop
  72. while (!WindowShouldClose()) // Detect window close button or ESC key
  73. {
  74. UpdateDrawFrame();
  75. }
  76. #endif
  77. // De-Initialization
  78. //--------------------------------------------------------------------------------------
  79. // SpriteFont unloading
  80. for (int i = 0; i < MAX_FONTS; i++) UnloadSpriteFont(fonts[i]);
  81. CloseWindow(); // Close window and OpenGL context
  82. //--------------------------------------------------------------------------------------
  83. return 0;
  84. }
  85. //----------------------------------------------------------------------------------
  86. // Module Functions Definition
  87. //----------------------------------------------------------------------------------
  88. void UpdateDrawFrame(void)
  89. {
  90. // Update
  91. //----------------------------------------------------------------------------------
  92. // TODO: Update your variables here
  93. //----------------------------------------------------------------------------------
  94. // Draw
  95. //----------------------------------------------------------------------------------
  96. BeginDrawing();
  97. ClearBackground(RAYWHITE);
  98. DrawText("free fonts included with raylib", 250, 20, 20, DARKGRAY);
  99. DrawLine(220, 50, 590, 50, DARKGRAY);
  100. for (int i = 0; i < 8; i++)
  101. {
  102. DrawTextEx(fonts[i], messages[i], positions[i], fonts[i].baseSize*2, spacings[i], colors[i]);
  103. }
  104. EndDrawing();
  105. //----------------------------------------------------------------------------------
  106. }