-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate-sprite.p8
177 lines (166 loc) · 14.7 KB
/
rotate-sprite.p8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
pico-8 cartridge // http://www.pico-8.com
version 30
__lua__
-- rotateSprite by sthilaid
-->8
-- rotateSprite
function round(x) return flr(x+0.5) end
function clamp(minv,maxv,v) return min(maxv, max(minv, v)) end
-- rotate x,y of angle around origin
function rot(x,y,ang)
local theta = atan2(x,y)
local l = sqrt(x*x+y*y)
return cos(theta+ang)*l, sin(theta+ang)*l
end
-- rotate sprite s into sprite ds
-- s: sprite index
-- spr_size: size of sprite (as used in spr calls)
-- angle: rotation angle (in pico angles [0,1] not radians or degrees)
-- ds: destination sprite index
function rotateSprite(s,spr_size,angle,ds)
local size = spr_size * 8
local halfsize = size*0.5
local sx,sy = (s % 16) * 8, (s \ 16) * 8
local dsx,dsy = (ds % 16) * 8, (ds \ 16) * 8
for i=0,size-1 do
for j=0,size-1 do
local u,v = rot(i-halfsize,j-halfsize,-angle)
local c = 0
if (u > -halfsize and u < halfsize and v > -halfsize and v < halfsize) then
local cx = clamp(sx,sx+size-1,sx+halfsize+round(u))
local cy = clamp(sy,sy+size-1,sy+halfsize+round(v))
c = sget(cx,cy)
end
sset(dsx+i, dsy+j, c)
end
end
end
-- @TheRoboZ https://www.lexaloffle.com/bbs/?pid=78451
function draw_rotated_tile(x,y,rot,mx,my,w,flip,scale)
scale = scale or 1
w+=.8
local halfw, cx = scale*-w/2, mx + w/2 -.4
local cs, ss, cy = cos(rot)/scale, -sin(rot)/scale, my-halfw/scale-.4
local sx, sy, hx, hy = cx + cs*halfw, cy - ss*halfw, w*(flip and -4 or 4)*scale, w*4*scale
--this just draw a bounding box to show the exact draw area
rect(x-hx,y-hy,x+hx,y+hy,5)
for py = y-hy, y+hy do
tline(x-hx, py, x+hx, py, sx + ss*halfw, sy + cs*halfw, cs/8, -ss/8)
halfw+=1/8
end
end
-->8
-- demo
g_index=0
g_angle=0
g_prevAngle=-999999
g_sprIndices={0,1,3,7}
function _update()
if btnp(5) then
g_index = (g_index+1) % 4
g_prevAngle = -999999 -- reset cache
end
local dir=0
if (btn(0)) dir = 1
if (btn(1)) dir = -1
g_angle += dir * 0.01
end
function _draw()
cls(0)
local s = g_sprIndices[g_index+1]
local size = 2^g_index
local dst = 128 -- out of the way of the data sprites
if (g_prevAngle != g_angle) rotateSprite(s,size,g_angle,dst)
g_prevAngle = g_angle
local offset = 64-size*4
spr(dst,offset,offset,size,size)
draw_rotated_tile(32,32,-g_angle,0,8,8,false,1)
color(7)
print("cpu:"..(stat(1)*100).."% @ "..stat(7).."fps",0,6)
print("⬅️➡️:rotate",0,116)
print("❎:swap",0,122)
print("angle: "..g_angle,80,116)
print("size: "..(size*8).."x"..(size*8))
end
__gfx__
00aaaa0000000aaaaaa0000000000000000099999999000000000000000000000000000000000000009999999999990000000000000000000000000000000000
0aaaaaa0000aaaaaaaaaa000000000000999aaaaaaaa99900000000000000000000000000000009999aaaaaaaaaaaa9999000000000000000000000000000000
aa8aa8aa00aaaaaaaaaaaa00000000099aaaaaaaaaaaaaa9900000000000000000000000000999aaaaaaaaaaaaaaaaaaaa999000000000000000000000000000
aaaaaaaa0aaaa88aa88aaaa00000009aaaaaaaaaaaaaaaaaa90000000000000000000000099aaaaaaaaaaaaaaaaaaaaaaaaaa990000000000000000000000000
aaaaaaaa0aaa8aaaaaa8aaa0000009aaaaaaaaaaaaaaaaaaaa90000000000000000000099aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9900000000000000000000000
aa8aa8aaaaaaa88aa88aaaaa00009aaaaaaaaaaaaaaaaaaaaaa90000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa90000000000000000000000
0aa88aa0aaaaa88aa88aaaaa0009aaaa00aaaaaaaaaaaa00aaaa900000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9000000000000000000000
00aaaa00aaaaaaaaaaaaaaaa009aaa00aaaaaaaaaaaaaaa00aaaa9000000000000099aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa990000000000000000000
00000000aaaaaaaaaaaaaaaa009aaa0aaaaaaaaaaaaaaaaaaaaaa90000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9000000000000000000
00000000aaaaaaaaaaaaaaaa09aaaaaaa0000aaaaaa0000aaaaaaa900000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa900000000000000000
00000000aaaa8aaaaaa8aaaa09aaaaa0077880aaa0077880aaaaaa90000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa90000000000000000
000000000aaaa8aaaa8aaaa009aaaaa07778800aa07778800aaaaa9000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9000000000000000
000000000aaaaa8888aaaaa09aaaaaa0777770aaa0777770aaaaaaa900000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9000000000000000
0000000000aaaaaaaaaaaa009aaaaaaa00000aaaaa00000aaaaaaaa90000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa900000000000000
00000000000aaaaaaaaaa0009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9000009aaaaaaaaaaaaaaaaaaaa0aaaaaaaaaaaaa0aaaaaaaaaaaaaaaaa90000000000000
0000000000000aaaaaa000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa900009aaaaaaaaaaaaaaaaaa00aa0aaaaaaaaaaa0aa00aaaaaaaaaaaaaaa9000000000000
0000000000000000000000009aaaaaaaaaaaaaaa0aaaaaaaaaaaaaa900009aaaaaaaaaaaaaa00000aaa0aaaaaaaaaaa0aaa00000aaaaaaaaaaa9000000000000
0000000000000000000000009aaaaaaaaaaaaa000aaaaaaaaaaaaaa90009aaaaaaaaaaaaa000aaaaaaaaaaaaaaaaaaaaaaaaaaa000aaaaaaaaaa900000000000
0000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa90009aaaaaaaaaaaa00aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00aaaaaaaaa900000000000
0000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9009aaaaaaaaaaaa00aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00aaaaaaaaa90000000000
00000000000000000000000009aaaaaaaaaaaaaaaaaaaaaa00aaaa90009aaaaaaaaaaa00aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00aaaaaaaa90000000000
00000000000000000000000009aaaaaaa00aaaaaaaaaaaaaa0aaaa90009aaaaaaaaaaa0aaaa000000000aaaaaaaaaaa000000000aaaa0aaaaaaaa90000000000
00000000000000000000000009aaaaaaaa000aaaaaaaaa0aaaaaaa9009aaaaaaaaaaa0aaaa00777774400aaaaaaaaa00777774400aaaa0aaaaaaaa9000000000
000000000000000000000000009aaaaaaaaa0000aa00000aaaaaa90009aaaaaaaaaaa0aaa0077777740000aaaaaaa0007777740000aaa0aaaaaaaa9000000000
000000000000000000000000009aaaaaaaaaaaa0000aaaaaaaaaa90009aaaaaaaaaaaaaa007777777400400aaaaa007777777400400aaaaaaaaaaa9000000000
0000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaa900009aaaaaaaaaaaaaa007777777444440aaaaa077777777444400aaaaaaaaaaa9000000000
00000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaa900009aaaaaaaaaaaaaaaa00777777777700aaaaa00777777777700aaaaaaaaaaaaa900000000
000000000000000000000000000009aaaaaaaaaaaaaaaaaaaa9000009aaaaaaaaaaaaaaaaa077777777000aaaaaaa000777777770aaaaaaaaaaaaaa900000000
0000000000000000000000000000009aaaaaaaaaaaaaaaaaa90000009aaaaaaaaaaaaaa000a000000000aaaaaaaaaaa000000000a000aaaaaaaaaaa900000000
000000000000000000000000000000099aaaaaaaaaaaaaa9900000009aaaaaaaaaaaaaaaaa00aaaaaaaaaaaaaaaaaaaaaaaaaaa00aaaaaaaaaaaaaa900000000
000000000000000000000000000000000999aaaaaaaa9990000000009aaaaaaaaaaaaaaaaaaa00000aaaaaaaaaaaaaaaaa00000aaaaaaaaaaaaaaaa900000000
000000000000000000000000000000000000999999990000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa900000000
000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa900000000
000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa900000000
000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa900000000
000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0aaaaaaaaaaaaaaaaaaaaaaaaaaa900000000
000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00aaaaaaaaaaaaaaaaaaaaaaaaaaa900000000
000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa000aaaaaaaaaaaaaaaaaaaaaaaaaaaa900000000
0000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00aaaaaaaa9000000000
0000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0aaaaa0aaaaaaaa9000000000
0000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00aaaaaa0aaaaaaa9000000000
0000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa000aaaaaaaaaaaaaa9000000000
00000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000aaaaaaaaaaaaa90000000000
00000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaa00aaaaaaaaaaaaaaaaaaaa000880aaaaaaaaaaaaaa90000000000
00000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaa0aaaaaaaaaaaaaaaaaaa00088880aaaaaaaaaaaaaa90000000000
000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaa0aaaaaaaaaaaaaaaaa008888880aaaaaaaaaaaaaa900000000000
000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaa0aaaaa00aaaaaaaaa0088888800aaaaaaaaaaaaaa900000000000
0000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaa0aaaa00000000000888888800aaaaaaaaaaaaaa9000000000000
0000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaa008888888888888800aaaaaaaaaaaaaaa9000000000000
00000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaa0008888888888800aaaaaaaaaaaaaaa90000000000000
000000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaa0000888888000aaaaaaaaaaaaaaa900000000000000
0000000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaa00000000aaaaaaaaaaaaaaaa9000000000000000
0000000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9000000000000000
00000000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa90000000000000000
000000000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa900000000000000000
0000000000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9000000000000000000
000000000000000000000000000000000000000000000000000000000000000000099aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa990000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000009aaaaaaaaaaaa00aaaaaaaaaaaaaaaaaaaa90000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000099aaaaaaaaaaaa00aaaaaaaaaaaaaaaa9900000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000099aaaaaaaaaaaaaaaaaaaaaaaaaa990000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000999aaaaaaaaaaaaaaaaaaaa999000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000009999aaaaaaaaaaaa9999000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000009999999999990000000000000000000000000000000000
__map__
0304050631313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1314151631313131313131313100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2324252631313131313131313100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3334353631313131313131313100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000031313131313131313100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000003131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000031310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0721212121212121210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0708090a0b0c0d0e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1718191a1b1c1d1e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2728292a2b2c2d2e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3738393a3b3c3d3e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
4748494a4b4c4d4e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
5758595a5b5c5d5e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
6768696a6b6c6d6e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
7778797a7b7c7d7e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000