user input! sorta playable
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sid 2020-10-12 22:16:30 +01:00
parent ec8fb167e7
commit b0797ff4af
2 changed files with 52 additions and 7 deletions

View File

@ -332,6 +332,10 @@ func (cpu *Chip8) fifteenIndexOpcodes() {
}
func (cpu *Chip8) UpdateKeys(newKeys [16]byte) {
cpu.keys = newKeys
}
func (cpu *Chip8) PerformCycle() {
cpu.opcode = (uint16(cpu.memory[cpu.pc]) << 8) + uint16(cpu.memory[cpu.pc + 1])
cpu.pc += 2

View File

@ -3,8 +3,8 @@ package main
import (
"image/color"
"sync"
"time"
"syscall/js"
"time"
"github.com/llgcode/draw2d/draw2dimg"
"github.com/llgcode/draw2d/draw2dkit"
@ -21,19 +21,57 @@ var sizeMultiplier = 8
var drawBuf = [64 * 32]byte{}
var drawNeeded = false
var graphicsLock sync.Mutex
var keysLock sync.Mutex
var window js.Value
var keys [16]byte
var keyPressHandler js.Func = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
println(args[0].Get("type").String())
println(args[0].Get("keyCode").Int())
return nil
})
var keyMap = map[int]int{
88: 0, // x
49: 1, // 1
50: 2, // 2
51: 3, // 3
81: 4, // q
87: 5, // w
69: 6, // e
65: 7, // a
83: 8, // s
68: 9, // d
90: 10, // z
67: 11, // c
52: 12, // 4
82: 13, // r
70: 14, // f
86: 15, // v
}
func keyEventHandle(event js.Value) {
println(event.Get("type").String())
println(event.Get("keyCode").Int())
elem, ok := keyMap[event.Get("keyCode").Int()]
if ok {
keysLock.Lock()
defer keysLock.Unlock()
if event.Get("type").String() == "keydown" {
keys[elem] = 1
} else if event.Get("type").String() == "keyup" {
keys[elem] = 0
}
}
}
func main() {
println("CHIP8 IS HERE!")
window = js.Global()
window.Call("addEventListener", "keydown", keyPressHandler)
window.Call("addEventListener", "keydown", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
keyEventHandle(args[0])
return nil
}))
window.Call("addEventListener", "keyup", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
keyEventHandle(args[0])
return nil
}))
cvs, _ = canvas.NewCanvas2d(false)
cvs.Create(int(width)*sizeMultiplier, int(height)*sizeMultiplier)
@ -54,6 +92,9 @@ func main() {
graphicsLock.Lock()
drawBuf = cpu.GetGraphicsBuffer()
graphicsLock.Unlock()
keysLock.Lock()
cpu.UpdateKeys(keys)
keysLock.Unlock()
}
i++
if i > 7 {