text_sprite_fonts.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - Font loading and usage
  4. *
  5. * This example has been created using raylib 1.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main(void)
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. const int screenWidth = 800;
  17. const int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [text] example - sprite fonts usage");
  19. const char msg1[50] = "THIS IS A custom SPRITE FONT...";
  20. const char msg2[50] = "...and this is ANOTHER CUSTOM font...";
  21. const char msg3[50] = "...and a THIRD one! GREAT! :D";
  22. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  23. Font font1 = LoadFont("resources/custom_mecha.png"); // Font loading
  24. Font font2 = LoadFont("resources/custom_alagard.png"); // Font loading
  25. Font font3 = LoadFont("resources/custom_jupiter_crash.png"); // Font loading
  26. Vector2 fontPosition1 = { screenWidth/2 - MeasureTextEx(font1, msg1, font1.baseSize, -3).x/2,
  27. screenHeight/2 - font1.baseSize/2 - 80 };
  28. Vector2 fontPosition2 = { screenWidth/2 - MeasureTextEx(font2, msg2, font2.baseSize, -2).x/2,
  29. screenHeight/2 - font2.baseSize/2 - 10 };
  30. Vector2 fontPosition3 = { screenWidth/2 - MeasureTextEx(font3, msg3, font3.baseSize, 2).x/2,
  31. screenHeight/2 - font3.baseSize/2 + 50 };
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. // TODO: Update variables here...
  40. //----------------------------------------------------------------------------------
  41. // Draw
  42. //----------------------------------------------------------------------------------
  43. BeginDrawing();
  44. ClearBackground(RAYWHITE);
  45. DrawTextEx(font1, msg1, fontPosition1, font1.baseSize, -3, WHITE);
  46. DrawTextEx(font2, msg2, fontPosition2, font2.baseSize, -2, WHITE);
  47. DrawTextEx(font3, msg3, fontPosition3, font3.baseSize, 2, WHITE);
  48. EndDrawing();
  49. //----------------------------------------------------------------------------------
  50. }
  51. // De-Initialization
  52. //--------------------------------------------------------------------------------------
  53. UnloadFont(font1); // Font unloading
  54. UnloadFont(font2); // Font unloading
  55. UnloadFont(font3); // Font unloading
  56. CloseWindow(); // Close window and OpenGL context
  57. //--------------------------------------------------------------------------------------
  58. return 0;
  59. }