fps counter

This commit is contained in:
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)
}
}