From 11cab091dcece6e5ec7583a2af6580236220bbd3 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 5 Nov 2022 18:08:00 -0400 Subject: [PATCH] Individually clear dirty cells --- backends/pixel/pixel.go | 53 +++++++++++++++++++++++++++++++---------- stone.go | 2 -- 2 files changed, 40 insertions(+), 15 deletions(-) delete mode 100644 stone.go diff --git a/backends/pixel/pixel.go b/backends/pixel/pixel.go index 6976f40..9d09c7f 100644 --- a/backends/pixel/pixel.go +++ b/backends/pixel/pixel.go @@ -1,19 +1,22 @@ package pixel +import "fmt" import "time" import "golang.org/x/image/font" import "github.com/faiface/pixel" +import "github.com/faiface/pixel/imdraw" import "github.com/faiface/pixel/pixelgl" import "golang.org/x/image/font/basicfont" import "git.tebibyte.media/sashakoshka/stone" type Backend struct { - window *pixelgl.Window - boundsDirty bool - previousBounds pixel.Vec - fontFace font.Face - application *stone.Application - config *stone.Config + window *pixelgl.Window + boundsDirty bool + previousBounds pixel.Vec + fontFace font.Face + application *stone.Application + config *stone.Config + backgroundStamper *imdraw.IMDraw metrics struct { cellWidth int @@ -37,6 +40,7 @@ func (backend *Backend) Run (callback func (application *stone.Application)) { } pixelgl.Run (func () { + // construct the window, and all that var err error backend.window, err = pixelgl.NewWindow (pixelgl.WindowConfig { Resizable: true, @@ -46,6 +50,7 @@ func (backend *Backend) Run (callback func (application *stone.Application)) { Title: backend.application.Title(), Bounds: backend.calculateWindowSize(), }) + backend.backgroundStamper = imdraw.New(nil) backend.Poll() if err != nil { panic(err.Error()) } @@ -115,6 +120,7 @@ func (backend *Backend) Resized () (resized bool) { func (backend *Backend) draw () { // didDrawing := false + width, height := backend.application.Size() if backend.boundsDirty { backend.window.Clear ( @@ -122,28 +128,42 @@ func (backend *Backend) draw () { backend.boundsDirty = false // didDrawing = true } else { - // TODO: clear out dirty cells before drawing them (we don't - // want to clear them out if we have already just cleared - // everything out) + // clear out dirty cells before drawing them. this is in an else + // block because if the bounds were dirty, we have already + // cleared the entire screen. - + backend.backgroundStamper.Clear() + backend.backgroundStamper.Color = + backend.config.Color(stone.ColorApplication) + for x := 0; x < width; x ++ { + for y := 0; y < height; y ++ { + clean := backend.application.Clean(x, y) + if clean { continue } + + backend.backgroundStamper.Push ( + backend.vectorAtPosition(x, y), + backend.vectorAtPosition(x + 1, y + 1)) + backend.backgroundStamper.Rectangle(1) + // didDrawing = true + } + } + backend.backgroundStamper.Draw(backend.window) } // TODO: draw dirty cells. - width, height := backend.application.Size() for x := 0; x < width; x ++ { for y := 0; y < height; y ++ { clean := backend.application.Clean(x, y) // cell := application.content[index] if clean { continue } + backend.application.MarkClean(x, y) // draw cell cell := backend.application.Cell(x, y) content := cell.Rune() if content < 32 { continue } + fmt.Println(content) - // didDrawing = true // TODO: set didDrawing up there ^ - backend.application.MarkClean(x, y) } } @@ -160,6 +180,13 @@ func (backend *Backend) processEvents () { } } +func (backend *Backend) vectorAtPosition (x, y int) (vector pixel.Vec) { + vector = pixel.V ( + float64(x * backend.metrics.cellWidth), + float64(y * backend.metrics.cellHeight)) + return +} + func (backend *Backend) calculateWindowSize () (bounds pixel.Rect) { width, height := backend.application.Size() bounds = pixel.R ( diff --git a/stone.go b/stone.go deleted file mode 100644 index 7fa45eb..0000000 --- a/stone.go +++ /dev/null @@ -1,2 +0,0 @@ -package stone -