Skip to content

Instantly share code, notes, and snippets.

@TheFern2
Last active March 9, 2022 13:35
Show Gist options
  • Save TheFern2/65e185cbc6f2399fd14001dc94431209 to your computer and use it in GitHub Desktop.
Save TheFern2/65e185cbc6f2399fd14001dc94431209 to your computer and use it in GitHub Desktop.
How to use go sfml with windows mingw64 powershell

Guide for using sfml on windows with golang

Credit to https://github.com/DennisPing/Windows-10-Cpp-MinGW64

Initial installs and setup

Run powershell as admin

Prequisites

Restart powershell as admin

If you don't use cpp sfml, you only need mingw, tdm compiler also seems to work.

choco install mingw cmake make

Add cmake to path C:\Program Files\CMake\bin

In powershell check versions:

g++ --version
>>  g++.exe (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 11.2.0
    Copyright (C) 2021 Free Software Foundation, Inc.

cmake --version
>>  cmake version 3.21.3

make --version
>>  GNU Make 4.3
    Built for Windows32

Download sfml and csfml 2.4 (Currently go-sfml bindings are compatible with 2.4)

Extract them to where you normally put your libraries, I put mine on D:\libs\x64

Env variables

Add the following env variables to system, change path accordingly on first two, also note I renamed mine to CSFML-2.4, instead of CSFML: Key : Value

CGO_SFML_INCLUDE D:\libs\x64\CSFML-2.4\include
CGO_SFML_LIB D:\libs\x64\CSFML-2.4\lib\gcc
CGO_CFLAGS -I%CGO_SFML_INCLUDE%
CGO_LDFLAGS -L%CGO_SFML_LIB%
CGO_ENABLED 1

CSFML DLLs

Copy csfml from CSFML-2.4\bin to where your main.exe compiled, normally you only need the dlls you're importing.

Build and Run

NB: In powershell

go build .
go run .
package main
import (
"runtime"
"gopkg.in/teh-cmc/go-sfml.v24/graphics"
"gopkg.in/teh-cmc/go-sfml.v24/window"
)
func init() { runtime.LockOSThread() }
func main() {
vm := window.NewSfVideoMode()
defer window.DeleteSfVideoMode(vm)
vm.SetWidth(800)
vm.SetHeight(600)
vm.SetBitsPerPixel(32)
/* Create the main window */
cs := window.NewSfContextSettings()
defer window.DeleteSfContextSettings(cs)
w := graphics.SfRenderWindow_create(vm, "SFML window", uint(window.SfResize|window.SfClose), cs)
defer window.SfWindow_destroy(w)
ev := window.NewSfEvent()
defer window.DeleteSfEvent(ev)
/* Start the game loop */
for window.SfWindow_isOpen(w) > 0 {
/* Process events */
for window.SfWindow_pollEvent(w, ev) > 0 {
/* Close window: exit */
if ev.GetXtype() == window.SfEventType(window.SfEvtClosed) {
return
}
}
graphics.SfRenderWindow_clear(w, graphics.GetSfRed())
graphics.SfRenderWindow_display(w)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment