Skip to content

Commit

Permalink
Merge pull request #41 from bvssvni/no_boilerplate
Browse files Browse the repository at this point in the history
No boilerplate
  • Loading branch information
bvssvni committed Jun 5, 2015
2 parents a46969d + 937419c commit 87bc5d4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 46 deletions.
10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "piston_window"
version = "0.1.0"
version = "0.2.0"
authors = ["bvssvni <[email protected]>"]
keywords = ["window", "piston"]
description = "The official Piston window back-end for the Piston game engine"
Expand All @@ -19,10 +19,8 @@ name = "piston_window"
[dependencies]
gfx = "0.6.1"
gfx_device_gl = "0.4.0"
piston = "0.1.4"
piston = "0.1.5"
piston2d-gfx_graphics = "0.1.22"
piston2d-graphics = "0.1.3"

[dev-dependencies]
pistoncore-glutin_window = "0.1.0"

shader_version = "0.1.0"
pistoncore-glutin_window = "0.2.0"
21 changes: 4 additions & 17 deletions examples/hello_piston.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
extern crate piston_window;
extern crate glutin_window;
extern crate piston;
extern crate graphics;

use std::cell::RefCell;
use std::rc::Rc;
use glutin_window::{ OpenGL, GlutinWindow };
use piston_window::*;
use piston::window::WindowSettings;

use piston::event::*;
use graphics::*;
use piston::input::*;

