Buffer, DamageBuffer, and Application are io.Writers
This commit is contained in:
parent
18ea5681de
commit
4b129a8391
@ -19,6 +19,7 @@ type Backend struct {
|
|||||||
backgroundStamper *imdraw.IMDraw
|
backgroundStamper *imdraw.IMDraw
|
||||||
fontAtlas *text.Atlas
|
fontAtlas *text.Atlas
|
||||||
textDrawer *text.Text
|
textDrawer *text.Text
|
||||||
|
showBounds bool
|
||||||
|
|
||||||
metrics struct {
|
metrics struct {
|
||||||
cellWidth int
|
cellWidth int
|
||||||
@ -176,15 +177,18 @@ func (backend *Backend) draw () {
|
|||||||
backend.textDrawer.Draw(backend.window, pixel.IM)
|
backend.textDrawer.Draw(backend.window, pixel.IM)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backend.backgroundStamper.Clear()
|
// draw a rectangle around the buffer if we are showing bounds
|
||||||
backend.backgroundStamper.Color =
|
if backend.showBounds {
|
||||||
backend.config.Color(stone.ColorBackground)
|
backend.backgroundStamper.Clear()
|
||||||
backend.backgroundStamper.Push (
|
backend.backgroundStamper.Color =
|
||||||
backend.vectorAtPosition(0, 0),
|
backend.config.Color(stone.ColorBackground)
|
||||||
backend.vectorAtPosition(width, height))
|
backend.backgroundStamper.Push (
|
||||||
backend.backgroundStamper.Rectangle(1)
|
backend.vectorAtPosition(0, 0),
|
||||||
backend.backgroundStamper.Draw(backend.window)
|
backend.vectorAtPosition(width, height))
|
||||||
|
backend.backgroundStamper.Rectangle(1)
|
||||||
|
backend.backgroundStamper.Draw(backend.window)
|
||||||
|
}
|
||||||
|
|
||||||
backend.window.SwapBuffers()
|
backend.window.SwapBuffers()
|
||||||
}
|
}
|
||||||
|
41
buffer.go
41
buffer.go
@ -43,6 +43,10 @@ type Buffer struct {
|
|||||||
content []Cell
|
content []Cell
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
|
Dot struct {
|
||||||
|
X int
|
||||||
|
Y int
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *Buffer) Size () (width, height 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
|
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 {
|
type DamageBuffer struct {
|
||||||
Buffer
|
Buffer
|
||||||
clean []bool
|
clean []bool
|
||||||
@ -99,6 +124,22 @@ func (buffer *DamageBuffer) SetRune (x, y int, content rune) {
|
|||||||
buffer.clean[x + y * buffer.width] = false
|
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) {
|
func (buffer *DamageBuffer) Clean (x, y int) (clean bool) {
|
||||||
clean = buffer.clean[x + y * buffer.width]
|
clean = buffer.clean[x + y * buffer.width]
|
||||||
return
|
return
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "time"
|
||||||
import "git.tebibyte.media/sashakoshka/stone"
|
import "git.tebibyte.media/sashakoshka/stone"
|
||||||
import _ "git.tebibyte.media/sashakoshka/stone/backends/pixel"
|
import _ "git.tebibyte.media/sashakoshka/stone/backends/pixel"
|
||||||
|
|
||||||
@ -9,30 +11,31 @@ func main () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run (application *stone.Application) {
|
func run (application *stone.Application) {
|
||||||
|
currentTime := time.Time { }
|
||||||
|
frameDelay := time.Second / 2
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if application.Resized() {
|
if application.Resized() || time.Since(currentTime) > frameDelay {
|
||||||
application.SetRune(0, 0, 'h')
|
currentTime = time.Now()
|
||||||
application.SetRune(1, 0, 'e')
|
|
||||||
application.SetRune(2, 0, 'l')
|
application.ResetDot()
|
||||||
application.SetRune(3, 0, 'l')
|
fmt.Fprintln(application, "hellorld!")
|
||||||
application.SetRune(4, 0, 'o')
|
|
||||||
application.SetRune(5, 0, 'r')
|
hour := currentTime.Hour()
|
||||||
application.SetRune(6, 0, 'l')
|
minute := currentTime.Minute()
|
||||||
application.SetRune(7, 0, 'd')
|
second := currentTime.Second()
|
||||||
application.SetRune(8, 0, '!')
|
|
||||||
application.SetRune(4, 1, ':')
|
application.SetRune(0, 1, rune(hour / 10 + 48))
|
||||||
application.SetRune(5, 1, '3')
|
application.SetRune(1, 1, rune(hour % 10 + 48))
|
||||||
application.SetRune(0, 2, 'A')
|
application.SetRune(2, 1, ':')
|
||||||
application.SetRune(1, 2, 'A')
|
application.SetRune(3, 1, rune(minute / 10 + 48))
|
||||||
application.SetRune(2, 2, 'A')
|
application.SetRune(4, 1, rune(minute % 10 + 48))
|
||||||
application.SetRune(3, 2, 'A')
|
application.SetRune(5, 1, ':')
|
||||||
application.SetRune(4, 2, 'A')
|
application.SetRune(6, 1, rune(second / 10 + 48))
|
||||||
application.SetRune(5, 2, 'A')
|
application.SetRune(7, 1, rune(second % 10 + 48))
|
||||||
application.SetRune(6, 2, 'A')
|
|
||||||
application.SetRune(7, 2, 'A')
|
|
||||||
application.SetRune(8, 2, 'A')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !application.Await(0) { break }
|
if !application.Await(frameDelay) { break }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user