doodle/pkg/wallpaper/texture.go
Noah Petherbridge db5760ee83 Optimize memory by freeing up SDL2 textures
* Added to the F3 Debug Overlay is a "Texture:" label that counts the number
  of textures currently loaded by the (SDL2) render engine.
* Added Teardown() functions to Level, Doodad and the Chunker they both use
  to free up SDL2 textures for all their cached graphics.
* The Canvas.Destroy() function now cleans up all textures that the Canvas
  is responsible for: calling the Teardown() of the Level or Doodad, calling
  Destroy() on all level actors, and cleaning up Wallpaper textures.
* The Destroy() method of the game's various Scenes will properly Destroy()
  their canvases to clean up when transitioning to another scene. The
  MainScene, MenuScene, EditorScene and PlayScene.
* Fix the sprites package to actually cache the ui.Image widgets. The game
  has very few sprites so no need to free them just yet.

Some tricky places that were leaking textures have been cleaned up:

* Canvas.InstallActors() destroys the canvases of existing actors before it
  reinitializes the list and installs the replacements.
* The DraggableActor when the user is dragging an actor around their level
  cleans up the blueprint masked drag/drop actor before nulling it out.

Misc changes:

* The player character cheats during Play Mode will immediately swap out the
  player character on the current level.
* Properly call the Close() function instead of Hide() to dismiss popup
  windows. The Close() function itself calls Hide() but also triggers
  WindowClose event handlers. The Doodad Dropper subscribes to its close
  event to free textures for all its doodad canvases.
2022-04-09 14:41:24 -07:00

107 lines
2.3 KiB
Go

package wallpaper
// The methods that deal in cached Textures for Doodle.
import (
"errors"
"image"
"git.kirsle.net/apps/doodle/pkg/shmem"
"git.kirsle.net/apps/doodle/pkg/userdir"
"git.kirsle.net/go/render"
)
/*
Free all SDL2 textures from memory.
The Canvas widget will free wallpaper textures in its Destroy method. Note that
if the wallpaper was still somehow in use, the textures will be regenerated the
next time a method like CornerTexture() asks for one.
Returns the number of textures freed (up to 4) or -1 if wallpaper was not ready.
*/
func (wp *Wallpaper) Free() int {
if !wp.ready {
return -1
}
var freed int
if wp.tex.corner != nil {
wp.tex.corner.Free()
wp.tex.corner = nil
freed++
}
if wp.tex.top != nil {
wp.tex.top.Free()
wp.tex.top = nil
freed++
}
if wp.tex.left != nil {
wp.tex.left.Free()
wp.tex.left = nil
freed++
}
if wp.tex.repeat != nil {
wp.tex.repeat.Free()
wp.tex.repeat = nil
freed++
}
return freed
}
// CornerTexture returns the Texture.
func (wp *Wallpaper) CornerTexture(e render.Engine) (render.Texturer, error) {
if !wp.ready {
return nil, errors.New("wallpaper not ready")
}
if wp.tex.corner == nil {
tex, err := texture(e, wp.corner, wp.Name+"c")
wp.tex.corner = tex
return tex, err
}
return wp.tex.corner, nil
}
// TopTexture returns the Texture.
func (wp *Wallpaper) TopTexture(e render.Engine) (render.Texturer, error) {
if wp.tex.top == nil {
tex, err := texture(e, wp.top, wp.Name+"t")
wp.tex.top = tex
return tex, err
}
return wp.tex.top, nil
}
// LeftTexture returns the Texture.
func (wp *Wallpaper) LeftTexture(e render.Engine) (render.Texturer, error) {
if wp.tex.left == nil {
tex, err := texture(e, wp.left, wp.Name+"l")
wp.tex.left = tex
return tex, err
}
return wp.tex.left, nil
}
// RepeatTexture returns the Texture.
func (wp *Wallpaper) RepeatTexture(e render.Engine) (render.Texturer, error) {
if wp.tex.repeat == nil {
tex, err := texture(e, wp.repeat, wp.Name+"x")
wp.tex.repeat = tex
return tex, err
}
return wp.tex.repeat, nil
}
// texture creates or returns a cached texture for a wallpaper.
func texture(e render.Engine, img *image.RGBA, name string) (render.Texturer, error) {
filename := userdir.CacheFilename("wallpaper", name+".bmp")
texture, err := shmem.CurrentRenderEngine.StoreTexture(filename, img)
return texture, err
}