Skip to content

Commit

Permalink
Timer.fps and Timer.fps_average
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Nov 30, 2024
1 parent 980daeb commit 04fe9a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion moderngl_window/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions moderngl_window/timers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions moderngl_window/timers/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit 04fe9a3

Please sign in to comment.