fps counter

This commit is contained in:
potassium 2025-01-26 22:04:52 +03:00
parent 5cf764db2d
commit 4bf3b2fc0d
3 changed files with 46 additions and 0 deletions

View File

@ -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)
}
}

5
go.mod
View File

@ -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
)

4
go.sum
View File

@ -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=