many_text2d/
many_text2d.rs1use std::ops::RangeInclusive;
4
5use bevy::{
6 camera::visibility::NoFrustumCulling,
7 diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
8 prelude::*,
9 text::FontAtlasSet,
10 window::{PresentMode, WindowResolution},
11 winit::WinitSettings,
12};
13
14use argh::FromArgs;
15use chacha20::ChaCha8Rng;
16use rand::{
17 seq::{IndexedRandom, IteratorRandom},
18 RngExt, SeedableRng,
19};
20
21const CAMERA_SPEED: f32 = 1000.0;
22
23const CODE_POINT_RANGES: [RangeInclusive<u32>; 5] = [
25 0x20..=0x7e,
26 0xa0..=0x17e,
27 0x180..=0x2b2,
28 0x3f0..=0x479,
29 0x48a..=0x52f,
30];
31
32#[derive(FromArgs, Resource)]
33struct Args {
35 #[argh(switch)]
37 many_glyphs: bool,
38
39 #[argh(switch)]
41 many_font_sizes: bool,
42
43 #[argh(switch)]
45 recompute: bool,
46
47 #[argh(switch)]
49 no_frustum_culling: bool,
50
51 #[argh(switch)]
53 center: bool,
54}
55
56#[derive(Resource)]
57struct FontHandle(Handle<Font>);
58impl FromWorld for FontHandle {
59 fn from_world(world: &mut World) -> Self {
60 Self(world.load_asset("fonts/FiraSans-Bold.ttf"))
61 }
62}
63
64fn main() {
65 #[cfg(not(target_arch = "wasm32"))]
67 let args: Args = argh::from_env();
68 #[cfg(target_arch = "wasm32")]
69 let args = Args::from_args(&[], &[]).unwrap();
70
71 let mut app = App::new();
72
73 app.add_plugins((
74 FrameTimeDiagnosticsPlugin::default(),
75 LogDiagnosticsPlugin::default(),
76 DefaultPlugins.set(WindowPlugin {
77 primary_window: Some(Window {
78 present_mode: PresentMode::AutoNoVsync,
79 resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
80 ..default()
81 }),
82 ..default()
83 }),
84 ))
85 .insert_resource(WinitSettings::continuous())
86 .init_resource::<FontHandle>()
87 .add_systems(Startup, setup)
88 .add_systems(Update, (move_camera, print_counts));
89
90 if args.recompute {
91 app.add_systems(Update, recompute);
92 }
93
94 app.insert_resource(args).run();
95}
96
97#[derive(Deref, DerefMut)]
98struct PrintingTimer(Timer);
99
100impl Default for PrintingTimer {
101 fn default() -> Self {
102 Self(Timer::from_seconds(1.0, TimerMode::Repeating))
103 }
104}
105
106fn setup(mut commands: Commands, font: Res<FontHandle>, args: Res<Args>) {
107 warn!(include_str!("warning_string.txt"));
108
109 let mut rng = ChaCha8Rng::seed_from_u64(42);
110
111 let tile_size = Vec2::splat(64.0);
112 let map_size = Vec2::splat(640.0);
113
114 let half_x = (map_size.x / 4.0) as i32;
115 let half_y = (map_size.y / 4.0) as i32;
116
117 commands.spawn(Camera2d);
120
121 let mut text2ds = vec![];
124 for y in -half_y..half_y {
125 for x in -half_x..half_x {
126 let position = Vec2::new(x as f32, y as f32);
127 let translation = (position * tile_size).extend(rng.random::<f32>());
128 let rotation = Quat::from_rotation_z(rng.random::<f32>());
129 let scale = Vec3::splat(rng.random::<f32>() * 2.0);
130 let color = Hsla::hsl(rng.random_range(0.0..360.0), 0.8, 0.8);
131
132 text2ds.push((
133 Text2d(random_text(&mut rng, &args)),
134 random_text_font(&mut rng, &args, font.0.clone()),
135 TextColor(color.into()),
136 TextLayout::justify(if args.center {
137 Justify::Center
138 } else {
139 Justify::Left
140 }),
141 Transform {
142 translation,
143 rotation,
144 scale,
145 },
146 ));
147 }
148 }
149
150 if args.no_frustum_culling {
151 let bundles = text2ds.into_iter().map(|bundle| (bundle, NoFrustumCulling));
152 commands.spawn_batch(bundles);
153 } else {
154 commands.spawn_batch(text2ds);
155 }
156}
157
158fn move_camera(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
160 let Ok(mut camera_transform) = camera_query.single_mut() else {
161 return;
162 };
163 camera_transform.rotate_z(time.delta_secs() * 0.5);
164 *camera_transform =
165 *camera_transform * Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());
166}
167
168fn print_counts(
170 time: Res<Time>,
171 mut timer: Local<PrintingTimer>,
172 texts: Query<&ViewVisibility, With<Text2d>>,
173 font_atlas_set: Res<FontAtlasSet>,
174 images: Res<Assets<Image>>,
175) {
176 timer.tick(time.delta());
177 if !timer.just_finished() {
178 return;
179 }
180
181 let num_atlases = font_atlas_set
182 .iter()
183 .map(|(_, atlases)| atlases.len())
186 .sum::<usize>();
187
188 let visible_texts = texts.iter().filter(|visibility| visibility.get()).count();
189
190 info!(
191 "Texts: {} Visible: {} Atlases: {} Bytes: {}",
192 texts.iter().count(),
193 visible_texts,
194 num_atlases,
195 font_atlas_set.total_bytes(images.as_ref())
196 );
197}
198
199fn random_text_font(rng: &mut ChaCha8Rng, args: &Args, font: Handle<Font>) -> TextFont {
200 let font_size = FontSize::Px(if args.many_font_sizes {
201 *[10.0, 20.0, 30.0, 40.0, 50.0, 60.0].choose(rng).unwrap()
202 } else {
203 60.0
204 });
205
206 TextFont {
207 font_size,
208 font: font.into(),
209 ..default()
210 }
211}
212
213fn random_text(rng: &mut ChaCha8Rng, args: &Args) -> String {
214 if !args.many_glyphs {
215 return "Bevy".to_string();
216 }
217
218 CODE_POINT_RANGES
219 .choose(rng)
220 .unwrap()
221 .clone()
222 .sample(rng, 4)
223 .into_iter()
224 .map(|cp| char::from_u32(cp).unwrap())
225 .collect::<String>()
226}
227
228fn recompute(mut query: Query<&mut Text2d>) {
229 for mut text2d in &mut query {
230 text2d.set_changed();
231 }
232}