Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

98 wiersze
4.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Image loading and drawing on it
  4. *
  5. * Example complexity rating: [] 2/4
  6. *
  7. * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
  8. *
  9. * Example originally created with raylib 1.4, last time updated with raylib 1.4
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2016-2025 Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [textures] example - image drawing");
  28. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  29. Image cat = LoadImage("resources/cat.png"); // Load image in CPU memory (RAM)
  30. ImageCrop(&cat, (Rectangle){ 100, 10, 280, 380 }); // Crop an image piece
  31. ImageFlipHorizontal(&cat); // Flip cropped image horizontally
  32. ImageResize(&cat, 150, 200); // Resize flipped-cropped image
  33. Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
  34. // Draw one image over the other with a scaling of 1.5f
  35. ImageDraw(&parrots, cat, (Rectangle){ 0, 0, (float)cat.width, (float)cat.height }, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f }, WHITE);
  36. ImageCrop(&parrots, (Rectangle){ 0, 50, (float)parrots.width, (float)parrots.height - 100 }); // Crop resulting image
  37. // Draw on the image with a few image draw methods
  38. ImageDrawPixel(&parrots, 10, 10, RAYWHITE);
  39. ImageDrawCircleLines(&parrots, 10, 10, 5, RAYWHITE);
  40. ImageDrawRectangle(&parrots, 5, 20, 10, 10, RAYWHITE);
  41. UnloadImage(cat); // Unload image from RAM
  42. // Load custom font for drawing on image
  43. Font font = LoadFont("resources/custom_jupiter_crash.png");
  44. // Draw over image using custom font
  45. ImageDrawTextEx(&parrots, font, "PARROTS & CAT", (Vector2){ 300, 230 }, (float)font.baseSize, -2, WHITE);
  46. UnloadFont(font); // Unload custom font (already drawn used on image)
  47. Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
  48. UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
  49. SetTargetFPS(60);
  50. //---------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. // TODO: Update your variables here
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, WHITE);
  63. DrawRectangleLines(screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, texture.width, texture.height, DARKGRAY);
  64. DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, DARKGRAY);
  65. DrawText("Source images have been cropped, scaled, flipped and copied one over the other.", 190, 370, 10, DARKGRAY);
  66. EndDrawing();
  67. //----------------------------------------------------------------------------------
  68. }
  69. // De-Initialization
  70. //--------------------------------------------------------------------------------------
  71. UnloadTexture(texture); // Texture unloading
  72. CloseWindow(); // Close window and OpenGL context
  73. //--------------------------------------------------------------------------------------
  74. return 0;
  75. }