zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

imagedumper_desktop.go (4045B)


      1 // Copyright 2018 The Ebiten Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 //go:build !android && !js && !ios
     16 // +build !android,!js,!ios
     17 
     18 package ebiten
     19 
     20 import (
     21 	"fmt"
     22 	"os"
     23 	"time"
     24 
     25 	"github.com/hajimehoshi/ebiten/v2/internal/atlas"
     26 )
     27 
     28 // availableFilename returns a filename that is valid as a new file or directory.
     29 func availableFilename(prefix, postfix string) (string, error) {
     30 	const datetimeFormat = "20060102030405"
     31 
     32 	now := time.Now()
     33 	name := fmt.Sprintf("%s%s%s", prefix, now.Format(datetimeFormat), postfix)
     34 	for i := 1; ; i++ {
     35 		if _, err := os.Stat(name); err != nil {
     36 			if os.IsNotExist(err) {
     37 				break
     38 			}
     39 			if !os.IsNotExist(err) {
     40 				return "", err
     41 			}
     42 		}
     43 		name = fmt.Sprintf("%s%s_%d%s", prefix, now.Format(datetimeFormat), i, postfix)
     44 	}
     45 	return name, nil
     46 }
     47 
     48 func takeScreenshot(screen *Image) error {
     49 	newname, err := availableFilename("screenshot_", ".png")
     50 	if err != nil {
     51 		return err
     52 	}
     53 
     54 	blackbg := !IsScreenTransparent()
     55 	if err := screen.mipmap.DumpScreenshot(newname, blackbg); err != nil {
     56 		return err
     57 	}
     58 
     59 	if _, err := fmt.Fprintf(os.Stderr, "Saved screenshot: %s\n", newname); err != nil {
     60 		return err
     61 	}
     62 	return nil
     63 }
     64 
     65 func dumpInternalImages() error {
     66 	dir, err := availableFilename("internalimages_", "")
     67 	if err != nil {
     68 		return err
     69 	}
     70 
     71 	if err := os.Mkdir(dir, 0755); err != nil {
     72 		return err
     73 	}
     74 
     75 	if err := atlas.DumpImages(dir); err != nil {
     76 		return err
     77 	}
     78 
     79 	if _, err := fmt.Fprintf(os.Stderr, "Dumped the internal images at: %s\n", dir); err != nil {
     80 		return err
     81 	}
     82 	return nil
     83 }
     84 
     85 type imageDumper struct {
     86 	g Game
     87 
     88 	keyState map[Key]int
     89 
     90 	hasScreenshotKey bool
     91 	screenshotKey    Key
     92 	toTakeScreenshot bool
     93 
     94 	hasDumpInternalImagesKey bool
     95 	dumpInternalImagesKey    Key
     96 	toDumpInternalImages     bool
     97 
     98 	err error
     99 }
    100 
    101 func (i *imageDumper) update() error {
    102 	if i.err != nil {
    103 		return i.err
    104 	}
    105 
    106 	const (
    107 		envScreenshotKey     = "EBITEN_SCREENSHOT_KEY"
    108 		envInternalImagesKey = "EBITEN_INTERNAL_IMAGES_KEY"
    109 	)
    110 
    111 	if err := i.g.Update(); err != nil {
    112 		return err
    113 	}
    114 
    115 	// If keyState is nil, all values are not initialized.
    116 	if i.keyState == nil {
    117 		i.keyState = map[Key]int{}
    118 
    119 		if keyname := os.Getenv(envScreenshotKey); keyname != "" {
    120 			if key, ok := keyNameToKeyCode(keyname); ok {
    121 				i.hasScreenshotKey = true
    122 				i.screenshotKey = key
    123 			}
    124 		}
    125 
    126 		if keyname := os.Getenv(envInternalImagesKey); keyname != "" {
    127 			if isDebug() {
    128 				if key, ok := keyNameToKeyCode(keyname); ok {
    129 					i.hasDumpInternalImagesKey = true
    130 					i.dumpInternalImagesKey = key
    131 				}
    132 			} else {
    133 				fmt.Fprintf(os.Stderr, "%s is disabled. Specify a build tag 'ebitendebug' to enable it.\n", envInternalImagesKey)
    134 			}
    135 		}
    136 	}
    137 
    138 	keys := map[Key]struct{}{}
    139 	if i.hasScreenshotKey {
    140 		keys[i.screenshotKey] = struct{}{}
    141 	}
    142 	if i.hasDumpInternalImagesKey {
    143 		keys[i.dumpInternalImagesKey] = struct{}{}
    144 	}
    145 
    146 	for key := range keys {
    147 		if IsKeyPressed(key) {
    148 			i.keyState[key]++
    149 			if i.keyState[key] == 1 {
    150 				if i.hasScreenshotKey && key == i.screenshotKey {
    151 					i.toTakeScreenshot = true
    152 				}
    153 				if i.hasDumpInternalImagesKey && key == i.dumpInternalImagesKey {
    154 					i.toDumpInternalImages = true
    155 				}
    156 			}
    157 		} else {
    158 			i.keyState[key] = 0
    159 		}
    160 	}
    161 	return nil
    162 }
    163 
    164 func (i *imageDumper) dump(screen *Image) error {
    165 	if i.toTakeScreenshot {
    166 		i.toTakeScreenshot = false
    167 		if err := takeScreenshot(screen); err != nil {
    168 			return err
    169 		}
    170 	}
    171 
    172 	if i.toDumpInternalImages {
    173 		i.toDumpInternalImages = false
    174 		if err := dumpInternalImages(); err != nil {
    175 			return err
    176 		}
    177 	}
    178 
    179 	return nil
    180 }