res
—resource loading and caching¶
-
load_img
(fn)[source]¶ ResourceManager
loader for images ('img'
).Takes the filename to load from, under
conf.IMG_DIR
.
-
load_font
(fn, size)[source]¶ ResourceManager
loader for Pygame fonts ('font'
).mk_font_keys(fn, size)
Parameters: - fn – font filename, under
conf.FONT_DIR
. - size – size this font should render at.
- fn – font filename, under
-
load_text
(text, renderer, options={}, **kwargs) -> (surface, num_lines)[source]¶ ResourceManager
loader for rendering text ('text'
).Parameters: renderer – text.TextRenderer
instance or the name a renderer is stored under inGame.text_renderers
.Other arguments are as taken by and the return value is as given by
TextRenderer.render()
.
-
load_snd
(snd) → new_sound[source]¶ ResourceManager
loader for rendering sounds ('snd'
).Parameters: snd – sound filename under conf.SOUND_DIR
to load.Returns: pygame.mixer.Sound
object.
-
class
ResourceManager
[source]¶ Bases:
object
Manage the loading and caching of resources.
Builtin resources loaders are in
resource_loaders
; to load a resource, you can useload()
, or you can do, eg.manager.img('border.png', pool='gui')
Documentation for builtin loaders is found in the
load_<loader>
functions in this module.-
resource_loaders
¶ A list of the resource loaders available to this manager.
-
pools
¶ A list of the resource pools contained by this manager.
-
load
(loader, *args, **kwargs, pool=conf.DEFAULT_RESOURCE_POOL, force_load=False) → data[source]¶ Load a resource.
Parameters: - loader – resource loader to use, as found in
resource_loaders
. - args – positional arguments to pass to the resource loader.
- kwargs – keyword arguments to pass the the resource loader.
- pool – the pool to cache the resource in.
- force_load – whether to bypass the cache and reload the object through
loader
.
Returns: the loaded resource data.
This is equivalent to
getattr(manager, loader)(*args, **kwargs, pool=conf.DEFAULT_RESOURCE_POOL)
.- loader – resource loader to use, as found in
-
register
(name, load, mk_keys[, measure])[source]¶ Register a new resource loader.
Parameters: - name – the name to give the loader, as used in
resource_loaders
; must be hashable, and must be a string and a valid variable name if you want to be able to load resources likeResourceManager.img()
. If already used, the existing loader is replaced. - load – a function to load a resource. Takes whatever arguments are
necessary (you’ll pass these to
load()
or the generated dedicated method). - mk_keys – a function to generate hashable caching keys for a resource,
given the same arguments as
load
. It should return an iterable object of keys, and the resource will be cached under all of them. - measure – a function to measure a resource’s size. Takes a resource as
returned by
load
, and returns its size as a number. The default is to return1
for any resource.
- name – the name to give the loader, as used in
-