Skip to content

Commit

Permalink
Add piston backend: In progress
Browse files Browse the repository at this point in the history
  • Loading branch information
malikolivier committed Mar 12, 2018
1 parent 4565ac9 commit d0f557b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ authors = ["Malik Olivier Boussejra <malik@boussejra.com>"]
matplotrs_backend = { path = "./backend" }
matplotrs_amethyst_backend = { path = "./amethyst_backend", optional = true }
matplotrs_conrod_backend = { path = "./conrod_backend", optional = true }
matplotrs_piston_backend = { path = "./piston_backend", optional = true }
matplotrs_printpdf_backend = { path = "./printpdf_backend", optional = true }

[features]
amethyst = ["matplotrs_amethyst_backend"]
conrod = ["matplotrs_conrod_backend"]
piston = ["matplotrs_piston_backend"]
printpdf = ["matplotrs_printpdf_backend"]

[dev-dependencies]
Expand Down
11 changes: 11 additions & 0 deletions piston_backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "matplotrs_piston_backend"
version = "0.1.0"
authors = ["Malik Olivier Boussejra <malik@boussejra.com>"]

[dependencies]
matplotrs_backend = { path = "../backend" }
piston = "0.36.0"
piston2d-graphics = "0.26.0"
pistoncore-glutin_window = "0.45.0"
piston2d-opengl_graphics = "0.52.0"
123 changes: 123 additions & 0 deletions piston_backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
extern crate matplotrs_backend;

extern crate glutin_window;
extern crate graphics;
extern crate opengl_graphics;
extern crate piston;

use piston::window::WindowSettings;
use piston::event_loop::*;
use piston::input::*;
use glutin_window::GlutinWindow as Window;
use opengl_graphics::{GlGraphics, OpenGL};

pub struct PistonBackend {
figures: Vec<Figure>,
}

struct Figure {
w: Window,
gl: GlGraphics, // OpenGL drawing backend.
rotation: f64, // Rotation for the square.
}

impl Figure {
fn render(&mut self, args: &RenderArgs) {
use graphics::*;

const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];

let square = rectangle::square(0.0, 0.0, 50.0);
let rotation = self.rotation;
let (x, y) = ((args.width / 2) as f64, (args.height / 2) as f64);

self.gl.draw(args.viewport(), |c, gl| {
// Clear the screen.
clear(GREEN, gl);

let transform = c.transform
.trans(x, y)
.rot_rad(rotation)
.trans(-25.0, -25.0);

// Draw a box rotating around the middle of the screen.
rectangle(RED, square, transform, gl);
});
}

fn update(&mut self, args: &UpdateArgs) {
// Rotate 2 radians per second.
self.rotation += 2.0 * args.dt;
}
}

#[derive(Debug)]
pub enum PistonError {
BackEndError(String),
}

// Change this to OpenGL::V2_1 if not working.
const OPENGL_VERSION: OpenGL = OpenGL::V3_2;

impl matplotrs_backend::Backend for PistonBackend {
type Err = PistonError;
fn new() -> Self {
PistonBackend {
figures: Vec::new(),
}
}

fn new_figure(&mut self, title: &str, size: &(f64, f64)) -> Result<(), Self::Err> {
let &(x, y) = size;
let window = WindowSettings::new(
title,
[x as u32, y as u32]
)
.opengl(OPENGL_VERSION)
// ↓ Required for this bug https://github.com/PistonDevelopers/piston/issues/1202
.srgb(false)
.exit_on_esc(true)
.build()?;
self.figures.push(Figure {
w: window,
gl: GlGraphics::new(OPENGL_VERSION),
rotation: 0.0,
});
Ok(())
}

fn draw_path(&mut self, _: &matplotrs_backend::Path) -> Result<(), Self::Err> {
Ok(())
}

fn draw_text(&mut self, _: &matplotrs_backend::Text) -> Result<(), Self::Err> {
Ok(())
}

fn draw_image(&mut self, _: &matplotrs_backend::Image) -> Result<(), Self::Err> {
Ok(())
}

fn show(mut self) -> Result<i32, Self::Err> {
let mut events = Events::new(EventSettings::new());
for figure in self.figures.iter_mut() {
while let Some(e) = events.next(&mut figure.w) {
if let Some(r) = e.render_args() {
figure.render(&r);
}

if let Some(u) = e.update_args() {
figure.update(&u);
}
}
}
Ok(0)
}
}

impl From<String> for PistonError {
fn from(err: String) -> Self {
PistonError::BackEndError(err)
}
}
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ pub mod backend {
pub type Backend = backend::PrintPdfBackend;
}

#[cfg(not(any(feature = "amethyst", feature = "printpdf")))]
#[cfg(feature = "piston")]
pub mod backend {
pub extern crate matplotrs_piston_backend as backend;
pub type Backend = backend::PistonBackend;
}

#[cfg(not(any(feature = "amethyst", feature = "printpdf", feature = "piston")))]
pub mod backend {
mod dummy;
pub type Backend = dummy::DummyBackend;
Expand Down

0 comments on commit d0f557b

Please sign in to comment.