-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.go
284 lines (246 loc) · 5.73 KB
/
screen.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
t "github.com/gdamore/tcell/v2"
"log"
"time"
)
const (
BORDER_WIDTH = 1
BORDER_PAD = 2 * BORDER_WIDTH
)
type EventKind uint8
const (
EventQuit EventKind = iota
EventClear
EventResize
EventSprite
)
type Event struct {
Kind EventKind
Width int
Height int
Sprite Sprite
}
type Display struct {
screen t.Screen
fb Fb
fbpos Vec2
Echan chan Event
Key chan uint8
Collide chan uint8
}
func NewDisplay() Display {
s, err := t.NewScreen()
if err != nil {
log.Fatalf("%+v", err)
}
if err := s.Init(); err != nil {
log.Fatalf("%+v", err)
}
if sw, sh := s.Size(); sw < FB_WIDTH || sh < FB_HEIGHT {
s.Clear()
s.Fini()
if sw < FB_WIDTH {
log.Fatalf("screen width %d < %d for rendering\n", sw, FB_WIDTH)
} else if sh < FB_HEIGHT {
log.Fatalf("screen height %d < %d for rendering\n", sh, FB_HEIGHT)
}
}
dp := Display{
screen: s,
fb: NewFb(FB_WIDTH, FB_HEIGHT),
fbpos: Vec2{BORDER_PAD, BORDER_PAD},
Echan: make(chan Event, 4),
Key: make(chan uint8),
Collide: make(chan uint8),
}
dp.drawBorder()
return dp
}
// Flash a FrameBuffer to the underlying screen
func (dp *Display) renderFb() {
s := dp.screen
fb := dp.fb
sw, sh := s.Size()
style := t.StyleDefault.Foreground(t.ColorBlue).Background(t.ColorReset)
idx := func(x, y int) int {
return y*fb.w + x
}
for y := 0; y < fb.h; y++ {
dy := dp.fbpos.y + y
if dy == 0 {
dy += BORDER_WIDTH
}
if dy == sh-1 {
dy -= BORDER_WIDTH
}
for x := 0; x < fb.w; x++ {
r := CELL_FILLED
if fb.buf[idx(x, y)] == 0 {
r = CELL_EMPTY
}
// respect the screen border
dx := dp.fbpos.x + x
if dx == 0 {
dx += BORDER_WIDTH
}
if dx == sw-1 {
dx -= BORDER_WIDTH
}
s.SetContent(dx, dy, r, nil, style)
}
}
}
// This is analogous to the Fetch-Execute-Cycle of the `Cpu`.
// It starts the window with content, and is only called if `isScreenInit` = true
//
// This is the driver/center of this abstraction, and must be a Goroutine to not block
func (dp *Display) StartRenderLoop() {
sw, sh := dp.screen.Size()
log.Printf("Screen: %dx%d\n", sw, sh)
// spinner shit
// spinnerStyles := []t.Style{
// t.StyleDefault.Background(t.ColorReset).Foreground(t.ColorRed),
// t.StyleDefault.Background(t.ColorReset).Foreground(t.ColorGreen),
// t.StyleDefault.Background(t.ColorReset).Foreground(t.ColorBlue),
// t.StyleDefault.Background(t.ColorReset).Foreground(t.ColorWhite),
// }
// lstyles := len(spinnerStyles)
// spinners := `←↖↑↗→↘↓↙`
// len_spinners := len(spinners)
go dp.pollEvent()
renderloop:
for {
select {
case ev := <-dp.Echan:
switch ev.Kind {
case EventQuit:
log.Println("LoopQuit::")
close(dp.Key)
close(dp.Echan)
break renderloop
case EventClear:
clear(dp.fb.buf)
dp.renderFb()
dp.screen.Show()
case EventResize:
dp.handleResize()
sw, sh := ev.Width, ev.Height
log.Printf("Resized to: %dx%d\n", sw, sh)
case EventSprite:
sprite := ev.Sprite
dp.Collide <- dp.fb.DrawSpriteAt(sprite.data, Vec2{int(sprite.x), int(sprite.y)})
dp.renderFb()
dp.screen.Show()
default:
}
default:
if dp.handleRemainingEvent() {
break renderloop
}
}
time.Sleep(8 * time.Millisecond)
}
log.Println("Done")
}
func (dp *Display) handleRemainingEvent() bool {
rest:
for {
select {
case ev := <-dp.Echan:
switch ev.Kind {
case EventQuit:
log.Println("LoopQuit::rest")
close(dp.Key)
close(dp.Echan)
return true
case EventClear:
clear(dp.fb.buf)
dp.renderFb()
dp.screen.Show()
case EventResize:
dp.handleResize()
sw, sh := ev.Width, ev.Height
log.Printf("Resized to: %dx%d\n", sw, sh)
case EventSprite:
sprite := ev.Sprite
dp.Collide <- dp.fb.DrawSpriteAt(sprite.data, Vec2{int(sprite.x), int(sprite.y)})
dp.renderFb()
dp.screen.Show()
default:
break rest
}
}
}
return false
}
// This async'ly polls events from the window and other sources. It also emits
// events to certain sources
//
// Thus, it must be it's own Goroutine
func (dp *Display) pollEvent() {
s := dp.screen
for {
ev := s.PollEvent()
switch ev := ev.(type) {
case *t.EventResize:
log.Printf("EventResize::")
sw, sh := ev.Size()
dp.Echan <- Event{Kind: EventResize, Width: sw, Height: sh}
case *t.EventKey:
log.Println("EventKey::")
if ev.Key() == t.KeyEscape || ev.Key() == t.KeyCtrlC {
log.Printf("stopping\n")
dp.Echan <- Event{Kind: EventQuit}
log.Printf("STOP\n")
return
} else if ev.Key() == t.KeyRune {
logmsg("rune\n")
key := uint8(ev.Rune())
if '0' <= key && key <= '9' {
logmsg("key 0-9: %c/%d\n", key, key-'0')
key = key - '0'
}
if 'a' <= key && key <= 'f' {
logmsg("key a-f: %c/%d\n", key, 0xa+key-'a')
key = 0xa + key - 'a'
}
dp.Key <- key
}
default:
}
}
}
func (dp *Display) handleResize() {
log.Println("to resize")
dp.screen.Clear()
dp.drawBorder()
dp.renderFb()
dp.screen.Sync()
}
func (dp *Display) HandleQuit() {
log.Println("Quit::")
maybePanic := recover()
dp.screen.Clear()
dp.screen.Fini()
if maybePanic != nil {
panic(maybePanic)
}
}
func (dp *Display) drawBorder() {
s := dp.screen
w, h := s.Size()
style := t.StyleDefault.Foreground(t.ColorDarkCyan).Background(t.ColorReset)
for y := 0; y < h; y++ {
s.SetContent(0, y, BORDER_VERT, nil, style)
s.SetContent(w-1, y, BORDER_VERT, nil, style)
}
for x := 0; x < w; x++ {
s.SetContent(x, 0, BORDER_HORI, nil, style)
s.SetContent(x, h-1, BORDER_HORI, nil, style)
}
s.SetContent(0, 0, BORDER_LEFT_TOP, nil, style)
s.SetContent(w-1, 0, BORDER_RIGHT_TOP, nil, style)
s.SetContent(0, h-1, BORDER_LEFT_BOTTOM, nil, style)
s.SetContent(w-1, h-1, BORDER_RIGHT_BOTTOM, nil, style)
}