Buffer, DamageBuffer, and Application are io.Writers

This commit is contained in:
Sasha Koshka 2022-11-06 14:17:43 -05:00
parent 18ea5681de
commit 4b129a8391
3 changed files with 79 additions and 31 deletions

View File

@ -19,6 +19,7 @@ type Backend struct {
backgroundStamper *imdraw.IMDraw
fontAtlas *text.Atlas
textDrawer *text.Text
showBounds bool
metrics struct {
cellWidth int
@ -176,15 +177,18 @@ func (backend *Backend) draw () {
backend.textDrawer.Draw(backend.window, pixel.IM)
}
}
backend.backgroundStamper.Clear()
backend.backgroundStamper.Color =
backend.config.Color(stone.ColorBackground)
backend.backgroundStamper.Push (
backend.vectorAtPosition(0, 0),
backend.vectorAtPosition(width, height))
backend.backgroundStamper.Rectangle(1)
backend.backgroundStamper.Draw(backend.window)
// draw a rectangle around the buffer if we are showing bounds
if backend.showBounds {
backend.backgroundStamper.Clear()
backend.backgroundStamper.Color =
backend.config.Color(stone.ColorBackground)
backend.backgroundStamper.Push (
backend.vectorAtPosition(0, 0),
backend.vectorAtPosition(width, height))
backend.backgroundStamper.Rectangle(1)
backend.backgroundStamper.Draw(backend.window)
}
backend.window.SwapBuffers()
}

View File

@ -43,6 +43,10 @@ type Buffer struct {
content []Cell
width int
height int
Dot struct {
X int
Y int
}
}
func (buffer *Buffer) Size () (width, height int) {
@ -74,6 +78,27 @@ func (buffer *Buffer) SetRune (x, y int, content rune) {
buffer.content[x + y * buffer.width].content = content
}
func (buffer *Buffer) Write (bytes []byte) (bytesWritten int, err error) {
text := string(bytes)
bytesWritten = len(bytes)
for _, character := range text {
buffer.SetRune(buffer.Dot.X, buffer.Dot.Y, character)
buffer.Dot.X ++
if buffer.Dot.X > buffer.width {
buffer.Dot.X = 0
buffer.Dot.Y --
}
}
return
}
func (buffer *Buffer) ResetDot () {
buffer.Dot.X = 0
buffer.Dot.Y = 0
}
type DamageBuffer struct {
Buffer
clean []bool
@ -99,6 +124,22 @@ func (buffer *DamageBuffer) SetRune (x, y int, content rune) {
buffer.clean[x + y * buffer.width] = false
}
func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
text := string(bytes)
bytesWritten = len(bytes)
for _, character := range text {
buffer.SetRune(buffer.Dot.X, buffer.Dot.Y, character)
buffer.Dot.X ++
if buffer.Dot.X > buffer.width {
buffer.Dot.X = 0
buffer.Dot.Y --
}
}
return
}
func (buffer *DamageBuffer) Clean (x, y int) (clean bool) {
clean = buffer.clean[x + y * buffer.width]
return

View File

@ -1,5 +1,7 @@
package main
import "fmt"
import "time"
import "git.tebibyte.media/sashakoshka/stone"
import _ "git.tebibyte.media/sashakoshka/stone/backends/pixel"
@ -9,30 +11,31 @@ func main () {
}
func run (application *stone.Application) {
currentTime := time.Time { }
frameDelay := time.Second / 2
for {
if application.Resized() {
application.SetRune(0, 0, 'h')
application.SetRune(1, 0, 'e')
application.SetRune(2, 0, 'l')
application.SetRune(3, 0, 'l')
application.SetRune(4, 0, 'o')
application.SetRune(5, 0, 'r')
application.SetRune(6, 0, 'l')
application.SetRune(7, 0, 'd')
application.SetRune(8, 0, '!')
application.SetRune(4, 1, ':')
application.SetRune(5, 1, '3')
application.SetRune(0, 2, 'A')
application.SetRune(1, 2, 'A')
application.SetRune(2, 2, 'A')
application.SetRune(3, 2, 'A')
application.SetRune(4, 2, 'A')
application.SetRune(5, 2, 'A')
application.SetRune(6, 2, 'A')
application.SetRune(7, 2, 'A')
application.SetRune(8, 2, 'A')
if application.Resized() || time.Since(currentTime) > frameDelay {
currentTime = time.Now()
application.ResetDot()
fmt.Fprintln(application, "hellorld!")
hour := currentTime.Hour()
minute := currentTime.Minute()
second := currentTime.Second()
application.SetRune(0, 1, rune(hour / 10 + 48))
application.SetRune(1, 1, rune(hour % 10 + 48))
application.SetRune(2, 1, ':')
application.SetRune(3, 1, rune(minute / 10 + 48))
application.SetRune(4, 1, rune(minute % 10 + 48))
application.SetRune(5, 1, ':')
application.SetRune(6, 1, rune(second / 10 + 48))
application.SetRune(7, 1, rune(second % 10 + 48))
}
if !application.Await(0) { break }
if !application.Await(frameDelay) { break }
}
}