text_sprite_fonts.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - SpriteFont loading and usage (adapted for HTML5 platform)
  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) 2015 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #if defined(PLATFORM_WEB)
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. //----------------------------------------------------------------------------------
  16. // Global Variables Definition
  17. //----------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. const char msg1[50] = "THIS IS A custom SPRITE FONT...";
  21. const char msg2[50] = "...and this is ANOTHER CUSTOM font...";
  22. const char msg3[50] = "...and a THIRD one! GREAT! :D";
  23. SpriteFont font1;
  24. SpriteFont font2;
  25. SpriteFont font3;
  26. Vector2 fontPosition1, fontPosition2, fontPosition3;
  27. //----------------------------------------------------------------------------------
  28. // Module Functions Declaration
  29. //----------------------------------------------------------------------------------
  30. void UpdateDrawFrame(void); // Update and Draw one frame
  31. //----------------------------------------------------------------------------------
  32. // Main Enry Point
  33. //----------------------------------------------------------------------------------
  34. int main()
  35. {
  36. // Initialization
  37. //--------------------------------------------------------------------------------------
  38. InitWindow(screenWidth, screenHeight, "raylib [text] example - sprite fonts usage");
  39. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  40. font1 = LoadSpriteFont("resources/custom_mecha.png"); // SpriteFont loading
  41. font2 = LoadSpriteFont("resources/custom_alagard.png"); // SpriteFont loading
  42. font3 = LoadSpriteFont("resources/custom_jupiter_crash.png"); // SpriteFont loading
  43. fontPosition1.x = screenWidth/2 - MeasureTextEx(font1, msg1, font1.baseSize, -3).x/2;
  44. fontPosition1.y = screenHeight/2 - font1.baseSize/2 - 80;
  45. fontPosition2.x = screenWidth/2 - MeasureTextEx(font2, msg2, font2.baseSize, -2).x/2;
  46. fontPosition2.y = screenHeight/2 - font2.baseSize/2 - 10;
  47. fontPosition3.x = screenWidth/2 - MeasureTextEx(font3, msg3, font3.baseSize, 2).x/2;
  48. fontPosition3.y = screenHeight/2 - font3.baseSize/2 + 50;
  49. #if defined(PLATFORM_WEB)
  50. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  51. #else
  52. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  53. //--------------------------------------------------------------------------------------
  54. // Main game loop
  55. while (!WindowShouldClose()) // Detect window close button or ESC key
  56. {
  57. UpdateDrawFrame();
  58. }
  59. #endif
  60. // De-Initialization
  61. //--------------------------------------------------------------------------------------
  62. UnloadSpriteFont(font1); // SpriteFont unloading
  63. UnloadSpriteFont(font2); // SpriteFont unloading
  64. UnloadSpriteFont(font3); // SpriteFont unloading
  65. CloseWindow(); // Close window and OpenGL context
  66. //--------------------------------------------------------------------------------------
  67. return 0;
  68. }
  69. //----------------------------------------------------------------------------------
  70. // Module Functions Definition
  71. //----------------------------------------------------------------------------------
  72. void UpdateDrawFrame(void)
  73. {
  74. // Update
  75. //----------------------------------------------------------------------------------
  76. // TODO: Update your variables here
  77. //----------------------------------------------------------------------------------
  78. // Draw
  79. //----------------------------------------------------------------------------------
  80. BeginDrawing();
  81. ClearBackground(RAYWHITE);
  82. DrawTextEx(font1, msg1, fontPosition1, font1.baseSize, -3, WHITE);
  83. DrawTextEx(font2, msg2, fontPosition2, font2.baseSize, -2, WHITE);
  84. DrawTextEx(font3, msg3, fontPosition3, font3.baseSize, 2, WHITE);
  85. EndDrawing();
  86. //----------------------------------------------------------------------------------
  87. }