bevy/examples/2d/many_sprites.rs
Carter Anderson 11b41206eb Add upstream bevy_ecs and prepare for custom-shaders merge (#2815)
This updates the `pipelined-rendering` branch to use the latest `bevy_ecs` from `main`. This accomplishes a couple of goals:

1. prepares for upcoming `custom-shaders` branch changes, which were what drove many of the recent bevy_ecs changes on `main`
2. prepares for the soon-to-happen merge of `pipelined-rendering` into `main`. By including bevy_ecs changes now, we make that merge simpler / easier to review. 

I split this up into 3 commits:

1. **add upstream bevy_ecs**: please don't bother reviewing this content. it has already received thorough review on `main` and is a literal copy/paste of the relevant folders (the old folders were deleted so the directories are literally exactly the same as `main`).
2. **support manual buffer application in stages**: this is used to enable the Extract step. we've already reviewed this once on the `pipelined-rendering` branch, but its worth looking at one more time in the new context of (1).
3. **support manual archetype updates in QueryState**: same situation as (2).
2021-09-14 06:14:19 +00:00

93 lines
3.0 KiB
Rust

use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
math::Quat,
prelude::*,
render::camera::Camera,
sprite::SpriteSettings,
};
use rand::Rng;
const CAMERA_SPEED: f32 = 1000.0;
/// This example is for performance testing purposes.
/// See https://github.com/bevyengine/bevy/pull/1492
fn main() {
App::new()
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.insert_resource(SpriteSettings {
// NOTE: this is an experimental feature that doesn't work in all cases
frustum_culling_enabled: true,
})
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(tick_system.label("Tick"))
.add_system(move_camera_system.after("Tick"))
.run()
}
fn setup(
mut commands: Commands,
assets: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let mut rng = rand::thread_rng();
let tile_size = Vec2::splat(64.0);
let map_size = Vec2::splat(320.0);
let half_x = (map_size.x / 2.0) as i32;
let half_y = (map_size.y / 2.0) as i32;
let sprite_handle = materials.add(assets.load("branding/icon.png").into());
// Spawns the camera
commands
.spawn()
.insert_bundle(OrthographicCameraBundle::new_2d())
.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);
sprites.push(SpriteBundle {
material: sprite_handle.clone(),
transform: Transform {
translation,
rotation,
scale,
},
sprite: Sprite::new(tile_size),
..Default::default()
});
}
}
commands.spawn_batch(sprites);
}
// 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();
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());
}
// 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();
timer.tick(time.delta());
if timer.just_finished() {
info!("Sprites: {}", sprites_query.iter().count(),);
}
}