The following program is a very minimalistic example of a large quantity of SpriteComponents. In this example, with an FPS counter, you can see how costly SpriteComponents seem to be.
I did notice that if I reuse the material handle, it does speed things up, but even still, the speed is incredibly slow. Also, I'm not certain how safe it is to reuse a material handle. I noticed in the breakout example, they did not reuse handles.
Still, with no additional systems I am getting around 10 frames per second.
use bevy:: {
prelude::*,
};
struct Box;
struct Name(String);
#[derive(Default)]
struct FPSCounter(Timer,u32);
const WIDTH: f32 = 640.0;
const HEIGHT: f32 = 480.0;
pub struct drawPlugin;
impl Plugin for drawPlugin {
fn build(&self, app: &mut AppBuilder) {
app
.add_resource(FPSCounter(Timer::from_seconds(1.0,true),0))
.add_startup_system(setup.system())
.add_system(fps_counter.system());
}
}
fn fps_counter(time: Res<Time>, mut timer: ResMut<FPSCounter>) {
timer.0.tick(time.delta_seconds);
timer.1 += 1;
if timer.0.finished {
println!("One-{}",timer.1);
timer.1 = 0;
}
}
fn main() {
App::build()
.add_resource(WindowDescriptor {
title: "Test Prog!".to_string(),
width: WIDTH as u32,
height: HEIGHT as u32,
vsync: true,
resizable: false,
..Default::default()
})
.add_default_plugins()
.add_plugin(drawPlugin)
.run();
}
fn setup(mut cmds: Commands, mut mats: ResMut<Assets<ColorMaterial>>,asset_server: Res<AssetServer>) {
const sq_width: f32 = 10.0;
const sq_height: f32 = 10.0;
let left = -(WIDTH /2.0) + sq_width / 2.0;
let right = (WIDTH / 2.0) - sq_width / 2.0;
let top = (HEIGHT / 2.0) - sq_height / 2.0;
let bot = -(HEIGHT / 2.0) + sq_height / 2.0;
let mymat = mats.add(Color::rgb(1.0,0.0,0.0).into());
cmds
.spawn(Camera2dComponents::default())
.spawn(UiCameraComponents::default());
for box_num2 in 1..=30 {
for box_num in 1..=30 {
cmds.spawn(SpriteComponents {
material: mymat,
translation: Translation(Vec3::new(left + ((sq_width + 1.0) * box_num as f32),top - ((sq_height + 1.0) * box_num2 as f32),0.0)),
sprite: Sprite {
size: Vec2::new(sq_width,sq_height),
},
..Default::default()
}).with(Box {}).with(Name("Box1".to_string()));
}
}
}