Introduction to C++

Welcome to the third series of Leadwerks tutorials. This series will teach you how to use the C++ programming language to make games with Leadwerks. C++ programming is optional in Leadwerks, and requires the Professional Edition DLC. If you want to make games using Lua only, feel free to skip this series.

Why C++?


C++ is the world most powerful programming language, and the backbone to the digital world. When you code with C++ you are using the absolute fastest path tool available. In fact everything that requires performance from operating systems to game engines relies on C++ because nothing is faster or more powerful. High-frequency trading software? Yeah, it's written in C++. Satellite navigation systems? Yeah, that too. It's no exaggeration to say that C++ powers the world, and learning this knowledge makes you part of an elite class of programmers.

Because C++ is used for virtually every third-party library for graphics, physics, science, and engineering, using C++ gives you access to the world's code. For example, the free networking library Raknet is written in C++ and can be compiled into a Leadwerks program.

C++ was invented as an extension of the C programming language. Therefore, any code written in C can be compiled into a C++ program.

If you understood the concepts we introduced in the Lua tutorials, you will find that C++ isn't much harder. However, there are a few mistakes to watch out for. Don't worry, we'll help you avoid the most comon problems programmers run into.

Required Software


If you are running Leadwerks on Windows, you will need to download and install Visual Studio Community 2017. (If you have the professional version of Visual Studio, that will also work.) If you are running Leadwerks on Linux you will need to install the Code::Blocks IDE for your distribution.

Project Setup


If you have the Professional Edition DLC then any new projects you create will include C++ source code, along with project files for Visual Studio and Code::Blocks. These will be found in the Projects/Windows and Projects/Linux folders in your game's directory. Navigate to this folder and open the project file for your operating system (.sln for Windows or .cbproj for Linux).

In the C++ IDE, open the main.cpp code file. As you might have guessed from the name, this is the most important code file in your project. C++ programs begin with an entry point called the main function. The main function takes a few arguments that contain the command line the program was launched with.

Hello, World!


We already learned how to make a "Hello, World!" program with Lua. In C++ it's a little different:
#include 

int main(int argc, const char *argv[])
{
std::cout << "Hello, World!";
}


Yeah, that is kind of weird. The #include line makes the program compile with a required library, and std::cout is a funny way of saying "print this line of text". Fortunately, Leadwerks includes a print function that makes life a lot simpler:
#include "Leadwerks.h";

int main(int argc, const char *argv[])
{
Print("Hello, World!");
}


That's much simpler! There's no reason to make things more complicated than they need to be, so let's just use the Leadwerks Print() command from now on.

You'll notice in both examples, both lines of code inside the main function have a semicolon (;) at the end. Every line of code in C++ has to end with a semi-colon or the program won't compile.'

One more thing: When you run a program from Visual Studio in Debug mode, it will open a console window while the game is running, but immediately close it when the program ends. The way around this is to select the Build > Run Without Debugging menu item. When you do this the console window will stay open after the program is done running so you can see the printed output.

API Reference


Most commands in the API Reference include an example that shows how to use the command. Navigate to the Entity::SetPosition page and copy the C++ code at the bottom of the page. (Make sure you are looking at the documentation for C++ and not Lua. There are links at the top of the page you can use to switch between languages.)

Paste the example code into your main.cpp file, completely replacing what as there before. (If this is a project you are working on, you might want to save a backup of this code file first.) Press F5 to compile and run your program and it will look like before:

Namespaces


Because C++ can use lots of different libraries that different people write, it uses a feature called namespaces to categorize different commands. All Leadwerks commands, for example, belong to a namespace called "Leadwerks". We can declare the namespace explicitly in our code like this:
Leadwerks::World::Create()

However, by adding the line "using namespace Leadwerks;" to the beginning of our code, we don't have to specify the Leadwerks namespace.

The Linux operating system uses some classes that coincide with the Leadwerks classes Time and Window. Unfortunately, these are not declared with any namespace, so we must specify the Leadwerks namespace when using these classes if we want our code to always compile on Linux:

Leadwerks::Time::Update();

Leadwerks::Window::Create();

Conclusion


That wasn't so hard, was it? There's definitely a lot of options in C++ but we're just going to teach you the things you need to know. Hang in there!