Allright Guys, here we go Part 0 of our Creating a Rogue Like Tutorial using Swift and libTCOD.
I'd be lying if i told you this part was exciting and wonderful, but its not. It IS brave though,
so pat yourself on the back for going off the beaten path by using Swift with libtcod to create your Rogue Like game.
Another thing this section is, is tedious, but very necessary, because this is the section where we configure
Xcode and Swift to be able to Interact with the Doryen Library (libtcod).
So without further ado, Lets get started!
I've tried to write and illustrate this in a way so that people dont have to be professional
programmers or master hackers in order to follow along. With that being said we ARE going off
the beaten path in terms of language selection, and on top of that we're implementing the use
of a C/C++ library, libtcod, and while it has been more or less seemlessly integrated into python
it's use in Swift is not without its hurdles, but dont blanche out, I have tried my best to make
this process as easy and painless as possible. That being said, being relativley familiar with Xcode,
and having moderate skills in Swift and prior programming knowledge in any language will be a big plus.
In starting any programming project, selecting the proper IDE, language, tools, and operating system are
a huge decision. I'm guessing you're reading this for a hobby project, so those selections will come down
to what is at hand/your comfortable/interested in. Since you're reading THIS particular tutorial i'm going to
assume that you are using Swift, That you're on macOS, and your probably using Xcode.
the setup i used in creating this tutorial is as follows:
ill just repeat myself here, any setup newer than this will probably work, and perhaps
a slightly older setup will too, but it has not been tested. If you happen to be using a different oonfiguration
please email me at firstname.lastname@example.org so i can compile a list of Swift/Xcode/macOS
versions this works on.
Ok, with that out of the way lets move on to setting up our programming environment.
Go ahead and launch XCode, and click on start a new project
Click on File -> New project, and select Command Line Tool under the macOS heading
Give your project a name, making sure the language is set to Swift
Make a folder for your project, and click create. This should bring up
the main editor with an empty swift project.
Now were going to leave Xcode for a minute to setup the file structure we need
in order to use libTCOD in Swift. Go a head an open up two Finder windows
In one window, navigate to the folder you created your Xcode project in
and in the second finder window navigate to the folder you unzipped the libtcod package to
You want to copy the libtcod.dylib file into your proects root directory
and you want to copy "libtcod.h" and the include folder into your projects source directory
You can go ahead and close finder and head back to Xcode.
In your projects general settings you want to click the "+" symbol under "Linked Frameworks and Libraries".
Navigate thru the windows until you have libtcod added to your project
Now click over to the build settings tab of your Xcode project, and scroll down to where you see a heading
called "Search Paths" change the value of "Library Search Path" to the folder you copied libtcod.dylib to
Ok, now the Swift compiler is setup so it knows it need to link libtcod to our program.
Now, lets setup Xcode so our wrapper class can read from the libtcod header files..
Navigate to File -> New -> File and click on "C file". click thru to create the file
naming it doesnt matter, because we are going to delete it in a second, but this part
is VERY, VERY important. as you're creating the C file, Xcode is going to ask if you want
to create a "bridging header" our answer is YES. this is what gives swift access to Xcodes
Objective-C compiler, which is what gives us the ability to interact with libraries that
were originally intended to be used with C or C++ from within Swift
You can go ahead and delete the C file you just created, but LEAVE THE BRIDGE HEADER IN YOUR PROJECT.
We're almost there, just a couple more things to do before we can get to coding.
Navigate to the bridge header you just created
add the line #import "libtcod.h"
Now Click on the file menu and select on Add file to project and add the TCOD-wrapper.swift file to your project
Your project file tree should now look like this:
The very last thing you need to do is copy the "terminal.png" file that comes with libtcod to the buiild
folder of your project. Xcode automatically creates this folder the first time you compile (Press the "play" button)
It's usualy located in the:
I'm convinced thats mainly because apple hates its own developers and found it totally illogical to place the compiled binary
in ya know, the project folder it made you create, maybe? possibly? anyway...
Thats it! if you followed all the steps and all has gone well, were ready to start coding our
Rogue Like Game! So grab another energy drink and head on over to Part 1 to get started!