From 066029539e3ab6e88a3585db2b2394a2f04aeac6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 28 Oct 2022 19:07:33 +0900 Subject: [PATCH] internal/ui: clean-up code A callback is preferred to a dirty flag. Updates #2341 --- internal/ui/context.go | 10 ++++++++-- internal/ui/image.go | 14 ++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/ui/context.go b/internal/ui/context.go index 054b2c665..54285a8ff 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -54,6 +54,8 @@ type context struct { outsideWidth float64 outsideHeight float64 + isOffscreenDirty bool + skipCount int } @@ -165,14 +167,15 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics, forceDraw boo c.offscreen.clear() } - c.offscreen.dirty = false + // isOffscreenDirty is updated when an offscreen's drawCallback. + c.isOffscreenDirty = false if err := c.game.DrawOffscreen(); err != nil { return err } const maxSkipCount = 3 - if !forceDraw && !theGlobalState.isScreenClearedEveryFrame() && !c.offscreen.dirty { + if !forceDraw && !theGlobalState.isScreenClearedEveryFrame() && !c.isOffscreenDirty { if c.skipCount < maxSkipCount { c.skipCount++ } @@ -234,6 +237,9 @@ func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFac } if c.offscreen == nil { c.offscreen = c.game.NewOffscreenImage(ow, oh) + c.offscreen.drawCallback = func() { + c.isOffscreenDirty = true + } } return ow, oh diff --git a/internal/ui/image.go b/internal/ui/image.go index 10e10aadf..457d38095 100644 --- a/internal/ui/image.go +++ b/internal/ui/image.go @@ -46,7 +46,9 @@ type Image struct { bigOffscreenBufferBlend graphicsdriver.Blend bigOffscreenBufferDirty bool - dirty bool + // drawCallback is a callback called when DrawTriangles or WritePixels is called. + // drawCallback is useful to detect whether the image is manipulated or not after a certain time. + drawCallback func() } func NewImage(width, height int, imageType atlas.ImageType) *Image { @@ -70,11 +72,13 @@ func (i *Image) MarkDisposed() { i.mipmap.MarkDisposed() i.mipmap = nil i.dotsBuffer = nil - i.dirty = false + i.drawCallback = nil } func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool, canSkipMipmap bool, antialias bool) { - i.dirty = true + if i.drawCallback != nil { + i.drawCallback() + } if antialias { // Flush the other buffer to make the buffers exclusive. @@ -146,7 +150,9 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ } func (i *Image) WritePixels(pix []byte, x, y, width, height int) { - i.dirty = true + if i.drawCallback != nil { + i.drawCallback() + } if width == 1 && height == 1 { // Flush the other buffer to make the buffers exclusive.