Skip to content

Added implementation for a wasm backend #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 70 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,89 @@
prototype
=========
# 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.
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.

![Games](https://github.com/gonutz/prototype/blob/master/samples/screenshots/games.png)

Installation
------------
## Installation

Install the [Go programming language](https://golang.org/dl/). After clicking the download link you will be referred to the installation instructions for your specific operating system.

Install [Git](https://git-scm.com/downloads) and make it available in the PATH so the go tool can use it.
Install [Git](https://git-scm.com/downloads) 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.
For Linux and macOS, 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):
### Supported Targets

`libx11-dev libxrandr-dev libgl1-mesa-dev libxcursor-dev libxinerama-dev libxi-dev`
The prototype framework supports multiple targets:

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:
#### Windows (default)

`libsdl2-dev libsdl2-mixer-dev libsdl2-image-dev`
- Uses Direct3D 9
- No additional dependencies needed

#### Linux/macOS (GLFW backend)

Install the library and samples by running the following on your command line:
- Uses OpenGL via GLFW
- Install required packages (example for Ubuntu/Debian):
```sh
sudo apt install libx11-dev libxrandr-dev libgl1-mesa-dev libxcursor-dev libxinerama-dev libxi-dev
```

go get github.com/gonutz/prototype/...
#### Linux (SDL2 backend)

Documentation
-------------
- Install SDL2 libraries:
```sh
sudo apt install libsdl2-dev libsdl2-mixer-dev libsdl2-image-dev
```
- Use build tag:
```sh
go run -tags sdl2 main.go
```

For a description of all library functions, see [the godoc page](http://godoc.org/github.com/gonutz/prototype/draw) 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.
#### WebAssembly (experimental)

Example
-------
- Implemented via HTML5 Canvas and Web Audio using `syscall/js`
- Requires Go 1.21+
- Compile using:
```sh
GOOS=js GOARCH=wasm go build -o main.wasm
```
- Use a simple HTML wrapper with a canvas and `wasm_exec.js` (from Go installation):
```html
<canvas id="gameCanvas" width="800" height="600" style="touch-action:none;"></canvas>
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
go.run(result.instance);
});
</script>
```

```Go
> [!NOTE]
> The required `wasm_exec.js` file is included in the Go installation. You can find it in the `$(go env GOROOT)/misc/wasm` or `$(go env GOROOT)/lib/wasm` directory. Copy it to your project directory.

To serve locally:

```sh
python3 -m http.server
```

## Installation (Library & Samples)

Install the library and samples by running:

```sh
go get github.com/gonutz/prototype/...
```

## Documentation

For a description of all library functions, see [the GoDoc page](http://godoc.org/github.com/gonutz/prototype/draw). Most functionality is in the `Window` interface, and documented via code comments.

## Example

```go
package main

import (
Expand All @@ -49,11 +97,9 @@ func main() {
}

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
Expand All @@ -66,16 +112,13 @@ func update(window draw.Window) {
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
if dx*dx+dy*dy <= 20*20 {
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.

This example displays a window with a round button in the middle to close it. It demonstrates basic drawing and event handling.
3 changes: 2 additions & 1 deletion draw/window_glfw.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//go:build glfw || (!windows && !sdl2)
//go:build (glfw || (!windows && !sdl2)) && !js
// +build glfw !windows,!sdl2
// +build !js

package draw

Expand Down
4 changes: 2 additions & 2 deletions draw/window_sdl2.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build sdl2 && !glfw
// +build sdl2,!glfw
//go:build sdl2 && !glfw && !js
// +build sdl2,!glfw,!js

package draw

Expand Down
Loading