Skip to content

[Merged by Bors] - Improve many sprites example #2785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions examples/2d/many_sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
math::Quat,
prelude::*,
render::camera::Camera,
sprite::SpriteSettings,
};

use rand::Rng;

const CAMERA_SPEED: f32 = 1000.0;

pub struct PrintTimer(Timer);
pub struct Position(Transform);

/// This example is for performance testing purposes.
/// See https://github.com/bevyengine/bevy/pull/1492
fn main() {
Expand All @@ -24,8 +22,8 @@ fn main() {
})
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(tick.label("Tick"))
.add_system(move_camera.after("Tick"))
.add_system(tick_system.label("Tick"))
.add_system(move_camera_system.after("Tick"))
.run()
}

Expand All @@ -44,22 +42,23 @@ fn setup(

let sprite_handle = materials.add(assets.load("branding/icon.png").into());

// Spawns the camera
commands
.spawn()
.insert_bundle(OrthographicCameraBundle::new_2d())
.insert(PrintTimer(Timer::from_seconds(1.0, true)))
.insert(Position(Transform::from_translation(Vec3::new(
0.0, 0.0, 1000.0,
))));
.insert(Timer::from_seconds(1.0, true))
.insert(Transform::from_xyz(0.0, 0.0, 1000.0));

// Builds and spawns the sprites
let mut sprites = vec![];
for y in -half_y..half_y {
for x in -half_x..half_x {
let position = Vec2::new(x as f32, y as f32);
let translation = (position * tile_size).extend(rng.gen::<f32>());
let rotation = Quat::from_rotation_z(rng.gen::<f32>());
let scale = Vec3::splat(rng.gen::<f32>() * 2.0);

commands.spawn().insert_bundle(SpriteBundle {
sprites.push(SpriteBundle {
material: sprite_handle.clone(),
transform: Transform {
translation,
Expand All @@ -71,26 +70,23 @@ fn setup(
});
}
}
commands.spawn_batch(sprites);
}

fn move_camera(time: Res<Time>, mut query: Query<(&mut Transform, &mut Position)>) {
for (mut transform, mut position) in query.iter_mut() {
position
.0
.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
position.0 =
position.0 * Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
transform.translation = position.0.translation;
transform.rotation *= Quat::from_rotation_z(time.delta_seconds() / 2.0);
}
// System for rotating and translating the camera
fn move_camera_system(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
let mut camera_transform = camera_query.single_mut().unwrap();
camera_transform.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
*camera_transform = *camera_transform
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
}

fn tick(time: Res<Time>, sprites: Query<&Sprite>, mut query: Query<&mut PrintTimer>) {
for mut timer in query.iter_mut() {
timer.0.tick(time.delta());
// System for printing the number of sprites on every tick of the timer
fn tick_system(time: Res<Time>, sprites_query: Query<&Sprite>, mut timer_query: Query<&mut Timer>) {
let mut timer = timer_query.single_mut().unwrap();
timer.tick(time.delta());

if timer.0.just_finished() {
info!("Sprites: {}", sprites.iter().count(),);
}
if timer.just_finished() {
info!("Sprites: {}", sprites_query.iter().count(),);
}
}