Complete roguelike tutorial using C++ and libtcod - extra 1: about editors and IDE

From RogueBasin
Revision as of 14:44, 21 October 2015 by Joel Pera (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Complete roguelike tutorial using C++ and libtcod
-originally written by Jice
Text 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.


So far we've seen how to compile the C++ code but we didn't mention how to write it. In this article, I present a few great tools to write C++ code, covering all the needs from the minimalist, distraction-free text editor to the most heavily featured Integrated Development Environment. This is my personal selection, but remember the best tool is the one you're comfortable with so don't hesitate to try others until you find your best fit.

We'll start with the lightweight tools and finish with the heavyweight.

Contents

SublimeText: minimalist and powerful [windows and linux]

Presentation

If you like minimalist UI and having every feature available with a few keystroke without having to browse huge menu trees or a swarm of icons, SublimeText is definitely the editor you need.

All you have to do is to add your project directory to the current project (Project / Add folder to project) and you're done. SublimeText can display a side bar with the content of the project but you don't even need it, thanks to the amazing "Go to anything" feature. You want to open the Actor.cpp file ? Hit ctrl+p and type act, a suggestion list will open and you'll be able to open it (or switch to the tab if it's already open) faster than you would do with a mouse click.

Want to find the render function in your huge Engine.cpp file ? Use ctrl+p and type eng#upd (or ctrl+r / ren if current tab is already Engine.cpp). If Engine.hpp is open rather than cpp, simply type engcp#upd, that should work.

Want to go to line 50 in Engine.cpp ? ctrl+p / eng:50 (or ctrl+g / 50 if current tab is already Engine.cpp).

Compilation

Select Tools / Build System / New Build System and paste this in the file :

{
   "cmd": ["g++.exe src/*.cpp -o tuto -Iinclude -Llib -ltcod-mingw -static-libgcc -static-libstdc++"],
   "shell":true,
   "working_dir":"${project_path:${folder}}",
   "file_regex":"^(...*?):([0-9]*)",
   "encoding": "cp1252"
}

Save it as tuto.sublime-build, for example. Then check than tuto is selected in Tools/Build Systems. And hit ctrl+b to build. You can click in a compilation error to open the corresponding file.

Note that to make this work on windows with SublimeText 2.0.1, I had to slightly change the Data/Packages/Default/exec.py file from SublimeText distribution. You need to add stdin=subprocess.PIPE parameter to the subprocess.Popen call :

self.proc = subprocess.Popen(arg_list, stdout=subprocess.PIPE,stdin=subprocess.PIPE,
   stderr=subprocess.PIPE, startupinfo=startupinfo, env=proc_env, shell=shell)

You also have to run SublimeText from the MSys terminal so that it has the path to g++.

PSPad: old school and full featured [windows and linux through wine]

Presentation

PSPad is a more classical and less minimalist all-purposes text editor. With ftp read/write, HTML formatting and file comparison tools, it's perfect for web development but it can also be used for C++, especially because of its code explorer window that helps you to find a method in the current tab.

To create your project, just select Project / create project from directory and select your project's directory.

Compilation

To compile with pspad on Windows, open Project / Project Settings and fill the compiler tab :

Compiler : full path to the g++ executable

Parameters : use the same parameters as the command line version, but use absolute paths, for example :

E:\path\tuto\src\*.cpp -IE:\path\tuto\include -LE:\path\tuto\lib -ltcod-mingw -static-libgcc -static-libstdc++ -o E:\path\tuto\tuto.exe

Default directory : the directory containing the g++ executable (else pspad will complain about missing mingw dlls)

Check "Capture program output window" and "hide output window"

Set the log parser to :

%F:%L:%C:*

That's it. Hit ctrl+f9 to compile.

CodeLite: lightweight open source IDE [windows and linux]

Presentation

CodeLite is an open source, cross platform IDE packed in a 15MB download. Perfect if you want a quick C++ start but want something more consistent than a simple text editor.

The main features you'll get compared to the text editors are :

robust code completion launching/debugging from the IDE refactoring subversion/git integration That's a nice list of features for such a small download.

If you're on Windows, download the version including the Mingw compiler (for example the current is codelite-4.1.5770-mingw4.6.1.exe).

Compilation

First create the project with Workspace/New project.

CodeLite will create a project with a placeholder main.cpp. Remove this file and add your own from the src/ directory :

Then right click on the project (the line in bold above) and select Settings.

Add the include directory in the Common Settings / Compiler / Include Paths textbox.

In the Common Settings / Linker tab, add lib to the library path and libtcod-mingw to the libraries :

All those options will allow CodeLite to use the same compilation options we've been using since the beginning of this tutorial :

-Iinclude -Llib -ltcod-mingw

Now we're able to compile inside CodeLite with f7. But if we try to run or debug the program, CodeLite will complain because he cannot find libtcod. To be able to run, we need to change the path of the program and the working directory in the Common Settings/General tab.

That's it. Press f7. You should see :

----------Build Started--------
G:\Windows\system32\cmd.exe /c ""mingw32-make.exe" -e  -j 4 -f "tuto_wsp.mk""
----------Building project:[ tuto - Debug ]----------
mingw32-make.exe[1]: Entering directory `E:/jice/doryen/svn-tuto'
g++  -c  "E:/jice/doryen/svn-tuto/src/Actor.cpp" -g -O0 -Wall  -o ./Debug/src_Actor.o -I. -I. -Iinclude 
g++  -c  "E:/jice/doryen/svn-tuto/src/Engine.cpp" -g -O0 -Wall  -o ./Debug/src_Engine.o -I. -I. -Iinclude 
g++  -c  "E:/jice/doryen/svn-tuto/src/main.cpp" -g -O0 -Wall  -o ./Debug/src_main.o -I. -I. -Iinclude 
g++  -c  "E:/jice/doryen/svn-tuto/src/Map.cpp" -g -O0 -Wall  -o ./Debug/src_Map.o -I. -I. -Iinclude 
g++ -o ./Debug/tuto @"E:\jice\doryen\svn-tuto\tuto.txt" -L. -Llib  -ltcod-mingw  
mingw32-make.exe[1]: Leaving directory `E:/jice/doryen/svn-tuto'
----------Build Ended----------
0 errors, 0 warnings

Then hit ctrl+f5 to start the program.

What about the others ?

As I already said, the best IDE is the one you're comfortable with. The tools I have presented all share the same feats : they're blazing fast, lightweight and keep you focused on your own program instead of wandering in a forest of menus/configuration dialog boxes/whatever.

Personal tools