doodle/pkg/level/screenshot.go
Noah Petherbridge da83231559 Level Screenshots and Thumbnails
Adds some support for "less giant" level screenshots.

* In the Editor, the Level->Take Screenshot menu will render a cropped screen
  shot of just the level viewport on screen. Note: it is not an SDL2 screen
  copy but generated from scratch from the level data.
* In levels themselves, screenshots can be stored inside the level data in
  three different sizes: large (1280x720), medium and small (each a halved
  size of the previous).
* The first screenshot is created when the level is saved, starting from
  wherever the scroll position in the editor is at, and recording the 720p
  view of the level from there.
* The level screenshot can be previewed and updated in the Level Properties
  window of the editor: so you can scroll the editor to just the right position
  and take a good screenshot to represent your level.
* In the future: these embedded level screenshots will be displayed on the
  Story Mode and other screens to see a preview of each level.

Other tweaks:

* When taking a Giant Screenshot: a confirm modal will warn the player that
  it may take a while. And during the screenshot, show the new Wait Modal to
  block player interaction until the screenshot has finished.
2023-12-08 19:48:02 -08:00

62 lines
1.5 KiB
Go

package level
import (
"bytes"
"image"
"image/png"
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
"git.kirsle.net/go/ui"
)
// Helper functions to get a level's embedded screenshot PNGs as textures.
// HasScreenshot returns whether screenshots exist for the level.
func (lvl *Level) HasScreenshot() bool {
var filenames = []string{
balance.LevelScreenshotLargeFilename,
balance.LevelScreenshotMediumFilename,
balance.LevelScreenshotSmallFilename,
}
for _, filename := range filenames {
if lvl.Files.Exists("assets/screenshots/" + filename) {
return true
}
}
return false
}
// GetScreenshotImage returns a screenshot from the level's data as a Go Image.
// The filename is like "large.png" or "medium.png" and is appended to "assets/screenshots"
func (lvl *Level) GetScreenshotImage(filename string) (image.Image, error) {
data, err := lvl.Files.Get("assets/screenshots/" + filename)
if err != nil {
return nil, err
}
return png.Decode(bytes.NewBuffer(data))
}
// GetScreenshotImageAsUIImage returns a ui.Image texture of a screenshot.
func (lvl *Level) GetScreenshotImageAsUIImage(filename string) (*ui.Image, error) {
// Have it cached recently?
if lvl.cacheImages == nil {
lvl.cacheImages = map[string]*ui.Image{}
} else if img, ok := lvl.cacheImages[filename]; ok {
return img, nil
}
img, err := lvl.GetScreenshotImage(filename)
if err != nil {
return nil, err
}
result, err := ui.ImageFromImage(img)
if err != nil {
return nil, err
}
lvl.cacheImages[filename] = result
return result, nil
}