Log In  

Not sure if anyone has posted a smap() yet, but it's reasonably straightforward to implement with tline, so here's mine.

Parameters are:
cx,cy,cw,ch Specify a region in the tile map. Measured in tiles.
sx,sy,sw,sh Region on the screen to map to.
flipx,flipy Whether to flip horizontally and/or vertically.
layers Layer flags. Same as for map().

You need to supply at least the c and s parameters.

function smap(cx,cy,cw,ch,sx,sy,sw,sh,flipx,flipy,layers)

 -- negative screen sizes
 if(sw<0)flipx=not flipx sx+=sw
 if(sh<0)flipy=not flipy sy+=sh
 sw,sh=abs(sw),abs(sh)

 -- delta
 local dx,dy=cw/sw,ch/sh

 -- apply flip
 if flipx then
  cx+=cw
  dx=-dx
 end
 if flipy then
  cy+=ch
  dy=-dy
 end

 -- clip
 if(sx<0)cx-=sx*dx sx=0
 if(sy<0)cy-=sy*dy sy=0
 if(sw>128)sw=128
 if(sh>128)sh=128

 -- render with tlines
 -- pick direction that results
 -- in fewest tline calls
 if sh<sw then
  -- horizontal lines
  for y=sy,sy+sh-1 do
   tline(sx,y,sx+sw-1,y,cx,cy,dx,0,layers)
   cy+=dy
  end
 else
  -- vertical lines
  for x=sx,sx+sw-1 do
   tline(x,sy,x,sy+sh-1,cx,cy,0,dy,layers)
   cx+=dx
  end   
 end
end

Cart #mot_smap-0 | 2020-07-22 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
11

P#79752 2020-07-22 05:44

This is so cool! Thanks for sharing! :)

P#79756 2020-07-22 09:20

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2022-11-27 18:19:44 | 0.008s | Q:12