Example of Arduboy2 Sprites

So um, like the title says. Anyone?

Basically I just want to make a dude walk. I was successful with this using ArduboyExtra but I want to use Arduboy2 since it has more support, however I can’t find a way to understand how to use the Sprites class.

Figured it out myself.

pats self on back

Thanks me!

#include <Arduboy2.h>

const byte PROGMEM player_bitmaps[] =
  8, 16,
  0x00, 0x00, 0xc0, 0x20, 0xf0, 0xfe, 0xf6, 0xe0, 0x00, 0x00, 0x03, 0x04, 0xff, 0x03, 0xff, 0x07, // R still
  0x00, 0x00, 0xe0, 0x10, 0xf8, 0xff, 0xfb, 0xf0, 0x00, 0x20, 0x21, 0x22, 0x1f, 0x01, 0xff, 0x03, // R walk 1
  0x00, 0x00, 0xc0, 0x20, 0xf0, 0xfe, 0xf6, 0xe0, 0x00, 0x00, 0x03, 0x24, 0xff, 0x23, 0x1f, 0x07, // R walk 2

  0x00, 0xe0, 0xf6, 0xfe, 0xf0, 0x20, 0xc0, 0x00, 0x00, 0x07, 0xff, 0x03, 0xff, 0x04, 0x03, 0x00,
  0x00, 0xf0, 0xfb, 0xff, 0xf8, 0x10, 0xe0, 0x00, 0x00, 0x03, 0xff, 0x01, 0x1f, 0x22, 0x21, 0x20,
  0x00, 0xe0, 0xf6, 0xfe, 0xf0, 0x20, 0xc0, 0x00, 0x00, 0x07, 0x1f, 0x23, 0xff, 0x24, 0x03, 0x00,

Arduboy2 arduboy;
Sprites sprites;
byte frame = 0;

void setup()
void loop() {
  if (!(arduboy.nextFrame())) return;
  sprites.drawSelfMasked(0, 0, player_bitmaps, frame);
  if (arduboy.everyXFrames(3)) frame++;
  if (frame > 2) frame = 0;

Yeah it took me a few attempts to get it. The documentation isn’t very clear.

If anyone wishes to submit a pull request with improved documentation in the .h file comments, I’d be happy to review and incorporate them. Even text for me to add myself would be fine.

The current documentation is pretty much what was in ARGlib, where it came from.

1 Like

I’d do that if I had a good handle on it already, I only know just enough to get by. I have one small improvement though:

A width and height should be defined on the bitmap variable.

I was trying to figure out where to plug that info in for a good 10 minutes. I don’t think the documentation noted this anywhere, or I’m blind.

Edit: I’m blind, it was in the header description for the class.

I should probably provide an example of a bitmap array in the Sprites class detailed description.

Perhaps checkout ArduboyExtra (my code) from which most of the Sprite code originates:

The example should still be instructive.

You know what ? I’ll write an article on this in the 3th Arduboy Magazine with examples and images.
(the second edition is about to be released, so it is to late to include it in that one)


Yay! \o/ \o/ \o/ \o/

For Sprites::drawExternalMask() does the mask array contain the width and height at the beginning, or only the image array?

Pretty sure the size is only in the core image data, not in the mask data (where it would be redundant).

Yes, main image only, not mask.

How would that even work if you only use drawExternalMask ?

So the correct answer is, if you use the separate functions for sprite and mask, the array for both will contain the width and height.

const unsigned char PROGMEM starFish[] =
// width, height,
7, 8,
// FRAME 00
0x0c, 0xd8, 0x78, 0x2e, 0x3b, 0x68, 0x44,

const unsigned char PROGMEM starFish_mask[] =
// width, height,
7, 8,
// FRAME 00
0x0c, 0xd8, 0x78, 0x3e, 0x3b, 0x68, 0x44,

if you use the combined function: drawPlusMask, arrays are combined and you only need width and height once

const unsigned char PROGMEM starFish_plus_mask[] =
// width, height,
7, 8,
// FRAME 00
0x0c, 0x0c, 0xd8, 0xd8, 0x78, 0x78, 0x2e, 
0x3e, 0x3b, 0x3b, 0x68, 0x68, 0x44, 0x44,

Most of my effort went into the embedded sprite code… only reason for separate sprites and masks is if you want to change the mask but not the sprite for some reason.

That sounds like what the image tool does, but I’m not sure it matches what the code expects to be honest… I just looked a minute ago and could have sworn the mask data is used raw, without skipping 2 bytes in for size info.

1 Like

oh wait, I never use drawExternalMask,

I use drawErase combined with drawSelfmasked (in shadowRunner for the eyes)

drawExternalMask only requires the size in the first image, not the mask. If you put it into the mask as well it screws up the resulting sprite.

I don’t know why but drawPlusMask always ends up with a messed up image for me.

I assure you it works if your image is encoded properly. :slight_smile:

@TheArduinoGuy Post the image, I’ll convert it with our online tool and give you the array AND function to use

Yes please, thank you. If you can cover all the methods under that class, that would be great. :slight_smile: