summaryrefslogtreecommitdiffstats
path: root/pongo.go
diff options
context:
space:
mode:
Diffstat (limited to 'pongo.go')
-rw-r--r--pongo.go26
1 files changed, 25 insertions, 1 deletions
diff --git a/pongo.go b/pongo.go
index 984bf58..122044d 100644
--- a/pongo.go
+++ b/pongo.go
@@ -196,6 +196,11 @@ func pong() error {
paddle1 := paddle{pos: screenHeight / 2, side: left}
paddle2 := paddle{pos: screenHeight / 2, side: right}
+ fpsTime := time.Duration(0)
+ frames := 0
+ fps := 0
+ showFPS := false
+
lastFrameTime := time.Now()
accumulator := time.Duration(0)
@@ -207,15 +212,21 @@ func pong() error {
case *sdl.QuitEvent:
return nil
case *sdl.KeyboardEvent:
+ if t.Type != sdl.KEYDOWN {
+ continue
+ }
switch t.Keysym.Sym {
case sdl.K_q, sdl.K_c, sdl.K_ESCAPE:
return nil
+ case sdl.K_f:
+ showFPS = !showFPS
}
}
}
- accumulator += frameStart.Sub(lastFrameTime)
+ frameTime := frameStart.Sub(lastFrameTime)
lastFrameTime = frameStart
+ accumulator += frameTime
const simulationSpeed = time.Second / 120
for ; accumulator >= simulationSpeed; accumulator -= simulationSpeed {
@@ -313,6 +324,19 @@ func pong() error {
fmt.Fprintln(os.Stderr, err)
}
+ fpsTime += frameTime
+ frames++
+ if fpsTime >= 5*time.Second {
+ fps = (frames*int(time.Second) + int(fpsTime)/2) / int(fpsTime)
+ fpsTime = 0
+ frames = 0
+ }
+ if showFPS {
+ if err := drawNumber(renderer, fps, 0, 0); err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ }
+ }
+
renderer.Present()
}
panic("unreachable")