prototype
Simply prototype 2D games using an easy, minimal interface that lets you draw simple primitives and images on the screen, easily handle mouse and keyboard events and play sounds.
Installation
Install the Go programming language. After clicking the download link you will be referred to the installation instructions for your specific operating system.
Install Git and make it available in the PATH so the go tool can use it.
For Linux and OS X you need a C compiler installed. On Windows this is not necessary.
On Linux there are two backends available, GLFW and SDL2. For GLFW you need these libraries installed (tested on Linux Mint, other distros might be slightly different):
libx11-dev libxrandr-dev libgl1-mesa-dev libxcursor-dev libxinerama-dev libxi-dev
GLFW is used by default. To use SDL2 you need to add -tags sdl2
to your Go builds, e.g. go run -tags sdl2 main.go
. Also you need the SDL2 libraries installed:
libsdl2-dev libsdl2-mixer-dev libsdl2-image-dev
Install the library and samples by running the following on your command line:
go get github.com/gonutz/prototype/...
Documentation
For a description of all library functions, see the godoc page for this project. Note that most of the functionality is in the Window interface and hence the descriptions are listed as code comments in the source for that type.
Example
package main
import (
"math"
"github.com/gonutz/prototype/draw"
)
func main() {
draw.RunWindow("Title", 640, 480, update)
}
func update(window draw.Window) {
// find the screen center
w, h := window.Size()
centerX, centerY := w/2, h/2
// draw a button in the center of the screen
mouseX, mouseY := window.MousePosition()
mouseInCircle := math.Hypot(float64(mouseX-centerX), float64(mouseY-centerY)) < 20
color := draw.DarkRed
if mouseInCircle {
color = draw.Red
}
window.FillEllipse(centerX-20, centerY-20, 40, 40, color)
window.DrawEllipse(centerX-20, centerY-20, 40, 40, draw.White)
if mouseInCircle {
window.DrawScaledText("Close!", centerX-40, centerY+25, 1.6, draw.Green)
}
// check all mouse clicks that happened during this frame
for _, click := range window.Clicks() {
dx, dy := click.X-centerX, click.Y-centerY
squareDist := dx*dx + dy*dy
if squareDist <= 20*20 {
// close the window and end the application
window.Close()
}
}
}
This example displays a window with a round button in the middle to close it. It demonstrates some basic drawing and event handling code.
Is there a way to have the screen size smaller than the window size?
I want to use this for pixel art games, but I don't want to have a tiny window. So how do I make the screen size smaller than the window size? For example, having the window 960x600 but drawing on it like it's 320x200.
Build errors on Windows w/go 1.15 and 1.16
What version of go is wanted/desired? It might be worth adding a go.mod file (
go mod init
) so you can specify the go and dependency versions inline?Steps to reproduce:
I tested with the built-in Windows Sandbox feature (if not enabled, hit start, type enough of 'turn windows features on' to get a completion, and enable 'Windows Sandbox' in the list of optional features). Could also be done with a virtual machine.
Install latest git: https://github.com/git-for-windows/git/releases/download/v2.30.1.windows.1/Git-2.30.1-64-bit.exe
Install go 1.16: https://golang.org/dl/go1.16.windows-amd64.msi
Open a Powershell session (I'm a linux/bash guy, powershell is based on the same spec as bash so it's a little more natural to me) -- hit start -- type 'powershell'
ensure git and go are installed (note: powershell supports completion on flags and arguments)
create a local go project:
initialize local go module
create a simple test file
import "github.com/gonutz/prototype/draw"
func main() { draw.RunWindow("Test", 640, 480, update) }
func update(window draw.Window) { window.DrawLine(100, 100, 440, 380, draw.Blue) }
install dependencies
attempt to run PS C:\Users\WDAGUtilityAccount\go\local\scratch> go run . # github.com/gonutz/prototype/draw ....\pkg\mod\github.com\gonutz\[email protected]\draw\window_windows.go:93:8: undefined: w32.UnregisterClassAtom ....\pkg\mod\github.com\gonutz\[email protected]\draw\window_windows.go:442:7: undefined: w32.WM_MOUSEHWHEEL ....\pkg\mod\github.com\gonutz\[email protected]\draw\window_windows.go:515:10: cannot use style & ^w32.WS_OVERLAPPEDWINDOW (type int32) as type uint32 in argument to w32.SetWindowLong
Web Assembly?
Will there be any support for Web Assembly? I want to use this for game jams, but usually people don't want to download games, so I usually put web builds of my games. :smile:
I know this isn't technically an issue but,
It is an issue that this is so underrated! I've been trying everything everywhere from Vulkan-go to glfw-go and NOTHING worked cross platform! Thank you guys, and I'll probably contribute to this in the future! :smile:
some tips
hello I am again sorry(I am only student who do not understand many things), but I am working on my bachalor and I would like to share tip how to improve code(if I do found and it is already exist, sorry):
structures:= make([]Shape,...)
before "animation loop"(or during first turn - I have "init turn" because I use func "GetScaledText") and your "main code" will look like:some errors on linux arm
hello, i am sorry(I am only begener) but I was trying this library on linux arm(andronix, you can see on Linux Distro on Android) and I found this:
Fix/w32 modules ref
Prototype references github.com/gonutz/w32 which, with modules, fetches the v1.0.0 version that does not build. This change updates prototype to build with go 1.15 and 1.16 in a fresh environment.
Testing:
fails:
then I used go mod edit to switch to using the local on-disk copy of prototype:
which works
Cannot call RunWindow mutliple times
Tested on Windows: calling RunWindow twice should open a window and once that is closed should open another one. Currently after closing the first window, no second window appears.
Is it possible to draw triangles or polygons?
Looking at the source code, there doesn't seem to be any way to draw triangles or polygons.
Is there any way to access lower-level functions to achieve this, or is this being planned for a future update?
Thanks!