-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_display.go
66 lines (54 loc) · 1.39 KB
/
standard_display.go
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
package chip8
import (
"io"
)
const White = "□"
const Black = "■"
// StandardDisplay implements interface Display
type StandardDisplay struct {
output io.Writer
screen [32][64]byte
}
type ConfigDisplay struct {
Output io.Writer
}
// NewStandardDisplay is a function that receive a config as param and return a pointer to StandardDisplay
func NewStandardDisplay(config *ConfigDisplay) *StandardDisplay {
return &StandardDisplay{output: config.Output}
}
// Flush is a function that paint the screen with information of attribute "screen"
func (sd *StandardDisplay) Flush() {
buf := ""
for i := 0; i < 32; i++ {
for j := 0; j < 64; j++ {
if sd.screen[i][j] == 1 {
buf += Black
} else {
buf += White
}
}
buf += "\n"
}
sd.output.Write([]byte(buf))
}
// Clear sets all pixels to 0
func (sd *StandardDisplay) Clear() {
for i := 0; i < 32; i++ {
for j := 0; j < 64; j++ {
sd.screen[i][j] = 0
}
}
}
// Draw draws a sprint on position xDisplay and yDisplay
func (sd *StandardDisplay) Draw(xDisplay, yDisplay, sprite byte) bool {
collision := false
for bitIdx := 0; bitIdx < 8; bitIdx++ {
newPixel := (sprite & (1 << (7 - bitIdx))) >> (7 - bitIdx)
oldPixel := sd.screen[yDisplay%32][(xDisplay+byte(bitIdx))%64]
if newPixel == 1 && oldPixel == 1 {
collision = true
}
sd.screen[yDisplay%32][(xDisplay+byte(bitIdx))%64] = newPixel ^ oldPixel
}
return collision
}