Skip to content

Commit

Permalink
added screenshot capture
Browse files Browse the repository at this point in the history
  • Loading branch information
imprity committed Aug 23, 2024
1 parent 58ec183 commit ae98bb4
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func RunApplication() {
InitUnitext()
InitAlert()
InitTransition()
InitScreenshot()
defer FreeTransition()
InitPopupDialog()
defer FreePopupDialog()
Expand Down Expand Up @@ -300,6 +301,7 @@ func RunApplication() {
UpdateTransition()
UpdateMenuManager(updateDelta)
UpdateAlert(updateDelta)
UpdateScreenshot()

//update screen
if !TheTransitionManager.ShowTransition {
Expand Down
15 changes: 8 additions & 7 deletions fnfbinding_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion game_screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2965,7 +2965,7 @@ func (gs *GameScreen) BeforeScreenEnd() {
}

if err := SaveSettings(); err != nil {
ErrorLogger.Printf("failed to save settings %v", err)
ErrorLogger.Printf("failed to save settings: %v", err)
DisplayAlert("failed to save settings")
}

Expand Down
6 changes: 6 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
ZoomOutKey
ZoomInKey

ScreenshotKey

ToggleDebugMsg
ToggleLogNoteEvent
ToggleDebugGraphics
Expand Down Expand Up @@ -93,6 +95,8 @@ func init() {
DefaultKM[ZoomOutKey] = rl.KeyLeftBracket
DefaultKM[ZoomInKey] = rl.KeyRightBracket

DefaultKM[ScreenshotKey] = rl.KeyF12

DefaultKM[ToggleDebugMsg] = rl.KeyF1
DefaultKM[ToggleLogNoteEvent] = rl.KeyF2
DefaultKM[ToggleDebugGraphics] = rl.KeyF3
Expand Down Expand Up @@ -141,6 +145,8 @@ func init() {
KeyHumanName[ZoomOutKey] = "note spacing up"
KeyHumanName[ZoomInKey] = "note spacing down"

KeyHumanName[ScreenshotKey] = "screenshot"

KeyHumanName[ToggleDebugMsg] = "toggle debug message"
KeyHumanName[ToggleLogNoteEvent] = "toggle note event"
KeyHumanName[ToggleDebugGraphics] = "toggle debug graphics"
Expand Down
8 changes: 7 additions & 1 deletion options_screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,13 @@ func NewOptionsControlsScreen() *BaseOptionsScreen {

// add extra bottom margin
switch key {
case SongResetKey, NoteScrollDownKey, AudioSpeedDownKey, AudioOffsetDownKey, JumpToBookMarkKey:
case SongResetKey,
NoteScrollDownKey,
AudioSpeedDownKey,
AudioOffsetDownKey,
JumpToBookMarkKey,
ZoomInKey:

item.BottomMargin += extraBottomMargin
}

Expand Down
84 changes: 84 additions & 0 deletions screenshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
}
}

0 comments on commit ae98bb4

Please sign in to comment.