Skip to content

Commit 945c0a6

Browse files
committed
feat: Add screenshot taking functionality
1 parent 7b01f1d commit 945c0a6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/world/camera.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use bevy::prelude::*;
22
use bevy::render::camera::ScalingMode;
3+
#[cfg(not(target_arch = "wasm32"))]
4+
use bevy::render::view::screenshot::ScreenshotManager;
35
use bevy::window::{PrimaryWindow, WindowMode};
46
use bevy_kira_audio::prelude::AudioReceiver;
57
use bevy_rapier2d::dynamics::Velocity;
@@ -100,6 +102,25 @@ fn toggle_full_screen(
100102
}
101103
}
102104

105+
#[cfg(not(target_arch = "wasm32"))]
106+
fn take_screenshot(
107+
keys: Res<Input<KeyCode>>,
108+
main_window: Query<Entity, With<PrimaryWindow>>,
109+
mut screenshot_manager: ResMut<ScreenshotManager>,
110+
mut counter: Local<u32>,
111+
) {
112+
if !keys.just_pressed(KeyCode::F12) {
113+
return;
114+
}
115+
116+
let path = format!("./screenshot-{}.png", *counter);
117+
*counter += 1;
118+
match screenshot_manager.save_screenshot_to_disk(main_window.single(), path) {
119+
Ok(()) => {}
120+
Err(err) => error!("failed to take screenshot, {}", err),
121+
}
122+
}
123+
103124
pub struct CameraPlugin;
104125

105126
impl Plugin for CameraPlugin {
@@ -109,6 +130,8 @@ impl Plugin for CameraPlugin {
109130
(
110131
#[cfg(not(target_arch = "wasm32"))]
111132
toggle_full_screen,
133+
#[cfg(not(target_arch = "wasm32"))]
134+
take_screenshot,
112135
apply_y_sort,
113136
zoom_camera,
114137
),

0 commit comments

Comments
 (0)