forked from tomnz/scroll-phat-hd-go
/
buffer.go
46 lines (42 loc) · 1.12 KB
/
buffer.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
package scrollphathd
// resetBuffer will clear and recreate the buffer with the device's width and height.
func (d *Display) resetBuffer() {
d.width = d.device.Width()
d.height = d.device.Height()
d.buffer = make([][]byte, d.height)
for y := range d.buffer {
d.buffer[y] = make([]byte, d.width)
}
}
// growBuffer will optionally grow the internal buffer as necessary to be able to capture
// the given x, y coordinate.
func (d *Display) growBuffer(newX, newY int) {
if newX < 0 || newY < 0 {
panic("coordinates must be 0 or greater")
}
if newX < d.width && newY < d.height {
// Coords already within buffer
return
}
newWidth, newHeight := d.width, d.height
if newX >= newWidth {
newWidth = newX + 1
}
if newY >= newHeight {
newHeight = newY + 1
}
if newWidth > d.width {
for y, row := range d.buffer {
// Expand each row as needed
newRow := make([]byte, newWidth)
copy(newRow, row)
d.buffer[y] = newRow
}
}
for y := d.height - 1; y < newHeight; y++ {
// Append new rows as needed, of width x coord
d.buffer = append(d.buffer, make([]byte, newWidth))
}
d.width = newWidth
d.height = newHeight
}