textures_image_drawing.lua 3.4 KB

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