-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4565ac9
commit d0f557b
Showing
4 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters