Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/stress_tests/many_glyphs.rs
6592 views
1
//! Simple text rendering benchmark.
2
//!
3
//! Creates a text block with a single span containing `100_000` glyphs,
4
//! and renders it with the UI in a white color and with Text2d in a red color.
5
//!
6
//! To recompute all text each frame run
7
//! `cargo run --example many_glyphs --release recompute-text`
8
use argh::FromArgs;
9
use bevy::{
10
color::palettes::basic::RED,
11
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
12
prelude::*,
13
text::{LineBreak, TextBounds},
14
window::{PresentMode, WindowResolution},
15
winit::{UpdateMode, WinitSettings},
16
};
17
18
#[derive(FromArgs, Resource)]
19
/// `many_glyphs` stress test
20
struct Args {
21
/// don't draw the UI text.
22
#[argh(switch)]
23
no_ui: bool,
24
25
/// don't draw the Text2d text.
26
#[argh(switch)]
27
no_text2d: bool,
28
29
/// whether to force the text to recompute every frame by triggering change detection.
30
#[argh(switch)]
31
recompute_text: bool,
32
}
33
34
fn main() {
35
// `from_env` panics on the web
36
#[cfg(not(target_arch = "wasm32"))]
37
let args: Args = argh::from_env();
38
#[cfg(target_arch = "wasm32")]
39
let args = Args::from_args(&[], &[]).unwrap();
40
41
let mut app = App::new();
42
app.add_plugins((
43
DefaultPlugins.set(WindowPlugin {
44
primary_window: Some(Window {
45
present_mode: PresentMode::AutoNoVsync,
46
resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
47
..default()
48
}),
49
..default()
50
}),
51
FrameTimeDiagnosticsPlugin::default(),
52
LogDiagnosticsPlugin::default(),
53
))
54
.insert_resource(WinitSettings {
55
focused_mode: UpdateMode::Continuous,
56
unfocused_mode: UpdateMode::Continuous,
57
})
58
.add_systems(Startup, setup);
59
60
if args.recompute_text {
61
app.add_systems(Update, force_text_recomputation);
62
}
63
64
app.insert_resource(args).run();
65
}
66
67
fn setup(mut commands: Commands, args: Res<Args>) {
68
warn!(include_str!("warning_string.txt"));
69
70
commands.spawn(Camera2d);
71
let text_string = "0123456789".repeat(10_000);
72
let text_font = TextFont {
73
font_size: 4.,
74
..Default::default()
75
};
76
let text_block = TextLayout {
77
justify: Justify::Left,
78
linebreak: LineBreak::AnyCharacter,
79
};
80
81
if !args.no_ui {
82
commands
83
.spawn(Node {
84
width: percent(100),
85
align_items: AlignItems::Center,
86
justify_content: JustifyContent::Center,
87
..default()
88
})
89
.with_children(|commands| {
90
commands
91
.spawn(Node {
92
width: px(1000),
93
..Default::default()
94
})
95
.with_child((Text(text_string.clone()), text_font.clone(), text_block));
96
});
97
}
98
99
if !args.no_text2d {
100
commands.spawn((
101
Text2d::new(text_string),
102
text_font.clone(),
103
TextColor(RED.into()),
104
bevy::sprite::Anchor::CENTER,
105
TextBounds::new_horizontal(1000.),
106
text_block,
107
));
108
}
109
}
110
111
fn force_text_recomputation(mut text_query: Query<&mut TextLayout>) {
112
for mut block in &mut text_query {
113
block.set_changed();
114
}
115
}
116
117