//! This example illustrates how `FontAtlas`'s are populated. //! Bevy uses `FontAtlas`'s under the hood to optimize text rendering. use bevy::{prelude::*, text::FontAtlasSet}; fn main() { App::new() .init_resource::() .insert_resource(ClearColor(Color::BLACK)) .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .add_systems(Update, (text_update_system, atlas_render_system)) .run(); } #[derive(Resource)] struct State { atlas_count: u32, handle: Handle, timer: Timer, } impl Default for State { fn default() -> Self { Self { atlas_count: 0, handle: Handle::default(), timer: Timer::from_seconds(0.05, TimerMode::Repeating), } } } fn atlas_render_system( mut commands: Commands, mut state: ResMut, font_atlas_sets: Res>, texture_atlases: Res>, ) { if let Some(set) = font_atlas_sets.get(&state.handle.cast_weak::()) { if let Some((_size, font_atlas)) = set.iter().next() { let x_offset = state.atlas_count as f32; if state.atlas_count == font_atlas.len() as u32 { return; } let texture_atlas = texture_atlases .get(&font_atlas[state.atlas_count as usize].texture_atlas) .unwrap(); state.atlas_count += 1; commands.spawn(ImageBundle { image: texture_atlas.texture.clone().into(), style: Style { position_type: PositionType::Absolute, top: Val::Px(0.0), left: Val::Px(512.0 * x_offset), ..default() }, ..default() }); } } } fn text_update_system(mut state: ResMut, time: Res