Skip to content

Commit 87bc5d4

Browse files
committed
Merge pull request #41 from bvssvni/no_boilerplate
No boilerplate
2 parents a46969d + 937419c commit 87bc5d4

File tree

3 files changed

+51
-46
lines changed

3 files changed

+51
-46
lines changed

Cargo.toml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "piston_window"
4-
version = "0.1.0"
4+
version = "0.2.0"
55
authors = ["bvssvni <[email protected]>"]
66
keywords = ["window", "piston"]
77
description = "The official Piston window back-end for the Piston game engine"
@@ -19,10 +19,8 @@ name = "piston_window"
1919
[dependencies]
2020
gfx = "0.6.1"
2121
gfx_device_gl = "0.4.0"
22-
piston = "0.1.4"
22+
piston = "0.1.5"
2323
piston2d-gfx_graphics = "0.1.22"
2424
piston2d-graphics = "0.1.3"
25-
26-
[dev-dependencies]
27-
pistoncore-glutin_window = "0.1.0"
28-
25+
shader_version = "0.1.0"
26+
pistoncore-glutin_window = "0.2.0"

examples/hello_piston.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
extern crate piston_window;
2-
extern crate glutin_window;
3-
extern crate piston;
4-
extern crate graphics;
52

6-
use std::cell::RefCell;
7-
use std::rc::Rc;
8-
use glutin_window::{ OpenGL, GlutinWindow };
93
use piston_window::*;
10-
use piston::window::WindowSettings;
11-
12-
use piston::event::*;
13-
use graphics::*;
14-
use piston::input::*;
154

