-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.go
More file actions
92 lines (64 loc) · 1.89 KB
/
buffer.go
File metadata and controls
92 lines (64 loc) · 1.89 KB
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
package ledgend
import (
"errors"
)
type Buffer struct {
length uint16
pixels []Color
animation_queue []Animation
}
type Change struct {
Index uint16
Pixel Color
}
// GenBuffer generates an empty buffer
func GenBuffer(length uint16) (Buffer) {
b := Buffer{}
b.length = length
b.pixels = make([]Color, length)
return b
}
// GetPixels copies and returns pixels data from Buffer
func (b *Buffer) GetPixels() ([]Color) {
copied_pixels := make([]Color, len(b.pixels))
copy(copied_pixels, b.pixels)
return copied_pixels
}
// XORPixels returns Changes between Color slices
func XORPixels(p1 []Color, p2 []Color) ([]Change, error) {
if ( len(p1) != len(p2) ) {
return []Change{}, errors.New("Pixel slices must be the same length!")
}
var changes []Change
for index, pixel := range p2 {
if ( p1[index] != pixel ) {
changes = append(changes, Change{uint16(index), pixel})
}
}
return changes, nil
}
// GetAnimationQueue returns Buffer's animation queue
func (b *Buffer) GetAnimationQueue() ([]Animation) {
return b.animation_queue
}
// ApplyQueue applies every animation from Buffer's animation_queue in order
//
// If the animation is finished, it removes it from the animation_queue
func (b *Buffer) ApplyQueue() {
var updated_queue []Animation
for _, animation := range b.animation_queue {
done := b.applyAnimation(animation)
if ( !done ) {
updated_queue = append(updated_queue, animation)
}
}
b.animation_queue = updated_queue
}
// ClearQueue empties the buffer's animation_queue
func (b *Buffer) ClearQueue() {
b.animation_queue = []Animation{}
}
// AddAnimation adds an Animation to Buffer's animation queue
func (b *Buffer) AddAnimation(a ...Animation) {
b.animation_queue = append(b.animation_queue, a...)
}