fn main() {
let window = Rc::new(RefCell::new(GlutinWindow::new(
OpenGL::_3_2,
WindowSettings::new("Hello Piston!", [640, 480])
.exit_on_esc(true)
)));
let window: PistonWindow = WindowSettings::new("Hello Piston!", [640, 480])
.exit_on_esc(true)
.into();
println!("Press any button to enter inner loop");
for e in PistonWindow::new(window, empty_app()) {
for e in window {
e.draw_2d(|_c, g| {
clear([0.5, 1.0, 0.5, 1.0], g);
});
Expand Down
66 changes: 43 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,32 @@ extern crate gfx;
extern crate gfx_device_gl;
extern crate gfx_graphics;
extern crate graphics;
extern crate shader_version;
extern crate glutin_window;

use glutin_window::GlutinWindow;
pub use shader_version::OpenGL;
pub use graphics::*;
pub use piston::window::*;
pub use piston::event::*;
pub use piston::input::*;

use std::cell::RefCell;
use std::rc::Rc;
use std::any::Any;

use piston::{ event, window };
use gfx::traits::*;
use gfx_graphics::{ Gfx2d, GfxGraphics };
use graphics::Context;

/// Actual gfx::Stream implementation carried by the window.
pub type GfxStream = gfx::OwnedStream<gfx_device_gl::Device, gfx_device_gl::Output>;
/// Glyph cache.
type Glyphs = gfx_graphics::GlyphCache<gfx_device_gl::Resources, gfx_device_gl::Factory>;
/// 2D graphics.
type G2d<'a> = GfxGraphics<'a, gfx_device_gl::Resources, gfx_device_gl::CommandBuffer, gfx_device_gl::Output>;

/// Contains everything required for controlling window, graphics, event loop.
pub struct PistonWindow<W: window::Window, T = ()> {
pub struct PistonWindow<W: Window = GlutinWindow, T = ()> {
/// The window.
pub window: Rc<RefCell<W>>,
/// GFX stream.
Expand All @@ -31,17 +42,26 @@ pub struct PistonWindow<W: window::Window, T = ()> {
/// Gfx2d.
pub g2d: Rc<RefCell<Gfx2d<gfx_device_gl::Resources>>>,
/// The event loop.
pub events: Rc<RefCell<event::WindowEvents<W, event::Event<W::Event>>>>,
pub events: Rc<RefCell<WindowEvents<W, Event<W::Event>>>>,
/// The event.
pub event: Option<event::Event<W::Event>>,
pub event: Option<Event<W::Event>>,
/// Application structure.
pub app: Rc<RefCell<T>>,
/// The factory that was created along with the device.
pub factory: Rc<RefCell<gfx_device_gl::Factory>>,
}

impl<T> From<WindowSettings> for PistonWindow<T>
where T: Window + OpenGLWindow + From<WindowSettings>,
T::Event: GenericEvent
{
fn from(settings: WindowSettings) -> PistonWindow<T> {
PistonWindow::new(Rc::new(RefCell::new(settings.into())), empty_app())
}
}

impl<W, T> Clone for PistonWindow<W, T>
where W: window::Window, W::Event: Clone
where W: Window, W::Event: Clone
{
fn clone(&self) -> Self {
PistonWindow {
Expand All @@ -58,11 +78,11 @@ impl<W, T> Clone for PistonWindow<W, T>
}

impl<W, T> PistonWindow<W, T>
where W: window::Window, W::Event: event::GenericEvent
where W: Window, W::Event: GenericEvent
{
/// Creates a new piston object.
pub fn new(window: Rc<RefCell<W>>, app: Rc<RefCell<T>>) -> Self
where W: window::OpenGLWindow
where W: OpenGLWindow
{
use piston::event::Events;
use piston::window::{ OpenGLWindow, Window };
Expand Down Expand Up @@ -140,7 +160,7 @@ impl<W, T> PistonWindow<W, T>
}

impl<W, T> Iterator for PistonWindow<W, T>
where W: window::Window, W::Event: event::GenericEvent
where W: Window, W::Event: GenericEvent
{
type Item = PistonWindow<W, T>;

Expand Down Expand Up @@ -174,13 +194,13 @@ impl<W, T> Iterator for PistonWindow<W, T>
}
}

impl<W, T> event::GenericEvent for PistonWindow<W, T>
where W: window::Window, W::Event: event::GenericEvent
impl<W, T> GenericEvent for PistonWindow<W, T>
where W: Window, W::Event: GenericEvent
{
fn event_id(&self) -> event::EventId {
fn event_id(&self) -> EventId {
match self.event {
Some(ref e) => e.event_id(),
None => event::EventId("")
None => EventId("")
}
}

Expand All @@ -190,9 +210,9 @@ impl<W, T> event::GenericEvent for PistonWindow<W, T>
self.event.as_ref().unwrap().with_args(f)
}

fn from_args(event_id: event::EventId, any: &Any, old_event: &Self) -> Option<Self> {
fn from_args(event_id: EventId, any: &Any, old_event: &Self) -> Option<Self> {
if let Some(ref e) = old_event.event {
match event::GenericEvent::from_args(event_id, any, e) {
match GenericEvent::from_args(event_id, any, e) {
Some(e) => {
Some(PistonWindow {
window: old_event.window.clone(),
Expand All @@ -211,22 +231,22 @@ impl<W, T> event::GenericEvent for PistonWindow<W, T>
}
}

impl<W, T> window::Window for PistonWindow<W, T>
where W: window::Window
impl<W, T> Window for PistonWindow<W, T>
where W: Window
{
type Event = <W as window::Window>::Event;
type Event = <W as Window>::Event;

fn should_close(&self) -> bool { self.window.borrow().should_close() }
fn size(&self) -> window::Size { self.window.borrow().size() }
fn draw_size(&self) -> window::Size { self.window.borrow().draw_size() }
fn size(&self) -> Size { self.window.borrow().size() }
fn draw_size(&self) -> Size { self.window.borrow().draw_size() }
fn swap_buffers(&mut self) { self.window.borrow_mut().swap_buffers() }
fn poll_event(&mut self) -> Option<Self::Event> {
window::Window::poll_event(&mut *self.window.borrow_mut())
Window::poll_event(&mut *self.window.borrow_mut())
}
}

impl<W, T> window::AdvancedWindow for PistonWindow<W, T>
where W: window::AdvancedWindow
impl<W, T> AdvancedWindow for PistonWindow<W, T>
where W: AdvancedWindow
{
fn get_title(&self) -> String { self.window.borrow().get_title() }
fn set_title(&mut self, title: String) {
Expand Down

0 comments on commit 87bc5d4

Please sign in to comment.