Wednesday, 21 February 2024

Chapter 2 // Exercise 1, 2, 3, 4, 5, 6, 7 - The C++ Programming Language


I bought this book a long time ago. The current edition was printed in 2013 and features up to C++11. On his website, Bjarne said he's never felt a need to update it but with all the new features in C++17 and C++20, I feel a 5th edition should be on the horizon and I'm wondering if it'll get released when I'm halfway through this one.

But either way. Welcome to this post featuring the exercises starting at chapter 2. I've done it this way as the first chapter was just writing exercises. 

Unlike Principles & Practice, the exercises can be found online and are not actually in the book:

Chapter 2 - A Tour of C++: The Basics
Exercise 1
When first reading this chapter, keep a record of information that was new or surprising to you. Later, use that list to focus your further studies

Will do.

Exercise 2
What does a compiler do? What does a linker do?

A compiler processes the source text from a project and turns it into object files. The linker turns the object files into an executable.

Exercise 3
Get the ‘‘Hello, world!’’ program (§2.2.1) to run. This is not an exercise in programming. It is an exercise to test your use of your edit-compile-link-execute tool chain.


Hello, world my old friend. I'm using Visual Studio Community 2022 with C++20 enabled. I'm also using my modified version of "std_lib_facilities" that's given in Principles & Practice. It can be found here:

Exercise 4
List three (or more) C++ compilers

GNU GCC, Clang, Intel C++ Compiler

Exercise 5
Write out a bool, a char, an int, a double, and a string.


Exercise 6
Read in a bool, a char, an int, a double, and a string.


Exercise 7
What is an invariant and what good might it do?

From what I can understand from Section 2.4.3.2 an invariant is a way of checking or asserting that some logic on class member variables is correct. So if you have an int member variable and that can only be initialised with positive numbers, the constructor would throw an error if you passed a negative number to it. There are other ways to enforce these "conditions" such as subscript operators checking that the value passed to it is within range of the size of the container.

Friday, 9 February 2024

Dad's Demo Disc Collection // Episode 1 - Planet PC Issue 6


So my dad passed away over a year ago now unexpectedly from a heart attack. I was very close to him and the past year has been tough, especially since my dad was a hoarder. It's taken me just over a year to finally clear out his house and get it sold. I lived with him for a long time before finally moving out in 2019 and after that the hoarding got exponentially worse without me throwing things out.

Amongst the hoard there was a lot of crap but there was also a lot of very interesting things. Dad was very big on PCs throughout the 80s and 90s and would make a lot of his own programs in DBase or Clipper. He was very much sold on DOS and by the time Windows 3.1 came around he was too set in his ways and hated it but kept buying PC Magazines until the early 2000s when he eventually lost interest in computing all together. 

Back then, PC Magazines always came with a disk each month with software demos on it, or fonts, clip art, games etc. I remember some of these disks as he would keep them in his shed and I would pick up a handful and go through them one by one on the family computer that was kept in the dining room.

I've found more boxes of disks than I can remember him having, and there's a lot of interesting things on these disks and I thought it would be a nice project to archive every single disk and upload the contents to internet archive and document that journey.

So welcome, to the first episode of Dad's Demo Disc Collection. I started off with a random box and took out the first disk which just so happened to be:
  • Planet PC Issue 6 - 19th April 2000
I eventually got the software working by using PCem: 

A full blown pc emulator. I ended up emulating a Pentium II computer running Voodoo 3 and Windows98. And I did that by following this great tutorial by PhilsComputerLab: 

You can find a zip of the disc here on Internet Archive: 

I especially enjoyed my first encounter with Sierra Home. I look forward to finding other Sierra Software.

Enjoy.




Friday, 15 December 2023

Dev Log // Fun Times With Github Copilot, GPT and Solitaire

So I had this idea to build Solitaire using as much AI as I could. This was after reading a quote from an engineer in a github article who said that he tried to use Copilot to build the entire app. I found that concept interesting.

I started using Copilot in October and at first found it fascinating. The way it could almost read my mind and provide me the exact code I was about to type makes me feel like I'm in the future.

I started off by having ChatGPT generate the class declarations for the game (but specifically no code). I would then paste these declarations into Visual Studio as comments and wait for Copilot to generate the code based on the comment. Then, I would create the functions, place my cursor in the blank function and wait for it to generate that code as well.

Dev Log // Fun Times With Github Copilot, GPT and Solitaire

It quickly got old and tedious. The code was not great. Copilot struggled on the more intricate parts of the game where classes started to really intertwine with each other and it would start making up variables or functions (even with all the relevant files open as tabs). It also struggled to create the graphical side of things, often suggesting code for SDL instead of SFML.

But most importantly; I had no idea what was going on. At all times, I felt like an engineer who had been hired to finish the game as the previous one who implemented everything had left abruptly. The further I got into the making the game, the harder it became to debug.

I ended up scrapping that version of the game and instead took the class declarations provided by GPT and started implementing them myself. I had a clear goal of what the finished game would look like and what I wanted to accomplish in each submit.


This is the way to use Copilot. I've never completed a game so quickly. When you know what you want to write and Copilot suggests it for you (or something very similar), it can shave hours off dev time. And that's not a hyperbole. The key though, is knowing what Copilot is suggesting you. If you can't read C++ or you're relying on it to know what it's suggesting is the right code for that particular section then you will quickly be led astray.

It's not the silver bullet, but it's pretty damn close and I highly recommend adding Copilot to your toolbelt.

As for Solitaire, I'm very happy to have finally created my version of it. It's one of those classic games I've dreamed of implementing since I started programming.

I used ChatGPT and Dall-e 3 to create the initial card designs and then edited them in PhotoShop to create a full deck spritesheet.

My favourite part of the whole process was figuring out how to do the classic "win animation" where the cards bounce down from the top of the screen, leaving a trail of the card behind them. This was the hardest part of the game and even GPT 4 couldn't help. Eventually, I asked some of the rendering wizards at work and they said the original solitaire used the mechanic of simply not clearing the screen after drawing so the previous frame was still there. This took some doing to get working in SFML as it has things in place to update the buffers anyway, even if you don't call explicitly call window.clear().

I ended up creating my own buffers when displaying the win animation, so only one buffer was ever drawn to and not cleared. Then that buffer would be turned into a render texture and displayed on the screen.

Solitaire Win Screen Classic Animation - C++ and SFML

This was also my first time using the command pattern to implement the undo feature. 

And that's my adventure with AI and Solitaire! It's pretty much got everything that I wanted to implement; undo, right click add to foundation, drag and drop single and multiple, selected card highlight and a status bar.


Solitaire Win Screen Classic Animation - C++ and SFML

What's wrong with it? A lot of hardcoded values. Good luck changing those card sizes or resizing the window and still expecting it to work...

Thursday, 2 November 2023

Chapter 27 // Exercise 16 - Principles & Practice Using C++

In this exercise I'm using Visual Studio 2022 and ISO C11 Standard.

Chapter 27 // Exercise 16

Use macros to obscure (simplify the notation for) the implementation in the previous exercise.


Of course the final exercise had to be creating some disgusting macros. And with that


I can't believe that was the final exercise. It's been 7 years. I bought this book back in January 2016 with no knowledge of programming (apart from some brief HTML back in '98) and now I've been programming in C++ for 7 years, 4 and half of them professionally; I get paid to make video games, it's crazy.

I'm going to spend a bit of time collecting my thoughts and then come back with a full review of the book. I think my title is going to be "Why you should learn C++ as your first language". I honestly don't think I would've wanted to learn C++ any other way now.

I've been feeling a bit nostalgic lately and going back over books I bought way back in 2016 and 2017 as well as reading my green text on the early exercises of P&P posted on here. I read a chapter 4 post and the memories of that night came flooding back to me. It took me 6 hours to write a function comparing the size of two ints. It was 3am and I was so frustrated I started crying. I developed a fear of bools. Its absolutely hilarious to me now as I sit here reading a random book, silently ripping apart the authors code because they stored an pointer to an enum.

My mantra at work is "git gud". I can relax a bit when I've "gitten gud", then I can "git gudder". I honestly believed I hadn't really changed at all but lately I've started to realise that I can "just do stuff now". 6 years ago I bought Programming Game AI by Example by Mat Buckland. I tried to read it and follow along with the code but it went completely over my head as I had no experience with inheritance and I was convinced OOP was evil (because I didn't want to understand it). Recently, I picked the book back up and not only implemented the code from snippets with no problem but changed it as I was going along, grumbling about the authors use of singletons and lack of interfaces.

2 years ago I read the online book Ray Tracing in one Weekend and attempted to multi-thread it. It was a disaster and no matter how many times I read MSDN or StackOverflow I just couldn't get my attempts to work. Earlier this year I had another go and got a hacky version working in an afternoon. It's not perfect but it worked and I just "knew" how to do it.

3 months after starting as an intern at Rare, I asked a senior in a catch-up "when do you stop feeling like shit?" and his response was "you don't, but over time it won't be as bad". I pressed him for an actual time for "over time" and he just laughed and said "3 years". It's been 4 years since I asked him that.

It took me a very long time to stop feeling like a waitress/receptionist and start thinking of myself as a programmer. 

Thank you Bjarne for giving me the tools to change my life. 

Wednesday, 1 November 2023

Chapter 27 // Exercise 15 - Principles & Practice Using C++

In this exercise I'm using Visual Studio 2022 and ISO C11 Standard.

Chapter 27 // Exercise 15

Simulate single inheritance in C. let each "base class" contain a pointer to an array of pointers to functions (to simulate virtual functions as freestanding functions taking a pointer to a "base class" object as their first argument); see section 27.2.3. Implement "derivation" by making the "base class" the type of the first member of the derived class. For each class, initialise the array of "virtual functions" appropriately. To test the ideas, implement a version of "the old shape example" with the base and derived draw() just printing out the name of their class. Use only language features and library facilities available in standard C.

Jesus christ, I had to get ChatGPT in for this one because I had no idea how start. What a load of faffing about. I used to think inheritance in C++ was confusing, well get a load of inheritance in C! It all kind of seems a bit pointless in C. 

I didn't go too far into this exercise, literally just creating the draw() functions for a shape and a circle. Shape can also be created, it's not abstract.