From ab2b895693088b2f75fe01f6e4c27b0ee4e4197b Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Sat, 19 Mar 2022 11:32:43 +0100 Subject: [PATCH 01/23] Add a module doc block to the `2d/contributors` example. The comment aims to explain what the example shows and to give an overview on how it works. --- examples/games/contributors.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/examples/games/contributors.rs b/examples/games/contributors.rs index 7cc038e06a728..a2d69ffa7ebc1 100644 --- a/examples/games/contributors.rs +++ b/examples/games/contributors.rs @@ -1,3 +1,32 @@ +//! This example collects a list of all contributors to the bevy source code and renders a bouncing +//! bevy logo for each of them, showing how to combine multiple systems to +//! - load some external information on startup +//! - animate 2d sprites with some basic collision detection and physics simulation +//! - uses a [`Timer`] to periodically highlight different logos +//! - displays the highlighted contributors name using the [`TextBundle`] from [`bevy::ui`]. +//! +//! This is broken down into distinct, simple systems that build upon each other. +//! The `setup_contributor_selection` system generates a list of contributors, generates a +//! random starting position, `Velocity` and color for each of them, and spawns one entity for +//! each contributor into the world, consisting of a [`SpriteBundle`] for rendering the logo, +//! the initial choose `Velocity` as well as the colour. +//! +//! The `setup` system then sets up the [`OrthographicCamera`](OrthographicCameraBundle) for 2D +//! rendering, the [`UiCamera`](UiCameraBundle) for rendering the text, and the [`TextBundle`] to +//! display the contributor name. +//! +//! The `velocity_system` applies some simple gravity simulation to the velocity of entities. +//! +//! The `move_system` system then uses that velocity to update the sprites position, based on the +//! time that has passed since the last update. +//! +//! The `collision_system` system makes sure all bouncing logos stay inside the viewport by checking +//! for collisions with the four edges, and updating the velocity. +//! +//! At last, the `select` system checks if the `SelectTimer` has expired and if it has, updates +//! the contributor text, resets the color of the currently highlighted sprite, and highlights the +//! next one. + use bevy::{prelude::*, utils::HashSet}; use rand::{prelude::SliceRandom, Rng}; use std::{ From 9f6925d49f63a24a648469a968ead4707c893415 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Sat, 19 Mar 2022 11:55:49 +0100 Subject: [PATCH 02/23] Add a module doc block to the `2d/many_sprites` benchmark example. --- examples/stress_tests/many_sprites.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/examples/stress_tests/many_sprites.rs b/examples/stress_tests/many_sprites.rs index ede9934aa3f89..f71cf43db4b6a 100644 --- a/examples/stress_tests/many_sprites.rs +++ b/examples/stress_tests/many_sprites.rs @@ -1,3 +1,16 @@ +//! Renders a lot of sprites to allow performance testing. +//! See +//! +//! It sets up many sprites in different sizes and rotations, and at different scales in the world, +//! and moves the camera over them to see how well frustum culling works. +//! +//! Since its a kind of benchmark, it also uses the [`LogDiagnosticsPlugin`] and the +//! [`FrameTimeDiagnosticsPlugin`] to display performance statistics in the console. +//! +//! It also displays on how to use a [`Local`] system parameter in `print_sprite_count`, where that +//! system is the only one able to access this value, in this case, the [`Timer`] used to determine +//! when to print the sprite count. + use bevy::{ diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, math::Quat, @@ -9,8 +22,6 @@ use rand::Rng; const CAMERA_SPEED: f32 = 1000.0; -/// This example is for performance testing purposes. -/// See fn main() { App::new() .add_plugin(LogDiagnosticsPlugin::default()) From c55dbf9ae228f7bd3d0f4e999ec4a1ee22c6cdff Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Sat, 19 Mar 2022 12:12:19 +0100 Subject: [PATCH 03/23] Add a very brief module doc block to the `2d/mesh2d` example. --- examples/2d/mesh2d.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/2d/mesh2d.rs b/examples/2d/mesh2d.rs index 8b968a3966892..6412c97f88b68 100644 --- a/examples/2d/mesh2d.rs +++ b/examples/2d/mesh2d.rs @@ -1,3 +1,5 @@ +//! Shows how to render a polygonal [`Mesh`], generated from a [`Quad`] primitive, in a 2D scene. + use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; fn main() { From c22897115f100a63b7884b85fe2e4a52c94a52c9 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Sat, 19 Mar 2022 12:13:04 +0100 Subject: [PATCH 04/23] Move the doc block for `2d/mesh2d_manual` from the main function, to the module level. --- examples/2d/mesh2d_manual.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/2d/mesh2d_manual.rs b/examples/2d/mesh2d_manual.rs index e585018aa5448..c6367b1abebe4 100644 --- a/examples/2d/mesh2d_manual.rs +++ b/examples/2d/mesh2d_manual.rs @@ -1,3 +1,8 @@ +//! This example shows how to manually render 2d items using "mid level render apis" with a custom +//! pipeline for 2d meshes. +//! It doesn't use the [`Material2d`] abstraction, but changes the vertex buffer to include vertex color +//! Check out the "mesh2d" example for simpler / higher level 2d meshes + use bevy::{ core_pipeline::Transparent2d, prelude::*, @@ -23,9 +28,6 @@ use bevy::{ utils::FloatOrd, }; -/// This example shows how to manually render 2d items using "mid level render apis" with a custom pipeline for 2d meshes -/// It doesn't use the [`Material2d`] abstraction, but changes the vertex buffer to include vertex color -/// Check out the "mesh2d" example for simpler / higher level 2d meshes fn main() { App::new() .add_plugins(DefaultPlugins) From f2225ec3157f26ade8b30dcdd397e3366d710638 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Sat, 19 Mar 2022 12:43:06 +0100 Subject: [PATCH 05/23] Add a module doc block to the `2d/move_sprite` example. --- examples/2d/move_sprite.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/examples/2d/move_sprite.rs b/examples/2d/move_sprite.rs index a895212b3ae51..c91dbfcc59b12 100644 --- a/examples/2d/move_sprite.rs +++ b/examples/2d/move_sprite.rs @@ -1,3 +1,11 @@ +//! Renders a single, moving sprite in a 2D scene. +//! +//! The `setup` systems creates the 2D camera by spawning a [`OrthographicCameraBundle`], and the +//! entity in form of a [`SpriteBundle`], loading an image via the [`AssetServer`]. +//! +//! The `movement_system` then animates the sprite moving up and down, changing direction when the +//! y offset reaches a threshold. + use bevy::prelude::*; fn main() { From 1fcbfb9e5036dde005de51b1f86dd06a4df5e3d1 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Sat, 19 Mar 2022 12:43:13 +0100 Subject: [PATCH 06/23] Add a module doc block to the `2d/rect` example. --- examples/2d/sprite.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/2d/sprite.rs b/examples/2d/sprite.rs index 6620aa9619035..452710e095adc 100644 --- a/examples/2d/sprite.rs +++ b/examples/2d/sprite.rs @@ -1,3 +1,8 @@ +//! Shows how to render simple rectangle sprite with a single color. +//! +//! Since this [`Sprite`] is not generated from an image, we have create it directly when spawning +//! the [`SpriteBundle`] and specify the size using `custom_size`. + use bevy::prelude::*; fn main() { From eef3fdb65a696840e8d30b0018401b31b4548c40 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Thu, 7 Apr 2022 10:03:04 +0200 Subject: [PATCH 07/23] Add a simple module doc block to the `2d/sprite` example. --- examples/2d/sprite.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/2d/sprite.rs b/examples/2d/sprite.rs index 452710e095adc..6ebe25dc19c95 100644 --- a/examples/2d/sprite.rs +++ b/examples/2d/sprite.rs @@ -1,7 +1,4 @@ -//! Shows how to render simple rectangle sprite with a single color. -//! -//! Since this [`Sprite`] is not generated from an image, we have create it directly when spawning -//! the [`SpriteBundle`] and specify the size using `custom_size`. +//! Displays a single [`Sprite`], created from an image. use bevy::prelude::*; From b8c681715060803dab3921ab565e58487fa32211 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Thu, 7 Apr 2022 10:03:17 +0200 Subject: [PATCH 08/23] Add a simple module doc block to the `2d/sprite_flipping` example. --- examples/2d/sprite_flipping.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/2d/sprite_flipping.rs b/examples/2d/sprite_flipping.rs index e89968cf962e6..761741572086e 100644 --- a/examples/2d/sprite_flipping.rs +++ b/examples/2d/sprite_flipping.rs @@ -1,3 +1,5 @@ +//! Displays a single [`Sprite`], created from an image, but flipped on one axis. + use bevy::prelude::*; fn main() { From 1c5c560e7f126835a83ba06b98aa15e70e34ead8 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Thu, 7 Apr 2022 10:06:17 +0200 Subject: [PATCH 09/23] moved the comment from main to the file head --- examples/2d/texture_atlas.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/2d/texture_atlas.rs b/examples/2d/texture_atlas.rs index 8aa7bcae4329a..cde80e51ec2e8 100644 --- a/examples/2d/texture_atlas.rs +++ b/examples/2d/texture_atlas.rs @@ -1,7 +1,8 @@ +//! In this example we generate a new texture atlas (sprite sheet) from a folder containing +//! individual sprites + use bevy::{asset::LoadState, prelude::*}; -/// In this example we generate a new texture atlas (sprite sheet) from a folder containing -/// individual sprites fn main() { App::new() .init_resource::() From c2e78cf8e56c7e99fafb973acd565be6324ab7a2 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Thu, 7 Apr 2022 10:06:42 +0200 Subject: [PATCH 10/23] Add a module doc block to the `2d/text2d` example. --- examples/2d/text2d.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index e346cee7242e5..f7b1b746cad86 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -1,3 +1,11 @@ +//! Example to show text rendering with moving, rotating and scaling text. +//! +//! Note that this uses [`Text2dBundle`] to display text using the same [`OrthographicCameraBundle`] +//! that also would show your sprites, instead of the [`TextBundle`] which requires a [`UiCameraBundle`] +//! to be displayed. +//! For an example on how to render text as part of a user interface, independent from the world +//! viewport, you may want to look at `2d/contributors.rs` or `ui/text.rs`. + use bevy::{prelude::*, text::Text2dBounds}; fn main() { From 9c0c7b1cd0e2c2da692525b5be80177c2d584b25 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Fri, 8 Apr 2022 13:11:46 +0200 Subject: [PATCH 11/23] Cut the long comments, reworded others. The plan here is having a short, but helpful, introduction to each example. Stating what is meant to be shown, without going into details. --- examples/2d/move_sprite.rs | 10 +++------ examples/2d/rotation.rs | 2 ++ examples/2d/sprite_sheet.rs | 3 +++ examples/2d/text2d.rs | 5 ++--- examples/games/contributors.rs | 29 +-------------------------- examples/stress_tests/many_sprites.rs | 8 +------- 6 files changed, 12 insertions(+), 45 deletions(-) diff --git a/examples/2d/move_sprite.rs b/examples/2d/move_sprite.rs index c91dbfcc59b12..856adcd31bea7 100644 --- a/examples/2d/move_sprite.rs +++ b/examples/2d/move_sprite.rs @@ -1,10 +1,4 @@ -//! Renders a single, moving sprite in a 2D scene. -//! -//! The `setup` systems creates the 2D camera by spawning a [`OrthographicCameraBundle`], and the -//! entity in form of a [`SpriteBundle`], loading an image via the [`AssetServer`]. -//! -//! The `movement_system` then animates the sprite moving up and down, changing direction when the -//! y offset reaches a threshold. +//! Renders a 2D scene containing a single, moving sprite. use bevy::prelude::*; @@ -33,6 +27,8 @@ fn setup(mut commands: Commands, asset_server: Res) { .insert(Direction::Up); } +/// The sprite is animated by changing its translation depending on the time that has passed since +/// the last frame. fn sprite_movement(time: Res