Add screenshot function (F10).
authorBernie Innocenti <bernie@codewiz.org>
Sat, 8 Apr 2017 22:49:26 +0000 (18:49 -0400)
committerBernie Innocenti <bernie@codewiz.org>
Sat, 8 Apr 2017 22:49:26 +0000 (18:49 -0400)
Cargo.toml
main.rs

index a858c2fb0bcaf798989304961f2f7bf971578d30..5c122ed1a6ebdbf199998d69c01256a1214e42af 100644 (file)
@@ -12,6 +12,7 @@ panic = 'abort'
 glium = "*"
 glutin = "*"
 genmesh = "0.4.1"
+image = "0.12.0"
 obj = { version = "0.5", features = ["usegenmesh"] }
 libxm = "1.0.0"
 sdl2 = "*"
diff --git a/main.rs b/main.rs
index 71495b41d09f310d0d051ae51e5bff0b78b4677f..d7e8c00052a2bfcf9bcd6d57683a7ec4c2847fad 100644 (file)
--- a/main.rs
+++ b/main.rs
@@ -4,6 +4,7 @@
 
 extern crate glium;
 extern crate glutin;
+extern crate image;
 extern crate libxm;
 extern crate sdl2;
 
@@ -19,6 +20,14 @@ mod mandelwow;
 mod sound;
 mod support;
 
+fn screenshot(display : &glium::Display) {
+    let image: glium::texture::RawImage2d<u8> = display.read_front_buffer();
+    let image = image::ImageBuffer::from_raw(image.width, image.height, image.data.into_owned()).unwrap();
+    let image = image::DynamicImage::ImageRgba8(image).flipv();
+    let mut output = std::fs::File::create(&std::path::Path::new("screenshot.png")).unwrap();
+    image.save(&mut output, image::ImageFormat::PNG).unwrap();
+}
+
 fn main() {
     let _soundplayer = sound::start();
 
@@ -121,6 +130,9 @@ fn main() {
                 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
                     t -= 0.01;
                 },
+                KeyboardInput(Pressed, _, Some(VirtualKeyCode::F10)) => {
+                    screenshot(&display);
+                },
                 ev => camera.process_input(&ev),
             }
         }