Friday, 19 November 2021

C++ & SFML // Simple Event Queue System with Timings

In a previous post (C++ & SFML // Simple Timer), I showed how you can create a simple timing system in SFML to display messages on the screen.

In this one I created a very simple "Messaging Dispatcher" that receives messages and displays them. I followed along with the Ring Buffer design by Robert Nystrom in "Game Programming Patterns" to create this little demonstration.

C++ & SFML // Simple Event Queue System with Timings

Here, when E is released, it fires a message to the dispatcher. Each message has a random time to display between 1 and 4 seconds.

The code can be found here:

There are a few caveats with this simple system:
  • The queue is a fixed size so it will reject messages when full,
  • The queue doesn't create an empty slot until the top message has finished displaying; if this was set to display for 5 minutes and all the other messages in the queue only want to display for 1 second, the queue will be held up waiting for the top message to finish.
  • The dispatcher shouldn't really be handling the time.
  • Everything is passed by value.
All these things are fixable with some brain power but getting the first implementation out of the way is key to figuring the fixes out. For the next step, it would make sense to create a class that handles the updating and displaying of the messages that it receives from the dispatcher. This way the dispatcher only has to worry about getting the messages to the right place each update.

As the class is static, you could have anything send messages to the dispatcher. it could also be modified to display sprites or play sounds (although I would define a set of events as enums instead of sending the sprite/sound itself).

The code may look a little odd in the dispatcher but the above link to the chapter in Robert's book explains what it's doing far better than I will be able to.

Thursday, 18 November 2021

C++ & SFML // Simple Timer

So I'm currently working my way through making a clone of Space Invaders using SFML and I got to the point where I needed to do something after X amount of seconds. Usually I use FLTK and that would've been simple using it's callback system however SFML is event based and as such doesn't really supply a "timer system" out of the bag like fltk.

Therefore I googled and eventually found pieces of what I was looking for and managed to put this simple demonstration together for those who just want to figure out how to get seconds displayed on the screen. This is what the "tutorial" will give you:

C++ & SFML // Simple Timer displaying seconds passed

The program does 2 things:
1) Display the number of seconds since the program started
2) Every second it chooses a random message (from 4) to display.

Very exciting. Let's begin.

Here is the full code:

Have a look, it's all in main for simplicity. 

There is a clock that is restarted at the beginning of the main loop and assigned to DeltaTime.
SFML have handily provided a way to get the DeltaTime as seconds. All you have to do then is += that to a variable defined outside the loop and you have your seconds since the program started.

For the text, I just made a variable that holds the max time we want to show the text for (maxDelay) and another variable to hold the elapsedTime. Getting the elapsed time is the same as getting the seconds above only when updating which message to display, there is a check to see if the elapsed time has gone above the max delay. If so, it changes the message and resets the elapsed time back to zero.

The concepts here can be used for many other things like displaying a sprite for a given amount of time as well creating more complicated things, like an event queue system that dispatches events based on the elapsed time (much more useful for larger projects than above).

for an example on how this can be put together.

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:

Friday, 12 November 2021

LP's 12 Months of Games // Zombie Arena [C++ & SFML]

Completed Via Tutorial

Another tutorial finished from "Beginning C++ Games Programming" by John Horton. 

Please do not use this book to learn C++. Some things boggled the mind when following this tutorial like:
  • variable + 0 (???)
  • Creating a header, only for it to contain functions that are neither marked as extern or static or within a namespace. One function returns a raw pointer which is deleted somewhere else...
  • #pragma once #ifndef #define combo (pick one. I like ifndef define because it's slightly more portable).
  • Constant float to int/int to float with no casting or safety in place. For example, he'll choose to make an int variable but only use it with a float???
  • Singleton classes. Don't get me wrong, I like singletons but this is not the best way to do it. The constructor should be private with copy and assignment deleted. There should instead be a static public function that declares and returns a reference to a static instance of the class to ensure it is destroyed properly.
  • Member functions that don't modify are not marked as const nor is anything returned by const reference if it can be.
  • Initialises class members in the constructor body instead of via initialiser list; this causes the member variable to be default initialised and then assigned again.
  • Const member variables are initialised in the class body definition??? static const integral types yes, but otherwise for linking sake, it's best to initialise const members in the constructor.
One thing that really bugged me was all the event handling for input was done in main. I am a firm believer that objects should handle input events themselves. That said, the author promised at the end of this game that there will be better code handling in the next two projects so I'll hold further judgement until I finish the book.

Finished Result:

