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.

82 lines
3.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [texture] example - Image text drawing using TTF generated spritefont
  4. *
  5. * This example has been created using raylib 1.8 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2017 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 [texture] example - image text drawing");
  19. Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
  20. // TTF Font loading with custom generation parameters
  21. Font font = LoadFontEx("resources/KAISG.ttf", 64, 0, 0);
  22. // Draw over image using custom font
  23. ImageDrawTextEx(&parrots, (Vector2){ 20.0f, 20.0f }, font, "[Parrots font drawing]", (float)font.baseSize, 0.0f, RED);
  24. Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
  25. UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
  26. Vector2 position = { (float)(screenWidth/2 - texture.width/2), (float)(screenHeight/2 - texture.height/2 - 20) };
  27. bool showFont = false;
  28. SetTargetFPS(60);
  29. //--------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. if (IsKeyDown(KEY_SPACE)) showFont = true;
  36. else showFont = false;
  37. //----------------------------------------------------------------------------------
  38. // Draw
  39. //----------------------------------------------------------------------------------
  40. BeginDrawing();
  41. ClearBackground(RAYWHITE);
  42. if (!showFont)
  43. {
  44. // Draw texture with text already drawn inside
  45. DrawTextureV(texture, position, WHITE);
  46. // Draw text directly using sprite font
  47. DrawTextEx(font, "[Parrots font drawing]", (Vector2){ position.x + 20,
  48. position.y + 20 + 280 }, (float)font.baseSize, 0.0f, WHITE);
  49. }
  50. else DrawTexture(font.texture, screenWidth/2 - font.texture.width/2, 50, BLACK);
  51. DrawText("PRESS SPACE to SEE USED SPRITEFONT ", 290, 420, 10, DARKGRAY);
  52. EndDrawing();
  53. //----------------------------------------------------------------------------------
  54. }
  55. // De-Initialization
  56. //--------------------------------------------------------------------------------------
  57. UnloadTexture(texture); // Texture unloading
  58. UnloadFont(font); // Unload custom spritefont
  59. CloseWindow(); // Close window and OpenGL context
  60. //--------------------------------------------------------------------------------------
  61. return 0;
  62. }