-
Notifications
You must be signed in to change notification settings - Fork 0
Setting up a new Gogue project
In this article, we'll walk through the initial setup required to start developing a project using Gogue. We'll assume you have BearLibTerminal setup correctly on your machine, as outlined in the setup articles. If you haven't done that yet, do so now, we'll need BearLibTerminal correctly configured to run a Gogue project.
The very first thing we'll need to do is start a new Go project. For this article, I'll call my project GogueRoguelike
. Create a new directory at $GOPATH/src/GogueRoguelike
. Once that is created we're going to initialize a Go module in this project (this assumes you're using at least Go version 1.11). We're eventually going to add github.com/jcerise/gogue
as an import in our project, and this automatically grab the dependency, and keep track of versioning for us, no go get ...
required.
go mod init
That command should create a go.mod
file in your directory.
Now, require Gogue:
require github.com/gogue-framework/gogue v0.0.2-alpha
I'm using v0.0.2-alpha, but this tutorial will be up to date for whatever version happens to be the latest. You can also just write the code below, and run go build
on your project to let Go figure out the latest version of Gogue to download and use.
Next, lets create a main.go
file in the project directory. Let's start out by putting the following code into the main file:
package main
import (
"github.com/gogue-framework/gogue/ui"
"runtime"
)
var (
windowHeight int
windowWidth int
)
func init() {
runtime.LockOSThread()
windowWidth = 50
windowHeight = 25
ui.InitConsole(windowWidth, windowHeight, "Gogue Powered Roguelike", false)
ui.SetPrimaryFont(16, "press-start.ttf")
}
We're first importing github.com/gogue-framework/gogue
as a standard import. Modules will handle grabbing Gogue once we build this project.
Next, we're setting a couple of variables for, you guessed it, the size of the window we want Gogue to create for our game.
Finally, we're creating an init function that sets up a Gogue terminal, which will display our game. This code will create a window that is 50x25, with a title of "Gogue Powered Roguelike", not in fullscreen mode. Init will also handle opening the terminal window for us, so there is no need to explicitly call open()
.
I'm also setting a font to use here, at size 16. I'm using the PressStart font (https://fonts.google.com/specimen/Press+Start+2P). If you want to use that, download the TTF file, and stick it in the root of your project, called press-start.ttf
. If you don't feel like using a custom font, you can omit the SetPrimaryFont
line, and use Gogues default font, which looks perfectly Rogue-y.
Next, lets add a main
method that will actually draw something to our window, and process a (very boring) game loop until the user terminates the program:
func main() {
// Set the global composition mode
gogue.SetCompositionMode(0)
gogue.Refresh()
// Initialize our main game loop
for {
text := "Welcome to Gogue!"
gogue.PrintText((windowWidth / 2) - (len(text) / 2), windowHeight / 2, text, "white", "", 0)
gogue.Refresh()
}
gogue.CloseConsole()
}
Our main function does a couple of things. First, we set the global composition mode for the game. Composition mode dictates how things are drawn to the terminal. We're using mode 0, which means no composition (anything drawn to a cell will replace the contents of that cell). We could also have chosen mode 1, which means anything drawn to a cell will be composed on top of the contents of that cell.
Next, we call gogue.Refresh()
, which will actually render our window (when refresh is called for the first time, it renders the window, each subsequent time will re-draw the window, which is why we have this initial call outside of our loop).
The actual game loop is pretty straight forward: we're setting some text, printing that text to the terminal, and then re-drawing the terminal with the text displayed. We do this indefinitely until the user terminates the program. Upon the loop exiting for some reason, we call gogue.CloseConsole()
, which will effectively terminate our game.
With all that done, a go build
in the project directory should cause Modules to reach out and grab Gogue, and use it to build your application. Running the resulting .exe will display a Gogue window, with the text we printed centered in the screen.
Congratulations! You've got your first Gogue application up and running!