Resources

We've already used resources in previous chapters, like the clock we use to control animations or the viewport that some examples retrieve as a resource. Resources are really easy to use but let's quickly see what they are, how they are created and retrieved and what resources are available by default in a rin application.

Resources are global data that can be accessed from anywhere, they are stored in the scene and available to every system in our application or even directly from the scene.

A resource can be any data type, it doesn't need any special annotation or implement or derive any specific trait. Let's say for example that we are working on a game with a grid like map. A possible resource could be the Map of the current level:

pub struct Cell {
    pub color: Rgb<f32>
}

pub struct LevelMap{
    pub grid: Vec<Cell>,
}

scene.add_resource(LevelMap{ grid: vec![] });

And that's it, now any system can read it or modify it:

pub fn map_updater(entities: Entities, resources: Resources) {
    let mut map = resources.get_mut::<LevelMap>().unwrap();
}

pub fn map_render(entities: Entities, resources: Resources) {
    let map = resources.get::<LevelMap>().unwrap();
}

Resources as traits

Resources can be added as traits so they can be accessed in a more generic way without knowing the exact type, only that they implement a certain trait:

trait Movement{
    fn translate(&mut self);
}

struct Ball;

impl Movement {
    fn translate(&mut self) {

    }
}

let ball = Ball;
scene.add_resource_as_trait(rin::ecs::cast!(ball as dyn Movement));

Only one trait is supported by now and resources added as traits can be also retrieved by their original type;

Thread local

A particular case of resources, as with entity components, are those that are not thread safe, not Send in rust terminology. They are used pretty much the same but have to be added using add_resource_thread_local and can only be used from thread local systems.

Resources can also be removed using remove_resource from either the scene or a CreationSystem.

Available resources in a rin application

When working with rin some resources are by default available that provide certain utilities. Here's the most important but every rin module usually documents which resources it makes publicly available.

When using a gl forward renderer and after adding a camera (which is mandatory) the following resources will be available:

  • rin::window::Window also available as rin::window::WindowExt: Allows to access the window properties, change the video mode, enable and disable vertical sync, etc
  • rin::scene::renderer::material::MaterialPool: Gives access to the materials in order to change their properties for example.
  • rin::scene::renderer::material::ShadowMaterialPool: Similar to MaterialPool but for shadow materials.
  • rin::graphics::CameraExt: Also available as the original camera we add to the scene.
  • rin::gl::Renderer: Useful to draw or create gl resources in auxiliary renderers or systems that use the gpu for general computing for example.
  • rin::scene::renderer::materials::TexturePool: A pool of textures, allows to store textures that can then be used via a handle in materials, image based lights, etc.
  • rin::scene::transformation::Viewport: Contains a rectangle with the current drawing viewport which depends on the application using a render surface or not, the gui being enabled or not, etc.
  • rin::scene::renderer::resources::ScreenRenderBuffer: Only present when the renderer is setup with a render buffer. It's usually not needed from applications but might be useful for advanced usages. It contains the textures and render buffers the application renders to.
  • rin::postpo::PostProcessing and rin::postpo::Parameters: The postprocessing pipeline and it's parameters, only available if the postprocessing bundle was added to the scene. Usually only used internally to apply the postprocessing effects but can be useful for certain advanced usages, mostly to access the final stage of the postprocessing in case we want to apply further effects externally before the final surface is blit to the screen.

When using the immediate renderer:

  • rin::window::Window also available as rin::window::WindowExt: Allows to access the window properties, change the video mode, enable and disable vertical sync, etc.
  • rin::scene::transformation::Viewport: Contains a rectangle with the current drawing viewport which depends on the application using a render surface or not, the gui being enabled or not, etc.
  • rin::gl::Renderer: Useful to draw or create gl resources in auxiliary renderers or systems that use the gpu for general computing for example.