Skip to content

Instantly share code, notes, and snippets.

@Liquidream
Last active October 23, 2021 16:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Liquidream/1b419261dc324708f008f24ee6d13d7b to your computer and use it in GitHub Desktop.
Save Liquidream/1b419261dc324708f008f24ee6d13d7b to your computer and use it in GitHub Desktop.
Useful sprite draw function for PICO-8 (and maybe Lua in general)
--
-- draws a sprite to the screen with an outline of the specified colour
--
function outline_sprite(n,col_outline,x,y,w,h,flip_x,flip_y)
-- reset palette to black
for c=1,15 do
pal(c,col_outline)
end
-- draw outline
for xx=-1,1 do
for yy=-1,1 do
spr(n,x+xx,y+yy,w,h,flip_x,flip_y)
end
end
-- reset palette
pal()
-- draw final sprite
spr(n,x,y,w,h,flip_x,flip_y)
end
@cpgb85
Copy link

cpgb85 commented Mar 10, 2020

Great simple script!

@Lambdanaut
Copy link

Awesome! Thanks for sharing!

@kikito
Copy link

kikito commented Oct 23, 2021

Two comments: first, the function above does one unnecessary draw operation (when xx==0 and yy==0 in the loop - that whole draw call will later be covered by the draw final sprite call at the end)

Second, if you are willing to let go of some corners, you can get a pretty decent-looking outline with only 4 draw operations:

function outline_sprite(n,col_outline,x,y,w,h,flip_x,flip_y)
  -- reset palette to col_outline
  for c=1,15 do
    pal(c,col_outline)
  end
  -- draw outline
  spr(n,x+1,y+yy,w,h,flip_x,flip_y)
  spr(n,x-1,y+yy,w,h,flip_x,flip_y)
  spr(n,x,y+1,w,h,flip_x,flip_y)
  spr(n,x,y-1,w,h,flip_x,flip_y)

  -- reset palette
  pal()
  -- draw final sprite
  spr(n,x,y,w,h,flip_x,flip_y)	
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment