textures_srcrec_dstrec.lua 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [textures] example - Texture source and destination rectangles
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. -- Initialization
  12. -------------------------------------------------------------------------------------------
  13. local screenWidth = 800
  14. local screenHeight = 450
  15. InitWindow(screenWidth, screenHeight, "raylib [textures] examples - texture source and destination rectangles")
  16. -- NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  17. local guybrush = LoadTexture("resources/guybrush.png") -- Texture loading
  18. local frameWidth = guybrush.width/7
  19. local frameHeight = guybrush.height
  20. -- NOTE: Source rectangle (part of the texture to use for drawing)
  21. local sourceRec = Rectangle(0, 0, frameWidth, frameHeight)
  22. -- NOTE: Destination rectangle (screen rectangle where drawing part of texture)
  23. local destRec = Rectangle(screenWidth/2, screenHeight/2, frameWidth*2, frameHeight*2)
  24. -- NOTE: Origin of the texture (rotation/scale point), it's relative to destination rectangle size
  25. local origin = Vector2(frameWidth, frameHeight)
  26. local rotation = 0
  27. SetTargetFPS(60)
  28. -------------------------------------------------------------------------------------------
  29. -- Main game loop
  30. while not WindowShouldClose() do -- Detect window close button or ESC key
  31. -- Update
  32. ---------------------------------------------------------------------------------------
  33. rotation = rotation + 1
  34. ---------------------------------------------------------------------------------------
  35. -- Draw
  36. ---------------------------------------------------------------------------------------
  37. BeginDrawing()
  38. ClearBackground(RAYWHITE)
  39. -- NOTE: Using DrawTexturePro() we can easily rotate and scale the part of the texture we draw
  40. -- sourceRec defines the part of the texture we use for drawing
  41. -- destRec defines the rectangle where our texture part will fit (scaling it to fit)
  42. -- origin defines the point of the texture used as reference for rotation and scaling
  43. -- rotation defines the texture rotation (using origin as rotation point)
  44. DrawTexturePro(guybrush, sourceRec, destRec, origin, rotation, WHITE)
  45. DrawLine(destRec.x, 0, destRec.x, screenHeight, GRAY)
  46. DrawLine(0, destRec.y, screenWidth, destRec.y, GRAY)
  47. EndDrawing()
  48. ---------------------------------------------------------------------------------------
  49. end
  50. -- De-Initialization
  51. -------------------------------------------------------------------------------------------
  52. UnloadTexture(guybrush) -- Texture unloading
  53. CloseWindow() -- Close window and OpenGL context
  54. -------------------------------------------------------------------------------------------