Skip to content
paul59 edited this page Jan 27, 2020 · 29 revisions

spr

spr id x y [colorkey=-1] [scale=1] [flip=0] [rotate=0] [w=1 h=1]

Parameters:

  • id : index of the sprite
  • x : x coordinate where the sprite will be drawn, starting from top left corner.
  • y : y coordinate where the sprite will be drawn, starting from top left corner.
  • colorkey : index (or array of indexes) of the color in the sprite that will be used as transparent color. Use -1 if you want an opaque sprite.
  • scale : scale factor applied to sprite.
  • flip : flip the sprite vertically or horizontally or both.
  • rotate : rotate the sprite by 0, 90, 180 or 270 degrees.
  • w : width of composite sprite
  • h : height of composite sprite

Description:

Draws the sprite number index at the x and y coordinate.

You can specify a colorkey in the palette which will be used as the transparent color or use a value of -1 for an opaque sprite.

The sprite can be scaled up by a desired factor. For example, a scale factor of 2 means an 8x8 pixel sprite is drawn to a 16x16 area of the screen.

You can flip the sprite where:

  • 0 = No Flip
  • 1 = Flip horizontally
  • 2 = Flip vertically
  • 3 = Flip both vertically and horizontally

When you rotate the sprite, it's rotated clockwise in 90° steps:

  • 0 = No rotation
  • 1 = 90° rotation
  • 2 = 180° rotation
  • 3 = 270° rotation

You can draw a composite sprite (consisting of a rectangular region of sprites from the sprite sheet) by specifying the w and h parameters (which default to 1).

Example

Example

-- spr demo

--Build sprite from text
data="010000000160010015240160170170170170170175170166250255170170170175106170170170170170051051051051"
for i=0,31 do
 poke(0x4000+32+i,
 tonumber(string.sub(data,i*3+1,i*3+3)))
end

p={
 x={val=100,min=0,max=240},
 y={val=68,min=0,max=136},
 colorkey={val=0,min=-1,max=15},
 scale={val=1,min=0,max=256},
 flip={val=0,min=0,max=3},
 rotate={val=0,min=0,max=3},
}
sel=next(p,nil)
  

function TIC()

 --Keys
 if btnp(0,30,6) then 
  for n=0,4 do
   sel=next(p,sel) or next(p,nil)
  end
 end
 if btnp(1,30,6) then
  sel=next(p,sel) or next(p,nil) 
 end
 
 if btnp(2,30,6) then
  p[sel].val=p[sel].val-1  
 end
 if btnp(3,30,6) then
  p[sel].val=p[sel].val+1
 end 
 
 --Clamp
 if p[sel].val>p[sel].max then
   p[sel].val=p[sel].max
 end
 if p[sel].val<p[sel].min then
   p[sel].val=p[sel].min
 end

 cls(0)
 print("Use up/down  to select parameter",0,0)
 print("left/right to change its value:",0,10)
  
 --Build menu with cursor
 r=0
 for k,v in pairs(p) do
  cur=(k==sel) and '>' or ' '
  print(cur..k..':'..v.val,0,30+10*r)
  r=r+1
 end 
 
 --Draw Sprite
 spr(1,
    p.x.val,
    p.y.val,
    p.colorkey.val,
    p.scale.val,
    p.flip.val,
    p.rotate.val)

end

Example

Sometimes you might want to copy sprite data to a table for manipulation, rather than display it on the screen directly. This example shows how to get pixel data for a given sprite by supplying its ID, width and height:

-- parameters: sprite number, width and height of the sprite(s). 
-- returns a table containing pixel data
function gspr(num, w, h)
  local addr=0x4000
  local offset = w * num % 128
  local hoffset= h * math.floor((w*num)/128)		
  local g = {}
		
  for x=0,w-1 do
    g[x] = {}
    for y=0, h-1 do
     g[x][y] = peek4((addr + (x//8+y//8*16)*32) *2+x%8+y%8*8) 
    end
  end
  return g
end
Clone this wiki locally