rspr.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. -- title: rspr
  2. -- author: pixelbath
  3. -- desc: rotate a sprite (code by ArchaicVirus)
  4. local t = 0
  5. vec2 = {}
  6. vec2_mt = {}
  7. vec2_mt.__index = vec2_mt
  8. setmetatable(vec2, { __call = function(V, x, y) return setmetatable({ x = x or 0, y = y or x or 0 }, vec2_mt) end })
  9. function rspr(id, x, y, colorkey, scaleX, scaleY, flip, rotate, tile_width, tile_height, pivot)
  10. colorkey = colorkey or -1
  11. scaleX = scaleX or 1
  12. scaleY = scaleY or 1
  13. flip = flip or 0
  14. rotate = rotate or 0
  15. tile_width = tile_width or 1
  16. tile_height = tile_height or 1
  17. pivot = pivot or vec2(0, 0)
  18. -- Draw a sprite using two textured triangles.
  19. -- Apply affine transformations: scale, shear, rotate, flip
  20. -- scale / flip
  21. if flip % 2 == 1 then
  22. scaleX = -scaleX
  23. end
  24. if flip >= 2 then
  25. scaleY = -scaleY
  26. end
  27. local ox = tile_width * 8 // 2
  28. local oy = tile_height * 8 // 2
  29. local ox = ox * -scaleX
  30. local oy = oy * -scaleY
  31. -- shear / rotate
  32. local shx1 = 0
  33. local shy1 = 0
  34. local shx2 = 0
  35. local shy2 = 0
  36. local shx1 = shx1 * -scaleX
  37. local shy1 = shy1 * -scaleY
  38. local shx2 = shx2 * -scaleX
  39. local shy2 = shy2 * -scaleY
  40. local rr = math.rad(rotate)
  41. local sa = math.sin(rr)
  42. local ca = math.cos(rr)
  43. local function rot(x, y, px, py)
  44. -- Translate point to origin (pivot)
  45. x, y = x - px, y - py
  46. -- Rotate
  47. local rx, ry = x * ca - y * sa, x * sa + y * ca
  48. -- Translate back
  49. return rx + px, ry + py
  50. end
  51. local rx1, ry1 = rot(ox + shx1, oy + shy1, pivot.x, pivot.y)
  52. local rx2, ry2 = rot(((tile_width * 8) * scaleX) + ox + shx1, oy + shy2, pivot.x, pivot.y)
  53. local rx3, ry3 = rot(ox + shx2, ((tile_height * 8) * scaleY) + oy + shy1, pivot.x, pivot.y)
  54. local rx4, ry4 = rot(((tile_width * 8) * scaleX) + ox + shx2, ((tile_height * 8) * scaleY) + oy + shy2, pivot.x,
  55. pivot.y)
  56. local x1 = x + rx1 - pivot.x
  57. local y1 = y + ry1 - pivot.y
  58. local x2 = x + rx2 - pivot.x
  59. local y2 = y + ry2 - pivot.y
  60. local x3 = x + rx3 - pivot.x
  61. local y3 = y + ry3 - pivot.y
  62. local x4 = x + rx4 - pivot.x
  63. local y4 = y + ry4 - pivot.y
  64. -- UV coords
  65. local u1 = (id % 16) * 8
  66. local v1 = math.floor(id / 16) * 8
  67. local u2 = u1 + tile_width * 8
  68. local v2 = v1 + tile_height * 8
  69. ttri(x1, y1, x2, y2, x3, y3, u1, v1, u2, v1, u1, v2, 0, colorkey)
  70. ttri(x3, y3, x4, y4, x2, y2, u1, v2, u2, v2, u2, v1, 0, colorkey)
  71. end
  72. function TIC()
  73. cls(0)
  74. rspr(1, 100, 50, 0, 1, 1, 0, t, 2, 2)
  75. t=t+1
  76. end
  77. -- <TILES>
  78. -- 001:999900009990000099000000900000000000000000000002000000220000022c
  79. -- 002:0000333300000333000000330000000300000000200000002200000022200000
  80. -- 017:00000222000000220000000200000000b0000000bb000000bbb00000bbbb0000
  81. -- 018:c220000022000000200000000000000000000005000000550000055500005555
  82. -- </TILES>
  83. -- <PALETTE>
  84. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  85. -- </PALETTE>