Move screenshotting code to its own module
authorBernie Innocenti <codewiz@google.com>
Sun, 29 Mar 2020 13:11:14 +0000 (22:11 +0900)
committerBernie Innocenti <codewiz@google.com>
Sun, 29 Mar 2020 13:11:14 +0000 (22:11 +0900)
lib.rs
main.rs
screenshot.rs [new file with mode: 0644]

diff --git a/lib.rs b/lib.rs
index 24ef697840c82b47f266cf512ed8310939f7f831..296f5b92787e6a500ecc70067a0077ba3d9b32ac 100644 (file)
--- a/lib.rs
+++ b/lib.rs
@@ -2,6 +2,7 @@ pub mod bounding_box;
 pub mod cube;
 pub mod mandelwow;
 pub mod shaded_cube;
+pub mod screenshot;
 pub mod sound;
 pub mod support;
 pub mod text;
@@ -12,17 +13,3 @@ pub use crate::cube::Cube;
 pub use crate::shaded_cube::ShadedCube;
 pub use crate::text::Text;
 pub use crate::timer::Timer;
-
-#[cfg(feature = "image")]
-pub fn screenshot(display : &glium::Display) {
-    let image: glium::texture::RawImage2d<'_, u8> = display.read_front_buffer().unwrap();
-    let image = image::ImageBuffer::from_raw(image.width, image.height, image.data.into_owned()).unwrap();
-    let image = image::DynamicImage::ImageRgba8(image).flipv().to_rgb();
-    let image = image::DynamicImage::ImageRgb8(image);
-    let mut output = std::fs::File::create(&std::path::Path::new("screenshot.png")).unwrap();
-    image.write_to(&mut output, image::ImageFormat::PNG).unwrap();
-}
-
-#[cfg(not(feature = "image"))]
-pub fn screenshot(_ : &glium::Display) {
-}
diff --git a/main.rs b/main.rs
index 2bdcae815801a7a1958ed770f282be4e593c4d00..86421b4941a7c1c68b1462ef3e26d13e6168df9b 100644 (file)
--- a/main.rs
+++ b/main.rs
@@ -72,6 +72,7 @@ impl World {
         println!("xstep={} ystep={:?}", sea_xstep, sea_zstep);
 
         let mut sea = [[Vector3::zero(); SEA_ZSIZE]; SEA_XSIZE];
+        #[allow(clippy::needless_range_loop)]
         for x in 0..SEA_XSIZE {
             for z in 0..SEA_ZSIZE {
                 sea[x][z] = Vector3 {
@@ -85,11 +86,11 @@ impl World {
         World {
             mandelwow_program,
             mandelwow_bbox: BoundingBox::new(
-                display, &mandelwow_bounds, bounding_box_program.clone()),
+                display, &mandelwow_bounds, bounding_box_program),
             mandelwow_bounds,
             bounding_box_enabled: true,
 
-            shaded_cube: ShadedCube::new(display, shaded_program.clone()),
+            shaded_cube: ShadedCube::new(display, shaded_program),
             text: text::Text::new(display),
             sea,
 
@@ -310,7 +311,7 @@ fn main() {
                                     VirtualKeyCode::P => timer.pause ^= true,
                                     VirtualKeyCode::PageUp => timer.t += 0.1,
                                     VirtualKeyCode::PageDown => timer.t -= 0.2,
-                                    VirtualKeyCode::F10 => screenshot(&display),
+                                    VirtualKeyCode::F10 => screenshot::take_screenshot(&display),
                                     VirtualKeyCode::F11 | VirtualKeyCode::Return => {
                                         fullscreen ^= true;
                                         let fs = if fullscreen {
diff --git a/screenshot.rs b/screenshot.rs
new file mode 100644 (file)
index 0000000..19f2b13
--- /dev/null
@@ -0,0 +1,13 @@
+#[cfg(feature = "image")]
+pub fn take_screenshot(display : &glium::Display) {
+    let image: glium::texture::RawImage2d<'_, u8> = display.read_front_buffer().unwrap();
+    let image = image::ImageBuffer::from_raw(image.width, image.height, image.data.into_owned()).unwrap();
+    let image = image::DynamicImage::ImageRgba8(image).flipv().to_rgb();
+    let image = image::DynamicImage::ImageRgb8(image);
+    let mut output = std::fs::File::create(&std::path::Path::new("screenshot.png")).unwrap();
+    image.write_to(&mut output, image::ImageFormat::PNG).unwrap();
+}
+
+#[cfg(not(feature = "image"))]
+pub fn take_screenshot(_ : &glium::Display) {
+}