A few months ago on KickStarter I sponsored a project called the Gameduino, an add on shield for the Arduino, advertised as a game adapter for the Arduino. This is an awesome little device that has 32Kb of RAM, brings support for VGA output, stereo sound and has a co-processor that runs forth. See the official site for more information: http://excamera.com/sphinx/gameduino/. About two weeks ago mine arrived and since then in some of my spare time I’ve been working out how to output sprites and generally figuring out how it works. In this post I’ll explain a bit about how to display sprites and animate them.
Before you start I’d suggest having a look at the reference poster here. Also, there’s an excellent reference book from derek available on the gameduino answer page. There’s a lot of very useful information regarding the memory layout and how the coprocessor works.
The Basics
The Gameduino is capable of displaying 256 sprites with each sprite being a 16 by 16 pixel image with a byte for each pixel. Each sprite has a 32bit control word that defines the sprite number, xy coordinates, rotation, collision class, and palette. The palette for a sprite defines what colour each pixel will be displayed as, there are two 4 colour palettes, two 16 colour palettes and four 256 palettes.
Getting Started
So, first things first, to display a sprite we need an image. We can write it manually in hexadecimal however there’s a gameduino python package available here that has a number of methods for converting images and sound for a format suitable on the Gameduino. After you’ve installed the package you can run this quick script I’ve put together To use the script create your 16x16 image and save it as a PNG file, then in the commandline/ terminal enter:
_python imageToH.py image_path.png header_name _
If you are using more than 4 colours (including transparency) then you’ll want to change the number of colours in this line ‘sprite = prep.palettize(im, 4)’ to be 16 or 256 depending on how many colours you’re using. You’ll also want to change this line_‘ir.addsprites(spritename, (16, 16), sprite, prep.PALETTE4A)’ to use _PALETTE16A/B _or PALETTE256A/B/C/D. This script will create a header file, which we’ll then include in our program.
Program steps
Below are the list of actions that are needed to display a single sprite.
- Include the SPI and gameduino header files
- Include the image header file created previously
- Begin the gameduino library
- Set the background colour
- Set up the palette with the colours we want to use
- Copy the sprite image into RAM
- Set the sprite control word and display
This code to display a single sprite can be found here
If you’ve been following so far then upload the code to your arduino and connect the gameduino to a monitor and you should see a little alien in the middle of the screen.
Animation
There are a few different ways that we can animate an image on the Gameduino.
The first method is perhaps the most obvious, we can switch the image that the sprite control word will display. If for example we have a sprite that is of a man with his arms out and then another sprite of a man with his arms up, and we flick between these sprites quickly enough it will look like the man is waving his arms.
I have modified the code to display a single sprite so that it loads two images into RAM and then switches between these after about 0.5 seconds. The code can be found here. It should display an alien jiggling around.
The second method is slightly different as we only need to use a single sprite. Say we have a sprite with a pair of eyes that are looking leftwards by changing the 3 bits in the sprite control word that control the rotation we can change the eyes so that they are looking towards the right.
A thrid way of animation does not involve switching or flipping the sprite, rather we change the colours that are displayed for the sprite. We can do this by changing the palette that is used to display the sprite.
You can find the three ways of animating sprites on the github project I’ve set up: https://github.com/biomood/SpriteTest-Gameduino