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

34 lines
871 B
Go
Raw Normal View History

//go:build gl
// 开发测试工具:自动进游戏 + 截图(dev 测试通过后再打包)。
package main
import (
"fmt"
"image"
"image/png"
"os"
"github.com/go-gl/gl/v3.3-core/gl"
)
// saveScreenshot 从默认帧缓冲读取像素并保存为 PNG(渲染线程调用)。
func (a *clientApp) saveScreenshot(path string) error {
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)
}