forked from danrl/pixelgrab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (118 loc) · 3.16 KB
/
main.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
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"log"
"math/rand"
"net"
"sync/atomic"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
const (
width = 1024
height = 640
scale float64 = 1 // Use 0.25 or 0.5
numConns = 1
serverWand = "151.217.24.77:1234"
serverBühne = "151.217.24.77:1337" // 4096x768
)
var showEveryPixel = int(1.0 / scale)
var currentPixel uint64
type myPixel struct {
x, y int
r, g, b uint8
}
func (p myPixel) getX() int {
return p.x / showEveryPixel
}
func (p myPixel) getY() int {
return p.y / showEveryPixel
}
func run() {
// Populate work array which will act as a queue
var queue []myPixel
for x := 0; x < width; x += showEveryPixel {
for y := 0; y < height; y += showEveryPixel {
queue = append(queue, myPixel{x: x, y: y})
}
}
// Randomize so that the image builds up quicker
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(queue), func(i, j int) { queue[i], queue[j] = queue[j], queue[i] })
queueLen := uint64(len(queue)) - 2*numConns
// Create our canvas
win, err := pixelgl.NewWindow(pixelgl.WindowConfig{
Title: "Pixel Grab",
Bounds: pixel.R(0, 0, width*scale, height*scale),
VSync: true,
})
if err != nil {
panic(err)
}
// image data structure to set pixels on
img := image.NewRGBA(image.Rectangle{
image.Point{0, 0},
image.Point{int(width * scale), int(height * scale)},
})
// Use multiple connections for faster buildup
for i := 0; i < numConns; i++ {
go func() { // Do connection in a separate goroutine
for { // Reconnect handling
conn, err := net.Dial("tcp", serverBühne)
if err != nil {
log.Printf("dial: %v", err)
time.Sleep(200 * time.Millisecond) // give me a break :)
continue
}
go func() {
defer conn.Close()
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
line := scanner.Text()
px := myPixel{}
_, err = fmt.Sscanf(line,
"PX %d %d %02x%02x%02x",
&px.x, &px.y,
&px.r, &px.g, &px.b)
if err != nil {
log.Printf("unable to parse `%s`: %v", line, err)
}
// When setting the pixel, we need to scale accordingly.
img.Set(px.getX(), px.getY(), color.RGBA{px.r, px.g, px.b, 255})
}
if err := scanner.Err(); err != nil {
log.Printf("scan error: %v", err)
return
}
}()
for {
readNext := atomic.AddUint64(¤tPixel, 1) - 1 // Increase first and get the previous pixel position
currentPixel = currentPixel % queueLen // Synchronization yolo
cmd := fmt.Sprintf("PX %d %d\n", queue[readNext].x, queue[readNext].y)
_, _ = conn.Write([]byte(cmd))
if err != nil {
log.Printf("write: %v", err)
conn.Close()
break
}
}
}
}()
}
// Render image periodically onto our canvas
for !win.Closed() {
time.Sleep(100 * time.Millisecond) // poor man's frame rate XoXo
// update window with current pixels
pic := pixel.PictureDataFromImage(img)
sprite := pixel.NewSprite(pic, pic.Bounds())
sprite.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
win.Update()
}
}
func main() {
pixelgl.Run(run)
}