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.

77 lines
3.6 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. * This example has been created using raylib 1.4 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2016 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [textures] example - image drawing");
  21. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  22. Image cat = LoadImage("resources/cat.png"); // Load image in CPU memory (RAM)
  23. ImageCrop(&cat, (Rectangle){ 100, 10, 280, 380 }); // Crop an image piece
  24. ImageFlipHorizontal(&cat); // Flip cropped image horizontally
  25. ImageResize(&cat, 150, 200); // Resize flipped-cropped image
  26. Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
  27. // Draw one image over the other with a scaling of 1.5f
  28. ImageDraw(&parrots, cat, (Rectangle){ 0, 0, cat.width, cat.height }, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f });
  29. ImageCrop(&parrots, (Rectangle){ 0, 50, parrots.width, parrots.height - 100 }); // Crop resulting image
  30. UnloadImage(cat); // Unload image from RAM
  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. SetTargetFPS(60);
  34. //---------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. // TODO: Update your variables here
  41. //----------------------------------------------------------------------------------
  42. // Draw
  43. //----------------------------------------------------------------------------------
  44. BeginDrawing();
  45. ClearBackground(RAYWHITE);
  46. DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, WHITE);
  47. DrawRectangleLines(screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, texture.width, texture.height, DARKGRAY);
  48. DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, DARKGRAY);
  49. DrawText("Source images have been cropped, scaled, flipped and copied one over the other.", 190, 370, 10, DARKGRAY);
  50. EndDrawing();
  51. //----------------------------------------------------------------------------------
  52. }
  53. // De-Initialization
  54. //--------------------------------------------------------------------------------------
  55. UnloadTexture(texture); // Texture unloading
  56. CloseWindow(); // Close window and OpenGL context
  57. //--------------------------------------------------------------------------------------
  58. return 0;
  59. }