graphics: Refactoring: screenshot dump: remove global variables

This commit is contained in:
Hajime Hoshi 2018-04-28 04:59:31 +09:00
parent 177f4c5108
commit 245dde9bc1

74
run.go
View File

@ -91,53 +91,51 @@ func run(width, height int, scale float64, title string, g *graphicsContext, mai
return nil return nil
} }
var screenshot = false type imageDumper struct {
var screenshotKey Key f func(screen *Image) error
func init() { keyState map[Key]int
keyname := os.Getenv("EBITEN_SCREENSHOT_KEY")
if keyname == "" { hasScreenshotKey bool
return screenshotKey Key
} toTakeScreenshot bool
key, ok := keyNameToKey(keyname)
if !ok {
return
}
screenshot = true
screenshotKey = key
} }
type screenshotTaker struct { func (i *imageDumper) update(screen *Image) error {
f func(screen *Image) error if err := i.f(screen); err != nil {
keyState int
needToTake bool
}
func (s *screenshotTaker) update(screen *Image) error {
if err := s.f(screen); err != nil {
return err return err
} }
if !screenshot {
return nil // If keyState is nil, all values are not initialized.
if i.keyState == nil {
i.keyState = map[Key]int{}
if keyname := os.Getenv("EBITEN_SCREENSHOT_KEY"); keyname != "" {
if key, ok := keyNameToKey(keyname); ok {
i.hasScreenshotKey = true
i.screenshotKey = key
}
}
} }
if IsKeyPressed(screenshotKey) { if i.hasScreenshotKey && IsKeyPressed(i.screenshotKey) {
s.keyState++ i.keyState[i.screenshotKey]++
if s.keyState == 1 { if i.keyState[i.screenshotKey] == 1 {
s.needToTake = true i.toTakeScreenshot = true
} }
} else { } else {
s.keyState = 0 i.keyState[i.screenshotKey] = 0
} }
if s.needToTake && !IsRunningSlowly() {
if i.toTakeScreenshot && !IsRunningSlowly() {
filename := "screenshot.png" filename := "screenshot.png"
i := 0 idx := 0
for { for {
if _, err := os.Stat(filename); os.IsNotExist(err) { if _, err := os.Stat(filename); os.IsNotExist(err) {
break break
} }
i++ idx++
filename = fmt.Sprintf("screenshot%d.png", i) filename = fmt.Sprintf("screenshot%d.png", idx)
} }
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
@ -148,7 +146,7 @@ func (s *screenshotTaker) update(screen *Image) error {
if err := png.Encode(f, screen); err != nil { if err := png.Encode(f, screen); err != nil {
return err return err
} }
s.needToTake = false i.toTakeScreenshot = false
} }
return nil return nil
} }
@ -183,10 +181,7 @@ func (s *screenshotTaker) update(screen *Image) error {
// //
// Don't call Run twice or more in one process. // Don't call Run twice or more in one process.
func Run(f func(*Image) error, width, height int, scale float64, title string) error { func Run(f func(*Image) error, width, height int, scale float64, title string) error {
if screenshot { f = (&imageDumper{f: f}).update
s := &screenshotTaker{f: f}
f = s.update
}
ch := make(chan error) ch := make(chan error)
go func() { go func() {
@ -212,10 +207,7 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
// Ebiten users should NOT call this function. // Ebiten users should NOT call this function.
// Instead, functions in github.com/hajimehoshi/ebiten/mobile package calls this. // Instead, functions in github.com/hajimehoshi/ebiten/mobile package calls this.
func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64, title string) <-chan error { func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64, title string) <-chan error {
if screenshot { f = (&imageDumper{f: f}).update
s := &screenshotTaker{f: f}
f = s.update
}
ch := make(chan error) ch := make(chan error)
go func() { go func() {