add some more games
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

improve rendering by drawing only when needed
This commit is contained in:
Sid 2020-10-12 20:16:18 +01:00
parent 0bd4180b85
commit 0308227dc5
6 changed files with 181 additions and 6 deletions

View File

@ -23,6 +23,8 @@ steps:
image: golang:latest
commands:
- GOOS=js GOARCH=wasm go build -o main.wasm ./cmd/web
- cp ./cmd/web/html/index.html .
- cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
- name: publish
image: plugins/gitea-release
@ -31,6 +33,7 @@ steps:
- build windows
- build linux
- build mac
- build wasm
# This step is only run when a branch is tagged in Gitea.
when:
event:
@ -43,5 +46,8 @@ steps:
- mac_test
- linux_test
- windows_test.exe
- main.wasm
- index.html
- wasm_exec.js
checksum:
- sha1

50
cmd/web/html/index.html Normal file
View File

@ -0,0 +1,50 @@
<html>
<head>
<meta charset="utf-8" />
<title>Go WebAssembly - Plot</title>
<meta name="author" content="Mark Farnan" />
<meta name="description" content="Prototype PLotter with GO, Canvas and WebAssembly" />
<meta name="theme-color" content="#000000" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<style>
body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
background-color: #FFFFFF;
color: #000000;
font-family: Arial, Helvetica, sans-serif
}
</style>
<!--
Add the following polyfill for Microsoft Edge 17/18 support:
<script src="https://cdn.jsdelivr.net/npm/text-encoding@0.7.0/lib/encoding.min.js"></script>
(see https://caniuse.com/#feat=textencoder)
-->
<script type="text/javascript" src="./wasm_exec.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
WebAssembly.instantiateStreaming(fetch('main.wasm'), go.importObject).then(res => {
go.run(res.instance)
})
</script>
<style>
</style>
</head>
<body>
</body>
</html>

View File

@ -2,8 +2,8 @@ package main
import (
"image/color"
"time"
"sync"
"time"
"github.com/llgcode/draw2d/draw2dimg"
"github.com/llgcode/draw2d/draw2dkit"
@ -18,6 +18,7 @@ var cvs *canvas.Canvas2d
var width, height float64 = 64, 32
var sizeMultiplier = 8
var drawBuf = [64 * 32]byte{}
var drawNeeded = false
var graphicsLock sync.Mutex
func main() {
@ -29,7 +30,7 @@ func main() {
height = float64(cvs.Height())
width = float64(cvs.Width())
cpu := chip8.NewCHIP8(getSpaceInvaders())
cpu := chip8.NewCHIP8(getPong())
cvs.Start(60, Render)
i := 0
@ -38,6 +39,7 @@ func main() {
go timeCycle(c)
cpu.PerformCycle()
if cpu.DrawIsNeeded() {
drawNeeded = true
graphicsLock.Lock()
drawBuf = cpu.GetGraphicsBuffer()
graphicsLock.Unlock()
@ -58,6 +60,10 @@ func timeCycle(c chan int) {
}
func Render(gc *draw2dimg.GraphicContext) bool {
if !drawNeeded {
return false
}
drawNeeded = false
gc.SetFillColor(color.RGBA{0x00, 0x00, 0x00, 0xff})
gc.Clear()

18
cmd/web/pong.go Normal file
View File

@ -0,0 +1,18 @@
package main
func getPong() []byte {
return []byte{
106, 2, 107, 12, 108, 63, 109, 12, 162, 234, 218, 182, 220, 214, 110, 0, 34, 212, 102, 3,
104, 2, 96, 96, 240, 21, 240, 7, 48, 0, 18, 26, 199, 23, 119, 8, 105, 255, 162, 240,
214, 113, 162, 234, 218, 182, 220, 214, 96, 1, 224, 161, 123, 254, 96, 4, 224, 161, 123, 2,
96, 31, 139, 2, 218, 182, 96, 12, 224, 161, 125, 254, 96, 13, 224, 161, 125, 2, 96, 31,
141, 2, 220, 214, 162, 240, 214, 113, 134, 132, 135, 148, 96, 63, 134, 2, 97, 31, 135, 18,
70, 2, 18, 120, 70, 63, 18, 130, 71, 31, 105, 255, 71, 0, 105, 1, 214, 113, 18, 42,
104, 2, 99, 1, 128, 112, 128, 181, 18, 138, 104, 254, 99, 10, 128, 112, 128, 213, 63, 1,
18, 162, 97, 2, 128, 21, 63, 1, 18, 186, 128, 21, 63, 1, 18, 200, 128, 21, 63, 1,
18, 194, 96, 32, 240, 24, 34, 212, 142, 52, 34, 212, 102, 62, 51, 1, 102, 3, 104, 254,
51, 1, 104, 2, 18, 22, 121, 255, 73, 254, 105, 255, 18, 200, 121, 1, 73, 2, 105, 1,
96, 4, 240, 24, 118, 1, 70, 64, 118, 254, 18, 108, 162, 242, 254, 51, 242, 101, 241, 41,
100, 20, 101, 0, 212, 85, 116, 21, 242, 41, 212, 85, 0, 238, 128, 128, 128, 128, 128, 128,
128, 0, 0, 0, 0, 0}
}

File diff suppressed because one or more lines are too long

30
cmd/web/tetris.go Normal file
View File

@ -0,0 +1,30 @@
package main
func getTetris() []byte {
return []byte{
162, 180, 35, 230, 34, 182, 112, 1, 208, 17, 48, 37, 18, 6, 113, 255, 208, 17, 96, 26,
208, 17, 96, 37, 49, 0, 18, 14, 196, 112, 68, 112, 18, 28, 195, 3, 96, 30, 97, 3,
34, 92, 245, 21, 208, 20, 63, 1, 18, 60, 208, 20, 113, 255, 208, 20, 35, 64, 18, 28,
231, 161, 34, 114, 232, 161, 34, 132, 233, 161, 34, 150, 226, 158, 18, 80, 102, 0, 246, 21,
246, 7, 54, 0, 18, 60, 208, 20, 113, 1, 18, 42, 162, 196, 244, 30, 102, 0, 67, 1,
102, 4, 67, 2, 102, 8, 67, 3, 102, 12, 246, 30, 0, 238, 208, 20, 112, 255, 35, 52,
63, 1, 0, 238, 208, 20, 112, 1, 35, 52, 0, 238, 208, 20, 112, 1, 35, 52, 63, 1,
0, 238, 208, 20, 112, 255, 35, 52, 0, 238, 208, 20, 115, 1, 67, 4, 99, 0, 34, 92,
35, 52, 63, 1, 0, 238, 208, 20, 115, 255, 67, 255, 99, 3, 34, 92, 35, 52, 0, 238,
128, 0, 103, 5, 104, 6, 105, 4, 97, 31, 101, 16, 98, 7, 0, 238, 64, 224, 0, 0,
64, 192, 64, 0, 0, 224, 64, 0, 64, 96, 64, 0, 64, 64, 96, 0, 32, 224, 0, 0,
192, 64, 64, 0, 0, 224, 128, 0, 64, 64, 192, 0, 0, 224, 32, 0, 96, 64, 64, 0,
128, 224, 0, 0, 64, 192, 128, 0, 192, 96, 0, 0, 64, 192, 128, 0, 192, 96, 0, 0,
128, 192, 64, 0, 0, 96, 192, 0, 128, 192, 64, 0, 0, 96, 192, 0, 192, 192, 0, 0,
192, 192, 0, 0, 192, 192, 0, 0, 192, 192, 0, 0, 64, 64, 64, 64, 0, 240, 0, 0,
64, 64, 64, 64, 0, 240, 0, 0, 208, 20, 102, 53, 118, 255, 54, 0, 19, 56, 0, 238,
162, 180, 140, 16, 60, 30, 124, 1, 60, 30, 124, 1, 60, 30, 124, 1, 35, 94, 75, 10,
35, 114, 145, 192, 0, 238, 113, 1, 19, 80, 96, 27, 107, 0, 208, 17, 63, 0, 123, 1,
208, 17, 112, 1, 48, 37, 19, 98, 0, 238, 96, 27, 208, 17, 112, 1, 48, 37, 19, 116,
142, 16, 141, 224, 126, 255, 96, 27, 107, 0, 208, 225, 63, 0, 19, 144, 208, 225, 19, 148,
208, 209, 123, 1, 112, 1, 48, 37, 19, 134, 75, 0, 19, 166, 125, 255, 126, 255, 61, 1,
19, 130, 35, 192, 63, 1, 35, 192, 122, 1, 35, 192, 128, 160, 109, 7, 128, 210, 64, 4,
117, 254, 69, 2, 101, 4, 0, 238, 167, 0, 242, 85, 168, 4, 250, 51, 242, 101, 240, 41,
109, 50, 110, 0, 221, 229, 125, 5, 241, 41, 221, 229, 125, 5, 242, 41, 221, 229, 167, 0,
242, 101, 162, 180, 0, 238, 106, 0, 96, 25, 0, 238, 55, 35}
}