Ok so weve completed Part 0 and our environment is
all set up and ready for action, so lets navigate to main.swift and get down
to work. You should have a section at the top of comments and below the comments
import Foundation should be present.
import Foundation is kind of like #include < iostream > or #include < stdio.h > if your
familiar with C++ or C. it gives us access to the base types and functions for
programming with swift on macOS.
Now were going to add the line:
let tcod = TCOD()
What this does is gives use acces to the main class of our tcod-wrapper.swift
so that we can call functions from libtcod
now we're going to set some variables that will make our life easier down the road:
What were doing here, is entering values for the height, width, and title of screen for the console
that we are asking libtcod to make for us. We use "let" descriptors instead of "var" descriptors for
these variables, because we wont be changing the value of them(through code at least) later on.
Now, Line 15:
is where the magic starts. were calling the function initRoot from the tcod class we initialized earlier
this function passes information to libtcod that describes the console we want to initialize.
A note for the curious:
there are other values that are passed to initRoot, but that would require you editing the wrapper
and that is outside the scope of this tutorial (although i WAS nice enough to comment the wrapper thoroughly
should the mood to tinker inspire you, if it stops working its your fault :-P)
Ok, Now were, going to add three more function calls from the tcod class and add two more variables
to our program, with the end result being that when our game launches, our characters symbol, "@", is visible on the screen
ill go through these one by one.
tcod.consoleClear() does it exactly what it sounds like, it clears the console screen buffer of every thing.
var playerx: Int32 = width / 2 - this declares a variable for the "x" position of our character
var playery: Int32 = height / 2 - this declares the "y" position. by setting the values of these variables
to (width / 2) and (height / 2), weve set them up to be displayed in the center of the console screen, neat huh?
now for the juicy bit:
tcod.putChar(x: playerx, y: playery, char: "@") does - you guessed it - prints the "@" symbol on the screen
at the coordinates (think T graph) we supplied, in this instance, right smack in the middle of the screen.
remember how i just said putChar displays the "@" symbol on the screen? I lied. technically what it does
is add it to the screen BUFFER (remember that thing we cleared earlier?) which is why we make the next call:
tcod.flush() this flushes the console buffer and displays its contents on the screen, which means any time
we want to put something on the screen, we call this function afterwards, tcod.flush() one of the unsung heros
Next up, were going to add our main program loop, and listen for input from the users keyboard:
Now, before we go any futher im gonna show you something. Go ahead and press the "Play" button
to compile and run the project we are working on...
Go ahead, run around and do some back flips, maybe a victory lap or two, ill be waiting for you
It may not look like much, but looking back on it we've setup our project and configured Xcode to
"talk" with libtcod - no small feat in itsself, and now we've setup the basics for being able to
generate output. Your basically a caveman that just made fire, youll be sleepin warm with the other
cave women tonight cause your the dude with fire. ok maybe were getting ahead of ourselves...
Theres only one thing left to do in this installment and thats make our "@" symbol move around the screen:
add The following to your loop:
this code uses tcod.chkKeypress() as a boolean value for an if statement.
When the user presses a key on the keyboard, the condition becomes true and it
is handed off to our switch function, which uses the value of tcod.key.vk, a struct in libtcod
that handles keystroke input. The switch statement matches cases for which key was pressed, specifically:
up, down, left, and right directional arrows on your keyboard. If the up or down arrow is pressed, our playery
value changes correspondingly, same for left or right arrows and our playerx value. after this we call
we would be leaving a trail of charachters.
Side note: if you want to recreate the game of "snake" youd alread be more than halfway done.
A neat feature of Swift is that there is no fall through switch statements, so we dont need a "break" statement after each case.
Now that our if statment recognized a keypress, and our switch statement reacted to which key it was and updated our characters
coordinates accordingly, the only thing left to do is print our characters symbol at the new coordinates and flush the buffer
Voila! you can now move your little "@" symbol where ever your heart desires...