textures_image_drawing.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #if defined(PLATFORM_WEB)
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. //----------------------------------------------------------------------------------
  18. // Global Variables Definition
  19. //----------------------------------------------------------------------------------
  20. int screenWidth = 800;
  21. int screenHeight = 450;
  22. Texture2D texture; // Texture, GPU memory (VRAM)
  23. //----------------------------------------------------------------------------------
  24. // Module Functions Declaration
  25. //----------------------------------------------------------------------------------
  26. void UpdateDrawFrame(void); // Update and Draw one frame
  27. //----------------------------------------------------------------------------------
  28. // Main Enry Point
  29. //----------------------------------------------------------------------------------
  30. int main()
  31. {
  32. // Initialization
  33. //--------------------------------------------------------------------------------------
  34. InitWindow(screenWidth, screenHeight, "raylib [textures] example - image drawing");
  35. Image cat = LoadImage("resources/cat.png"); // Load image in CPU memory (RAM)
  36. ImageCrop(&cat, (Rectangle){ 100, 10, 280, 380 }); // Crop an image piece
  37. ImageFlipHorizontal(&cat); // Flip cropped image horizontally
  38. ImageResize(&cat, 150, 200); // Resize flipped-cropped image
  39. Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
  40. // Draw one image over the other with a scaling of 1.5f
  41. ImageDraw(&parrots, cat, (Rectangle){ 0, 0, cat.width, cat.height }, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f });
  42. ImageCrop(&parrots, (Rectangle){ 0, 50, parrots.width, parrots.height - 100 }); // Crop resulting image
  43. UnloadImage(cat); // Unload image from RAM
  44. texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
  45. UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
  46. #if defined(PLATFORM_WEB)
  47. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  48. #else
  49. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. UpdateDrawFrame();
  55. }
  56. #endif
  57. // De-Initialization
  58. //--------------------------------------------------------------------------------------
  59. UnloadTexture(texture); // Texture unloading
  60. CloseWindow(); // Close window and OpenGL context
  61. //--------------------------------------------------------------------------------------
  62. return 0;
  63. }
  64. //----------------------------------------------------------------------------------
  65. // Module Functions Definition
  66. //----------------------------------------------------------------------------------
  67. void UpdateDrawFrame(void)
  68. {
  69. // Update
  70. //----------------------------------------------------------------------------------
  71. // TODO: Update your variables here
  72. //----------------------------------------------------------------------------------
  73. // Draw
  74. //----------------------------------------------------------------------------------
  75. BeginDrawing();
  76. ClearBackground(RAYWHITE);
  77. DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, WHITE);
  78. DrawRectangleLines(screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, texture.width, texture.height, DARKGRAY);
  79. DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, DARKGRAY);
  80. DrawText("Source images have been cropped, scaled, flipped and copied one over the other.", 190, 370, 10, DARKGRAY);
  81. EndDrawing();
  82. //----------------------------------------------------------------------------------
  83. }