-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreenshot.go
84 lines (64 loc) · 1.79 KB
/
screenshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package fnf
import (
"fmt"
"os"
"path/filepath"
rl "github.com/gen2brain/raylib-go/raylib"
)
var TheScreenshotManager struct {
screenshotCounter int
InputId InputGroupId
}
func InitScreenshot() {
ts := &TheScreenshotManager
ts.InputId = NewInputGroupId()
}
func takeScreenshot() {
ts := &TheScreenshotManager
dirPath, err := RelativePath("./")
if err != nil {
ErrorLogger.Printf("failed to take screenshot: %v", err)
DisplayAlert("failed to take screenshot")
return
}
entries, err := os.ReadDir(dirPath)
if err != nil {
ErrorLogger.Printf("failed to take screenshot: %v", err)
DisplayAlert("failed to take screenshot")
return
}
const fmtStr = "screenshot-%03d.png"
screenshotName := fmt.Sprintf(fmtStr, ts.screenshotCounter)
for _, entry := range entries {
if entry.Name() == screenshotName {
ts.screenshotCounter += 1
screenshotName = fmt.Sprintf(fmtStr, ts.screenshotCounter)
}
}
// actually take screenshot
img := rl.LoadImageFromTexture(TheRenderTexture.Texture)
if !rl.IsImageReady(img) {
ErrorLogger.Printf("failed to take screenshot: failed to load image from render texture")
DisplayAlert("failed to take screenshot")
return
}
defer rl.UnloadImage(img)
rl.ImageFlipVertical(img)
data := rl.ExportImageToMemory(*img, ".png")
fullPath := filepath.Join(dirPath, screenshotName)
err = os.WriteFile(fullPath, data, 0664)
if err != nil {
ErrorLogger.Printf("failed to take screenshot: %v", err)
DisplayAlert("failed to take screenshot")
return
}
FnfLogger.Printf("saved screenshot as %s", fullPath)
DisplayAlert(fmt.Sprintf("saved screenshot as %s", screenshotName))
ts.screenshotCounter += 1
}
func UpdateScreenshot() {
ts := &TheScreenshotManager
if AreKeysPressed(ts.InputId, TheKM[ScreenshotKey]) {
takeScreenshot()
}
}