text_bmfont_ttf.c 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - BMFont and TTF SpriteFonts loading (adapted for HTML5 platform)
  4. *
  5. * This example has been created using raylib 1.4 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2016 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 msgBm[64] = "THIS IS AN AngelCode SPRITE FONT";
  21. const char msgTtf[64] = "THIS SPRITE FONT has been GENERATED from a TTF";
  22. // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
  23. SpriteFont fontBm; // BMFont (AngelCode)
  24. SpriteFont fontTtf; // TTF font
  25. Vector2 fontPosition;
  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 - bmfont and ttf sprite fonts loading");
  38. fontBm = LoadSpriteFont("resources/bmfont.fnt"); // BMFont (AngelCode)
  39. fontTtf = LoadSpriteFont("resources/pixantiqua.ttf"); // TTF font
  40. fontPosition.x = screenWidth/2 - MeasureTextEx(fontBm, msgBm, fontBm.baseSize, 0).x/2;
  41. fontPosition.y = screenHeight/2 - fontBm.baseSize/2 - 80;
  42. #if defined(PLATFORM_WEB)
  43. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  44. #else
  45. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  46. //--------------------------------------------------------------------------------------
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. UpdateDrawFrame();
  51. }
  52. #endif
  53. // De-Initialization
  54. //--------------------------------------------------------------------------------------
  55. UnloadSpriteFont(fontBm); // AngelCode SpriteFont unloading
  56. UnloadSpriteFont(fontTtf); // TTF SpriteFont unloading
  57. CloseWindow(); // Close window and OpenGL context
  58. //--------------------------------------------------------------------------------------
  59. return 0;
  60. }
  61. //----------------------------------------------------------------------------------
  62. // Module Functions Definition
  63. //----------------------------------------------------------------------------------
  64. void UpdateDrawFrame(void)
  65. {
  66. // Update
  67. //----------------------------------------------------------------------------------
  68. // TODO: Update variables here...
  69. //----------------------------------------------------------------------------------
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. DrawTextEx(fontBm, msgBm, fontPosition, fontBm.baseSize, 0, MAROON);
  75. DrawTextEx(fontTtf, msgTtf, (Vector2){ 75.0f, 240.0f }, fontTtf.baseSize*0.8f, 2, LIME);
  76. EndDrawing();
  77. //----------------------------------------------------------------------------------
  78. }