internal/ui: clean-up code

A callback is preferred to a dirty flag.

Updates #2341
This commit is contained in:
Hajime Hoshi 2022-10-28 19:07:33 +09:00
parent b019a3723a
commit 066029539e
2 changed files with 18 additions and 6 deletions

View File

@ -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

View File

@ -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.