diff --git a/moderngl_window/__init__.py b/moderngl_window/__init__.py index 9e73853..57a23a8 100644 --- a/moderngl_window/__init__.py +++ b/moderngl_window/__init__.py @@ -292,7 +292,7 @@ def run_window_config_instance(config: WindowConfig) -> None: _, duration = timer.stop() window.destroy() if duration > 0: - logger.info("Duration: {0:.2f}s @ {1:.2f} FPS".format(duration, window.frames / duration)) + logger.info("Duration: {0:.2f}s @ {1:.2f} FPS".format(duration, timer.fps_average)) def create_parser() -> argparse.ArgumentParser: diff --git a/moderngl_window/timers/base.py b/moderngl_window/timers/base.py index ca9cb3f..d226ea5 100644 --- a/moderngl_window/timers/base.py +++ b/moderngl_window/timers/base.py @@ -31,6 +31,20 @@ def time(self) -> float: def time(self, value: float) -> None: raise NotImplementedError() + @property + def fps(self) -> float: + """Get or set the current frames per second. + + Returns: + float: The current frames per second + """ + raise NotImplementedError() + + @property + def fps_average(self) -> float: + """The average fps since the timer was started""" + raise NotImplementedError() + def next_frame(self) -> tuple[float, float]: """Get timer information for the next frame. diff --git a/moderngl_window/timers/clock.py b/moderngl_window/timers/clock.py index 80fe7ea..2e02fa8 100644 --- a/moderngl_window/timers/clock.py +++ b/moderngl_window/timers/clock.py @@ -13,6 +13,8 @@ def __init__(self, **kwargs: Any) -> None: self._pause_time: Optional[float] = None self._last_frame = 0.0 self._offset = 0.0 + self._frames = 0 # similar to ticks + self._fps = 0.0 @property def is_paused(self) -> bool: @@ -47,6 +49,22 @@ def time(self, value: float) -> None: self._offset += self.time - value + @property + def fps_average(self) -> float: + """The average fps since the timer was started""" + if self._frames == 0: + return 0.0 + return self._frames / self.time + + @property + def fps(self) -> float: + """Get or set the current frames per second. + + Returns: + float: The current frames per second + """ + return self._fps + def next_frame(self) -> tuple[float, float]: """ Get the time and frametime for the next frame. @@ -55,8 +73,10 @@ def next_frame(self) -> tuple[float, float]: Returns: tuple[float, float]: current time and frametime """ + self._frames += 1 current = self.time delta, self._last_frame = current - self._last_frame, current + self._fps = 1.0 / delta return current, delta def start(self) -> None: