textures_rectangle.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [textures] example - Texture loading and drawing a part defined by a rectangle
  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 [texture] example - texture rectangle")
  16. -- NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  17. local guybrush = LoadTexture("resources/guybrush.png") -- Texture loading
  18. local position = Vector2(350.0, 240.0)
  19. local frameRec = Rectangle(0, 0, guybrush.width/7, guybrush.height)
  20. local currentFrame = 0
  21. -------------------------------------------------------------------------------------------
  22. -- Main game loop
  23. while not WindowShouldClose() do -- Detect window close button or ESC key
  24. -- Update
  25. ---------------------------------------------------------------------------------------
  26. if (IsKeyPressed(KEY.RIGHT)) then
  27. currentFrame = currentFrame + 1
  28. if (currentFrame > 6) then currentFrame = 0 end
  29. frameRec.x = currentFrame*guybrush.width/7
  30. end
  31. ---------------------------------------------------------------------------------------
  32. -- Draw
  33. ---------------------------------------------------------------------------------------
  34. BeginDrawing()
  35. ClearBackground(RAYWHITE)
  36. DrawTexture(guybrush, 35, 40, WHITE)
  37. DrawRectangleLines(35, 40, guybrush.width, guybrush.height, LIME)
  38. DrawTextureRec(guybrush, frameRec, position, WHITE) -- Draw part of the texture
  39. DrawRectangleLines(35 + frameRec.x, 40 + frameRec.y, frameRec.width, frameRec.height, RED)
  40. DrawText("PRESS RIGHT KEY to", 540, 310, 10, GRAY)
  41. DrawText("CHANGE DRAWING RECTANGLE", 520, 330, 10, GRAY)
  42. DrawText("Guybrush Ulysses Threepwood,", 100, 300, 10, GRAY)
  43. DrawText("main character of the Monkey Island series", 80, 320, 10, GRAY)
  44. DrawText("of computer adventure games by LucasArts.", 80, 340, 10, GRAY)
  45. EndDrawing()
  46. ---------------------------------------------------------------------------------------
  47. end
  48. -- De-Initialization
  49. -------------------------------------------------------------------------------------------
  50. UnloadTexture(guybrush) -- Texture unloading
  51. CloseWindow() -- Close window and OpenGL context
  52. -------------------------------------------------------------------------------------------