mainloop child

This commit is contained in:
Frank DeMarco 2013-03-12 19:13:07 +09:00
parent 917370d502
commit 92ea55e016
5 changed files with 83 additions and 110 deletions

View File

@ -1,64 +0,0 @@
import pygame
class Animation:
def __init__(self, frame_duration, skip_frames=False):
self.reset_ticks()
self.updates_this_cycle = 1
self.overflow = 0
self.update_count = 1
self.actual_frame_duration = 0
self.target_frame_duration = frame_duration
self.skip_frames = skip_frames
self.stopping = False
def reset_ticks(self):
self.last_ticks = self.get_ticks()
def play(self):
while not self.stopping:
self.advance_frame()
self.update_frame_duration()
self.update_overflow()
self.stopping = False
def advance_frame(self):
while self.update_count > 0:
self.sequence()
self.update_count -= 1
if not self.skip_frames:
break
def sequence(self):
pass
def update_frame_duration(self):
last_ticks = self.last_ticks
actual_frame_duration = self.get_ticks() - last_ticks
last_ticks = self.get_ticks()
wait_duration = self.get_configuration().get("display", "wait-duration")
while actual_frame_duration < self.target_frame_duration:
pygame.time.wait(wait_duration)
actual_frame_duration += self.get_ticks() - last_ticks
last_ticks = self.get_ticks()
self.actual_frame_duration = actual_frame_duration
self.last_ticks = last_ticks
def get_ticks(self):
return pygame.time.get_ticks()
def update_overflow(self):
self.update_count = 1
target_frame_duration = self.target_frame_duration
overflow = self.overflow
overflow += self.actual_frame_duration - target_frame_duration
while overflow > target_frame_duration:
self.update_count += 1
overflow -= target_frame_duration
overflow = self.overflow
def stop(self):
self.stopping = True
def clear_queue(self):
self.update_count = 1

View File

@ -69,6 +69,7 @@ class Configuration(RawConfigParser):
set_option(section, "caption", "")
set_option(section, "centered", "yes")
set_option(section, "icon-path", "")
set_option(section, "skip-frames", "no")
section = "screen-captures"
add_section(section)
set_option(section, "rel-path", "caps")
@ -239,7 +240,7 @@ class TypeDeclarations(dict):
list_member_sep = ','
defaults = {"display": {"int": ["frame-duration", "wait-duration"],
"bool": "centered",
"bool": ["centered", "skip-frames"],
"int-list": "dimensions"},
"screen-captures": {"path": "path"},
"setup": {"list": ["classifiers", "resources-search-path",

View File

@ -1,3 +1,5 @@
from os import environ
from pygame import display, image, mouse
from pygame.locals import *
@ -7,7 +9,8 @@ class Display(GameChild):
def __init__(self, game):
GameChild.__init__(self, game)
self.config = self.get_configuration().get_section("display")
self.config = self.get_configuration("display")
self.align_window()
self.set_screen()
self.set_caption()
self.set_icon()
@ -30,6 +33,10 @@ class Display(GameChild):
visibility = self.get_configuration("mouse", "visible")
mouse.set_visible(visibility)
def align_window(self):
if self.config["centered"]:
environ["SDL_VIDEO_CENTERED"] = "1"
def get_screen(self):
return self.screen

View File

@ -1,83 +1,57 @@
from os import environ
import pygame
from pygame import display
from pygame.time import get_ticks, wait
from pygame.locals import *
from GameChild import *
from Animation import *
from Audio import *
from Display import *
from Configuration import *
from Delegate import *
from Input import *
from ScreenGrabber import *
from GameChild import GameChild
from Mainloop import Mainloop
from Audio import Audio
from Display import Display
from Configuration import Configuration
from Delegate import Delegate
from Input import Input
from ScreenGrabber import ScreenGrabber
class Game(GameChild, Animation):
class Game(GameChild):
resources_path = None
def __init__(self, config_rel_path=None, type_declarations=None):
self.init_gamechild()
GameChild.__init__(self)
self.print_debug(pygame.version.ver)
self.config_rel_path = config_rel_path
self.type_declarations = type_declarations
self.set_configuration()
self.init_animation()
self.align_window()
pygame.init()
self.set_children()
self.subscribe(self.end, QUIT)
self.subscribe(self.end)
self.clear_queue()
self.delegate.enable()
def init_gamechild(self):
GameChild.__init__(self)
def set_configuration(self):
self.configuration = Configuration(self.config_rel_path,
self.resources_path,
self.type_declarations)
def init_animation(self):
Animation.__init__(self,
self.configuration.get("display", "frame-duration"))
def align_window(self):
if self.configuration.get("display", "centered"):
environ["SDL_VIDEO_CENTERED"] = "1"
def set_children(self):
self.set_delegate()
self.set_display()
self.set_input()
self.set_audio()
self.set_screen_grabber()
def set_display(self):
self.display = Display(self)
def set_delegate(self):
self.delegate = Delegate(self)
def set_input(self):
self.mainloop = Mainloop(self)
self.display = Display(self)
self.input = Input(self)
def set_audio(self):
self.audio = Audio(self)
def set_screen_grabber(self):
self.screen_grabber = ScreenGrabber(self)
def sequence(self):
def frame(self):
self.delegate.dispatch()
self.update()
display.update()
def run(self):
self.mainloop.run()
def update(self):
pass
def end(self, evt):
if evt.type == QUIT or self.delegate.compare(evt, "quit"):
self.stop()
self.mainloop.stop()

55
pgfw/Mainloop.py Normal file
View File

@ -0,0 +1,55 @@
from pygame.time import get_ticks, wait
from GameChild import GameChild
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.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
def run(self):
while not self.stopping:
self.advance_frame()
self.update_frame_duration()
self.update_overflow()
self.stopping = False
def advance_frame(self):
while self.frame_count > 0:
self.get_game().frame()
self.frame_count -= 1
if not self.skip_frames:
break
def update_frame_duration(self):
last_ticks = self.last_ticks
actual_frame_duration = get_ticks() - last_ticks
last_ticks = get_ticks()
while actual_frame_duration < self.target_frame_duration:
wait(self.wait_duration)
actual_frame_duration += get_ticks() - last_ticks
last_ticks = get_ticks()
self.actual_frame_duration = actual_frame_duration
self.last_ticks = last_ticks
def update_overflow(self):
self.frame_count = 1
target_frame_duration = self.target_frame_duration
overflow = self.overflow
overflow += self.actual_frame_duration - target_frame_duration
while overflow > target_frame_duration:
self.frame_count += 1
overflow -= target_frame_duration
overflow = self.overflow
def stop(self):
self.stopping = True