From bbdd84afef3694dadfa2bb08c4d0b4ebee8f81dd Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Tue, 27 Sep 2022 22:59:22 +0300 Subject: Performance improvements --- sneed.p8 | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/sneed.p8 b/sneed.p8 index fb29349..e66a476 100644 --- a/sneed.p8 +++ b/sneed.p8 @@ -9,6 +9,8 @@ pl = { ["a"] = 0.7, } +sprite_size = 8 + -- low quality raycast -- returns x, y :: the coordinates of the tile that was hit function shit_cast(x, y, dx, dy) @@ -74,45 +76,38 @@ function cast(x, y, a) end -- returns --- + dist :: distance to the hit --- + tile :: tile index --- + col :: tile column index +-- + dist :: distance to the hit +-- + sprite :: index in spritemap function ray(x, y, a) local tx, ty, lx, ly, dist = cast(x, y, a) - local tile = mget(tx, ty) + local sprite = mget(tx, ty) if lx == 0 then - return dist, tile, 1-ly + return dist, sprite_size * (sprite + 1 - ly) elseif lx == 1 then - return dist, tile, ly + return dist, sprite_size * (sprite + ly) elseif ly == 0 then - return dist, tile, lx + return dist, sprite_size * (sprite + lx) else - return dist, tile, 1-lx + return dist, sprite_size * (sprite + 1 - lx) end end +function _init() + -- I use index 0 (black) in my sprites + palt(0, false) +end function _draw() cls(12) - local col - for col=0,1-1/128,1/128 do - local c,d,da,h,x,y,t - da = col*fov-fov/2 - d,t,c = ray(pl.x,pl.y,pl.a+da) - c *= 8 - d = d*cos(da) - h = flr(128/d) - x = 128*col - y = (128-h)/2 - for row=0,1-1/8,1/8 do - local tc, sy, ey - tc = sget(8*t+c,row*8) - sy = y+row*h - ey = y+(row+1/8)*h - line(x,sy,x,ey,tc) - end - line(x, h+y, x, 128, 3) + rectfill(0, 64, 128, 128, 3) + local x + for x = 0, 127 do + local da = x * fov / 128 - fov / 2 + local dist, sprite = ray(pl.x, pl.y, pl.a + da) + local height = 128 / (dist * cos(da) + 0.1) + local y = (128 - height) / 2 + sspr(sprite, 0, 1, sprite_size, x, y, 1, height) end ?"cpu "..(stat(1)*100).."%",0,0,7 -- cgit v1.2.3