'$DYNAMIC
'$INCLUDE: 'USELESS.BI'
DEFINT A-Z

REM $STATIC
SUB USgetSprite (X1 AS INTEGER, Y1 AS INTEGER, X2 AS INTEGER, Y2 AS INTEGER, destSegment AS INTEGER, destOffset AS INTEGER, sourceSegment AS INTEGER, sourceOffset AS INTEGER)
'Gets a group of elements into a sprite
IF X1 > X2 THEN SWAP X1, X2
IF Y1 > Y2 THEN SWAP Y1, Y2
totalx = X2 - X1
totaly = Y2 - Y1
dude = 4
DEF SEG = destSegment
POKE (destOffset), totalx
POKE (destOffset + 2), totaly
FOR aa = 0 TO totaly
FOR bb = 0 TO totalx
IF (aa + Y1) < 1 OR (aa + Y1) > USinternalScreenY THEN
  DEF SEG = destSegment
  POKE (destOffset + dude), 0
  POKE (destOffset + dude + 1), 0
  GOTO endofgetloop
END IF
IF (bb + X1) < 1 OR (bb + X1) > USinternalScreenX THEN
  DEF SEG = destSegment
  POKE (destOffset + dude), 0
  POKE (destOffset + dude + 1), 0
  GOTO endofgetloop
END IF
todo& = USgetElement(bb + X1, aa + Y1, sourceSegment, sourceOffset)
pixel = todo& AND 255
IF todo& > 255 THEN Colour = INT(todo& - pixel) / 256 ELSE Colour = 0
DEF SEG = destSegment
POKE (destOffset + dude), Colour
POKE (destOffset + dude + 1), pixel
endofgetloop:
dude = dude + 2
NEXT bb
NEXT aa
END SUB

SUB USputSprite (x AS INTEGER, y AS INTEGER, sourceSegment AS INTEGER, sourceOffset AS INTEGER, destSegment AS INTEGER, destOffset AS INTEGER)
'Puts a transparent sprite on the screen
DEF SEG = sourceSegment
totalx = PEEK(sourceOffset)
totaly = PEEK(sourceOffset + 2)
dood = 4
FOR aa = 0 TO totaly
  FOR bb = 0 TO totalx
    DEF SEG = sourceSegment
    Colour = PEEK(sourceOffset + dood)
    pixel = PEEK(sourceOffset + dood + 1)
    IF pixel <> 0 AND pixel <> 32 AND pixel <> 255 THEN USplaceElement bb + x, aa + y, pixel, Colour, destSegment, destOffset
    dood = dood + 2
  NEXT
NEXT
END SUB

SUB USputSpriteS (x AS INTEGER, y AS INTEGER, sourceSegment AS INTEGER, sourceOffset AS INTEGER, Colour AS INTEGER, destSegment AS INTEGER, destOffset AS INTEGER)
'Puts a transparent single-colour sprite on the screen
DEF SEG = sourceSegment
totalx = PEEK(sourceOffset)
totaly = PEEK(sourceOffset + 2)
dood = 5
FOR aa = 0 TO totaly
  FOR bb = 0 TO totalx
    DEF SEG = sourceSegment
    pixel = PEEK(sourceOffset + dood)
    IF pixel <> 0 AND pixel <> 32 AND pixel <> 255 THEN USplaceElement bb + x, aa + y, pixel, Colour, destSegment, destOffset
    dood = dood + 2
  NEXT
NEXT
END SUB