165
fn main() {
17-
let window = Rc::new(RefCell::new(GlutinWindow::new(
18-
OpenGL::_3_2,
19-
WindowSettings::new("Hello Piston!", [640, 480])
20-
.exit_on_esc(true)
21-
)));
6+
let window: PistonWindow = WindowSettings::new("Hello Piston!", [640, 480])
7+
.exit_on_esc(true)
8+
.into();
229
println!("Press any button to enter inner loop");
23-
for e in PistonWindow::new(window, empty_app()) {
10+
for e in window {
2411
e.draw_2d(|_c, g| {
2512
clear([0.5, 1.0, 0.5, 1.0], g);
2613
});

src/lib.rs

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,32 @@ extern crate gfx;
77
extern crate gfx_device_gl;
88
extern crate gfx_graphics;
99
extern crate graphics;
10+
extern crate shader_version;
11+
extern crate glutin_window;
12+
13+
use glutin_window::GlutinWindow;
14+
pub use shader_version::OpenGL;
15+
pub use graphics::*;
16+
pub use piston::window::*;
17+
pub use piston::event::*;
18+
pub use piston::input::*;
1019

1120
use std::cell::RefCell;
1221
use std::rc::Rc;
1322
use std::any::Any;
1423

15-
use piston::{ event, window };
1624
use gfx::traits::*;
1725
use gfx_graphics::{ Gfx2d, GfxGraphics };
18-
use graphics::Context;
1926

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

2334
/// Contains everything required for controlling window, graphics, event loop.
24-
pub struct PistonWindow<W: window::Window, T = ()> {
35+
pub struct PistonWindow<W: Window = GlutinWindow, T = ()> {
2536
/// The window.
2637
pub window: Rc<RefCell<W>>,
2738
/// GFX stream.
@@ -31,17 +42,26 @@ pub struct PistonWindow<W: window::Window, T = ()> {
3142
/// Gfx2d.
3243
pub g2d: Rc<RefCell<Gfx2d<gfx_device_gl::Resources>>>,
3344
/// The event loop.
34-
pub events: Rc<RefCell<event::WindowEvents<W, event::Event<W::Event>>>>,
45+
pub events: Rc<RefCell<WindowEvents<W, Event<W::Event>>>>,
3546
/// The event.
36-
pub event: Option<event::Event<W::Event>>,
47+
pub event: Option<Event<W::Event>>,
3748
/// Application structure.
3849
pub app: Rc<RefCell<T>>,
3950
/// The factory that was created along with the device.
4051
pub factory: Rc<RefCell<gfx_device_gl::Factory>>,
4152
}
4253

54+
impl<T> From<WindowSettings> for PistonWindow<T>
55+
where T: Window + OpenGLWindow + From<WindowSettings>,
56+
T::Event: GenericEvent
57+
{
58+
fn from(settings: WindowSettings) -> PistonWindow<T> {
59+
PistonWindow::new(Rc::new(RefCell::new(settings.into())), empty_app())
60+
}
61+
}
62+
4363
impl<W, T> Clone for PistonWindow<W, T>
44-
where W: window::Window, W::Event: Clone
64+
where W: Window, W::Event: Clone
4565
{
4666
fn clone(&self) -> Self {
4767
PistonWindow {
@@ -58,11 +78,11 @@ impl<W, T> Clone for PistonWindow<W, T>
5878
}
5979

6080
impl<W, T> PistonWindow<W, T>
61-
where W: window::Window, W::Event: event::GenericEvent
81+
where W: Window, W::Event: GenericEvent
6282
{
6383
/// Creates a new piston object.
6484
pub fn new(window: Rc<RefCell<W>>, app: Rc<RefCell<T>>) -> Self
65-
where W: window::OpenGLWindow
85+
where W: OpenGLWindow
6686
{
6787
use piston::event::Events;
6888
use piston::window::{ OpenGLWindow, Window };
@@ -140,7 +160,7 @@ impl<W, T> PistonWindow<W, T>
140160
}
141161

142162
impl<W, T> Iterator for PistonWindow<W, T>
143-
where W: window::Window, W::Event: event::GenericEvent
163+
where W: Window, W::Event: GenericEvent
144164
{
145165
type Item = PistonWindow<W, T>;
146166

@@ -174,13 +194,13 @@ impl<W, T> Iterator for PistonWindow<W, T>
174194
}
175195
}
176196

177-
impl<W, T> event::GenericEvent for PistonWindow<W, T>
178-
where W: window::Window, W::Event: event::GenericEvent
197+
impl<W, T> GenericEvent for PistonWindow<W, T>
198+
where W: Window, W::Event: GenericEvent
179199
{
180-
fn event_id(&self) -> event::EventId {
200+
fn event_id(&self) -> EventId {
181201
match self.event {
182202
Some(ref e) => e.event_id(),
183-
None => event::EventId("")
203+
None => EventId("")
184204
}
185205
}
186206

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

193-
fn from_args(event_id: event::EventId, any: &Any, old_event: &Self) -> Option<Self> {
213+
fn from_args(event_id: EventId, any: &Any, old_event: &Self) -> Option<Self> {
194214
if let Some(ref e) = old_event.event {
195-
match event::GenericEvent::from_args(event_id, any, e) {
215+
match GenericEvent::from_args(event_id, any, e) {
196216
Some(e) => {
197217
Some(PistonWindow {
198218
window: old_event.window.clone(),
@@ -211,22 +231,22 @@ impl<W, T> event::GenericEvent for PistonWindow<W, T>
211231
}
212232
}
213233

214-
impl<W, T> window::Window for PistonWindow<W, T>
215-
where W: window::Window
234+
impl<W, T> Window for PistonWindow<W, T>
235+
where W: Window
216236
{
217-
type Event = <W as window::Window>::Event;
237+
type Event = <W as Window>::Event;
218238

219239
fn should_close(&self) -> bool { self.window.borrow().should_close() }
220-
fn size(&self) -> window::Size { self.window.borrow().size() }
221-
fn draw_size(&self) -> window::Size { self.window.borrow().draw_size() }
240+
fn size(&self) -> Size { self.window.borrow().size() }
241+
fn draw_size(&self) -> Size { self.window.borrow().draw_size() }
222242
fn swap_buffers(&mut self) { self.window.borrow_mut().swap_buffers() }
223243
fn poll_event(&mut self) -> Option<Self::Event> {
224-
window::Window::poll_event(&mut *self.window.borrow_mut())
244+
Window::poll_event(&mut *self.window.borrow_mut())
225245
}
226246
}
227247

228-
impl<W, T> window::AdvancedWindow for PistonWindow<W, T>
229-
where W: window::AdvancedWindow
248+
impl<W, T> AdvancedWindow for PistonWindow<W, T>
249+
where W: AdvancedWindow
230250
{
231251
fn get_title(&self) -> String { self.window.borrow().get_title() }
232252
fn set_title(&mut self, title: String) {

0 commit comments

Comments
 (0)