Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/stress_tests/text_pipeline.rs
6592 views
1
//! Text pipeline benchmark.
2
//!
3
//! Continuously recomputes a large block of text with 100 text spans.
4
5
use bevy::{
6
color::palettes::basic::{BLUE, YELLOW},
7
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
8
prelude::*,
9
text::{LineBreak, TextBounds},
10
window::{PresentMode, WindowResolution},
11
winit::{UpdateMode, WinitSettings},
12
};
13
14
fn main() {
15
App::new()
16
.add_plugins((
17
DefaultPlugins.set(WindowPlugin {
18
primary_window: Some(Window {
19
present_mode: PresentMode::AutoNoVsync,
20
resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
21
..default()
22
}),
23
..default()
24
}),
25
FrameTimeDiagnosticsPlugin::default(),
26
LogDiagnosticsPlugin::default(),
27
))
28
.insert_resource(WinitSettings {
29
focused_mode: UpdateMode::Continuous,
30
unfocused_mode: UpdateMode::Continuous,
31
})
32
.add_systems(Startup, spawn)
33
.add_systems(Update, update_text_bounds)
34
.run();
35
}
36
37
fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
38
warn!(include_str!("warning_string.txt"));
39
40
commands.spawn(Camera2d);
41
42
let make_spans = |i| {
43
[
44
(
45
TextSpan("text".repeat(i)),
46
TextFont {
47
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
48
font_size: (4 + i % 10) as f32,
49
..Default::default()
50
},
51
TextColor(BLUE.into()),
52
),
53
(
54
TextSpan("pipeline".repeat(i)),
55
TextFont {
56
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
57
font_size: (4 + i % 11) as f32,
58
..default()
59
},
60
TextColor(YELLOW.into()),
61
),
62
]
63
};
64
65
let spans = (1..50).flat_map(|i| make_spans(i).into_iter());
66
67
commands
68
.spawn((
69
Text2d::default(),
70
TextLayout {
71
justify: Justify::Center,
72
linebreak: LineBreak::AnyCharacter,
73
},
74
TextBounds::default(),
75
))
76
.with_children(|p| {
77
for span in spans {
78
p.spawn(span);
79
}
80
});
81
}
82
83
// changing the bounds of the text will cause a recomputation
84
fn update_text_bounds(time: Res<Time>, mut text_bounds_query: Query<&mut TextBounds>) {
85
let width = (1. + ops::sin(time.elapsed_secs())) * 600.0;
86
for mut text_bounds in text_bounds_query.iter_mut() {
87
text_bounds.width = Some(width);
88
}
89
}
90
91