FPS counter in title bar

This commit is contained in:
Luke Meyers 2020-02-08 19:13:02 -08:00
parent bed8e58489
commit f56c28ac67
2 changed files with 20 additions and 17 deletions

View File

@ -5,7 +5,6 @@ import (
"image" "image"
"image/color" "image/color"
_ "image/png" _ "image/png"
"log"
"math" "math"
"math/rand" "math/rand"
"os" "os"
@ -153,23 +152,14 @@ func renderRacer(ctx context, oldRacer, racer game.Racer, active bool, c pixel.R
X: pos.X + w, X: pos.X + w,
Y: pos.Y - w, Y: pos.Y - w,
} }
ur := pixel.Vec{
var ur pixel.Vec X: pos.X + w*float64(racer.Kinetics.V+1),
if ctx.tween == 1 { Y: pos.Y + w,
ur = pixel.Vec{
X: pos.X + w*float64(racer.Kinetics.V+1),
Y: pos.Y + w,
}
log.Printf("ur: %+v", ur)
ur.X = math.Max(ur.X, ll.X)
} else {
ur = pixel.Vec{
X: pos.X + w*float64(racer.Kinetics.V+1),
Y: pos.Y + w,
}
ur.X = math.Min(ur.X, newPos.X+racerWidth)
ur.X = math.Max(ur.X, ll.X)
} }
if ctx.tween < 1 {
ur.X = math.Min(ur.X, newPos.X+racerWidth)
}
ur.X = math.Max(ur.X, ll.X)
im.Push(ll) im.Push(ll)
im.Push(ur) im.Push(ur)

13
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"math/rand" "math/rand"
"relay/game" "relay/game"
@ -38,6 +39,11 @@ func run() error {
sOld := s sOld := s
turn := 1 turn := 1
var (
frames = 0
second = time.Tick(time.Second)
)
for !w.Closed() && !s.GameOver { for !w.Closed() && !s.GameOver {
switch { switch {
case w.Pressed(pixelgl.KeyQ): case w.Pressed(pixelgl.KeyQ):
@ -61,6 +67,13 @@ func run() error {
} }
w.Update() w.Update()
frames++
select {
case <-second:
w.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
default:
}
} }
return nil return nil
} }