Repository navigation
Expand file tree
/
Copy pathframepace.rs
More file actions
76 lines (68 loc) · 2.24 KB
/
Copy pathframepace.rs
File metadata and controls
76 lines (68 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! This examples shows how to use bevy's frame pacing and frame limiting
//!
//! The demo should also let you feel the effect of enabling it
use bevy::{
color::palettes,
diagnostic::LogDiagnosticsPlugin,
prelude::*,
render::framepace::{
debug::FramepaceDiagnosticsPlugin, FramepacePlugin, FramepaceSettings, Limiter,
},
window::{CursorIcon, SystemCursorIcon, Window},
};
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
LogDiagnosticsPlugin::default(),
FramepacePlugin,
FramepaceDiagnosticsPlugin,
))
.add_systems(Startup, setup)
.add_systems(Update, (toggle_plugin, update_ui, update_cursor))
.run();
}
#[derive(Component)]
struct EnableText;
fn toggle_plugin(mut settings: ResMut<FramepaceSettings>, input: Res<ButtonInput<KeyCode>>) {
if input.just_pressed(KeyCode::Space) {
settings.limiter = match settings.limiter {
Limiter::Auto => Limiter::Off,
Limiter::Off => Limiter::from_framerate(30.0),
Limiter::Manual(_) => Limiter::Auto,
}
}
}
fn update_ui(mut text: Single<&mut TextSpan, With<EnableText>>, settings: Res<FramepaceSettings>) {
text.0 = format!("{}", settings.limiter);
}
fn update_cursor(window: Single<&Window>, mut gizmos: Gizmos) {
if let Some(pos) = window.cursor_position() {
let pos = Vec2::new(pos.x - window.width() / 2.0, window.height() / 2.0 - pos.y);
gizmos.circle_2d(pos, 10.0, palettes::basic::GREEN);
}
}
/// set up the scene
fn setup(mut commands: Commands, window: Single<Entity, With<Window>>) {
commands
.entity(*window)
.insert(CursorIcon::System(SystemCursorIcon::Crosshair));
commands.spawn((
Camera2d,
Camera {
order: 10,
..default()
},
));
commands.spawn(Camera3d::default());
// UI
let text_font = TextFont {
font_size: FontSize::Px(50.0),
..default()
};
commands
.spawn(Text::default())
.with_child((TextSpan::new("Frame pacing: "), text_font.clone()))
.with_child((TextSpan::new(""), text_font.clone(), EnableText))
.with_child((TextSpan::new("\n[press space]"), text_font));
}