Saturday 13 November 2021

LP's 12 Months of Games // Thomas Was Late [C++ & SFML]

Completed Via Tutorial

The 4th game taught in "Beginning C++ Games Programming" by John Horton and there are some more things I found strange:
  • The engine class had 4 functions but only the constructor and 1 other function were defined in the cpp; the others were given their own cpp file....I have honestly never seen this. I can understand wanting to keep the code "manageable" but just collapse the function? There are class definitions in Unreal Engine thousands of lines long. If you're having trouble use Alt+G to directly go to the function or just Shift+F and search.
  • The private const member variables in classes. If they're private, they can only be used within the class anyway so move them out of the class and into a private namespace in the cpp. Those variables are padding out the size of the class; all those bytes add up; especially if you are creating many instances of the class (as games often do). The const variables in the namespace will still be static but at least they're only being created once instead however many times you have created instances of the class they were in.
  • There are also other implications on your classes to do with move/copy constuctors caused by const members.
  • I've also seen some "Variable const x", especially in function argument parameters. Yes the compiler will accept it but it's good practice when declaring a const variable to have the const before the type. You may see const after a function declaration but that is promising that function doesn't modify any member variables inside the function. The const before the function return type specifies that return value cannot be modified.
  • The given code doesn't actually work or compile. 
  • Pointer pointers were introduced....in a book aimed at those who have never programmed before....I have no words. I know you can't create a 2d array without using values defined at compile time but that's because it's an array; just use a vector. This is not Beginning C Games Programming.
  • The LevelManager should be holding an instance of the level vector array, this eliminates the need to new anything up. In the original code the pointer is returned to a different class. If it has to be a pointer at least ensure that whatever is holding the pointer frees it properly in the destructor (or better yet, use smart pointers).
  • He includes headers in cpp's that have already been included in the matching header file.
I will admit, I did find this funny though:


Finished Result:
The tutorial spanned from chapters 14 - 18 and I stopped halfway through 17 because the game is severely broken:
LP's 12 Months of Games // Thomas Was Late [C++ & SFML]

I don't have time to get this working nor do I really care. There was something going on with the drawing and ordering of the sprites vs the background. Left and right were only for the green shape, A and D were for the red. It knew this when input was received yet for some reason it decided to apply the updates to some other sprite.

There's only space invaders left now which should be more fun; this one was a bit dry.

Code:
No code drop for this one. The authors repository for the book is here though:

No comments:

Post a Comment