From 4bf3b2fc0d6308e862c7c0b96d41b65b5aa91cd6 Mon Sep 17 00:00:00 2001 From: potassium Date: Sun, 26 Jan 2025 22:04:52 +0300 Subject: [PATCH] fps counter --- chatgpt.go | 37 +++++++++++++++++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 4 ++++ 3 files changed, 46 insertions(+) diff --git a/chatgpt.go b/chatgpt.go index edc2ad0..a0ca264 100644 --- a/chatgpt.go +++ b/chatgpt.go @@ -1,15 +1,22 @@ package main import ( + "fmt" "image" "image/color" "image/draw" "math" + "time" "git.niplace.ru/XoxJlopeZi4BB/dev/fb" + "github.com/golang/freetype" + "github.com/golang/freetype/truetype" + "golang.org/x/image/font" + "golang.org/x/image/font/gofont/gomono" ) var Width, Height = fb.Resolution() + const ( cubeSize = 50 Zoom = 1000.0 @@ -118,12 +125,31 @@ func main() { // Palette for the GIF palette := []color.Color{color.Black, color.White} + var ( + fontSize = 96.0 + DPI = 72.0 + spacing = 1.5 + ) + f, err := truetype.Parse(gomono.TTF) + if err != nil { + panic(err) + } + ctx := freetype.NewContext() + ctx.SetDPI(DPI) + ctx.SetFont(f) + ctx.SetFontSize(fontSize) + ctx.SetClip(image.Rect(0, 0, Width, Height)) + ctx.SetSrc(image.NewUniform(palette[1])) + ctx.SetHinting(font.HintingNone) + var text string = "text" + var fps int // Generate frames for i := 0; i < frameCount; i++ { angle := float64(i) * rotationStep img := image.NewPaletted(image.Rect(0, 0, Width, Height), palette) draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.Point{}, draw.Src) + ctx.SetDst(img) // Rotate and project vertices projVertices := make([]Point2D, len(vertices)) @@ -139,9 +165,20 @@ func main() { drawLine(img, p1, p2, color.White) } + text = fmt.Sprint(fps) + pt := freetype.Pt(30, 30+int(ctx.PointToFixed(fontSize)>>6)) + _, err = ctx.DrawString(text, pt) + if err != nil { + panic(err) + } + pt.Y += ctx.PointToFixed(fontSize * spacing) + + t := time.Now() err := fb.Draw(img) if err != nil { panic(err) } + d := time.Since(t) + fps = int(time.Second / d) } } diff --git a/go.mod b/go.mod index ac27739..df747ff 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,8 @@ module git.niplace.ru/XoxJlopeZi4BB/cube go 1.23.4 require git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033838-b2021445b668 + +require ( + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect + golang.org/x/image v0.23.0 // indirect +) diff --git a/go.sum b/go.sum index 1e75d79..9aaac9f 100644 --- a/go.sum +++ b/go.sum @@ -2,3 +2,7 @@ git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033410-5c05476970ae h1:anDr2+Kui git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033410-5c05476970ae/go.mod h1:7YjlidjZZr0+dNjfgYgy5hPfdE3pV1bBT5UcNvz5LjY= git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033838-b2021445b668 h1:Kh9BL+0ia8Lo+lvIsZ3a5pYqzq9ZYwk0+gho7L7BASI= git.niplace.ru/XoxJlopeZi4BB/dev v0.0.0-20250126033838-b2021445b668/go.mod h1:7YjlidjZZr0+dNjfgYgy5hPfdE3pV1bBT5UcNvz5LjY= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68= +golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY=