Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions rendercanvas/_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ def on_draw(self):
count, last_time = self._draw_stats
count += 1
if time.perf_counter() - last_time > 1.0:
fps = count / (time.perf_counter() - last_time)
frame_time = (time.perf_counter() - last_time) / count
self._draw_stats = 0, time.perf_counter()
else:
fps = None
frame_time = None
self._draw_stats = count, last_time

# Return fps or None. Will change with better stats at some point
return fps
# Return frame_time or None. Will change with better stats at some point
return frame_time
21 changes: 14 additions & 7 deletions rendercanvas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class BaseRenderCanvas:
Arguments:
size (tuple): the logical size (width, height) of the canvas.
title (str): The title of the canvas. Can use '$backend' to show the RenderCanvas class name,
and '$fps' to show the fps.
'$fps' to show the fps, and '$ms' to show the frame-time.
update_mode (UpdateMode): The mode for scheduling draws and events. Default 'ondemand'.
min_fps (float): A minimal frames-per-second to use when the ``update_mode`` is 'ondemand'. The default is 0:
max_fps (float): A maximal frames-per-second to use when the ``update_mode`` is 'ondemand'
Expand Down Expand Up @@ -147,6 +147,7 @@ def __init__(
self.__title_info = {
"raw": "",
"fps": "?",
"ms": "?",
"backend": self.__class__.__name__,
"loop": self._rc_canvas_group.get_loop().__class__.__name__
if (self._rc_canvas_group and self._rc_canvas_group.get_loop())
Expand Down Expand Up @@ -533,12 +534,14 @@ def _draw_frame_and_present(self):

# Notify the scheduler
if self.__scheduler is not None:
fps = self.__scheduler.on_draw()
frame_time = self.__scheduler.on_draw()

# Maybe update title
if fps is not None:
self.__title_info["fps"] = f"{fps:0.1f}"
if "$fps" in self.__title_info["raw"]:
if frame_time is not None:
self.__title_info["fps"] = f"{min(9999, 1 / frame_time):0.1f}"
self.__title_info["ms"] = f"{min(9999, 1000 * frame_time):0.1f}"
raw_title = self.__title_info["raw"]
if "$fps" in raw_title or "$ms" in raw_title:
self.set_title(self.__title_info["raw"])

# Perform the user-defined drawing code. When this errors,
Expand Down Expand Up @@ -630,8 +633,12 @@ def set_logical_size(self, width: float, height: float) -> None:
def set_title(self, title: str) -> None:
"""Set the window title.

The words "$backend", "$loop", and "$fps" can be used as variables that
are filled in with the corresponding values.
A few special placeholders are supported:

* "$backend": the name of the backends's RenderCanvas subclass.
* "$loop": the name of the used Loop subclass.
* "$fps": the current frames per second, useful as an indication how smooth the rendering feels.
* "$ms": the time between two rendered frames in milliseconds, useful for benchmarking.
"""
self.__title_info["raw"] = title
for k, v in self.__title_info.items():
Expand Down