textures_image_text.c 3.6 KB

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