A small C++ Tip. Return values from Constructors
You can’t return anything from a constructor. One way is to use exceptions but those can bring their own issues. Google’s C++ guidelines actually stipulate no exceptions and I’ve heard it from others as well that they prefer not to use them. Some people avoid it by having a mostly empty constructor and then an init() method to do the real initialisation and return a success/fail state.
But there is a simple trick that you can use, just return the state as a reference. Here’s an example. If you want to return more values, use a struct.
#include <iostream>
using namespace std;
class Simple {
public:
Simple(bool& failure) {
failure = true;
}
~Simple() {
}
};
int main() {
bool fail_flag = false;
Simple f(fail_flag);
if (fail_flag)
cout << "failed " << endl;
else
cout << "Success" << endl;
}
This is a screenshot of it as it stands and yes those asteroids are moving! It looks identical to the C version; the only difference is the code, not the appearance.
I got my Pi 4 a week ago and have been doing experiments on it with my Asteroids game. If I disable the line of code that kills the player ship in the DestroyObject() function and just add a return after case tPlayer: and uncomment the code that adds Asteroids when you press A then I can have lots of asteroids on screen. Also set the MAXASTEROIDS #define to 128.
I was interested in seeing what frame rate I got out of it and how much it warmed the PI.
As always bugs are the fault of the creator and mea culpa (my bad!). I can trace this back to my conversion from the Windows source to the Ubuntu version. This line in LoadMask
I made the mistake of starting by trying to convert the final version of Asteroid; all 2,200 lines of C into C++.