display average frame creation time along with framerate
This commit is contained in:
parent
0954cd768f
commit
314b722528
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue