From 2f5a58367427289e255a1ff74183aa2f785b9766 Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 7 Sep 2016 13:02:07 +0200 Subject: [PATCH] Add 'draw_fps' setting and support --- tabakrolletjie/constants.py | 8 ++++++++ tabakrolletjie/engine.py | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tabakrolletjie/constants.py b/tabakrolletjie/constants.py index 92a7c6f..8edc2e9 100644 --- a/tabakrolletjie/constants.py +++ b/tabakrolletjie/constants.py @@ -7,6 +7,9 @@ TITLE = "Space Turnips" # Debug DEBUG = os.environ.get("TABAK_DEBUG", "").lower() in ("1", "y", "yes") +DRAW_FPS = os.environ.get("TABAK_DRAW_FPS", "").lower() in ("1", "y", "yes") +# Intervals to average fps over +FPS_FRAMES = 50 # 704 is 768 minus space for window decorations :) SCREEN_SIZE = (1024, 704) @@ -19,3 +22,8 @@ OBSTACLE_CATEGORY = 1 << 0 LIGHT_CATEGORY = 1 << 1 MOULD_CATEGORY = 1 << 2 FITTINGS_CATEGORY = 1 << 3 + +# Font definitions +FONTS = { + 'sans': 'DejaVuSans.ttf', +} diff --git a/tabakrolletjie/engine.py b/tabakrolletjie/engine.py index 4dac1dd..cdea160 100644 --- a/tabakrolletjie/engine.py +++ b/tabakrolletjie/engine.py @@ -7,7 +7,8 @@ import pygame.time import pygame.locals as pgl -from .constants import FPS +from .constants import FPS, DRAW_FPS, FONTS, FPS_FRAMES +from .loader import loader from .events import QuitEvent, SceneChangeEvent @@ -26,6 +27,9 @@ class Engine(object): def run(self): clock = pygame.time.Clock() + fpses = [FPS] * FPS_FRAMES + frame = 0 + font = loader.load_font(FONTS['sans'], size=16) while True: events = pygame.event.get() @@ -42,5 +46,12 @@ class Engine(object): # Time is assumed to flow perfectly, so no dt parameter for now self._scene.tick(self._gamestate) self._scene.render(self._screen, self._gamestate) + if DRAW_FPS: + fps = sum(fpses) / FPS_FRAMES + text = font.render("FPS: %.2f" % fps, True, (255, 255, 255)) + self._screen.blit(text, (10, 10), None) pygame.display.flip() - clock.tick(FPS) + fpses[frame] = 1000.0 / clock.tick(FPS) + frame += 1 + if frame >= FPS_FRAMES: + frame = 0 -- 2.34.1