From 314b722528a65e6f0053f8be1844c2d8818319f4 Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 30 Nov 2022 22:31:36 -0500 Subject: [PATCH] display average frame creation time along with framerate --- pgfw/Mainloop.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/pgfw/Mainloop.py b/pgfw/Mainloop.py index f21cc24..3cd5784 100644 --- a/pgfw/Mainloop.py +++ b/pgfw/Mainloop.py @@ -5,6 +5,13 @@ from pygame.time import get_ticks, wait from .GameChild import GameChild class Mainloop(GameChild): + """ + Maintain and display framerate. Call the parent's (the main game object) frame method once per frame. If frame skipping is enabled and the frame rate + is below the requested framerate, the frame method will be called multiple times to try to keep pace (although this rarely makes sense, since calling + the frame method multiple times would most likely cause further slow down). It is missing some important game loop features. For example, game logic + is not separated from draw logic, and frame rate is expected to remain constant based on the definition in the config file. The time of each frame is + tracked by the TimeFilter class. + """ def __init__(self, parent): GameChild.__init__(self, parent) @@ -13,6 +20,7 @@ class Mainloop(GameChild): self.actual_frame_duration = 0 self.frames_this_second = 0 self.last_framerate_display = 0 + self.frame_times = [] self.load_configuration() self.init_framerate_display() self.last_ticks = get_ticks() @@ -40,13 +48,12 @@ class Mainloop(GameChild): self.render_framerate() def framerate_display_active(self): - return self.check_command_line(self.framerate_display_flag) or \ - self.show_framerate + return self.check_command_line(self.framerate_display_flag) or self.show_framerate def render_framerate(self): - text = self.font.render(str(self.last_framerate_count), False, - self.framerate_text_color, - self.framerate_text_background) + average_frame_time = sum(self.frame_times) / len(self.frame_times) if self.frame_times else 0 + self.frame_times = [] + text = self.font.render(f"{self.last_framerate_count} ({average_frame_time: .1f})", False, self.framerate_text_color, self.framerate_text_background) rect = text.get_rect() rect.topright = self.framerate_topright self.framerate_text = text @@ -76,6 +83,7 @@ class Mainloop(GameChild): last_ticks = self.last_ticks actual_frame_duration = get_ticks() - last_ticks last_ticks = get_ticks() + self.frame_times.append(actual_frame_duration) while actual_frame_duration < self.target_frame_duration: wait(self.wait_duration) actual_frame_duration += get_ticks() - last_ticks @@ -96,9 +104,8 @@ class Mainloop(GameChild): def update_framerate(self): count = self.frames_this_second + 1 if get_ticks() - self.last_framerate_display > 1000: - if count != self.last_framerate_count: - self.last_framerate_count = count - self.render_framerate() + self.last_framerate_count = count + self.render_framerate() self.last_framerate_display = get_ticks() count = 0 self.display_surface.blit(self.framerate_text, self.framerate_text_rect)