LP's 12 Months of Games // Zombie Arena [C++ & SFML]

I was having fun with this one by the end. I quite like "Demakes" and I think this style would make a good Left 4 Dead demake. The hitboxes on the zombies are a bit janky and I'd like some background music but other than that I'm actually pretty happy with this one. 

It taught some solid SFML concepts that I can take away to personal projects. I'm itching now to start Tetris or my own version of pong to apply what I've learnt but I'm going to finish the book first as he mentioned using more advanced sound features, particle effects an split-screen in the upcoming chapters.

Code:

There's a code drop for this one as I feel I changed it enough. Assets haven't been included for copyright reasons but here is the authors repository:
https://github.com/PacktPublishing/Beginning-Cpp-Game-Programming-Second-Edition

Tuesday, 9 November 2021

Chapter 22 // Exercise 7, 8, 9, 10, 11, 12, 13, 14 - Principles & Practice Using C++

The following exercises are once again all word-y ones. Some of this stuff is interesting to learn but I'm not writing any essays; I've done my time.

Chapter 22 // Exercise 7

We have "missed" many important languages. In particular, we essentially had to cut all developments after C++. Make a list of five modern languages that you think ought to be covered and write a page and a half (lol) - along the lines of the language sections in this chapter - on three of those.

So I guess the 4 "major" ones not covered would be; Python, Java, Javascript (and all it's fecking friends) and C#. I couldn't decide on the 5th but I guess Objective-C is pretty popular with those iOS programmers *shudder*.

Chapter 22 // Exercise 8, 9, 10

8) What is C++ used for and why? Write a 10- to 20-page report.
9) What is C used for and why? Write a 10- to 20-page report.
10) Pick a language (not C or C++) and write a 10- to 20-page description of its aims, origins and facilities.


In all seriousness; C++ is used by people who like programming and C is used by people who like torturing themselves; just use C++ (unless you're in embedded systems or something).
Java - Used by people for reasons unknown.

Chapter 22 // Exercise 11

Who currently holds the Lucasian Chair in Cambridge?

According to this source:
It's Professor Michael Cates. I had no idea what the Lucasian Chair was before this; it was founded in 1663 and is one of the most prestigious academic postings in the world? Apparently, in Star Trek: TNG, Data is the current holder.

Chapter 22 // Exercise 12, 13, 14

12) Of the language designers mentioned in this chapter, who has a degree in mathematics, who does not?
13) Of the language designers mentioned in this chapter, who has a Ph.D? In which field? Who does not have a Ph.D?
14) Of the language designers mentioned in this chapter, who has received the Turing Award?

David Wheeler: Awarded the worlds first PhD in Computer Science in 1951 (very cool). 
Maurice Wilkes: PhD in Physics. Turing Award in 1967.
Stanley Gill: Mathematic degree (I think). Has PhD, not sure what in.
John Backus: Doesn't have a PhD, but has a bachelors and masters in mathematics. Turing Award in 1977.
Grace Murray Hopper: PhD and BSc in mathematics.
John McCarthy: PhD and BSc in mathematics. Turing Award in 1971.
Peter Naur: PhD in Philosophy. Turing Award in 2005.
Edsger Dijkstra: Has a PhD, doesn't say what in. I'm guessing Computer Science. Turing Award in 1972.
Niklaus Wirth: PhD in Electrical Engineering and Computer Science. Turing Award in 1984.
Anders Hejlsberg: Has no PhD but is the current lead dev for C# at Microsoft, found him on Teams. I always find it amusing that I can just send fellow Microsoft employees a DM. There is a part of me that just wants to send Satya Nadella "yo whattup??". But I like my job and don't want to lose it.
Kristen Nygaard: Msc in Mathematics. No PhD. Turing Award in 2001.
Ole-Johan Dahl: Unsure what education he has. Turing Award in 2001.
Dennis Ritchie: BSc in Physics and Applied Mathematics. PhD never officially received. Turing Award in 1983.
Ken Thompson: BSc in eletrical engineering and computer science. No PhD. Turing Award in 1983.
Doug McIlroy: BSc in Engineering Physics. PhD in Applied Mathematics. 
Brian Kernighan: BSc in Enegineering Physics. PhD in Electrical Engineering.
Martin Richards: BSc in Mathematics. PhD in Programming Language Design and Implementation.
Christopher Strachey: BSc in Natural Sciences Tripos.
Bjarne Stroustrup: MSc in Mathematics and Computer Science. PhD in Computer Science.
Alex Stepanov: Not a lot of info on this man.