Difference between revisions of "Complete roguelike tutorial using C++ and libtcod - part 1: setting up"

From RogueBasin
Jump to navigation Jump to search
(expanding →‎First program)
Line 77: Line 77:
*lib/libtcod-mingw-debug.a
*lib/libtcod-mingw-debug.a
*SDL.dll
*SDL.dll
==First program==
We'll create a very simple program, just to check that we can compile and run. Let's create a src/main.cpp file :
main.cpp
#include "libtcod.hpp"
int main() {
    TCODConsole::initRoot(80,50,"libtcod C++ tutorial",false);
    while ( !TCODConsole::isWindowClosed() ) {
        TCODSystem::checkForEvent(TCOD_EVENT_KEY_PRESS,NULL,NULL);
        TCODConsole::root->clear();
        TCODConsole::root->putChar(40,25,'@');
        TCODConsole::flush();
    }
    return 0;
}
You can compile this on Windows with (type a single line) :
> g++ src/*.cpp -o tuto -Iinclude -Llib -ltcod-mingw-static-libgcc -static-libstdc++ -Wall
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.
and on Linux with :
> g++ src/*.cpp -o tuto -Iinclude -L. -ltcod -ltcodxx -Wl,-rpath=. -Wall

Revision as of 12:04, 26 September 2015

This article is the first part of a series heavily inspired by Jotaf's excellent "Complete roguelike tutorial using python + libtcod".

It is intended for C++ beginners and people who want to learn how to use libtcod to create a simple roguelike video game. It covers both Linux and Windows operating systems.

The source code of this tutorial uses the C99 standard. That means that it won't compile out of the box on Visual Studio.

While an experienced C++ developer won't have much trouble to port the code to Visual Studio, if you're a C++ beginner, I strongly advise to use one of the suggested compilers/IDE.

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.

libtcod functions used in this article

TCODConsole::initRoot

TCODConsole::isWindowClosed

TCODSystem::checkForEvent

TCODConsole::clear

TCODConsole::putChar

TCODConsole::flush

Pre-requisites

Installing the compiler

First you need a C++ compiler. On Linux, it's easy :

> sudo apt-get install g++ gdb libsdl1.2debian-all

On Windows, follow this tutorial : libtcod 1.5.2 documentation.

When installing Mingw, you must absolutely choose the "Use pre-packaged repository catalogues" option. Apparently, the latest version is not working with libraries compiled with the previous one.

Installing libtcod

All you have to do is download the library corresponding to your platform from this URL and extract it to your hard drive.

Note that the Windows/Mingw precompiled library only works with a 32 bits compiler. If you want to use a 64 bits compiler, you'll have to recompile the library.

Setting up the project

Create an empty directory somewhere on your hardrive. Inside, create 3 empty directories :

> mkdir include src lib

Then you'll have to copy those files from the libtcod directory to your project directory :

  • include/*.h
  • include/*.hpp
  • terminal.png

For Linux only :

  • libtcod.so
  • libtcodxx.so
  • libtcod_debug.so
  • libtcodxx_debug.so

For Windows only :

  • libtcod-mingw.dll
  • libtcod-mingw-debug.dll
  • lib/libtcod-mingw.a
  • lib/libtcod-mingw-debug.a
  • SDL.dll

First program

We'll create a very simple program, just to check that we can compile and run. Let's create a src/main.cpp file :

main.cpp

#include "libtcod.hpp"
int main() {
   TCODConsole::initRoot(80,50,"libtcod C++ tutorial",false);
   while ( !TCODConsole::isWindowClosed() ) {
       TCODSystem::checkForEvent(TCOD_EVENT_KEY_PRESS,NULL,NULL);
       TCODConsole::root->clear();
       TCODConsole::root->putChar(40,25,'@');
       TCODConsole::flush();
   }
   return 0;
}

You can compile this on Windows with (type a single line) :

> g++ src/*.cpp -o tuto -Iinclude -Llib -ltcod-mingw-static-libgcc -static-libstdc++ -Wall

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.

and on Linux with :

> g++ src/*.cpp -o tuto -Iinclude -L. -ltcod -ltcodxx -Wl,-rpath=. -Wall