Arduboy Classes and Header files?

Readability is in the eye of the beholder.
The majority of programmers would argue that == false is less readable than !.
Examples here and here.

Yes. I added a “However…” while you were typing your post.

1 Like

If we’re mentioning alternatives for the sake of it, then it makes sense to mention that not is also a viable alternative since it’s one of the alternative operators.
I.e.

if(not arduboy.nextFrame()) { return; }

Is also valid.

Though both == false and not are rarely used.

1 Like

I like how the other method look more than the quoted and the two bytes gained seem a bit meaningless.

1 Like

Yeah, sometimes you get cases like that where savings are unexpected. When looking for savings it pays to try several different approaches before settling on one.

Ive shared my boilerplate code for arduboy games (at least for me) along with my next project. Take a look if you want.

Also, unrelated question, but how to other people have different color arduboys (with the circuit board green/red/blue)?

The colour units were available from the Kickstarter.

Other people have modified their Arduboys by doing stuff like colouring the circuit board with a pen (eried) or lining the case with a sweet wrapper (KeyboardCamper).

(And then obviously the new special edition units are black with gold buttons.)


I also have boilerplate that’s been steadily evolving,
but I’ve only got round to releasing one game because I keep trying to do too many things at once.

A Sharpie seems…risky to say the least, but possible, Im just afraid i will end up with either a bad pen job or a dead arduboy.

Another question.
Say I have this spritesheet (Its very simple).

flappePlayer

How would I draw a specific sprite out of this sheet with the background of the sprite filled? (I remember seeing some function that did this but i cant remember it)

I’m not sure what you mean by having the background filled, but for doing framed stuff, you’ll want to use the Sprites functions.

E.g.

Sprite::drawOverwrite(x, y, image, frame);

Obviously they have to be in the right format as well.
As I mentioned recently on another thread.

1 Like

Alright, So I have this:

const unsigned char flappeSprite[] PROGMEM = {
8, 8, // W/H
0xfc, 0x2, 0xe1, 0xa1, 0xe1, 0x1, 0x1, 0x49, 0x42, 0xfc, 0x00, 0x1, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x1, 0x00, // Flying

0xfc, 0x2, 0x31, 0x9, 0x31, 0x1, 0x1, 0x49, 0x42, 0xfc, 0x00, 0x1, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x1, 0x00 // Falling
}

Will this work?

(Also drawOverwrite was what i was thinking of)

Im planning to do this with another project of mine, I will have a class, and it will contain its own update function, coords, rect, image, etc. But the next layer up (a.k.a the game class) will take care of its drawing since a class should not draw itself.

1 Like

Without testing it looks like it should work if you add the missing semicolon (after the second curly brace).

Aaaaand some vague error that the compiler never bothers to explain:
I wrote this in my class:

  public:
    static const unsigned char image[];
    Rect rect;

    uint8_t rect.width = 10;
    uint8_t rect.height = 10;
    uint16_t rect.x = 5;
    uint16_t rect.y = 22;

And it raises this error

In file included from sketch\game.h:4:0,

                 from C:\Users\weebu\Documents\GitHub\ArduboyWorks\Flappe\Flappe.ino:1:

classes.h:16: error: expected ';' at end of member declaration

     uint8_t rect.width = 10;

             ^

classes.h:16: error: redeclaration of 'uint8_t Flappe::rect'

sketch\classes.h:14:10: note: previous declaration 'Rect Flappe::rect'

     Rect rect;

          ^

classes.h:16: error: expected unqualified-id before '.' token

     uint8_t rect.width = 10;

                 ^

classes.h:17: error: expected ';' at end of member declaration

     uint8_t rect.height = 10;

             ^

classes.h:17: error: redeclaration of 'uint8_t Flappe::rect'

sketch\classes.h:14:10: note: previous declaration 'Rect Flappe::rect'

     Rect rect;

          ^

classes.h:17: error: expected unqualified-id before '.' token

     uint8_t rect.height = 10;

                 ^

classes.h:18: error: expected ';' at end of member declaration

     uint16_t rect.x = 5;

              ^

classes.h:18: error: redeclaration of 'uint16_t Flappe::rect'

sketch\classes.h:14:10: note: previous declaration 'Rect Flappe::rect'

     Rect rect;

          ^

classes.h:18: error: expected unqualified-id before '.' token

     uint16_t rect.x = 5;

                  ^

classes.h:19: error: expected ';' at end of member declaration

     uint16_t rect.y = 22;

              ^

classes.h:19: error: redeclaration of 'uint16_t Flappe::rect'

sketch\classes.h:14:10: note: previous declaration 'Rect Flappe::rect'

     Rect rect;

          ^

classes.h:19: error: expected unqualified-id before '.' token

     uint16_t rect.y = 22;

                  ^

exit status 1
expected ';' at end of member declaration

Its like you need a gosh darn dictionary in order to get these errors

You can’t do that, that’s not legal syntax.

I’m guessing you’re using the Rect defined by Arduboy2?
You can do

Rect rect = { 10, 10, 22, 22 };

Depending on what that’s for you might not even need to make rect a member.

Sort of.
A ‘member’ is something that’s part of a class (e.g. member variable, member function)
A ‘declaration’ is something that declares a thing to exist and sometimes define it (e.g. Rect rect; here is a member declaration because it declares a member).

I’m not 100% sure what an ‘unqualified’ id is, but an ‘id’ is usually an ‘identifier’ which just means the name of a type, function or variable - pretty much anything that looks like a word (including underscores) and isn’t a keyword, number, operator or general syntax symbol.


By the way, you can edit your comments afterwards.
Within the first 5 minutes it won’t even log the comment as having been edited.

I know, but theres this glitch where sometimes it wont notify you even if you reply to that user directly. ex: the previous comments

The rect is for collisions, the sprite is going to iterate through a bunch of objects and check for collisions with any of their rects

And ive just realized I dont even need a class, This game is a port of a previous game I made with python so im better off with having some shared variables I can do some stuff with which will save progmem

It does notify me, I’m just not always immediately to hand.
Either I’m busy typing elsewhere, working on something or making coffee.

Then you’ll probably want to make the rect at runtime, and maybe just store the height and width as constants.

Not having a class might not necessarily save progmem.
It depends on the usage.

Essentially these should be roughly equivalent in terms of memory (RAM and progmem):

int someVariable;

void doSomething()
{
  someVariable = 5;
}

void setup()
{
  doSomething();
}

Vs

class SomeClass
{
private:
  int someVariable;
public:
  void someFunction()
  {
    someVariable = 5;
  }
};

SomeClass thing;

void setup()
{
  thing.doSomething();
}

But in the latter case you can always do:

class SomeClass
{
private:
  int someVariable;
public:
  void someFunction()
  {
    someVariable = 5;
  }
};

SomeClass thing1;
SomeClass thing2;

void setup()
{
  thing1.doSomething();
  thing2.doSomething();
}

Its just that in the python port I took alot of liberties with variables, each class has their own toggle for movement, and some other variables that would not be there if i wanted to make the file as tiny as possible, here though I’m just going to use ? with a public gravity variable to evaluate if some things should happen. There’s just not alot of reasons to use objects in what I’m doing since nothing is truly complex enough for deserve the effort of implementing a full class of it’s own when I can just have a couple of variables and functions.

In that case you could always put that variable in an outer class in charge of doing the movement.

You could just use a variable if you want, just remember to mark it as extern in places where it’s used but not defined.

Off topic question. Is there a combined version of drawRect and fillRect? Where it would both fill in the space you specified and draw an outline around the rect?

Nope.
Probably not considered ‘common’ enough to warrant it (though I would like a function like that for drawing text boxes).
Just call one after the other - fill with one colour, border with the other.

arduboy.fillRect(x, y, width, height, BLACK);
arduboy.drawRect(x, y, width, height, WHITE);

Maybe stick that in a function if you’re going to use it a lot.

// The & is important here
void drawBox(Arduboy2 & arduboy, uint8_t x, uint8_t y, uint8_t width, uint8_t height)
{
  arduboy.fillRect(x, y, width, height, BLACK);
  arduboy.drawRect(x, y, width, height, WHITE);
}