-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.go
More file actions
89 lines (83 loc) · 1.78 KB
/
canvas.go
File metadata and controls
89 lines (83 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// +build !nocanvas
package main
import (
"fmt"
"github.com/tfriedel6/canvas/sdlcanvas"
)
var CANVAS = true
// Scancodes for arrow keys. Names correspond to "name" arg in wnd.KeyUp/KeyDown.
const (
ArrowUp = 82
ArrowDown = 81
ArrowLeft = 80
ArrowRight = 79
)
func newCanvas(width, height, scale int, memStart addr, mem *memory) {
wnd, cv, err := sdlcanvas.CreateWindow(width*scale, height*scale, "schoolasm")
if err != nil {
panic(fmt.Errorf("Failed to create window: %v", err))
}
defer wnd.Destroy()
// addresses for key events
size := addr(width * height)
keyUp := memStart + size + 1
keyDown := keyUp + 1
keyLeft := keyDown + 1
keyRight := keyLeft + 1
wnd.KeyUp = func(scancode int, rn rune, name string) {
switch scancode {
case ArrowUp:
mem[keyUp] = 0
case ArrowDown:
mem[keyDown] = 0
case ArrowLeft:
mem[keyLeft] = 0
case ArrowRight:
mem[keyRight] = 0
}
Println("UP", scancode, rn, name)
}
wnd.KeyDown = func(scancode int, rn rune, name string) {
isArrow := true
switch scancode {
case ArrowUp:
mem[keyUp] = 1
case ArrowDown:
mem[keyDown] = 1
case ArrowLeft:
mem[keyLeft] = 1
case ArrowRight:
mem[keyRight] = 1
default:
isArrow = false
}
if isArrow {
go func() {
KBINTERRUPT <- true
}()
}
Println("DOWN", scancode, rn, name)
}
// rows, then columns
wnd.MainLoop(func() {
offset := 0
for sy := 0; sy <= height; sy++ {
y := sy * scale
for sx := 0; sx < width; sx++ {
x := sx * scale
off := addr(sx) + addr(offset)
if off >= size {
break
}
white := mem[memStart+off]
if white == 1 {
cv.SetFillStyle("#ffffff")
} else {
cv.SetFillStyle("#000000")
}
cv.FillRect(float64(x), float64(y), float64(scale), float64(scale))
}
offset += width
}
})
}