/
colorDrawer.go
97 lines (79 loc) · 2.04 KB
/
colorDrawer.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
package main
import (
// "fmt"
"github.com/dradtke/go-allegro/allegro"
"github.com/nordsoyv/colorDrawer/config"
"github.com/nordsoyv/colorDrawer/strategy"
)
const FPS int = 60
func main() {
var (
display *allegro.Display
eventQueue *allegro.EventQueue
running bool = true
err error
)
configuration := config.Read("config.json")
strat := strategy.NearestNeighbor(configuration)
if err := allegro.Install(); err != nil {
panic(err)
}
defer allegro.Uninstall()
// Create a 640x480 window and give it a title.
allegro.SetNewDisplayFlags(allegro.WINDOWED)
if display, err = allegro.CreateDisplay(512, 512); err == nil {
defer display.Destroy()
display.SetWindowTitle("colorDrawer")
} else {
panic(err)
}
// Create an event queue. All of the event sources we care about should
// register themselves to this queue.
if eventQueue, err = allegro.CreateEventQueue(); err == nil {
defer eventQueue.Destroy()
} else {
panic(err)
}
// Calculate the timeout value based on the desired FPS.
// timeout := float64(1) / float64(FPS)
// timeout := float64(1)
// Register event sources.
eventQueue.Register(display)
// Set the screen to black.
allegro.ClearToColor(allegro.MapRGB(0, 0, 0))
allegro.FlipDisplay()
doneChan := make(chan bool)
imageUpdateChan := make(chan strategy.ImageUpdate, 100)
go strat.GenerateImage(doneChan, imageUpdateChan)
running = true
// Main loop.
var event allegro.Event
for running {
e, _ := eventQueue.GetNextEvent(&event)
if e != nil {
switch e.(type) {
case allegro.DisplayCloseEvent:
running = false
panic("quit")
break
// Handle other events here.
}
}
if numItems := len(imageUpdateChan); numItems > 0 {
buffer := display.Backbuffer()
allegro.SetTargetBitmap(buffer)
for i := 0; i < numItems; i++ {
imgUp := <-imageUpdateChan
allegro.PutPixel(imgUp.X, imgUp.Y, allegro.MapRGB(imgUp.R, imgUp.G, imgUp.B))
}
allegro.FlipDisplay()
}
select {
case <-doneChan:
running = false
default:
continue
}
}
panic("quit2")
}