This commit is contained in:
Frank DeMarco 2013-04-14 23:46:43 +09:00
parent a8ca15a630
commit 0d633badc7
6 changed files with 41 additions and 23 deletions

View File

@ -14,6 +14,7 @@ class Audio(GameChild):
def __init__(self, game):
GameChild.__init__(self, game)
self.delegate = self.get_delegate()
self.load_fx()
self.subscribe(self.respond)
@ -27,7 +28,7 @@ class Audio(GameChild):
self.fx = fx
def respond(self, event):
if self.get_delegate().compare(event, "mute"):
if self.delegate.compare(event, "mute"):
self.mute()
def mute(self):

View File

@ -9,7 +9,8 @@ class Display(GameChild):
def __init__(self, game):
GameChild.__init__(self, game)
self.config = self.get_configuration("display")
self.delegate = self.get_delegate()
self.load_configuration()
self.align_window()
self.init_screen()
self.set_caption()
@ -17,21 +18,29 @@ class Display(GameChild):
self.set_mouse_visibility()
self.subscribe(self.toggle_fullscreen)
def load_configuration(self):
config = self.get_configuration("display")
self.centered = config["centered"]
self.fullscreen = config["fullscreen"]
self.dimensions = config["dimensions"]
self.caption = config["caption"]
self.mouse_visibility = self.get_configuration("mouse", "visible")
def align_window(self):
if self.config["centered"]:
if self.centered:
environ["SDL_VIDEO_CENTERED"] = "1"
def init_screen(self):
flags = 0
if self.config["fullscreen"]:
if self.fullscreen:
flags = FULLSCREEN
self.set_screen(flags)
def set_screen(self, flags=0):
self.screen = display.set_mode(self.config["dimensions"], flags)
self.screen = display.set_mode(self.dimensions, flags)
def set_caption(self):
display.set_caption(self.config["caption"])
display.set_caption(self.caption)
def set_icon(self):
if self.get_configuration().has_option("display", "icon-path"):
@ -40,7 +49,7 @@ class Display(GameChild):
def set_mouse_visibility(self, visibility=None):
if visibility is None:
visibility = self.get_configuration("mouse", "visible")
visibility = self.mouse_visibility
mouse.set_visible(visibility)
def get_screen(self):
@ -50,7 +59,7 @@ class Display(GameChild):
return self.screen.get_size()
def toggle_fullscreen(self, event):
if self.get_delegate().compare(event, "toggle-fullscreen"):
if self.delegate.compare(event, "toggle-fullscreen"):
screen = self.screen
cpy = screen.convert()
self.set_screen(self.screen.get_flags() ^ FULLSCREEN)

View File

@ -10,6 +10,7 @@ class GameChild:
def __init__(self, parent=None):
self.parent = parent
self.game = self.get_game()
def get_game(self):
current = self
@ -18,7 +19,7 @@ class GameChild:
return current
def get_configuration(self, section=None, option=None):
config = self.get_game().configuration
config = self.game.configuration
if option and section:
return config.get(section, option)
if section:
@ -26,10 +27,10 @@ class GameChild:
return config
def get_input(self):
return self.get_game().input
return self.game.input
def get_screen(self):
return self.get_game().display.get_screen()
return self.game.display.get_screen()
def get_display_surface(self):
current = self
@ -41,10 +42,10 @@ class GameChild:
return current.display.get_screen()
def get_audio(self):
return self.get_game().audio
return self.game.audio
def get_delegate(self):
return self.get_game().delegate
return self.game.delegate
def get_resource(self, section, option):
config = self.get_configuration()
@ -75,7 +76,7 @@ class GameChild:
return normpath(path) == abspath(path)
def subscribe(self, callback, kind=None):
self.get_game().delegate.add_subscriber(callback, kind)
self.game.delegate.add_subscriber(callback, kind)
def unsubscribe(self, callback, kind=None):
self.get_game().delegate.remove_subscriber(callback, kind)
self.game.delegate.remove_subscriber(callback, kind)

View File

@ -11,6 +11,7 @@ class Input(GameChild):
GameChild.__init__(self, game)
self.last_mouse_down_left = None
self.joystick = Joystick()
self.delegate = self.get_delegate()
self.load_configuration()
self.unsuppress()
self.subscribe_to_events()
@ -57,7 +58,7 @@ class Input(GameChild):
self.post_command(cmd)
def post_command(self, cmd, **attributes):
self.get_delegate().post(cmd, **attributes)
self.delegate.post(cmd, **attributes)
def translate_key_release(self, event):
if not self.suppressed:

View File

@ -50,7 +50,7 @@ class Mainloop(GameChild):
refresh = False
while self.frame_count > 0:
refresh = True
self.get_game().frame()
self.parent.frame()
if self.show_framerate:
self.update_framerate()
self.frame_count -= 1

View File

@ -12,11 +12,19 @@ class ScreenGrabber(GameChild):
def __init__(self, game):
GameChild.__init__(self, game)
self.delegate = self.get_delegate()
self.load_configuration()
self.subscribe(self.save_display)
def load_configuration(self):
config = self.get_configuration("screen-captures")
self.save_path = config["path"]
self.file_name_format = config["file-name-format"]
self.file_extension = config["file-extension"]
def save_display(self, event):
if self.get_delegate().compare(event, "capture-screen"):
directory = self.get_configuration().get("screen-captures", "path")
if self.delegate.compare(event, "capture-screen"):
directory = self.save_path
try:
if not exists(directory):
makedirs(directory)
@ -29,7 +37,5 @@ class ScreenGrabber(GameChild):
(directory, exc_info()[1]))
def build_name(self):
config = self.get_configuration().get_section("screen-captures")
prefix = config["file-name-format"]
extension = config["file-extension"]
return "{0}.{1}".format(strftime(prefix), extension)
return "{0}.{1}".format(strftime(self.file_name_format),
self.file_extensions)