Skip to content

Resource Management

Matt Styles edited this page May 7, 2016 · 4 revisions

xygine provides an interface for creating managed resource classes for resources that are loaded at run time, and generally considered too bulky to have a lot of copies of. For example xygine implements the interface in classes such as xy::TextureResource and xy::FontResource. Not only does the resource management class ensure that a single instance of a resource exists at any one time, it also provides a way to deal with fallbacks, should a requested resource not be found. xy::Texture Resource will return a texture with a solid colour as defined by xy::TextureResource::setFallbackColour() and xy:::FontResource will supply a default font. xygine offers the following resource management classes:

  • xy::FontResource - manages instances of sf::Font
  • xy::ImageResource - resource management for sf::Image
  • xy::ShaderResource - precaches compiled sf::Shaders
  • xy::SoundResource - resource management for sf::SoundBuffer objects
  • xy::TextureResource - manages sf::Texture instances

These all implement the xy::BaseResource interface with the exception of xy::ShaderResource (see below). To implement your own resource manager create a class which inherits xy::BaseResource and implement the pure virtual function errorHandle() which should return a unique_ptr of the type to be managed by the class. This unique_ptr should point to the object used as a default resource should loading a resource from disk fail.

######xy::ShaderResource The shader resource manager differs slightly from the other resource management classes. Shaders are created from strings in memory (how you aquire these is up to you) and mapped to an integer ID, stored (for example) as an enum value. As no reasonable default values can be returned for a shader xygine will throw an assertion error in debug mode when creating a shader fails. Shaders should all be loaded via the precache() function during load time, as some shaders may take a small amount of time to compile and cause interruptions should they be loaded during gameplay. Shaders can then be quickly accessed from the resource manager by using the get() function along with the specific shader ID. Remember that, as each shader exists only once, when a drawable using a shader modifies the shader's property via sf::Shader::setUniform (or setParamter() in older versions of SFML) any drawable using that shader will have the same uniform parameters applied and will need to update these values itself should it need to.

Clone this wiki locally