Skip to content

Commit 89e05ae

Browse files
authored
Allow displaying frame-time in ms in title ($ms instead of $fps) (#140)
1 parent e28ce39 commit 89e05ae

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

rendercanvas/_scheduler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ def on_draw(self):
209209
count, last_time = self._draw_stats
210210
count += 1
211211
if time.perf_counter() - last_time > 1.0:
212-
fps = count / (time.perf_counter() - last_time)
212+
frame_time = (time.perf_counter() - last_time) / count
213213
self._draw_stats = 0, time.perf_counter()
214214
else:
215-
fps = None
215+
frame_time = None
216216
self._draw_stats = count, last_time
217217

218-
# Return fps or None. Will change with better stats at some point
219-
return fps
218+
# Return frame_time or None. Will change with better stats at some point
219+
return frame_time

rendercanvas/base.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class BaseRenderCanvas:
9090
Arguments:
9191
size (tuple): the logical size (width, height) of the canvas.
9292
title (str): The title of the canvas. Can use '$backend' to show the RenderCanvas class name,
93-
and '$fps' to show the fps.
93+
'$fps' to show the fps, and '$ms' to show the frame-time.
9494
update_mode (UpdateMode): The mode for scheduling draws and events. Default 'ondemand'.
9595
min_fps (float): A minimal frames-per-second to use when the ``update_mode`` is 'ondemand'. The default is 0:
9696
max_fps (float): A maximal frames-per-second to use when the ``update_mode`` is 'ondemand'
@@ -147,6 +147,7 @@ def __init__(
147147
self.__title_info = {
148148
"raw": "",
149149
"fps": "?",
150+
"ms": "?",
150151
"backend": self.__class__.__name__,
151152
"loop": self._rc_canvas_group.get_loop().__class__.__name__
152153
if (self._rc_canvas_group and self._rc_canvas_group.get_loop())
@@ -533,12 +534,14 @@ def _draw_frame_and_present(self):
533534

534535
# Notify the scheduler
535536
if self.__scheduler is not None:
536-
fps = self.__scheduler.on_draw()
537+
frame_time = self.__scheduler.on_draw()
537538

538539
# Maybe update title
539-
if fps is not None:
540-
self.__title_info["fps"] = f"{fps:0.1f}"
541-
if "$fps" in self.__title_info["raw"]:
540+
if frame_time is not None:
541+
self.__title_info["fps"] = f"{min(9999, 1 / frame_time):0.1f}"
542+
self.__title_info["ms"] = f"{min(9999, 1000 * frame_time):0.1f}"
543+
raw_title = self.__title_info["raw"]
544+
if "$fps" in raw_title or "$ms" in raw_title:
542545
self.set_title(self.__title_info["raw"])
543546

544547
# Perform the user-defined drawing code. When this errors,
@@ -630,8 +633,12 @@ def set_logical_size(self, width: float, height: float) -> None:
630633
def set_title(self, title: str) -> None:
631634
"""Set the window title.
632635
633-
The words "$backend", "$loop", and "$fps" can be used as variables that
634-
are filled in with the corresponding values.
636+
A few special placeholders are supported:
637+
638+
* "$backend": the name of the backends's RenderCanvas subclass.
639+
* "$loop": the name of the used Loop subclass.
640+
* "$fps": the current frames per second, useful as an indication how smooth the rendering feels.
641+
* "$ms": the time between two rendered frames in milliseconds, useful for benchmarking.
635642
"""
636643
self.__title_info["raw"] = title
637644
for k, v in self.__title_info.items():

0 commit comments

Comments
 (0)