From 92a21a9fce0cf40362a59507bd2ff0546e7e8b17 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 14 Aug 2020 17:25:20 +0800 Subject: [PATCH] graphics: initial version Signed-off-by: Sean Cross --- examples/graphics/Cargo.toml | 15 +++++++++ examples/graphics/src/backend/minifb.rs | 44 +++++++++++++++++++++++++ examples/graphics/src/backend/mod.rs | 4 +++ examples/graphics/src/main.rs | 10 ++++++ 4 files changed, 73 insertions(+) create mode 100644 examples/graphics/Cargo.toml create mode 100644 examples/graphics/src/backend/minifb.rs create mode 100644 examples/graphics/src/backend/mod.rs create mode 100644 examples/graphics/src/main.rs diff --git a/examples/graphics/Cargo.toml b/examples/graphics/Cargo.toml new file mode 100644 index 000000000..36f719bf5 --- /dev/null +++ b/examples/graphics/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "graphics" +version = "0.1.0" +authors = ["Sean Cross "] +edition = "2018" +description = "Manage the video output" + +[dependencies] +xous = { path = "../../xous-rs" } + +[target.'cfg(not(target_os = "none"))'.dependencies] +minifb = "0.17" +#pixels = "0.1" + +rand = "*" diff --git a/examples/graphics/src/backend/minifb.rs b/examples/graphics/src/backend/minifb.rs new file mode 100644 index 000000000..995c43ac3 --- /dev/null +++ b/examples/graphics/src/backend/minifb.rs @@ -0,0 +1,44 @@ +use minifb::{Key, Window, WindowOptions}; + +const WIDTH: usize = 336; +const HEIGHT: usize = 536; +const MAX_FPS: u64 = 10; +const LIGHT_COLOUR: u32 = 0xB5B5AD; +const DARK_COLOUR: u32 = 0x1B1B19; + +use rand::prelude::*; + +pub fn run() { + let mut buffer: Vec = vec![0; WIDTH * HEIGHT]; + + let mut window = Window::new( + "Betrusted", + WIDTH, + HEIGHT, + WindowOptions { + scale_mode: minifb::ScaleMode::AspectRatioStretch, + resize: true, + ..WindowOptions::default() + }, + ) + .unwrap_or_else(|e| { + panic!("{}", e); + }); + + // Limit the maximum refresh rate + window.limit_update_rate(Some(std::time::Duration::from_micros( + 1000 * 1000 / MAX_FPS, + ))); + + let mut rng = rand::thread_rng(); + while window.is_open() && !window.is_key_down(Key::Escape) { + for i in buffer.iter_mut() { + *i = if rng.gen() { LIGHT_COLOUR } else { DARK_COLOUR }; + } + + // We unwrap here as we want this code to exit if it fails. + // Real applications may want to handle this in a different way + window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap(); + } + std::process::exit(0); +} diff --git a/examples/graphics/src/backend/mod.rs b/examples/graphics/src/backend/mod.rs new file mode 100644 index 000000000..afb23a4d4 --- /dev/null +++ b/examples/graphics/src/backend/mod.rs @@ -0,0 +1,4 @@ +#[cfg(not(target_os = "none"))] +mod minifb; +#[cfg(not(target_os = "none"))] +pub use crate::backend::minifb::*; diff --git a/examples/graphics/src/main.rs b/examples/graphics/src/main.rs new file mode 100644 index 000000000..694556664 --- /dev/null +++ b/examples/graphics/src/main.rs @@ -0,0 +1,10 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +mod backend; + +#[xous::xous_main] +fn xmain() -> ! { + backend::run(); + loop {}; +}