A lazy-loading resource manager, for use with pygame-ce
Project description
Simply Resourceful
The Simple Resource Manager
Explore the docs »
Report Bug
·
Request Feature
Table of Contents
About The Project
Simply Resourceful is a simple resource manager, designed to work alongside pygame. It offers deferred loading of resources, as well as a simple way of accessing those resources in your code, making swapping out resources a breeze.
Getting Started
Simply Resourceful is written in pure python, with no system dependencies, and should be OS-agnostic.
Installation
Simply Resourceful can be installed from the PyPI using pip:
pip install simply_resourceful
and can be imported for use with:
import resourceful
Simply Resourceful also requires Pygame Community edition to be installed.
Usage
Assets can often be a heavy burden on memory. The process of loading them from file or downloading them can also be intensives. Having your assets loaded only once, and then reused whenever needed, can help reduce the load. Additionally, instead of loading all assets immediatly, lazy loading allows the load times to be deferred until an asset is needed, and never coming into memory at all if never requested.
Resource Managers are created with
import resourceful
MANAGER = resourceful.getResourceManager(<type>, "handle")
This will ensure that a resource manager for the given type and of the given handle exists. Handles are optional, but are useful for having resource managers with different loading behavior despite the same resource type.
Note that for the rest of the document, 'resource' and 'asset' may be used interchangeably.
Types
Resource Managers are generic, and can handle any type of data. The type you want the resource manager to handle must be specified before use.
import pygame
import resourceful
MANAGER = resourceful.getResourceManager(pygame.Surface)
In this example, a resource manager that handles pygame surfaces will be created or found, if it exists, with a blank handle. This might be useful for storing images loaded into memory in one single place.
Using Asset Handles
Resources are referenced by asset handles, which are strings that supply common names to their resources. They are used as such:
sprite.image = image_manager.get("Hero")
This will check the manager for an asset called "Hero", load it if necessary, and supply the sprite with it for display.
In more complicated use cases, it may be desirable to supply a default value. This may be because the asset handle being requested is coming from elsewhere, and might not be guaranteed to be correct, and something is needed to fill the gap.
That use case would look something like this:
sprite.image = image_manager.get(current_hero_pose, hero_blank_sprite)
This way, if current_hero_pose accidentally refers to an invalid resource, the experience of the player will not be interrupted.
Preloading
Before any assets are requested, the resource manager must be made aware of the assets it will manage. This must be done early on in the program, before any manager.get() operations are called.
# ---Program set up stuff---
# Global variables and such, module initialization, whatever.
manager.preload("Hero", "path/to/hero.png")
In this example, the manager now is aware of an asset called "Hero", as well as a path to find it for loading.
Assets can also be force-loaded, which is similar to preloading, but loads the asset into memory from its location data immediately, no need to have something call get(). This might be useful for assets that are guaranteed to be used immediately.
Configuration
Because resource managers are generic, they have no inherent knowledge of how to load any given resource. So, a loader function must be supplied. For example:
def image_loader(resource_location: Path | str) -> pygame.Surface:
asset = pygame.image.load(resource_location)
return asset.convert()
# ---Program set up stuff---
# Global variables and such, module initialization, whatever.
manager.config(loader_helper=image_loader)
manager.preload("Hero", "path/to/hero.png")
This will load the image from the path, and convert it to be ready for use.
Loader functions only have two requirements:
- They must take the same data as is used for location data by the resource manager. That is, if the location data is a path, the loader must take a path.
- They must return the same type as the resource manager, or None, indicating a load failure. They can do anything else you'd like, and can be a simple or as complicated as you want. to extend the above example, you could have it check the file extension to determine if convert() is sufficient, or if convert_alpha() would be preferable.
Additionally, in an async-aware environment, you could have your loader begin a coroutine to download your asset, and return a default asset to tide things over until then, updating the asset upon completion.
From there, the resource manager will take over, loading and supplying resources as needed by other parts of the program.
Roadmap
- Make pickleable for saving and loading resource managers.
- Allow for objects to request proxies that don't load the asset until it is called upon.
See the open issues for a full list of proposed features (and known issues).
License
Distributed under the MIT License. See LICENSE.txt for more information.
Contact
Better Built Fool - betterbuiltfool@gmail.com
Bluesky - @betterbuiltfool.bsky.social
Project Link: https://github.com/BetterBuiltFool/simply_resourceful
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simply_resourceful-0.8.0.tar.gz.
File metadata
- Download URL: simply_resourceful-0.8.0.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20516b444ed92a94eb55671d4cac8e6095d0aa871f7708e73c7915bb7c0c92fa
|
|
| MD5 |
671ad571cc3acf18536506f5dff8713c
|
|
| BLAKE2b-256 |
29a9db041461e034fb927985425564948c7b2527a98300230336bd5d6ab4431c
|
File details
Details for the file simply_resourceful-0.8.0-py3-none-any.whl.
File metadata
- Download URL: simply_resourceful-0.8.0-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13d62b47893b308d77cf9bbe7d50ad071435208d0a58311c6048c9b216c93616
|
|
| MD5 |
af767fc2c475d2e45888545d4231af62
|
|
| BLAKE2b-256 |
ab8d2dad5745c1fb7c2545c1b4e399034f2e5f3ebe9999b81591d7b7105a2a72
|