framerate display
This commit is contained in:
parent
2e4fd0b3df
commit
fa697af573
|
@ -15,7 +15,7 @@ class Audio(GameChild):
|
|||
def __init__(self, game):
|
||||
GameChild.__init__(self, game)
|
||||
self.load_fx()
|
||||
self.subscribe(self.mute)
|
||||
self.subscribe(self.respond)
|
||||
|
||||
def load_fx(self):
|
||||
fx = {}
|
||||
|
@ -26,10 +26,13 @@ class Audio(GameChild):
|
|||
fx[name.split(".")[0]] = Sound(join(root, name))
|
||||
self.fx = fx
|
||||
|
||||
def mute(self, event):
|
||||
def respond(self, event):
|
||||
if self.get_delegate().compare(event, "mute"):
|
||||
self.muted = not self.muted
|
||||
self.set_volume()
|
||||
self.mute()
|
||||
|
||||
def mute(self):
|
||||
self.muted = not self.muted
|
||||
self.set_volume()
|
||||
|
||||
def set_volume(self):
|
||||
volume = int(not self.muted)
|
||||
|
|
|
@ -71,6 +71,10 @@ class Configuration(RawConfigParser):
|
|||
set_option(section, "icon-path", "")
|
||||
set_option(section, "skip-frames", "no")
|
||||
set_option(section, "fullscreen", "no")
|
||||
set_option(section, "show-framerate", "no")
|
||||
set_option(section, "framerate-text-size", "12")
|
||||
set_option(section, "framerate-text-color", "0, 0, 0")
|
||||
set_option(section, "framerate-text-background", "255, 255, 255")
|
||||
section = "input"
|
||||
add_section(section)
|
||||
set_option(section, "release-suffix", "-release")
|
||||
|
@ -247,23 +251,40 @@ class Configuration(RawConfigParser):
|
|||
class TypeDeclarations(dict):
|
||||
|
||||
list_member_sep = ','
|
||||
defaults = {"display": {"int": ["frame-duration", "wait-duration"],
|
||||
"bool": ["centered", "skip-frames", "fullscreen"],
|
||||
"int-list": "dimensions"},
|
||||
"screen-captures": {"path": "path"},
|
||||
"setup": {"list": ["classifiers", "resources-search-path",
|
||||
"requirements", "data-exclude",
|
||||
"additional-packages"],
|
||||
"path": ["installation-dir", "changelog",
|
||||
"description-file", "main-object",
|
||||
"icon-path", "windows-dist-path",
|
||||
"package-root"]},
|
||||
"mouse": {"float": "double-click-time-limit",
|
||||
"bool": "visible"},
|
||||
"keys": {"list": ["up", "right", "down", "left"]},
|
||||
"joy": {"int": ["advance", "pause"]},
|
||||
"audio": {"path": "sfx-path"},
|
||||
"event": {"int": "command-id-offset"}}
|
||||
|
||||
defaults = {
|
||||
|
||||
"display": {"int": ["frame-duration", "wait-duration",
|
||||
"framerate-text-size"],
|
||||
|
||||
"bool": ["centered", "skip-frames", "fullscreen",
|
||||
"show-framerate"],
|
||||
|
||||
"int-list": ["dimensions", "framerate-text-color",
|
||||
"framerate-text-background"]},
|
||||
|
||||
"screen-captures": {"path": "path"},
|
||||
|
||||
"setup": {"list": ["classifiers", "resources-search-path",
|
||||
"requirements", "data-exclude",
|
||||
"additional-packages"],
|
||||
|
||||
"path": ["installation-dir", "changelog", "description-file",
|
||||
"main-object", "icon-path", "windows-dist-path",
|
||||
"package-root"]},
|
||||
|
||||
"mouse": {"float": "double-click-time-limit",
|
||||
|
||||
"bool": "visible"},
|
||||
|
||||
"keys": {"list": ["up", "right", "down", "left"]},
|
||||
|
||||
"joy": {"int": ["advance", "pause"]},
|
||||
|
||||
"audio": {"path": "sfx-path"},
|
||||
|
||||
"event": {"int": "command-id-offset"}}
|
||||
|
||||
additional_defaults = {}
|
||||
|
||||
def __init__(self):
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import pygame
|
||||
from pygame import display
|
||||
from pygame.time import get_ticks, wait
|
||||
from pygame.locals import *
|
||||
|
||||
|
@ -35,8 +34,8 @@ class Game(GameChild):
|
|||
|
||||
def set_children(self):
|
||||
self.delegate = Delegate(self)
|
||||
self.mainloop = Mainloop(self)
|
||||
self.display = Display(self)
|
||||
self.mainloop = Mainloop(self)
|
||||
self.input = Input(self)
|
||||
self.audio = Audio(self)
|
||||
self.screen_grabber = ScreenGrabber(self)
|
||||
|
@ -44,7 +43,6 @@ class Game(GameChild):
|
|||
def frame(self):
|
||||
self.delegate.dispatch()
|
||||
self.update()
|
||||
display.update()
|
||||
|
||||
def run(self):
|
||||
self.mainloop.run()
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from pygame import display
|
||||
from pygame.font import Font
|
||||
from pygame.time import get_ticks, wait
|
||||
|
||||
from GameChild import GameChild
|
||||
|
@ -6,15 +8,35 @@ class Mainloop(GameChild):
|
|||
|
||||
def __init__(self, parent):
|
||||
GameChild.__init__(self, parent)
|
||||
config = self.get_configuration("display")
|
||||
self.overflow = 0
|
||||
self.frame_count = 1
|
||||
self.actual_frame_duration = 0
|
||||
self.frames_this_second = 0
|
||||
self.last_framerate_display = 0
|
||||
self.load_configuration()
|
||||
self.init_framerate_display()
|
||||
self.last_ticks = get_ticks()
|
||||
self.stopping = False
|
||||
|
||||
def load_configuration(self):
|
||||
config = self.get_configuration("display")
|
||||
self.target_frame_duration = config["frame-duration"]
|
||||
self.wait_duration = config["wait-duration"]
|
||||
self.skip_frames = config["skip-frames"]
|
||||
self.last_ticks = get_ticks()
|
||||
self.stopping = False
|
||||
self.show_framerate = config["show-framerate"]
|
||||
self.framerate_text_size = config["framerate-text-size"]
|
||||
self.framerate_text_color = config["framerate-text-color"]
|
||||
self.framerate_text_background = config["framerate-text-background"]
|
||||
|
||||
def init_framerate_display(self):
|
||||
if self.show_framerate:
|
||||
screen = self.get_screen()
|
||||
self.framerate_topright = screen.get_rect().topright
|
||||
self.display_surface = screen
|
||||
self.font = Font(self.get_resource("main-menu", "font-path"),
|
||||
self.framerate_text_size)
|
||||
self.font.set_bold(True)
|
||||
self.render_framerate(0)
|
||||
|
||||
def run(self):
|
||||
while not self.stopping:
|
||||
|
@ -24,11 +46,17 @@ class Mainloop(GameChild):
|
|||
self.stopping = False
|
||||
|
||||
def advance_frame(self):
|
||||
refresh = False
|
||||
while self.frame_count > 0:
|
||||
refresh = True
|
||||
self.get_game().frame()
|
||||
if self.show_framerate:
|
||||
self.update_framerate()
|
||||
self.frame_count -= 1
|
||||
if not self.skip_frames:
|
||||
break
|
||||
if refresh:
|
||||
display.update()
|
||||
|
||||
def update_frame_duration(self):
|
||||
last_ticks = self.last_ticks
|
||||
|
@ -51,5 +79,22 @@ class Mainloop(GameChild):
|
|||
overflow -= target_frame_duration
|
||||
overflow = self.overflow
|
||||
|
||||
def update_framerate(self):
|
||||
count = self.frames_this_second + 1
|
||||
if get_ticks() - self.last_framerate_display > 1000:
|
||||
self.render_framerate(count)
|
||||
self.last_framerate_display = get_ticks()
|
||||
count = 0
|
||||
self.display_surface.blit(self.framerate_text, self.framerate_text_rect)
|
||||
self.frames_this_second = count
|
||||
|
||||
def render_framerate(self, count):
|
||||
text = self.font.render(str(count), False, self.framerate_text_color,
|
||||
self.framerate_text_background)
|
||||
rect = text.get_rect()
|
||||
rect.topright = self.framerate_topright
|
||||
self.framerate_text = text
|
||||
self.framerate_text_rect = rect
|
||||
|
||||
def stop(self):
|
||||
self.stopping = True
|
||||
|
|
Loading…
Reference in New Issue