I started by looking at the Arduboy examples that come with the Arduboy source files. Start with the most basic games you can find and study the source code line by line.
If you haven’t coded at before, look up tutorials on C or C++ for beginners. I recommend C since C++ was created as a general purpose version of C, often at the cost of less storage efficiency and the Arduboy is very limited on resources…
Every Arduboy game should have ‘setup()’ and ‘loop()’ methods in an ino file. The former being the first piece of code to execute at run-time and the latter is executed sequentially until the power is switched off. Basically, setup is to declare your game, and loop is to play it.
Technically you can’t actually use “C” in the Arduino compiler, the Arduino compiler compiles C++, which is not a strict superset of C because it has a wide variety of differences.
“often at the cost of less storage efficiency” - This isn’t actually completely true, there are hidden storage costs for using some C++ features, but only a handful of them, like virtual functions and (only in some cases, mostly due to padding) inheritance. Using other C++ specific features such as templates, namespaces, const-correctness, operator overloading and classes typically don’t incur an overhead.
The Arduino compiler is actually C/C++ so it will recognize both since they’re basically the same thing. You can use classes and and templates but coding in this way often consumes more space. I don’t claim to know what type of testing you are doing, but programming in standard class practice form versus generic C structs ends up taking more space from all the tests I’ve done. C is primarily used for systems programming because it is barebones compared to C++ and you don’t have the standard libraries for C++ that make C++ more general purpose. Templates are only adaptable for the programmers sake, I believe it’s at compile time the compiler creates each version of a templated function separately for each case used, not actually saving program space.
Studying C concepts has been much more valuable for Arduboy than C++.
C++ has nice tricks that would be quite handy if not for the limited sketch space of the hardware.
C and C++ are not ‘basically the same thing’, they are strictly different. They might be very similar in appearance and share some functionality, but they are different in terms of both syntax and semantic. What you are talking about is writing C-style C++ code.
A simple way to test whether you are using C or C++: use malloc to allocate a block of memory to a pointer without casting. If you don’t have to cast the result, you’re using C, if the compiler complains that you need to cast the result, you’re using C++. int * ptr = malloc(10)
Classes do not consume more space than structs because they’re the same thing. The only difference between writing class and struct is that the former has private access by default and the latter has public access by default. Templates only use more space if you don’t keep track of how they’re being used. In some cases they can actually save space, like in the typical implementation of std::integral_constant.
Templated functions do create multiple versions of the same function for different types, that’s their intention. Writing a single swap<T>(a,b) function is easier for the programmer than writing swapInt(a,b), swapChar(a,b), swapVoidPointer(a,b) etc for every type. Using the template version uses no more memory than writing each implementation individually.
I’m here to stop beginners being misinformed, so in other words, I’m trying to be helpful here.
It’s fair to say C is similar to C++ and that C tutorials are a good starting point but it’s also important to acknowledge that the two are different, that C is not a subset of C++ and that some bits of code that compile for C will not work in a C++ compiler.
Also saying that using C++ sacrifices storage is incorrect and likely to put a beginner off using C++ altogether. C++ does not cost storage space, some of the techniques used do cost memory, but only some of them, using C++ style code does not automatically mean you’re using more memory.
There’s actually a recommended quickstart guide for getting to grips with the Arduboy itself when it arrives.
(Note that Codebender isn’t used as much any more though, it’s lost favour with some people.)
There’s also an entire tag/category on this website dedicated to guides on how to do various things with the Arduboy: http://community.arduboy.com/c/arduboy/guides
And if you’re struggling with something or can’t find an existing post that solves your problem, you can always ask for help (remembering to provide a copy of the code causing the problem along with your explanation). Even though this is a website about the Arduboy specifically, you can still ask questions about the C++ language in general.
For learning about the Arduino board itself and the IDE you’ll probably be using (until you’re comfortable with it and are ready to try using other tools alongside it), the Arduino website has this page: https://www.arduino.cc/en/Tutorial/Foundations
Note that the information about the pins aren’t necissarily going to be useful to you in the early stages, but the Programming section and the part about the different types of memory available on Arduino boards are handy things you can read even before you have your Arduboy.
As for programming, your best place to look (assuming you have some programming knowledge already) is right here on the Arduboy forums. Most people put the source code for their games up on Github, where you can download it and read it. This won’t always be helpful if you don’t already have your Arduboy to try the code out on, but it provides an insight into how others choose to write their code and helps to illustrate some of the features of the Arduboy library (and the Arduboy2 library, an alternative).
“Some” is putting it mildly. There’s a long list of the things C++ has that C doesn’t.
What I was pointing out is that even if you ignore the things C++ ‘added’ to C, some bits of code written in C behave differently in C++ because C++ has some different rules as well, hence you can only expect about 95-98% of valid C code to compile in a C++ compiler, there’s still (roughly) 2-5% of cases where the rules are different. Attempting to compile valid C code in a C++ compiler and having it refuse to compile because it’s one of the edge cases could leave you mystified if you weren’t aware there were a handful of small but significant differences, which is why I think it’s important to warn beginners when telling them to start with C and move up to C++ afterwards.
Definitely baby steps. Especially if C++ will be your first programming language. There’s no rush though, C++ isn’t something that can be mastered within a year, it’s an ongoing process, much like learning a martial art or (I would presume) an instrument.
I prefer structs to classes (for the Arduboy) since exactly what Pharap said, they are public by default. The standard practice of privitizing member variables and using accessors and mutators in C++ will take extra space and since projects will remain very small in the Arduboy there is not a lot of reason to hide components. Dynamic memory will take extra space whether doing it the C way or the C++ way so I avoid it unless it is completely necessary. It is a difficult subject to understand at first anyway so it’s better left for more advanced projects than what your first few will be.
Feel free to pm me if you have questions. (I may be a mere mortal, but I produce results ;D)
Contrary to what Gavin does, I prefer to use the keyword class and always manually declare what’s public and what’s private so there’s never any ambiguity to people reading the code (even those who don’t have much experience with C++). As for using getters and setters, I tend to use them because they discourage other people from trying to take the address of a member variable and the compiler almostalwaysinlines them anyway.
I completely agree about avoiding dynamic memory use though. I’ve never actually tested it to see how far you can get before it causes issues, but unless you’re only using very small amounts of dynamic memory or you’ve got a very good memory manager, eventually you’ll run into problems. So basically just avoid malloc, free, new and delete until you are ready to find out how they work.