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.

87 lines
3.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [texture] example - Image text drawing using TTF generated font
  4. *
  5. * Example originally created with raylib 1.8, last time updated with raylib 4.0
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2017-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. //------------------------------------------------------------------------------------
  15. // Program main entry point
  16. //------------------------------------------------------------------------------------
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [texture] example - image text drawing");
  24. Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
  25. // TTF Font loading with custom generation parameters
  26. Font font = LoadFontEx("resources/KAISG.ttf", 64, 0, 0);
  27. // Draw over image using custom font
  28. ImageDrawTextEx(&parrots, font, "[Parrots font drawing]", (Vector2){ 20.0f, 20.0f }, (float)font.baseSize, 0.0f, RED);
  29. Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
  30. UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
  31. Vector2 position = { (float)(screenWidth/2 - texture.width/2), (float)(screenHeight/2 - texture.height/2 - 20) };
  32. bool showFont = false;
  33. SetTargetFPS(60);
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. if (IsKeyDown(KEY_SPACE)) showFont = true;
  41. else showFont = false;
  42. //----------------------------------------------------------------------------------
  43. // Draw
  44. //----------------------------------------------------------------------------------
  45. BeginDrawing();
  46. ClearBackground(RAYWHITE);
  47. if (!showFont)
  48. {
  49. // Draw texture with text already drawn inside
  50. DrawTextureV(texture, position, WHITE);
  51. // Draw text directly using sprite font
  52. DrawTextEx(font, "[Parrots font drawing]", (Vector2){ position.x + 20,
  53. position.y + 20 + 280 }, (float)font.baseSize, 0.0f, WHITE);
  54. }
  55. else DrawTexture(font.texture, screenWidth/2 - font.texture.width/2, 50, BLACK);
  56. DrawText("PRESS SPACE to SHOW FONT ATLAS USED", 290, 420, 10, DARKGRAY);
  57. EndDrawing();
  58. //----------------------------------------------------------------------------------
  59. }
  60. // De-Initialization
  61. //--------------------------------------------------------------------------------------
  62. UnloadTexture(texture); // Texture unloading
  63. UnloadFont(font); // Unload custom font
  64. CloseWindow(); // Close window and OpenGL context
  65. //--------------------------------------------------------------------------------------
  66. return 0;
  67. }