Added bounds checking to buffer
This commit is contained in:
parent
ff7240d553
commit
f98b551ba2
27
buffer.go
27
buffer.go
@ -49,6 +49,15 @@ type Buffer struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (buffer *Buffer) isOutOfBounds (x, y int) (outOfBounds bool) {
|
||||||
|
outOfBounds =
|
||||||
|
x < 0 ||
|
||||||
|
y < 0 ||
|
||||||
|
x >= buffer.width ||
|
||||||
|
y >= buffer.height
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (buffer *Buffer) Size () (width, height int) {
|
func (buffer *Buffer) Size () (width, height int) {
|
||||||
width = buffer.width
|
width = buffer.width
|
||||||
height = buffer.height
|
height = buffer.height
|
||||||
@ -62,19 +71,23 @@ func (buffer *Buffer) SetSize (width, height int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *Buffer) Cell (x, y int) (cell Cell) {
|
func (buffer *Buffer) Cell (x, y int) (cell Cell) {
|
||||||
|
if buffer.isOutOfBounds(x, y) { return }
|
||||||
cell = buffer.content[x + y * buffer.width]
|
cell = buffer.content[x + y * buffer.width]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *Buffer) SetColor (x, y int, color Color) {
|
func (buffer *Buffer) SetColor (x, y int, color Color) {
|
||||||
|
if buffer.isOutOfBounds(x, y) { return }
|
||||||
buffer.content[x + y * buffer.width].color = color
|
buffer.content[x + y * buffer.width].color = color
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *Buffer) SetStyle (x, y int, style Style) {
|
func (buffer *Buffer) SetStyle (x, y int, style Style) {
|
||||||
|
if buffer.isOutOfBounds(x, y) { return }
|
||||||
buffer.content[x + y * buffer.width].style = style
|
buffer.content[x + y * buffer.width].style = style
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *Buffer) SetRune (x, y int, content rune) {
|
func (buffer *Buffer) SetRune (x, y int, content rune) {
|
||||||
|
if buffer.isOutOfBounds(x, y) { return }
|
||||||
buffer.content[x + y * buffer.width].content = content
|
buffer.content[x + y * buffer.width].content = content
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,18 +123,24 @@ func (buffer *DamageBuffer) SetSize (width, height int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *DamageBuffer) SetColor (x, y int, color Color) {
|
func (buffer *DamageBuffer) SetColor (x, y int, color Color) {
|
||||||
|
if buffer.isOutOfBounds(x, y) { return }
|
||||||
|
index := x + y * buffer.width
|
||||||
|
buffer.clean[index] = buffer.content[index].color == color
|
||||||
buffer.Buffer.SetColor(x, y, color)
|
buffer.Buffer.SetColor(x, y, color)
|
||||||
buffer.clean[x + y * buffer.width] = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *DamageBuffer) SetStyle (x, y int, style Style) {
|
func (buffer *DamageBuffer) SetStyle (x, y int, style Style) {
|
||||||
|
if buffer.isOutOfBounds(x, y) { return }
|
||||||
|
index := x + y * buffer.width
|
||||||
|
buffer.clean[index] = buffer.content[index].style == style
|
||||||
buffer.Buffer.SetStyle(x, y, style)
|
buffer.Buffer.SetStyle(x, y, style)
|
||||||
buffer.clean[x + y * buffer.width] = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *DamageBuffer) SetRune (x, y int, content rune) {
|
func (buffer *DamageBuffer) SetRune (x, y int, content rune) {
|
||||||
|
if buffer.isOutOfBounds(x, y) { return }
|
||||||
|
index := x + y * buffer.width
|
||||||
|
buffer.clean[index] = buffer.content[index].content == content
|
||||||
buffer.Buffer.SetRune(x, y, content)
|
buffer.Buffer.SetRune(x, y, content)
|
||||||
buffer.clean[x + y * buffer.width] = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
|
func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
|
||||||
@ -141,10 +160,12 @@ func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *DamageBuffer) Clean (x, y int) (clean bool) {
|
func (buffer *DamageBuffer) Clean (x, y int) (clean bool) {
|
||||||
|
if buffer.isOutOfBounds(x, y) { return }
|
||||||
clean = buffer.clean[x + y * buffer.width]
|
clean = buffer.clean[x + y * buffer.width]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *DamageBuffer) MarkClean (x, y int) {
|
func (buffer *DamageBuffer) MarkClean (x, y int) {
|
||||||
|
if buffer.isOutOfBounds(x, y) { return }
|
||||||
buffer.clean[x + y * buffer.width] = true
|
buffer.clean[x + y * buffer.width] = true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user