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.

96 lines
4.4 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Image loading and drawing on it
  4. *
  5. * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
  6. *
  7. * Example originally created with raylib 1.4, last time updated with raylib 1.4
  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) 2016-2024 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 drawing");
  26. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  27. Image cat = LoadImage("resources/cat.png"); // Load image in CPU memory (RAM)
  28. ImageCrop(&cat, (Rectangle){ 100, 10, 280, 380 }); // Crop an image piece
  29. ImageFlipHorizontal(&cat); // Flip cropped image horizontally
  30. ImageResize(&cat, 150, 200); // Resize flipped-cropped image
  31. Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
  32. // Draw one image over the other with a scaling of 1.5f
  33. ImageDraw(&parrots, cat, (Rectangle){ 0, 0, (float)cat.width, (float)cat.height }, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f }, WHITE);
  34. ImageCrop(&parrots, (Rectangle){ 0, 50, (float)parrots.width, (float)parrots.height - 100 }); // Crop resulting image
  35. // Draw on the image with a few image draw methods
  36. ImageDrawPixel(&parrots, 10, 10, RAYWHITE);
  37. ImageDrawCircleLines(&parrots, 10, 10, 5, RAYWHITE);
  38. ImageDrawRectangle(&parrots, 5, 20, 10, 10, RAYWHITE);
  39. UnloadImage(cat); // Unload image from RAM
  40. // Load custom font for drawing on image
  41. Font font = LoadFont("resources/custom_jupiter_crash.png");
  42. // Draw over image using custom font
  43. ImageDrawTextEx(&parrots, font, "PARROTS & CAT", (Vector2){ 300, 230 }, (float)font.baseSize, -2, WHITE);
  44. UnloadFont(font); // Unload custom font (already drawn used on image)
  45. Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
  46. UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
  47. SetTargetFPS(60);
  48. //---------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. // TODO: Update your variables here
  55. //----------------------------------------------------------------------------------
  56. // Draw
  57. //----------------------------------------------------------------------------------
  58. BeginDrawing();
  59. ClearBackground(RAYWHITE);
  60. DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, WHITE);
  61. DrawRectangleLines(screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, texture.width, texture.height, DARKGRAY);
  62. DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, DARKGRAY);
  63. DrawText("Source images have been cropped, scaled, flipped and copied one over the other.", 190, 370, 10, DARKGRAY);
  64. EndDrawing();
  65. //----------------------------------------------------------------------------------
  66. }
  67. // De-Initialization
  68. //--------------------------------------------------------------------------------------
  69. UnloadTexture(texture); // Texture unloading
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }