Files
ngzz-mc/cmd/client/devshot_gl.go

46 lines
1.3 KiB
Go
Raw Permalink Normal View History

//go:build gl
// 开发测试工具:自动进游戏 + 截图(dev 测试通过后再打包)。
package main
import (
"fmt"
"image"
"image/png"
"os"
"path/filepath"
"mc/internal/paths"
"github.com/go-gl/gl/v3.3-core/gl"
)
// saveScreenshot 从默认帧缓冲读取像素并保存为 PNG(渲染线程调用)。
// 截图统一存项目根目录 jietu/:裸文件名自动归入该目录,
// 带目录/绝对路径保持原样(打包产物 bin/ 不再混入截图)。
func (a *clientApp) saveScreenshot(path string) error {
if filepath.Base(path) == path { // 无目录成分 → 根目录 jietu/
dir := filepath.Join(paths.RootDir(), "jietu")
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("创建截图目录: %w", err)
}
path = filepath.Join(dir, path)
}
w, h := int32(a.winW), int32(a.winH)
pix := make([]uint8, w*h*4)
gl.ReadPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pix))
img := image.NewRGBA(image.Rect(0, 0, int(w), int(h)))
// 垂直翻转(GL 原点在左下)
for y := int32(0); y < h; y++ {
srcRow := (h - 1 - y) * w * 4
dstRow := y * w * 4
copy(img.Pix[dstRow:dstRow+w*4], pix[srcRow:srcRow+w*4])
}
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("创建截图文件: %w", err)
}
defer f.Close()
return png.Encode(f, img)
}