-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.go
52 lines (47 loc) · 1.03 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
package antwar
import (
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/xgraphics"
"github.com/BurntSushi/xgbutil/xwindow"
"image"
"sync"
"time"
)
type GUI struct {
board *Board
mutex sync.Mutex
canvas *xgraphics.Image
queue map[Pos]bool
win *xwindow.Window
}
func (gui *GUI) Update(pos Pos) {
gui.mutex.Lock()
gui.queue[pos] = true
gui.mutex.Unlock()
}
func (gui *GUI) StartLoop() {
go func() {
for {
gui.mutex.Lock()
for pos, _ := range gui.queue {
gui.canvas.Set(pos.X, pos.Y, gui.board.At(pos).color())
delete(gui.queue, pos)
}
gui.mutex.Unlock()
gui.canvas.XDraw()
gui.canvas.XPaint(gui.win.Id)
time.Sleep(20000000)
}
}()
}
func NewGUI(board *Board) *GUI {
gui := new(GUI)
X, _ := xgbutil.NewConn()
gui.canvas = xgraphics.New(X, image.Rect(0, 0, board.Width(), board.Height()))
gui.queue = make(map[Pos]bool, board.Width()*board.Height())
gui.win = gui.canvas.XShow()
gui.board = board
board.onUpdate = func(p Pos) { gui.Update(p) }
gui.StartLoop()
return gui
}