Skip to content

Instantly share code, notes, and snippets.

@leenattress
Created September 17, 2017 17:58
Show Gist options
  • Save leenattress/c324ee96d7f215e1f704526021d78526 to your computer and use it in GitHub Desktop.
Save leenattress/c324ee96d7f215e1f704526021d78526 to your computer and use it in GitHub Desktop.
function _init()
hexchar="0123456789abcdef"
function hex(x)
x=flr(x)+1
return sub(hexchar,x,x)
end
charhex={}
for i=1,16 do
charhex[sub(hexchar,i,i)]=i-1
end
-- delete this line and
-- paste your
-- animation in there
vectordata = strtovec("{{{0f0f{19b116971f8719b11f872a882a883a597e5019b12a887e5019b17e50be5b19b1be5bce7719b1ce77d38719b1d387e08819b1e088ea9619b1ea96e7ad19b1e7add8c419b1d8c4d0c619b1d0c6c6db19b1c6db9bf519b19bf57ffc19b17ffc66f519b166f553eb53eb3ddd2fc819b153eb2fc819b12fc823c1}}{0404{49615f586c636c6370715f6649616c635f6649615f665268496152684773}}{0404{95639f58b460b460b872ac669563b460ac669563ac669e6795639e679174}}{0f0e{189621892d8a18962d8a30c8189630c824bd189624bd1aae}}{0404{2faf2ba42a862faf2a862d6c2faf2d6c396f2faf396f369d}}{0f0e{e7aad7c1d0c6e7aad0c6d1ade7aad1add689e7aad689df8be7aadf8be796}}{0404{d0adc79ac96cd0adc96cd781d0add781d4a7}}{0808{21801c682945218029453f2721803f275c0c21805c0c7f0721807f0795089508b618ca2f9508ca2fe0589508e058e476e476d988cf759508e476cf759508cf75b15e9508b15e7e5595087e554263218095084263218042632c7921802c792a86}}{080e{d669d179d26acf5ad669d26acf5ad26aca5da546cf5aca5da546ca5da44ba546a44b6a482e652d76296541542e652965415429653d506a4841543d506a483d506a43a5466a486a43}}{0707{661c8113981b661c981ba42d661ca42d9944661c99446643661c66435c30}}{0808{8c199d36943d8c19943d8a2680238c198a2680238a267f31741a80237f31741a7f3175266437741a7526643775266d3d}}{0707{527b5d746b7a527b6b7a7289527b72897193527b71936499527b64995ea5527b5ea554a1527b54a15098527b50984e8c}}{0707{90819777a2759081a275ac7c9081ac7cb0869081b086af969081af96aca1aca1a2a69f9d9081aca19f9d90819f9d9695908196958e93}}{0404{4daa5bad5eb83e9f4daa5eb83e9f5eb86ac7a1b8a4adafac96c8a1b8afac96c8afacb9a7b9a7c29ec7a796c8b9a7c7a796c8c7a7c9b3c9b3c3bcbcc1c9b3bcc1b5bf96c8c9b3b5bf96c8b5bfb3c880cd96c8b3c8b3c8abd1a1d0b3c8a1d09bcf80cdb3c89bcf80cd9bcf95d56ac780cd95d56ac795d58ad96ac78ad97fd46ac77fd475d83e9f6ac775d83e9f75d869d43e9f69d464ce3e9f64ce5cd03e9f5cd053ce3e9f53ce4ac33e9f4ac340c03e9f40c038b73e9f38b738aa}}{0c0c{588d5a82627a588d627a6c7a588d6c7a7188588d71886f93588d6f936598588d659860a0588d60a05a99}}{0c0c{8f85947a9d7b8f859d7ba5828f85a582a78d8f85a78da69a8f85a69a9fa08f859fa098978f8598978e92}}{0000{63846a846d8963846d896f9363846f93639b6384639b609363846093608b}}{0000{938d94869983938d9983a087938da087a093938da0939c9c938d9c9c9796938d97969393}}{0707{638967866a8963896a89678c}}{0707{968999869b8996899b89998c}}{0e0e{90daa0d590dd81dd90da90dd81dd90dd81e071da81dd81e05fd671da81e05fd681e071dd}}}}")
-- ^ ^ ^ ^
pl_x = 64
pl_y = 64
pl_scale = 0.5
pl_rot = 0
c1_x=0
c1_y=0
frame = 1;
end
function _update()
if (btn(0)) pl_x -= 1
if (btn(1)) pl_x += 1
if (btn(2)) pl_y -= 1
if (btn(3)) pl_y += 1
if (btn(4) and pl_scale > 0.1) pl_scale -= 0.01
if (btn(5) and pl_scale < 2) pl_scale += 0.01
c1_x += (((pl_x)-60)-c1_x) * 0.05
c1_y += (((pl_y)-60)-c1_y) * 0.05
camera(c1_x,c1_y)
frame +=0.4;
if(frame>#vectordata+1) then frame=1; end
end
function _draw()
cls(7)
tris = sprv(vectordata,flr(frame),pl_x,pl_y,pl_scale)
print(stat(1).." cpu",5,5,1)
print(tris..' tris',5,16,1)
print(flr(frame)..' frame',5,27,1)
end
function strtovec(s, i)
local v={}
if (i==nil) i=2 -- skip top {
local i0=i
while true do
if (sub(s,i,i)=="{") then
-- add table
local t,len=strtovec(s,i+1)
add(v,t)
i+=len
elseif (sub(s,i,i)=="}") then
return v,i-i0+2
else
-- add number
x= charhex[sub(s,i,i)]*16 +
charhex[sub(s,i+1,i+1)]
add(v,x)
i+=2
end
end
end
-- 'sprite vector'
function sprv(anim,frame,x,y,scale)
x=x-flr(128*scale)
y=y-flr(128*scale)
tris = 0 --debug
foreach(anim[frame], function(v)
col = v[1] --first item in each poly is colour
col2 = v[2] --second item in each poly is colour2
t = v[3]
for i=1, #t/6 do
b=(i-1)*6
--scale
if(scale==1) then
x0=t[1+b]
y0=t[2+b]
x1=t[3+b]
y1=t[4+b]
x2=t[5+b]
y2=t[6+b]
else
x0=flr(t[1+b]*scale)
y0=flr(t[2+b]*scale)
x1=flr(t[3+b]*scale)
y1=flr(t[4+b]*scale)
x2=flr(t[5+b]*scale)
y2=flr(t[6+b]*scale)
end
tris = tris + 1
layer_render( { x+x0,y+y0,x+x1,y+y1,x+x2,y+y2, col, col2 } )
end
end)
return tris
end
--@catatafish
-- expects an array in the form { x0, y0, x1, y1, x2, y2, color, color2 }
function layer_render(vbuf)
--for n=1,tri_count do
-- local vbuf = vertexbuffer[n]
local v0x, v0y, v1x, v1y, v2x, v2y, ps = vbuf[1], vbuf[2], vbuf[3], vbuf[4], vbuf[5], vbuf[6]
if v1y<v0y then v0x,v1x = v1x,v0x;v0y,v1y = v1y,v0y;end
if v2y<v0y then v0x,v2x = v2x,v0x;v0y,v2y = v2y,v0y;end
if v2y<v1y then
v1x,v2x = v2x,v1x
v1y,v2y = v2y,v1y
end
if v0y == v1y then -- flat top
rasterizetri_top(v0x,v0y,v1x,v2x,v2y,vbuf[7],vbuf[8])
elseif v1y == v2y then -- flat bottom
rasterizetri_bottom(v0x,v0y,v1x,v2x,v2y,vbuf[7],vbuf[8])
else -- general case
local newx = v0x + ((v1y-v0y)*(v2x-v0x)/(v2y-v0y))
rasterizetri_bottom(v0x,v0y,newx,v1x,v1y,vbuf[7],vbuf[8])
rasterizetri_top(v1x,v1y,newx,v2x,v2y,vbuf[7],vbuf[8])
end -- triangle cases
--end -- triangle loop
end
function rasterizetri_top(v0x,v0y, v1x, v2x,v2y,c1,c2)
if (v1x<v0x) v0x, v1x = v1x, v0x
local height=v2y-v0y
local dx_left, dx_right = (v2x-v0x)/height, (v2x-v1x)/height
-- if v0y<c1_y then
-- v0x-=dx_left*v0y
-- v1x-=dx_right*v0y
-- v0y=0
-- end
-- if (v2y>c1_y+128) v2y=c1_y+128
for y=v0y,v2y do
--if(y>0 and y<128) then
if(c1==c2) then color(c1) else
if(y%2==0) then color(c1) else color(c2) end
end
rectfill(v0x,y,v1x,y)
--end
v0x+=dx_left
v1x+=dx_right
end
end
function rasterizetri_bottom(v0x,v0y, v1x,v2x,v2y,c1,c2)
if (v2x<v1x) v1x, v2x = v2x, v1x
local height=v2y-v0y
local dx_left, dx_right, xend = (v1x-v0x)/height, (v2x-v0x)/height, v0x
-- if v0y<c1_y then
-- v0x -=dx_left*v0y
-- xend-=dx_right*v0y
-- v0y=0
-- end
-- if (v2y>c1_y+128) v2y=c1_y+128
for y=v0y,v2y do
--if(y>0 and y<128) then
if(c1==c2) then color(c1) else
if(y%2==0) then color(c1) else color(c2) end
end
rectfill(v0x,y,xend,y)
--end
v0x+=dx_left
xend+=dx_right
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment