Hello and welcome to C++ Weekly. I'm your host, Jason Turner. In this episode I'm
going to show you a project I've been working on that is a game intended for teaching C++.
Now first of all, before we get too far into this I want to show you some of the other things that
I've been working on. I've got C++ Best Practices, which I just published the second edition of,
and I've got these puzzle books that I've been working on. I've done a previous episode about
those if you would like to learn more about them, but the idea is that I am trying to make C++
fun and accessible. That is my goal for 2022, so as part of that I realized I could make a
game that makes it fun to learn C++, and I think I've accomplished that and I'm going to show you
how to get set up with it in this episode. So right now I'm starting with a very clean,
just installed version of Ubuntu. I'm going to be focusing on getting Linux set up because I
think it is the easiest way to get all of the components that you need with one quick script,
although I will say I am open to people helping to set up scripts for other operating systems that
also make this easy to set up; and by the way, if you want one of these puzzle books or my Best
Practices books then check out Leanpub and Amazon. You can get digital and print copies of them
and there will be links to all of this in the episode description. So I've set up this C++
Best Practices organization on Github and it's a place for me to put all of the different projects
that I'm working on that I think are going to help people use best practices in their C++ code. So
I've got this cmake_conan_boilerplate_template that has a bunch of defaults in it
and the gui_starter_template that's got SDL and a couple of other simple things in it
and this ftxui_template, and this ftxui_template is actually one that was used in my game jam that
I just held earlier this year where a bunch of people created games that ran on the terminal.
So I've been working on this myself since then. So to make it easy to get set up with all the tools
that you'll need to do a C++ Best Practices style coding on your Ubuntu computer, virtual machine
or whatever you have I have provided this bash script on the cpp-best-practices organization
under setup-cpp, and you can just copy and paste this into your terminal window
and you can see that this is in fact a brand new install of Ubuntu. So we're just going to go ahead
and paste this in here it's going to ask for our password because it requires sudo access - I do
strongly suggest that you look at what the script is doing yourself before you run it
on your own computer if you have any concerns at all. Let's go ahead and put in our password
and wait.
So the script got a couple of errors that you can safely ignore for the moment. Like I said
it needs still a little bit of cleanup, and I am more than happy to accept pull requests
for cleaning up that script; but now that everything is installed let's go ahead and
log out and log back in again so all of our environment variables are what we need them to be.
So we installed VS Code and we're going to go ahead and launch that.
Now VS Code is great for presenting with because it is so easy to zoom the entire
interface. I really like that aspect of it, but we're going to close a couple of things here.
If you want to you can spend time browsing the themes and stuff. I installed Gruvbox
because it's my favorite and it was one of the things that uh got installed with
that script here. A lot of people comment on Gruvbox when I use it in these videos.
So we've got Gruvbox and I don't need to redefine my editing skills or anything like that.
Okay, so we're going to go ahead and hit "Clone Git Repository" here
and you're going to clone lefticus/my_awesome_game. Now this might
change at some point in the future - probably move it under the Cpp Best Practices organization,
but that is what it is for now, and I'm not too concerned about showing you this at the moment
because it will automatically redirect in the future if we need it to. Github will take care
of that for us. And... we're going to select this programming folder here and hit (select) "Clone
This Repository," and I do want to go ahead and open it, and I am going to trust the authors of
this. So it is going to go ahead and automatically start doing CMake configuration. This is up to you
if you want it to always configure projects upon opening. I'm going to go ahead and say yes.
I am not going to use containers at the moment - that is up to you if you are familiar with
containers. There are scripts in here to help with using containers but I'm going to say no to
containers. So we're going to go ahead and click over here on the CMake and make sure that
everything is configuring like it should. Here we go. It's taking off. So at the moment it is using
Conan's CMake integration to automatically set up all the dependencies that you need
and we are going to select "awesome game" here. So I hit the debug button
down here and then I chose "Awesome game", and it is compiling it now.
Now once the game is launched this is going to look kind of weird, you can't really tell
what's going on. You have this message box you can't read but we just need to make this
a little bit bigger, and this is the game as it currently stands. You can walk around here
and you can enter a room. You can have a conversation
with the innkeeper here, or store person whatever, walk around. So this is simple. It will be growing
by the time you actually see this when this episode has been released. So while this game
is cool and fun to play with it's not really the point of this project. This isn't how you
are going to learn C++. So the very first thing that you're going to have to do to actually start
learning C++ is modify this code so that the lesson is launched instead of the game itself.
So currently in main it says "auto game = lefticus::awesome_game::make_game",
and we're going to change this
so now it will read as "lefticus::awesome_game:: hacking::lesson_00::make lesson"
and we just hit the debug button again, wait for it to build and relink main and
launch the lesson. So here we are - "Welcome to 'Learning C++ With Game Hacking Lesson 00'.
This lesson is just to get started with the project, but you'll also learn some basic
logic and how to get around the source code. Your job is to get into the special square in
the bottom right corner of the map but to do that you'll have to modify the source code.
(Be sure to look for clues in the bottom message box.)" This is a very abstract
game view - you've got just this little character moving around some animated tiles
and it says "Hint you'll have to edit the code. Go to location {4, 3}," and we can see here we're
at location {4, 1}. So we go ahead and go down to {4, 3} and we get another hint "(You need to)
You need file game_hacking_lesson_00.cpp, go to location {8, 8}." So if we want to as
we're going along here we could go ahead and click through the files and say, "Okay where
is game_hacking_lesson_00?" Okay we've got that. Now it says go to location {8, 8}
and we try to get down to location {8, 8}. We see "A wall is blocking our way," and if we go
all the way around the square it says "You need to remove the wall." Okay, how am I going to remove
the wall? There is a "special location" on line 114 of game_hacking_lesson_00, so let's go ahead
and go look there. Okay so we are at line 114, and we see where the special message has been actually
printed to the screen. This thing's saying "look for the special_location
that's at here." Okay. We're at "special_location" and we've got this Flashing_tile here and
we have the logic - "are you allowed to enter this location or not?", and I describe what the logic
is. Right now it's saying "if the direction is from the south or from the east then we are
allowed to enter that particular location", so let's go ahead and add to this the logic of...
so now if we're coming from the south, the east,
or the north then we're allowed to enter the special square, and we'll go ahead and launch
the game again. Okay so we've just modified the source code and we are re-running our game
and now if we navigate our way back down to this bottom right corner we will see that
one of the walls is now missing here - the top wall because we allowed entry from the north.
So let's go ahead and step down into there and we will get this uh "Congratulations.
You found the secret room, now change your call to play_game to start lesson 01." So
now you have to go back into the source code for main.cpp and change it to start lesson one, and
there you go. So this got us set up. Now some key points to discuss before I wrap up this episode -
first of all, all of this code is running with address sanitizer and undefined behavior sanitizer
and a very strict set of static analysis and warnings, so these are all tools that are going to
be in place so that as you are learning C++ if you do something like accidentally have like a memory
error or something like that the tools will catch this for you so you don't have to think of C++ as
being an unsafe language. This is giving you a safety net and a playground to play in while you
learn C++ and you will get very specific errors back from the run time if you were to do something
that isn't allowed and could be a real security problem in a more realistic application; and at
this point I can go ahead and hit "Continue Game" and continue to walk around here if I want to,
or I can go back in and hit "Exit Game" and there, the game exited just like it was supposed to.
Now another comment that I need to make is at some point you may have noticed
that the visuals and the frame rate changed in the middle of doing this example,
and that is because I changed computers. I changed from Linux on x86_64 Ubuntu to Ubuntu running on
my Raspberry Pi 4. So I can see down here that I'm running the Raspberry Pi version of Ubuntu
and this is aarc64, the 64-bit version of the ARM Raspberry Pi Linux distribution;
and so I have four ARM cores here. Now I did this just to prove a point. I wanted to make this as
accessible as possible. This has been a far from ideal experience because compiling and running
in a full IDE environment on the Raspberry Pi is kind of slow. If you're going to do this you
should have a Raspberry Pi 4 with as much RAM as possible, and you should probably be booting
from SSD. I've been booting from an SD card and it's not great. You should do a boot from USB
with a much faster storage device that GCC is actually going to be using,
but I think that this is a good succinct intro to the point of this game that is here to teach C++.
You can use the lessons themselves. You can hack on the game. I am going to continue developing
this game - it is my personal playground for some game that I have been wanting to develop,
but as you can see it requires very little in the way of computing resources and has all the safety
net like I said before for helping you to learn C++. So thank you for watching this episode of C++
Weekly. I hope you liked it. Be sure to subscribe. I have so far published many, many episodes
about a wide variety of C++ topics and I don't plan on stopping anytime soon. I expect that
this episode will draw in a lot of new viewers and this is not too far off from what a regular
episode of C++ Weekly would be like if this is the first one that you are watching. And of course, if
you're still hanging out here now I will be sure to tell you that I am available for code reviews
and training and contracting. If you have anything that you would like to discuss check out the links
in the video description. Contact me, send me a DM on twitter, whatever you would like to do.