-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
184 lines (156 loc) · 4.42 KB
/
render.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
package maze
import (
"fmt"
"github.com/nsf/termbox-go"
)
var (
termboxInitialized = false
)
const (
// KBEventUnknown -- unknown keyboard event
KBEventUnknown = iota
// KBEventCancel -- user has canceled the rendering (ESC or ^C)
KBEventCancel
// KBEventPause -- user has paused the rendering (^S or break)
KBEventPause
)
// KeyboardEventChannel is used for passing keyboard events from the renderer to it's client.
type KeyboardEventChannel chan int
// Renderer is an interface that has knows how to display a maze on a terminal
// The interface is intended to be compatible with a dummy terminal so that the
// maze can be rendered into a file if needed.
type Renderer interface {
Reset()
PutChar(c rune)
GetKeyboardEvent() KeyboardEventChannel
NextLine()
Flush()
Done()
Size() (int, int)
}
// Render draws the level and the path through it
func Render(level Level, banner string, r Renderer) {
// Map out actors and their paths for quick lookup
actorMap := make(map[Position]rune)
for _, actor := range level.Actors {
for _, pos := range actor.Path {
// Avoid overriding actors with breadcrumbs, hence the lookup
if _, ok := actorMap[pos]; !ok {
actorMap[pos] = '.'
}
}
actorMap[actor.CurrPos] = actor.Character
}
r.Reset()
for _, c := range banner {
r.PutChar(rune(c))
}
r.NextLine()
// Display the level, tiles, actors and paths
for row, tileRow := range level.tiles {
for col, tile := range tileRow {
pos := Position{row: row, col: col}
c := tile.Character
if ac, ok := actorMap[pos]; ok {
c = ac
}
r.PutChar(c)
}
r.NextLine()
}
r.NextLine()
r.Flush()
}
// TermboxRenderer uses the termbox library for rendering the maze.
type TermboxRenderer struct {
row, col int
kbEvents KeyboardEventChannel
}
// NewTermboxRenderer initialized the termbox library and creates a new instance of a renderer.
func NewTermboxRenderer() *TermboxRenderer {
t := TermboxRenderer{}
err := termbox.Init()
if err != nil {
panic(err)
}
t.kbEvents = make(KeyboardEventChannel)
go func() {
for {
ev := termbox.PollEvent()
if ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyEsc:
fallthrough
case termbox.KeyCtrlC:
t.kbEvents <- KBEventCancel
default:
t.kbEvents <- KBEventUnknown
}
}
}
}()
return &t
}
// GetKeyboardEvent returns a channel that can be polled for keyboard events from this renderer.
func (t *TermboxRenderer) GetKeyboardEvent() KeyboardEventChannel {
return t.kbEvents
}
// Done is called to shut down the renderer.
func (t *TermboxRenderer) Done() {
termbox.Close()
}
// Size returns terminal width and height.
func (t *TermboxRenderer) Size() (int, int) {
return termbox.Size()
}
// NextLine advances the current row and resets the column to the start of the row.
func (t *TermboxRenderer) NextLine() {
t.row++
t.col = 0
}
// PutChar puts the character into the current position indicated by row and column and
// advances the column.
func (t *TermboxRenderer) PutChar(c rune) {
termbox.SetCell(t.col, t.row, c, termbox.ColorDefault, termbox.ColorDefault)
t.col++
}
// Reset the terminal so that we start again from the top left corner.
func (t *TermboxRenderer) Reset() {
t.col = 0
t.row = 0
}
// Flush renders the current state of the buffer.
func (t *TermboxRenderer) Flush() {
termbox.Flush()
}
// StreamRenderer renders the maze on an output stream (file, stdout, etc.)
type StreamRenderer struct {
}
// NewStreamRenderer initializes and returns a new StreamRenderer
func NewStreamRenderer() *StreamRenderer {
return &StreamRenderer{}
}
// GetKeyboardEvent returns a channel that can be polled for keyboard events from this renderer.
func (t *StreamRenderer) GetKeyboardEvent() KeyboardEventChannel {
return nil
}
// Done is called to shut down the renderer.
func (t *StreamRenderer) Done() {
}
// Size returns terminal width and height.
func (t *StreamRenderer) Size() (int, int) {
panic("StreamRenderer has no Size()")
}
// NextLine advances the current row and resets the column to the start of the row.
func (t *StreamRenderer) NextLine() {
fmt.Print("\n")
}
// PutChar puts the character into the current position indicated by row and column and
// advances the column.
func (t *StreamRenderer) PutChar(c rune) {
fmt.Print(string(c))
}
// Reset the terminal so that we start again from the top left corner.
func (t *StreamRenderer) Reset() {}
// Flush renders the current state of the buffer.
func (t *StreamRenderer) Flush() {}