Getting Started with SDL2 on Go

In the article SDL GUI in C on Windows I looked at programming the SDL in C, its native language.

There are SDL bindings for other programming languages and one I’ve always wanted to try was Go (Golang). It’s a 78 MB download for 64 bit Windows from their downloads page.
After you’ve installed it, you can work from the command line or you have two main choices for an IDE, VS Code with the Luke Hoban Go extension or JetBrains Gogland which is commercial but is in an early release program. I installed it and used it for testing. Make sure you verify your go setup first though, especially the two environment variables before installinmg Gogland. It’s less hassle that way.
As well as Go, you are going to need MinGW, (short for Minimalist GNU for Windows), is a small development environment for native Microsoft Windows applications, basically the GCC C compiler. It’ll be needed to compile some C code.
Next you’ll need the veandco bindings which lets Go and SDL2 play nicely together. Finally you’ll need to download SDL2.
Note, when you are installing the veandco ” target=”_blank” title=”Link to gosdl2″>SDL 2 Go bindings , on Windows it says MinGW-w64. You get this from MinGW-w64 It’s not MinGW which is the older 32 bit version and also available from sourceforge. It’s easy to get the wrong version. I did.

After installing MinGW-64 you should edit the path variable in Windows. Under Windows, you set environment variables through the “Environment Variables” button on the “Advanced” tab of the “System” control panel. Some versions of Windows provide this control panel in the “Advanced System Settings” option in the “System” control panel. Add the location of your MinGW bin folder. On my computer, most things are on d: (don’t ask!) and the path starts with

D:\MinGW-W64\mingw64\bin

After installing MinGW-W64, check the compiler version from a command line with

gcc –version

The first line of the output should say something like:

gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 7.1.0

Install SDL2 files

On the SDL2 download page you’ll see source code, runtime binaries and Development libraries sections. It’s the latter you need, particularly the one that says MinGW 32/64 bit after it. If you open the zip file you’ll see several folders. The one we are interested in is x86_64-w64-mingw32. Extract that entire folder into your MinGW-w64\mingw64 folder.

Now do exactly the same for the SDL_image, SDL_mixer and ttf files. Unzip the x86_64-w64-mingw32 folder from each of those into the same MinGW\mingw64 folder.

Setting GOPATH and GOROOT

There are two windows environment variables. GOROOT which specifies the GO system files. This defaults to c:\go but if you change it you need to set GOROOT to wherever. On my system it’s d:\go.

GOPATH is the variable that you points to your workspace. It’s also where your Go packages and source files can be found. Downloaded packages are put in the GOPATH\pkg folder. GOPATH can have multiple paths separated by semi-colons ;. Note that with multiple paths in GOPATH, when you use the go get command to fetch packages, it uses the first location in the GOPATH string for pkg.

My GOPATH is this:

GOPATH=D:\development\Godev\bin;D:\development\Godev\src\github.com\veandco\go-sdl2\

Now on the veandco page where it says installation there are four go get commands to fetch the veandco bindings. Copy the four commands one by one and run each one from a command line. If you’ve done everything above correctly they should work. On my system they were installed in

D:\development\Godev\bin\pkg\windows_amd64\github.com\veandco

Testing

The veandcol install includes a large number of examples. On my PC they’re under

D:\development\Godev\src\github.com\veandco\go-sdl2\examples\

and the one they recommend to try is in the events folder and is called events.go.
From the command line you compile and run it with

go run events.go

If you get an error message like exit status 3221225781, it means that it needs sdl2.dll and possibly one of the other sdl.dlls. You can find all four SDL dlls in the MinGW-w64 bin folder. They are SDL2.dll, SDL2_ttf.dll, SDL2_image.dll and SDL2_mixer.dll. Just copy them into the folder where the source code is.

For some of the examples such as the openGL3 example, when you first compile it with

go run opengl3.go

You’ll get a “cannot find package “github.com/chsc/gogl/gl33” in any of… error message and it lists folders that it’s unsuccessfully searched in GOROOT and GOPATH. It just needs that package downloaded so do this go get command to fetch the package from github.

go get github.com/chsc/gogl/gl33

And after that the go run opengl3.go command will compile ok and run.

Conclusion

It’s perhaps not as simple as it could be, as there are three different bits to install (Go, MinGW and SDL2) then the veandco ‘glue’ but once you’ve got each installed then done the four veandco go gets, it all works very well.

Gogland looks very promising and is certainly way better than using notepad! I had a slight spot of bother because I hadn’t correctly setup GOROOT/GOPATH environment variables and it found a Go installation but it was an older one I’d forgotten I had. My fault, just set up those environment variables first and you’ll be fine.

Post a Comment

Your email address will not be published.