-- title: rspr
-- author: pixelbath
-- desc: rotate a sprite (code by ArchaicVirus)
local t = 0
vec2 = {}
vec2_mt = {}
vec2_mt.__index = vec2_mt
setmetatable(vec2, { __call = function(V, x, y) return setmetatable({ x = x or 0, y = y or x or 0 }, vec2_mt) end })
function rspr(id, x, y, colorkey, scaleX, scaleY, flip, rotate, tile_width, tile_height, pivot)
colorkey = colorkey or -1
scaleX = scaleX or 1
scaleY = scaleY or 1
flip = flip or 0
rotate = rotate or 0
tile_width = tile_width or 1
tile_height = tile_height or 1
pivot = pivot or vec2(0, 0)
-- Draw a sprite using two textured triangles.
-- Apply affine transformations: scale, shear, rotate, flip
-- scale / flip
if flip % 2 == 1 then
scaleX = -scaleX
end
if flip >= 2 then
scaleY = -scaleY
end
local ox = tile_width * 8 // 2
local oy = tile_height * 8 // 2
local ox = ox * -scaleX
local oy = oy * -scaleY
-- shear / rotate
local shx1 = 0
local shy1 = 0
local shx2 = 0
local shy2 = 0
local shx1 = shx1 * -scaleX
local shy1 = shy1 * -scaleY
local shx2 = shx2 * -scaleX
local shy2 = shy2 * -scaleY
local rr = math.rad(rotate)
local sa = math.sin(rr)
local ca = math.cos(rr)
local function rot(x, y, px, py)
-- Translate point to origin (pivot)
x, y = x - px, y - py
-- Rotate
local rx, ry = x * ca - y * sa, x * sa + y * ca
-- Translate back
return rx + px, ry + py
end
local rx1, ry1 = rot(ox + shx1, oy + shy1, pivot.x, pivot.y)
local rx2, ry2 = rot(((tile_width * 8) * scaleX) + ox + shx1, oy + shy2, pivot.x, pivot.y)
local rx3, ry3 = rot(ox + shx2, ((tile_height * 8) * scaleY) + oy + shy1, pivot.x, pivot.y)
local rx4, ry4 = rot(((tile_width * 8) * scaleX) + ox + shx2, ((tile_height * 8) * scaleY) + oy + shy2, pivot.x,
pivot.y)
local x1 = x + rx1 - pivot.x
local y1 = y + ry1 - pivot.y
local x2 = x + rx2 - pivot.x
local y2 = y + ry2 - pivot.y
local x3 = x + rx3 - pivot.x
local y3 = y + ry3 - pivot.y
local x4 = x + rx4 - pivot.x
local y4 = y + ry4 - pivot.y
-- UV coords
local u1 = (id % 16) * 8
local v1 = math.floor(id / 16) * 8
local u2 = u1 + tile_width * 8
local v2 = v1 + tile_height * 8
ttri(x1, y1, x2, y2, x3, y3, u1, v1, u2, v1, u1, v2, 0, colorkey)
ttri(x3, y3, x4, y4, x2, y2, u1, v2, u2, v2, u2, v1, 0, colorkey)
end
function TIC()
cls(0)
rspr(1, 100, 50, 0, 1, 1, 0, t, 2, 2)
t=t+1
end
--
-- 001:999900009990000099000000900000000000000000000002000000220000022c
-- 002:0000333300000333000000330000000300000000200000002200000022200000
-- 017:00000222000000220000000200000000b0000000bb000000bbb00000bbbb0000
-- 018:c220000022000000200000000000000000000005000000550000055500005555
--
--
-- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
--