imagedumper.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. package ebiten
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "runtime"
  20. "time"
  21. "github.com/hajimehoshi/ebiten/v2/internal/debug"
  22. "github.com/hajimehoshi/ebiten/v2/internal/ui"
  23. )
  24. func datetimeForFilename() string {
  25. const datetimeFormat = "20060102150405"
  26. now := time.Now()
  27. return now.Format(datetimeFormat)
  28. }
  29. func takeScreenshot(screen *Image, transparent bool) error {
  30. name := "screenshot_" + datetimeForFilename() + ".png"
  31. // Use the home directory for mobiles as a provisional implementation.
  32. if runtime.GOOS == "android" || runtime.GOOS == "ios" {
  33. home, err := os.UserHomeDir()
  34. if err != nil {
  35. return err
  36. }
  37. name = filepath.Join(home, name)
  38. }
  39. dumpedName, err := screen.image.DumpScreenshot(name, !transparent)
  40. if err != nil {
  41. return err
  42. }
  43. if _, err := fmt.Fprintf(os.Stderr, "Saved screenshot: %s\n", dumpedName); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. func dumpInternalImages() error {
  49. dumpedDir, err := ui.Get().DumpImages("internalimages_" + datetimeForFilename())
  50. if err != nil {
  51. return err
  52. }
  53. // Use the home directory for mobiles as a provisional implementation.
  54. if runtime.GOOS == "android" || runtime.GOOS == "ios" {
  55. home, err := os.UserHomeDir()
  56. if err != nil {
  57. return err
  58. }
  59. dumpedDir = filepath.Join(home, dumpedDir)
  60. }
  61. if _, err := fmt.Fprintf(os.Stderr, "Dumped the internal images at: %s\n", dumpedDir); err != nil {
  62. return err
  63. }
  64. return nil
  65. }
  66. type imageDumper struct {
  67. keyState map[Key]int
  68. hasScreenshotKey bool
  69. screenshotKey Key
  70. toTakeScreenshot bool
  71. hasDumpInternalImagesKey bool
  72. dumpInternalImagesKey Key
  73. toDumpInternalImages bool
  74. err error
  75. }
  76. func envScreenshotKey() string {
  77. if env := os.Getenv("EBITENGINE_SCREENSHOT_KEY"); env != "" {
  78. return env
  79. }
  80. // For backward compatibility, read the EBITEN_ version.
  81. return os.Getenv("EBITEN_SCREENSHOT_KEY")
  82. }
  83. func envInternalImagesKey() string {
  84. if env := os.Getenv("EBITENGINE_INTERNAL_IMAGES_KEY"); env != "" {
  85. return env
  86. }
  87. // For backward compatibility, read the EBITEN_ version.
  88. return os.Getenv("EBITEN_INTERNAL_IMAGES_KEY")
  89. }
  90. func (i *imageDumper) update() error {
  91. if i.err != nil {
  92. return i.err
  93. }
  94. // If keyState is nil, all values are not initialized.
  95. if i.keyState == nil {
  96. i.keyState = map[Key]int{}
  97. if keyname := envScreenshotKey(); keyname != "" {
  98. if key, ok := keyNameToKeyCode(keyname); ok {
  99. i.hasScreenshotKey = true
  100. i.screenshotKey = key
  101. }
  102. }
  103. if keyname := envInternalImagesKey(); keyname != "" {
  104. if debug.IsDebug {
  105. if key, ok := keyNameToKeyCode(keyname); ok {
  106. i.hasDumpInternalImagesKey = true
  107. i.dumpInternalImagesKey = key
  108. }
  109. } else {
  110. fmt.Fprintf(os.Stderr, "EBITENGINE_INTERNAL_IMAGES_KEY is disabled. Specify a build tag 'ebitenginedebug' to enable it.\n")
  111. }
  112. }
  113. }
  114. keys := map[Key]struct{}{}
  115. if i.hasScreenshotKey {
  116. keys[i.screenshotKey] = struct{}{}
  117. }
  118. if i.hasDumpInternalImagesKey {
  119. keys[i.dumpInternalImagesKey] = struct{}{}
  120. }
  121. for key := range keys {
  122. if IsKeyPressed(key) {
  123. i.keyState[key]++
  124. if i.keyState[key] == 1 {
  125. if i.hasScreenshotKey && key == i.screenshotKey {
  126. i.toTakeScreenshot = true
  127. }
  128. if i.hasDumpInternalImagesKey && key == i.dumpInternalImagesKey {
  129. i.toDumpInternalImages = true
  130. }
  131. }
  132. } else {
  133. i.keyState[key] = 0
  134. }
  135. }
  136. return nil
  137. }
  138. func (i *imageDumper) dump(screen *Image, transparent bool) error {
  139. if i.toTakeScreenshot {
  140. i.toTakeScreenshot = false
  141. if err := takeScreenshot(screen, transparent); err != nil {
  142. return err
  143. }
  144. }
  145. if i.toDumpInternalImages {
  146. i.toDumpInternalImages = false
  147. if err := dumpInternalImages(); err != nil {
  148. return err
  149. }
  150. }
  151. return nil
  152. }