Skip to content

Instantly share code, notes, and snippets.

@Achie72
Last active April 10, 2023 12:39
Show Gist options
  • Save Achie72/a249edb0e1c32cdaedf721726f2d8d34 to your computer and use it in GitHub Desktop.
Save Achie72/a249edb0e1c32cdaedf721726f2d8d34 to your computer and use it in GitHub Desktop.
Pico-8 Sprite with color outline
-- https://ko-fi.com/achiegamedev
-- draw the myspr sprite at x and y with a 1 pixel
-- wide outline of clr. if clr is not given on the call
-- default 7 is used instead. thickness is how thick the outline
-- should be in pixels. defaults to 1 if none given
function draw_outline(myspr, x, y, clr, thickness, x_size, y_size, flip_h, flip_v)
-- nil check for few parameters so you can
-- call much simple versions of the function, see first example in draw
-- nil is false if checked for boolean so flip_h and flip_v can stay nil
if (clr == nil) clr = 7
if (thickness == nil) thickness = 1
if (x_size == nil) x_size = 1
if (y_size == nil) y_size = 1
-- set color palette to outline
for i=1,15,1 do
pal(i, clr)
end
-- handle black outline transparency issues
if clr == 0 then
palt(0, false)
end
-- draw the sprite 9 times by 1-1 offsets
-- in each direction. the created blob is
-- which is the sprite's outline
for i=-thickness,thickness do
for j=-thickness,thickness do
spr(myspr, x-i, y-j, x_size, y_size, flip_h, flip_v)
end
end
-- reset black color transparency
if clr == 0 then
palt(0, true)
end
-- reset color palette, if you are using
-- a custom palette reset to that
pal()
-- draw the original sprite in the middle
-- which causes the outline effect
spr(myspr, x, y, x_size, y_size, flip_h, flip_v)
end
function _draw()
cls()
-- draw a simple 8x8 sprite
-- with outline color white = 7
draw_outline(1,10,10,7)
-- draw a simple 8x8 sprite
-- with outline color white = 7
-- horizontal flipped
-- we need the thickness and
-- the size as well for the
-- params, so 1,1,1 where first
-- is outline thickness second
-- two is sprite tile size like
-- normal spr(), then we can flip
-- with a true. Same sprite as first
-- but flipped
draw_outline(1,10,20,7,1,1,1, true)
-- draw a 16x16 sprite,
-- outline color 9, two wide
-- flipped horizontal
draw_outline(2,10,60,9,2,2,2,true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment