Sprites to the people!

I’ve done quite a lot of programming today, and I’ve got to say the new tool that automatically compile and download to the Brick is awesome! I’ve also made it so it automatically starts the downloaded program, so it is a real time saver!

In any case, besides modifying the tool so it also starts the downloaded app, I’ve also made an image converted that can output sprites with transparency, and I’ve made a function that read the sprite and draws it on screen on the Brick.

Look!sprites

As you can see there’s a character sprite that are drawn in two different places in this example, and it has three colors. Black, white and transparent.

This is the code to load the sprite:

  HANDLE hFile
  DATA32 FileSize32
  DATA8 BombWidth
  DATA8 BombHeight

  HANDLE BombData
  FILE( OPEN_READ, 'spritetest.spr', hFile, FileSize32 )
  FILE( READ_BYTES, hFile, 1, BombWidth )
  FILE( READ_BYTES, hFile, 1, BombHeight )
  SUB32( FileSize32, 2, FileSize32 )
  ARRAY( CREATE8, FileSize32, BombData )
  CALL( File_ReadToArray, hFile, BombData, FileSize32 )
  FILE( CLOSE, hFile )
subcall File_ReadToArray
{
  IN_16   FileHandle
  IN_16   ArrayHandle
  IN_32   BytesToRead

  DATA8   TempData
  DATA32  WriteIndex

  MOVE32_32( 0, WriteIndex )

Loop:
  FILE( READ_BYTES, FileHandle, 1, TempData )
  ARRAY( WRITE_CONTENT, -1, ArrayHandle, WriteIndex, 1, TempData )
  ADD32( WriteIndex, 1, WriteIndex )
  JR_LT32( WriteIndex, BytesToRead, Loop )
}

As you can see I’ve created a helper function that take a file handle and load it into an array. I am planning on making a function that accept a filename and return an array handle to completely wrap it up, but in this case I want to read the width and height of the image before reading the rest into an array.

I was also planning on reading the width and height as 16 bit values so a sprite can be bigger than 127 but then I figured I want to have a function that is something like ReadSprite, and it would return a single handle for me to use, and ten I could use that handle with a function like DrawSprite, and the handle would hold both width, height and pixel data.

If I try to do that I would create two arrays. One array would be with three 16 bit numbers, and the first number would be width, second number would be height, and third number would be a handle to an 8 bit array that holds the pixel data. Kinda like a little object. :)

Anyways, here is the code that draws the sprite:

subcall DrawSprite
{
  IN_8    SpriteWidth
  IN_8    SpriteHeight
  IN_16   SpriteDataHandle
  IN_16   ScreenX
  IN_16   ScreenY

  DATA32  ReadOfs
  DATA16  WriteX
  DATA16  WriteY
  DATA16  MaxX
  DATA16  MaxY
  DATA8   ReadPixel
  DATA8   WritePixel

  // Setup
  MOVE8_16( SpriteWidth, MaxX )
  ADD16( MaxX, ScreenX, MaxX )

  MOVE8_16( SpriteHeight, MaxY )
  ADD16( MaxY, ScreenY, MaxY )

  // The whole thing
  MOVE32_32( 0, ReadOfs )
  MOVE16_16( ScreenY, WriteY )

Loop_Y:
  MOVE16_16( ScreenX, WriteX )

Loop_X:
  ARRAY( READ_CONTENT, -1, SpriteDataHandle, ReadOfs, 1, ReadPixel )
  JR_EQ8( ReadPixel, 0, DoneDrawing )   // Don't do any drawing at all
  JR_EQ8( ReadPixel, 1, DrawBlack )
  MOVE8_8( BG_COLOR, WritePixel )       // Set WritePixel to White
  JR( Draw )

DrawBlack:
  MOVE8_8( FG_COLOR, WritePixel )       // Set WritePixel to Black

Draw:
  UI_DRAW( PIXEL, WritePixel, WriteX, WriteY )

DoneDrawing:
  ADD32( ReadOfs, 1, ReadOfs )
  ADD16( WriteX, 1, WriteX )
  JR_LT16( WriteX, MaxX, Loop_X )

  ADD16( WriteY, 1, WriteY )
  JR_LT16( WriteY, MaxY, Loop_Y )
}

Leave a Reply