Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Adding a way to start an example in the browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryBrownEEngr committed Dec 13, 2023
1 parent ce0c752 commit a59366f
Show file tree
Hide file tree
Showing 16 changed files with 654 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode/*
*.wasm
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ The goals are many:

## Install

Go 1.21 or later is required.<br>
Go 1.21 or later is required.

Ebitengine is the main dependency. [Check here for the system specific instructions](https://ebitengine.org/en/documents/install.html).

## Example
Expand Down Expand Up @@ -109,7 +110,7 @@ go run github.com/GaryBrownEEngr/scratch/examples/fallingturtles@latest

Here is a simulation of a rotating box filled with circles, boxes, and rounded rectangles. This uses the library github.com/jakecoffman/cp for the physics simulation. The sprites are being drawn using Golang Scratch.

This is a recreation of https://jakecoffman.com/cp-ebiten/tumble/ except without drawing the shapes using ebiten and cp directly. Here, cp is only performing the physics updates. The original code can be found [here](https://github.com/jakecoffman/cp-examples/blob/master/tumble/tumble.go)
This is a recreation of <https://jakecoffman.com/cp-ebiten/tumble/> except without drawing the shapes using ebiten and cp directly. Here, cp is only performing the physics updates. The original code can be found [here](https://github.com/jakecoffman/cp-examples/blob/master/tumble/tumble.go)

```bash
go run github.com/GaryBrownEEngr/scratch/examples/tumbler@latest
Expand Down Expand Up @@ -145,7 +146,7 @@ GOOS=js GOARCH=wasm go build ./examples/fallingturtles/

## Things to Research

* https://jakecoffman.com/cp-ebiten/
* https://github.com/jakecoffman/cp
* https://github.com/jakecoffman/cp-examples
* https://github.com/jakecoffman/cp-ebiten
- <https://jakecoffman.com/cp-ebiten/>
- <https://github.com/jakecoffman/cp>
- <https://github.com/jakecoffman/cp-examples>
- <https://github.com/jakecoffman/cp-ebiten>
Binary file added cmd/convertimages/RedBall.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cmd/convertimages/SilverBall.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cmd/convertimages/arrow.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions cmd/convertimages/convertImages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"encoding/base64"
"fmt"
"os"
)

func main() {
fileData, err := os.ReadFile("turtle.png")
if err != nil {
panic(err)
}
result := base64.StdEncoding.EncodeToString(fileData)
fmt.Printf("var turtleImage string = \"%s\"\n", result)

fileData, err = os.ReadFile("arrow.png")
if err != nil {
panic(err)
}
result = base64.StdEncoding.EncodeToString(fileData)
fmt.Printf("var arrowImage string = \"%s\"\n", result)
}
Binary file added cmd/convertimages/turtle.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions cmd/runinweb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Run Example In Browser

## Info

Based on info from: <https://ebitengine.org/en/documents/webassembly.html>

## Run

```bash
. ./cmd/runinweb/build.sh ./examples/fallingturtles/
```

Then open: <http://localhost:3000/game.html>

OR: <http://localhost:3000/main.html> for an iframe.
4 changes: 4 additions & 0 deletions cmd/runinweb/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo $1
cp $(go env GOROOT)/misc/wasm/wasm_exec.js ./cmd/runinweb/
env GOOS=js GOARCH=wasm go build -o ./cmd/runinweb/yourGame.wasm $1
go run ./cmd/runinweb
16 changes: 16 additions & 0 deletions cmd/runinweb/game.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<script src="wasm_exec.js"></script>
<script>
// Polyfill
if (!WebAssembly.instantiateStreaming) {
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}

const go = new Go();
WebAssembly.instantiateStreaming(fetch("yourGame.wasm"), go.importObject).then(result => {
go.run(result.instance);
});
</script>
15 changes: 15 additions & 0 deletions cmd/runinweb/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"log"
"net/http"
)

func main() {
http.Handle("/", http.FileServer(http.Dir("./cmd/runinweb")))
fmt.Println("Golang: Serving directory")
log.Fatal(http.ListenAndServe(":3000", nil))
}

// http://localhost:3000/
3 changes: 3 additions & 0 deletions cmd/runinweb/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!DOCTYPE html>
<div>Example Golang Scratch game running in the browser as WASM.</div>
<iframe src="game.html" width="1000" height="1000" scrolling="no"></iframe>

0 comments on commit a59366f

Please sign in to comment.