From e4f97a918ad91c8748a32176f3f7bb66fe066cf2 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 11 Nov 2022 23:24:20 -0500 Subject: [PATCH] Application must now manually call application.Draw in all cases This behavior is way more consistent, and it makes the drawing code a bit simpler. --- backends/x/x.go | 43 +++++++++++++++--------------------------- examples/hello/main.go | 4 ++-- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/backends/x/x.go b/backends/x/x.go index 78845c7..12513c7 100644 --- a/backends/x/x.go +++ b/backends/x/x.go @@ -53,6 +53,11 @@ type Backend struct { paddingY int descent int } + + memory struct { + windowWidth int + windowHeight int + } } func (backend *Backend) Run (channel chan(stone.Event)) { @@ -86,6 +91,16 @@ func (backend *Backend) Run (channel chan(stone.Event)) { func (backend *Backend) Draw () { backend.drawLock.Lock() defer backend.drawLock.Unlock() + + boundsChanged := + backend.memory.windowWidth != backend.metrics.windowWidth || + backend.memory.windowHeight != backend.metrics.windowHeight + backend.memory.windowWidth = backend.metrics.windowWidth + backend.memory.windowHeight = backend.metrics.windowHeight + + if boundsChanged { + backend.reallocateCanvas() + } backend.drawCells(true) backend.canvas.XDraw() @@ -152,10 +167,6 @@ func (backend *Backend) handleXEvent (event xgb.Event) { if sizeChanged { configureEvent = backend.compressConfigureNotify(configureEvent) - // TODO: remove draw functions from reallocate canvas, - // and only reallocate when drawing and the window - // dimensions have changed - backend.reallocateCanvas() backend.application.SetSize(backend.calculateBufferSize()) backend.channel <- stone.EventResize { } } @@ -214,9 +225,6 @@ func (backend *Backend) calculateBufferSize () (width, height int) { } func (backend *Backend) reallocateCanvas () { - backend.drawLock.Lock() - defer backend.drawLock.Unlock() - if backend.canvas != nil { backend.canvas.Destroy() } @@ -230,28 +238,7 @@ func (backend *Backend) reallocateCanvas () { return backend.colors[stone.ColorApplication] }) - // backend.drawRune(0, 0, 'T') - // backend.drawRune(1, 0, 'h') - // backend.drawRune(2, 0, 'e') - // backend.drawRune(4, 0, 'q') - // backend.drawRune(5, 0, 'u') - // backend.drawRune(6, 0, 'i') - // backend.drawRune(7, 0, 'c') - // backend.drawRune(8, 0, 'k') - // backend.drawRune(0, 1, 'b') - // backend.drawRune(1, 1, 'r') - // backend.drawRune(2, 1, 'o') - // backend.drawRune(3, 1, 'w') - // backend.drawRune(4, 1, 'n') - // backend.drawRune(6, 1, 'f') - // backend.drawRune(7, 1, 'o') - // backend.drawRune(8, 1, 'x') - - backend.drawCells(true) - backend.canvas.XSurfaceSet(backend.window.Id) - backend.canvas.XDraw() - backend.canvas.XPaint(backend.window.Id) } func (backend *Backend) drawCells (forceRedraw bool) (areas []image.Rectangle) { diff --git a/examples/hello/main.go b/examples/hello/main.go index 0c37f84..86444f0 100644 --- a/examples/hello/main.go +++ b/examples/hello/main.go @@ -33,14 +33,12 @@ func main () { if err != nil { panic(err) } redraw() - application.Draw() go tick() for { select { case <- tickPing: redraw() - application.Draw() case event := <- channel: switch event.(type) { @@ -72,6 +70,8 @@ func redraw () { application.SetRune(5, 1, ':') application.SetRune(6, 1, rune(second / 10 + 48)) application.SetRune(7, 1, rune(second % 10 + 48)) + + application.Draw() } func tick () {