Hey I thought I'd introduce myself and start a topic - what projects are you all working on?


(Pharap) #21

Draw it on by hand.
(It’s a shame it’s not easy to add new fonts/glyphs to the label maker though.)


#22

Maybe I’ll try etching it on to the handles of all my metal tools via anodization!


(Scott R) #23

But then there’s always That Guy

Saltwater etching works ok on knives.


(Pharap) #24

The world needs more of these people.
May everything be hackable!


#25

I think my unit is a brother (I forget the model though), it’s one of the vertical style handhelds. My weapon of choice is pic though so I may roll my own mod if I get bored on a rainy day. For now though I have far too many projects I need to finish.


#26

I’ve been using SoloLearn’s C++ Tutorials the past few days. I’m through half of it. I knew most already because programming is mostly the same everywhere. I’m finally at functions and classes. I learned a few minutes ago that “Arduboy arduboy;” is an instantiation of the Arduboy class.

I’m gonna have a lot of fun with C++

@chame Your space shooter looks amazing. I never played Capcom 1942 but I can’t wait to play your game


(Pharap) #27

Usually that’s the case, but be warned that C++ is quite a different beast from languages like Java and C#.
The syntax is similar in places, but the semantics can be drastically different.

For example, in Java and C# objects are allocated on the heap and variables only hold references to the objects,
whereas in C++ the variables hold the objects themselves, not merely references, thus assignment actually means copying the object, not a reference to the obejct.

(And of course, there’s no garbage collector.)


(Matt) #28

For me the biggest shift after having not used C++ in over a decade was not having a runtime to check array bounds for me. Wrote past the end of an array? C++ don’t care :slight_smile: And man it can cause some very hard to track down bugs.


(Scott) #29

Yes, it is, but note that you’ll probably want to use the Arduboy2 class from the Arduboy2 Library. The standard is to still name the instance arduboy, though.

Arduboy2 arduboy;

Help me with C++ for my game
#30

would doing new MyClass varName; make it act like instances in C# and Java and other languages?


(Matt) #31

The syntax is MyClass *varName = new MyClass();

kind of. That would place the object on the heap instead of the stack or in global memory. But C++ has no garbage collector, so it would be up to you to later delete it when you were done with it. In Arduboy programs it’s very possible you’d just never delete it.

The downside is this is now dynamic memory. This means you will bring the memory manager into your sketch, which takes up precious space. You also run the risk of memory fragmentation which can cause your game to crash.

General rule of thumb, avoid dynamic memory on arduino unless you have a really good reason to use it.


(Simon) #32

Amen brother.     

Waiting on @Pharap to jump in.


(Pharap) #33

In a word: no.

The closest you can get to C# and Java’s behaviour on the Arduboy is through using references and pointers.

In a full-scale C++ environment (i.e. on a laptop/computer) there’s the option of std::shared_ptr which functions much more similarly to C# and Java’s references, but it’s not available for most Arduino chips.

The reason for this is the same reason you can’t program for the Arduboy using C# or Java.
Traced references and garbage collection require a lot of power and memory - power and memory than the Arduboy’s 8-bit AVR-based ATmega32u4 chip just doesn’t have.

But that’s ok, because using the objects directly instead of using references is a lot more efficient in terms of both speed and memory.

It can be more difficult at times and you have to know what you’re doing, but that’s C++'s philosophy - hand extraordinary amounts of power and control to the programmer, and trust them to not shoot themselves in the foot.


As @city41 has just pointed out, C++ does have a new keyword, but it’s not quite the same as what C# and Java do.

It’s similar in that it allocates an object and returns a kind of reference, but it’s also quite different, and a lot more dangerous.
Firstly, what it actually returns is a pointer, which isn’t the same as a C#/Java reference type, it’s much more dangerous.
Secondly, allocating on the heap is already dangerous because a lot can go wrong with a heap.
Dynamic/heap allocation is the most common way for programs to end up with memory leaks, and there’s a chance of hitting a problem called ‘heap fragmentation’.

On top of that, you run into the issue of running out of memory.
On a full-scale computer, that takes quite some time, though it still happens.
On the Arduboy, that happens very quickly.
When that happens, your Arduboy will crash and reset itself if you’re lucky.
If you’re unlucky, your program could keep running with corrupted memory, causing unpredictable problems.


Before you go learning about pointers, I recommend learning about C++'s references first.
Unfortunately most tutorials teach pointers first, which are more flexible, but also more dangerous and much easier to screw up.

Here’s an example Arduboy program that demonstrates the use of a mutable reference:

#include <Arduboy2.h>

// Pass by value
void tryToModify(int value)
{
	value = 5;
}

// Pass by reference
void modify(int & value)
{
	value = 5;
}

Arduboy2 arduboy;

void setup(void)
{
	arduboy.begin();
}

void loop(void)
{
	if(!arduboy.nextFrame())
		return;
		
	arduboy.clear();
	
	int a = 1;
	
	arduboy.println(a);
	
	tryToModify(a);
	arduboy.print(F("Pass by value: "));
	arduboy.println(a);
	
	modify(a);
	arduboy.print(F("Pass by reference: "));
	arduboy.println(a);
	
	arduboy.display();
}

There’s more to it than that, but that’s a good introduction.

If you’re looking for tutorials, I recommend this one:

It has an introduction to references here and here.


If you have any other questions, I’m happy to answer them.

(E.g. “What’s the heap?”, “What’s a pointer?”, “What’s dynamic memory allocation?”, “Why are these languages so different?”, “What’s the relationship between RAM and a pointer?”)


I was here first, my text walls just take longer to type. :P


#34

Thank you! I’ll read what you linked tomorrow, it’s already late where I am. I apparently got a lot of learning to do


(Matt) #35

This is one of my favorite things about the Arduboy as far as it being a teaching tool. Beginner programmers (not saying you are a beginner @SamSibbens, I’m going on a bit of a tangent here) are given a clean, simple API and easy to use IDE that makes it pretty easy to make simple games. But if the beginner gets hooked and gets into it, C++ and tight constraints of the platform provide excellent learning experiences. I wish I had an Arduboy when I was a kid.


(Pharap) #36

C++ is indeed a massive language, but you don’t have to learn absolutely everything about it to be able to use it effectively, and you don’t have to learn everything at once.

It does force you to learn about how the hardware works, but that’s a good thing, because knowing how the hardware works makes you a better programmer overall.

I didn’t see my first line of code until I reached college,
so every time I see someone say “I learned BASIC when I was 12” I get annoyed at what could have been.

Now the government has finally pulled their finger out and made programming a mandatory part of the curriculum, but it’s too little, too late as far as I’m concerned.

</grumble>

I’ve certainly learned a lot about microoptimisation, when to trust the compiler, when not to trust the compiler and what you can reasonably expect the compiler to do (for AVR at least).