Skip to content

A little test program that opens a window with a little white square you can control with arrow keys. Built for desktop (minifb) and web (wasm)

Notifications You must be signed in to change notification settings

phillvancejr/golang-moving-square

Repository files navigation

Golang Moving Square Test

A simple program that opens a window with a black background and a white square that you can move with the arrow keys. I wanted to test Go's interop with C and its Webassembly capabilities.

I used emoon's minifb for the desktop version. I've only built it for Mac but it should work on Windows, Linux and FreeBSD as these platforms are supported by minifb.

I used tinygo to build the web version

screen-gif screen-gif

Build Instructions

clone recursively with:

git clone --recursive https://github.com/phillvancejr/golang-moving-square.git

Desktop with Minifb

Mac

For mac you can use the makefile provided. Minifb uses CMake so you need that in your path.

make minifb
make desktop

The platform defaults to mac, so if you're on a different unix you should pass the platform and the cmake flags to make minifb, for example

make minifb platform=linux cmake_flags='-DUSE_WAYLAND_API=OFF'

For other desktop OS' look at minifb's build instructions

minifb.go

In minifb.go add #cgo <os> LDFLAGS: -Ldeps/<os> -lminifb

#cgo CFLAGS: -Ideps/minifb/include
#cgo darwin LDFLAGS: -Ldeps/mac -lminifb
#cgo darwin LDFLAGS: -framework MetalKit -framework Metal -framework Cocoa
#cgo <os> LDFLAGS: -Ldeps/<os> -lminifb
#include <MiniFB.h>
#include <stdlib.h>

Web with Wasm & TinyGo

You need tinygo in your path of course, then if using the makefile

make web
make server
./server

The server defaults to port 8000 but you can pass another port on the command line

Controls

Just use the arrow keys to move around and escape key to close the window on desktop

About

A little test program that opens a window with a little white square you can control with arrow keys. Built for desktop (minifb) and web (wasm)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published