Complete roguelike tutorial using C++ and libtcod - part 1: setting up

Complete roguelike tutorial using C++ and libtcod -originally written by JiceText in this tutorial was released under the Creative Commons Attribution-ShareAlike 3.0 Unported and the GNU Free Documentation License (unversioned, with no invariant sections, front-cover texts, or back-cover texts) on 2015-09-21.

Introduction

Why C++ ?

While being often criticized for being complex, for lacking features or for failing to enforce a single programming style, C++ is still one of the most used languages. Here are a few reasons that make it still relevant :

the language itself is public domain. It's not owned by a private company like the more elegant java or C#

there are compilers that produce high-performance native binaries for almost any existing platform

it has a great compatibility with C, which makes it easy to use with C libraries like SDL

Pre-requisite : this tutorial does not replace a C++ manual. You're supposed to have a basic knowledge of the C++ syntax and object oriented programming.

Why libtcod ?

It's easy to use and provides out of the box a lot of tools that are frequently used in a roguelike (Field of view and path finding algorithms amongst others). While browsing this tutorial, you should always have the libtcod C++ manual open. The code in this article series works with libtcod versions 1.5.1 and 1.5.2.

If you want to get rid of the debug console when running the game from windows explorer, add the -mwindows flag. But since we're in the development phase, it's good to be able to get the program standard output.

Line by line explanation

Here we're importing all the libtcod classes declaration. Actually, before compiling the main.cpp file, the compiler will replace this line by the content of libtcod.hpp.

int main() {

This is the function that will be called when the program starts. It returns an error code. 0 : no error. Any other value : an error occured.

TCODConsole::initRoot(80,50,"libtcod C++ tutorial",false);

Here we're calling our first libtcod function to create the game window. It will contain a 80x50 console using the default font "./terminal.png". The false value indicates that we don't want to start in fullscreen mode.

Now we're starting the main game loop and we'll keep looping until the game window is closed by the player.

TCOD_key_t key;

This line creates a key variable we can use for handling input later.

TCODSystem::checkForEvent(TCOD_EVENT_KEY_PRESS,&key,NULL);

Even though we're not using keyboard input yet, we need to call checkForEvent for libtcod to process the screen redraw events.

TCODConsole::root->clear();

This is the first non static function we use. We access the static "root" variable inside the TCODConsole namespace. It's a pointer to the console bound to the main game window. We call the clear function on this console to erase everything with default color (black).

TCODConsole::root->putChar(40,25,'@');

Now we use putChar to print an @ in the middle of the screen, using the default color (white).

TCODConsole::flush();

This line actually generates the console image and displays it on the window.

The walking @

Now we're ready to reach the first step in any roguelike : the walking @ step :