bevy_double_res

Crates.iobevy_double_res
lib.rsbevy_double_res
version0.1.0
sourcesrc
created_at2022-09-03 09:29:41.143609
updated_at2022-09-03 09:29:41.143609
descriptionStraightforward double buffering implementation for bevy resources
homepagehttps://github.com/necromfox/bevy_double_res
repositoryhttps://github.com/necromfox/bevy_double_res
max_upload_size
id657810
size98,886
(aleqdev)

documentation

README

bevy_double_res

Straightforward implementation of double buffering of ordinary bevy engine resources

How to use

First, create your buffer:

fn main() {
    use bevy_double_res::prelude::*;
    
    let mut tuple = (10, 20).into_double_buf();
    
    //...
}

or

fn main() {
    use bevy_double_res::prelude::*;
    
    let mut tuple = DoubleBuffer::new((10, 20));
    
    //...
}

Second, mutate items:

fn main() {
    //...

    tuple.apply(|current, next| {
        next.0 = current.1;
        next.1 = current.0;
    });
    tuple.swap();
    
    //...
}

or

fn main() {
    //...
    
    let (current, next) = tuple.split_ordered();
    next.0 = current.1;
    next.1 = current.0;
    tuple.swap();
    
    //...
}

Don't forget about the swap!

Last, display contents of your buffer:

fn main() {
    use bevy_double_res::prelude::*;
    
    let mut tuple = (10, 20).into_double_buf();
    
    tuple.apply(|current, next| {
        next.0 = current.1;
        next.1 = current.0;
    });
    tuple.swap();
    
    println!("{:?}", tuple.current()); // outputs: (20, 10)
}

Using in systems

Creating resource is the same:

fn setup(mut commands: Commands) {
    let tuple = (10, 20).into_double_buf();

    commands.insert_resource(tuple);
}

Accessing resource in systems:

  • readonly? then use DoubleRes (matches Res)
  • mutable? then use DoubleResMut (matches ResMut)
fn circular_dependent_system(mut tuple: DoubleResMut<(i32, i32)>) {
    tuple.apply(|current, next| {
        next.0 = current.1;
        next.1 = current.0;
    });
    tuple.swap();
}

Also see an example of usage

Commit count: 5

cargo fmt