def load(filename, namehint=""): try: filename = rwops_encode_file_path(filename) c_surface = sdl.IMG_Load(filename) except SDLError: # filename is not a string, try as file object try: rwops = rwops_from_file(filename) if namehint: name, ext = path.splitext(namehint) elif hasattr(filename, 'name'): name, ext = path.splitext(filename.name) else: # No name info, so we pass an empty extension to # SDL to indicate we can only load files with # suitable magic format markers in the file. name, ext = '', '' if len(ext) == 0: ext = name ext = rwops_encode_file_path(ext) c_surface = sdl.IMG_LoadTyped_RW(rwops, 1, ext) except TypeError: raise TypeError("file argument must be a valid path " "string or file object") if not c_surface: raise SDLError(ffi.string(sdl.IMG_GetError())) return Surface._from_sdl_surface(c_surface)
def queue(filename): """ queue(filename) -> None queue a music file to follow the current """ check_mixer() global _queue_music try: filename = rwops_encode_file_path(filename) _queue_music = sdl.Mix_LoadMUS(filename) except SDLError: # try as file object rwops = rwops_from_file(filename) _queue_music = sdl.Mix_LoadMUS_RW(rwops) if not _queue_music: raise SDLError.from_sdl_error()
def __init__(self, font, fontsize): check_font() if not isinstance(fontsize, int): raise TypeError("expected an integer, but got %r" % type(fontsize)) if fontsize < 1: fontsize = 1 file_obj = None if font is None or font == _font_defaultname: file_obj = getResource(_font_defaultname) self._font_file = file_obj # Scaling as from pygame/src/font.c fontsize = int(fontsize * 0.6875) if fontsize < 1: fontsize = 1 elif isinstance(font, (bytes_, unicode_)): filepath = rwops_encode_file_path(font) # According to the pygame comments, we need to ensure the # file exists and is readable before calling out to SDL f = open(filepath, 'r') # pygame raises IOError if this fails, so we don't catch the # exception f.close() self._sdl_font = sdl.TTF_OpenFont(filepath, fontsize) else: file_obj = font if file_obj: # Get a new handle on the file to load the font from. # Otherwise, if the file handle is closed elsewhere, font # rendering will segfault. if self._font_file is None: file_obj = open(os.path.abspath(file_obj.name)) self._font_file = file_obj rwops = rwops_from_file(file_obj) self._sdl_font = sdl.TTF_OpenFontRW(rwops, 1, fontsize) if not self._sdl_font: raise SDLError.from_sdl_error()
def load(filename, namehint=""): try: filename = rwops_encode_file_path(filename) c_surface = sdl.IMG_Load(filename) except SDLError: # filename is not a string, try as file object try: rwops = rwops_from_file(filename) if namehint: name, ext = path.splitext(namehint) else: name, ext = path.splitext(filename.name) if len(ext) == 0: ext = name c_surface = sdl.IMG_LoadTyped_RW(rwops, 1, ext) except TypeError: raise TypeError("file argument must be a valid path " "string or file object") if not c_surface: raise SDLError(ffi.string(sdl.IMG_GetError())) return Surface._from_sdl_surface(c_surface)
def load(obj): """load(filename): return None load(object): return None Load a music file for playback""" check_mixer() global _current_music, _queue_music if isinstance(obj, (bytes_, unicode_)): filename = rwops_encode_file_path(obj) new_music = sdl.Mix_LoadMUS(filename) else: rwops = rwops_from_file(obj) new_music = sdl.Mix_LoadMUS_RW(rwops) if not new_music: raise SDLError.from_sdl_error() # Cleanup if _current_music: sdl.Mix_FreeMusic(_current_music) if _queue_music: sdl.Mix_FreeMusic(_queue_music) _queue_music = None _current_music = new_music
def __init__(self, obj=None, **kwargs): check_mixer() self.chunk = None self._mem = None # nasty mangling of parameters! # if 1 position arg: could be filename, file or buffer # if 1 keyword arg: could be filename, file, buffer or array where # filename and file use the same keyword 'file' if obj is not None: if kwargs: raise TypeError("Sound takes either 1 positional or " "1 keyword argument") filename = None buff = None err = None if isinstance(obj, string_types): filename = obj if not isinstance(obj, unicode_): buff = obj elif isinstance(obj, bytes): # For python3, we need to try both paths filename = obj buff = obj elif isinstance(obj, IOBase): rwops = rwops_from_file(obj) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) else: buff = obj if filename is not None: try: filename = rwops_encode_file_path(filename) rwops = rwops_from_file_path(filename) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) except SDLError as e: err = e if not self.chunk and buff is not None: if isinstance(buff, unicode_): raise TypeError("Unicode object not allowed as " "buffer object") try: self._load_from_buffer(buff) except TypeError: # Pygame is special here, and falls through to a # different error if the object doesn't support # the buffer interface. pass else: if len(kwargs) != 1: raise TypeError("Sound takes either 1 positional or " "1 keyword argument") # Py3k Dictionary Views are iterables, not iterators arg_name, arg_value = next(iter(kwargs.items())) if arg_name == 'file': if isinstance(arg_value, string_types): filename = rwops_encode_file_path(arg_value) rwops = rwops_from_file_path(filename, 'rb') elif isinstance(arg_value, bytes): # Needed for python 3 filename = rwops_encode_file_path(arg_value) rwops = rwops_from_file_path(filename, 'rb') else: rwops = rwops_from_file(arg_value) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) elif arg_name == 'buffer': if isinstance(arg_value, unicode_): raise TypeError("Unicode object not allowed as " "buffer object") self._load_from_buffer(arg_value) elif arg_name == 'array': raise NotImplementedError("Loading from array not " "implemented yet") else: raise TypeError("Unrecognized keyword argument '%s'" % arg_name) # pygame uses the pointer address as the tag to ensure # uniqueness, we use id for the same effect # Since we don't have the some automatic casting rules as # C, we explicitly cast to int here. This matches pygames # behaviour, so we're bug-compatible self._chunk_tag = ffi.cast("int", id(self.chunk)) if not self.chunk: if not err: raise TypeError("Unrecognized argument (type %s)" % type(obj).__name__) raise SDLError.from_sdl_error()
def __init__(self, obj=None, **kwargs): check_mixer() self.chunk = None # nasty mangling of parameters! # if 1 position arg: could be filename, file or buffer # if 1 keyword arg: could be filename, file, buffer or array where # filename and file use the same keyword 'file' if obj is not None: if kwargs: raise TypeError("Sound takes either 1 positional or " "1 keyword argument") filename = None buff = None err = None if isinstance(obj, basestring): filename = obj if not isinstance(obj, unicode): buff = obj elif isinstance(obj, file): rwops = rwops_from_file(obj) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) else: buff = obj if filename is not None: try: filename = rwops_encode_file_path(filename) rwops = rwops_from_file_path(filename) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) except SDLError as e: err = e if not self.chunk and buff is not None: raise NotImplementedError("Loading from buffer not " "implemented yet") # TODO: check if buff implements buffer interface. # If it does, load from buffer. If not, re-raise # error from filename if filename is not None. else: if len(kwargs) != 1: raise TypeError("Sound takes either 1 positional or " "1 keyword argument") arg_name = kwargs.keys()[0] arg_value = kwargs[arg_name] if arg_name == 'file': if isinstance(arg_value, basestring): filename = rwops_encode_file_path(arg_value) rwops = rwops_from_file_path(filename, 'rb') else: rwops = rwops_from_file(arg_value) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) elif arg_name == 'buffer': if isinstance(arg_name, unicode): raise TypeError("Unicode object not allowed as " "buffer object") raise NotImplementedError("Loading from buffer not " "implemented yet") elif arg_name == 'array': raise NotImplementedError("Loading from array not " "implemented yet") else: raise TypeError("Unrecognized keyword argument '%s'" % arg_name) # pygame uses the pointer address as the tag to ensure # uniqueness, we use id for the same effect # Since we don't have the some automatic casting rules as # C, we explicitly cast to int here. This matches pygames # behaviour, so we're bug-compatible self._chunk_tag = ffi.cast("int", id(self.chunk)) if not self.chunk: raise SDLError.from_sdl_error()