-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.go
152 lines (124 loc) · 3.31 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"github.com/veandco/go-sdl2/mix"
"github.com/veandco/go-sdl2/sdl"
)
type Display struct {
Window *sdl.Window
Surface *sdl.Surface
Instructions chan Instruction
PixelStatuses [100][100]bool
}
type Pixel struct {
X int32
Y int32
IsOn bool
}
type Instruction struct {
Fn func(args ...any)
args []any
}
var globalKey *Key
var keys map[byte]*Key
var onColor sdl.Color = sdl.Color{R: 255, G: 255, B: 255, A: 255}
var offColor sdl.Color = sdl.Color{R: 0, G: 0, B: 0, A: 0}
func (d *Display) InitDisplay() {
keys = make(map[byte]*Key)
d.Instructions = make(chan Instruction, 1000)
d.InitPixelStatuses()
go d.PerformInstructions()
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
panic(err)
}
var err error
defer sdl.Quit()
d.Window, err = sdl.CreateWindow("CHIP 8 Emulator", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 640, 320, sdl.WINDOW_SHOWN)
if err != nil {
panic(err)
}
defer d.Window.Destroy()
d.Surface, err = d.Window.GetSurface()
if err != nil {
panic(err)
}
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch e := event.(type) {
case *sdl.KeyboardEvent:
// Practical behavior: sdl can detect which key is up and down independently
if e.Type == uint32(sdl.KEYDOWN) {
ck := keys[MapKey(byte(e.Keysym.Sym))]
if keys[MapKey(byte(e.Keysym.Sym))] == nil {
ck = &Key{}
keys[MapKey(byte(e.Keysym.Sym))] = ck
}
ck.SetKeyboardEvent(e.Keysym.Sym)
ck.SetMappedKey()
ck.SetIsPressed(true)
globalKey = ck
} else {
globalKey = &Key{MappedKey: MapKey(byte(e.Keysym.Sym)), IsPressed: false}
ck := keys[MapKey(byte(e.Keysym.Sym))]
ck.ClearMappedKey()
ck.SetIsPressed(false)
}
case *sdl.QuitEvent:
fmt.Println("QUIT")
destroy = true
mix.CloseAudio()
display.Window.Destroy()
running = false
}
}
}
}
// Input of x and y should be the original pixel coordinate, and will be scaled by a factor of 10
func (d *Display) TurnOnPixel(x int32, y int32) {
d.PixelStatuses[x][y] = true
rect := sdl.Rect{X: x * 10, Y: y * 10, W: 10, H: 10}
pixel := sdl.MapRGBA(d.Surface.Format, onColor.R, onColor.G, onColor.B, onColor.A)
d.Surface.FillRect(&rect, pixel)
}
// Input of x and y should be the original pixel coordinate, and will be scaled by a factor of 10
func (d *Display) TurnOffPixel(x int32, y int32) {
d.PixelStatuses[x][y] = false
rect := sdl.Rect{X: x * 10, Y: y * 10, W: 10, H: 10}
pixel := sdl.MapRGBA(d.Surface.Format, offColor.R, offColor.G, offColor.B, offColor.A)
d.Surface.FillRect(&rect, pixel)
}
func (d *Display) IsPixelOn(x int32, y int32) bool {
return d.PixelStatuses[x][y]
}
func (d *Display) TogglePixel(x int32, y int32, flag *byte) {
if d.IsPixelOn(x, y) {
d.TurnOffPixel(x, y)
*flag = 0x1
} else {
d.TurnOnPixel(x, y)
}
}
func (d *Display) ClearScreen() {
d.Surface.FillRect(nil, 0)
d.InitPixelStatuses()
d.Window.UpdateSurface()
}
func (d *Display) Run() {
running := true
for running {
sdl.PollEvent()
}
}
func (d *Display) SendInstruction(i Instruction) {
d.Instructions <- i
}
func (d *Display) InitPixelStatuses() {
var arr [100][100]bool
d.PixelStatuses = arr
}
func (d *Display) PerformInstructions() {
for i := range d.Instructions {
i.Fn(i.args...)
}
}