video capture; animation accounts playing; sprite file extension

This commit is contained in:
Frank DeMarco 2013-11-13 13:06:14 +09:00
parent 50de5d523b
commit fd196fe2f9
5 changed files with 116 additions and 11 deletions

View File

@ -48,7 +48,10 @@ class Animation(GameChild):
if self.accounts.has_key(method):
self.accounts[method].halt()
def is_playing(self, method=None):
def is_playing(self, method=None, check_all=False):
if check_all:
return any(account.playing for method, account in
self.accounts.iteritems())
return self.accounts[self.get_default(method)].playing
def update(self):

View File

@ -88,6 +88,13 @@ class Configuration(RawConfigParser):
set_option(section, "rel-path", "caps")
set_option(section, "file-name-format", "%Y%m%d%H%M%S")
set_option(section, "file-extension", "png")
section = "video-recordings"
add_section(section)
set_option(section, "rel-path", "vids")
set_option(section, "directory-name-format", "%Y%m%d%H%M%S")
set_option(section, "file-extension", "png")
set_option(section, "frame-format", "RGB")
set_option(section, "framerate", "100")
section = "mouse"
add_section(section)
set_option(section, "visible", "yes")
@ -101,6 +108,7 @@ class Configuration(RawConfigParser):
set_option(section, "capture-screen", "K_F9")
set_option(section, "toggle-fullscreen", "K_F11")
set_option(section, "reset-game", "K_F8")
set_option(section, "record-video", "K_F10")
section = "joy"
add_section(section)
set_option(section, "advance", "7")
@ -148,6 +156,7 @@ class Configuration(RawConfigParser):
self.set_installation_path()
self.set_resource_search_path()
self.set_screen_captures_path()
self.set_video_recordings_path()
self.set_data_exclusion_list()
self.set_requirements()
@ -210,6 +219,12 @@ class Configuration(RawConfigParser):
def build_home_path(self):
return join("~", "." + self.get("setup", "package-root"))
def set_video_recordings_path(self):
section, option = "video-recordings", "path"
if not self.has_option(section, option):
self.set(section, option, join(self.build_home_path(),
self.get(section, "rel-path")))
def set_data_exclusion_list(self):
section, option = "setup", "data-exclude"
exclude = []
@ -266,7 +281,11 @@ class TypeDeclarations(dict):
"int-list": ["dimensions", "framerate-text-color",
"framerate-text-background"]},
"screen-captures": {"path": "path"},
"screen-captures": {"path": ["rel-path", "path"]},
"video-recordings": {"path": ["rel-path", "path"],
"int": "framerate"},
"setup": {"list": ["classifiers", "resource-search-path",
"requirements", "data-exclude",

View File

@ -11,6 +11,7 @@ from Delegate import Delegate
from Input import Input
from ScreenGrabber import ScreenGrabber
from Profile import Profile
from VideoRecorder import VideoRecorder
class Game(GameChild):
@ -41,10 +42,12 @@ class Game(GameChild):
self.input = Input(self)
self.audio = Audio(self)
self.screen_grabber = ScreenGrabber(self)
self.video_recorder = VideoRecorder(self)
def frame(self):
self.delegate.dispatch()
self.update()
self.video_recorder.update()
def run(self):
self.mainloop.run()

View File

@ -1,6 +1,7 @@
from os import listdir
from os.path import isfile, join
from sys import exc_info, stdout
from glob import glob
from traceback import print_exc, print_stack
from pygame import Color, Rect, Surface
@ -32,11 +33,15 @@ class Sprite(Animation):
def set_framerate(self, framerate):
self.register(self.shift_frame, interval=framerate)
def load_from_path(self, path, transparency=False, ppa=True, key=None):
def load_from_path(self, path, transparency=False, ppa=True, key=None,
extension=None):
if isfile(path):
paths = [path]
else:
paths = [join(path, name) for name in sorted(listdir(path))]
if extension:
paths = sorted(glob(join(path, "*." + extension)))
else:
paths = [join(path, name) for name in sorted(listdir(path))]
for path in paths:
img = load(path)
if transparency:
@ -110,13 +115,17 @@ class Sprite(Animation):
def clear_frames(self):
self.frames = []
def add_location(self, offset=(0, 0), count=1):
base = self.locations[0]
current_offset = list(offset)
for ii in xrange(count):
self.locations.append(base.move(*current_offset))
current_offset[0] += offset[0]
current_offset[1] += offset[1]
def add_location(self, topleft=None, offset=(0, 0), count=1, base=0):
if topleft is not None:
for ii in xrange(count):
self.locations.append(Rect(topleft, self.locations[0].size))
else:
base = self.locations[base]
current_offset = list(offset)
for ii in xrange(count):
self.locations.append(base.move(*current_offset))
current_offset[0] += offset[0]
current_offset[1] += offset[1]
def update(self):
Animation.update(self)

71
pgfw/VideoRecorder.py Normal file
View File

@ -0,0 +1,71 @@
from os import makedirs
from os.path import exists, join
from tempfile import TemporaryFile
from time import strftime
from pygame.image import tostring, frombuffer, save
from pygame.time import get_ticks
from GameChild import GameChild
class VideoRecorder(GameChild):
def __init__(self, parent):
GameChild.__init__(self, parent)
self.display_surface = self.get_display_surface()
self.delegate = self.get_delegate()
self.load_configuration()
self.reset()
self.subscribe(self.respond)
def load_configuration(self):
config = self.get_configuration("video-recordings")
self.root = config["path"]
self.directory_name_format = config["directory-name-format"]
self.file_extension = config["file-extension"]
self.frame_format = config["frame-format"]
self.framerate = config["framerate"]
def reset(self):
self.recording = False
self.frame_length = None
self.frames = None
self.last_frame = 0
def respond(self, event):
compare = self.delegate.compare
if compare(event, "record-video"):
self.toggle_record()
elif compare(event, "reset-game"):
self.reset()
def toggle_record(self):
recording = not self.recording
if recording:
self.frame_length = len(self.get_string())
self.frames = TemporaryFile()
else:
self.write_frames()
self.recording = recording
def get_string(self):
return tostring(self.display_surface, self.frame_format)
def write_frames(self):
root = join(self.root, strftime(self.directory_name_format))
if not exists(root):
makedirs(root)
size = self.display_surface.get_size()
frames = self.frames
frames.seek(0)
for ii, frame in enumerate(iter(lambda: frames.read(self.frame_length),
"")):
path = join(root, "%04i.png" % ii)
save(frombuffer(frame, size, self.frame_format), path)
print "wrote video frames to " + root
def update(self):
ticks = get_ticks()
if self.recording and ticks - self.last_frame >= self.framerate:
self.frames.write(self.get_string())
self.last_frame = ticks