Skip to content

Commit

Permalink
Upgrade to latest Gfx
Browse files Browse the repository at this point in the history
  • Loading branch information
bvssvni committed Feb 28, 2015
1 parent e4b761e commit f2ed4bf
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
/bin/test-internal
/bin/test-external
/doc/
/examples/
/target/
/build/
/.rust/
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "piston2d-gfx_graphics"
version = "0.1.2"
version = "0.1.3"
authors = ["bvssvni <[email protected]>"]
keywords = ["graphics", "2d", "gfx"]
description = "A Gfx 2D back-end for the Piston game engine"
Expand Down Expand Up @@ -31,6 +31,10 @@ git = "https://github.com/pistondevelopers/image"
git = "https://github.com/gfx-rs/gfx-rs"
#version = "0.1.0"

[dependencies.gfx_device_gl]
git = "https://github.com/gfx-rs/gfx_device_gl"
#version = "0.0.0"

[dependencies.gfx_macros]
git = "https://github.com/gfx-rs/gfx-rs"
#version = "0.1.0"
Expand Down
4 changes: 3 additions & 1 deletion examples/imagetest.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![feature(old_path)]

extern crate piston;
extern crate shader_version;
extern crate graphics;
extern crate sdl2_window;
extern crate gfx;
extern crate gfx_device_gl;
extern crate gfx_graphics;
extern crate sdl2;

Expand All @@ -29,7 +31,7 @@ fn main() {
}
);

let mut device = gfx::GlDevice::new(|s| unsafe {
let mut device = gfx_device_gl::GlDevice::new(|s| unsafe {
std::mem::transmute(sdl2::video::gl_get_proc_address(s))
});
let piston::window::Size([w, h]) = window.get();
Expand Down
70 changes: 47 additions & 23 deletions src/g2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

use gfx;
use graphics::{ Context, BackEnd };
use gfx_device_gl::GlResources;
use graphics::{ Context, Graphics };
use graphics::BACK_END_MAX_VERTEX_COUNT as BUFFER_SIZE;

use Texture;
Expand Down Expand Up @@ -117,28 +118,31 @@ struct ColorFormat { color: [f32; 4] }
#[vertex_format]
struct TexCoordsFormat { uv: [f32; 2] }

#[shader_param]
#[shader_param(GlResources)]
struct Params {
color: [f32; 4],
}

#[shader_param]
#[shader_param(GlResources)]
struct ParamsUV {
color: [f32; 4],
s_texture: gfx::shade::TextureParam,
s_texture: gfx::shade::TextureParam<GlResources>,
}

/// The graphics back-end.
pub struct G2D {
buffer_pos: gfx::BufferHandle<gfx::GlResources, f32>,
buffer_uv: gfx::BufferHandle<gfx::GlResources, f32>,
buffer_pos: gfx::BufferHandle<GlResources, f32>,
buffer_uv: gfx::BufferHandle<GlResources, f32>,
batch: gfx::batch::OwnedBatch<Params>,
batch_uv: gfx::batch::OwnedBatch<ParamsUV>,
}

impl G2D {
/// Creates a new G2D object.
pub fn new<D: gfx::Device>(device: &mut D) -> G2D {
pub fn new<D>(device: &mut D) -> G2D
where
D: gfx::Device<Resources = GlResources>
{
use gfx::DeviceExt;

let shader_model = device.get_capabilities().shader_model;
Expand Down Expand Up @@ -184,14 +188,14 @@ impl G2D {

let mut mesh = gfx::Mesh::new(BUFFER_SIZE as u32);
mesh.attributes.extend(gfx::VertexFormat::generate(
None::<PositionFormat>,
None::<&PositionFormat>,
buffer_pos.raw()
).into_iter());

// Reuse parameters from `mesh`.
let mut mesh_uv = mesh.clone();
mesh_uv.attributes.extend(gfx::VertexFormat::generate(
None::<TexCoordsFormat>,
None::<&TexCoordsFormat>,
buffer_uv.raw()
).into_iter());

Expand Down Expand Up @@ -247,12 +251,12 @@ impl G2D {
/// Renders graphics to a Gfx renderer.
pub fn draw<C, F>(
&mut self,
renderer: &mut gfx::Renderer<C>,
frame: &gfx::Frame,
renderer: &mut gfx::Renderer<C::CommandBuffer>,
frame: &gfx::Frame<C::Resources>,
mut f: F
)
where
C: gfx::Device,
C: gfx::Device<Resources = GlResources>,
F: FnMut(Context, &mut GraphicsBackEnd<C>)
{
let ref mut g = GraphicsBackEnd::new(
Expand All @@ -275,17 +279,28 @@ pub struct GraphicsBackEnd<'a, C>
where
C: 'a + gfx::Device,
<C as gfx::Device>::CommandBuffer: 'a,
<C as gfx::Device>::Resources: 'a
<C as gfx::Device>::Resources: 'a,
<<C as gfx::device::Device>::Resources as gfx::device::Resources>::Sampler: 'a,
<<C as gfx::device::Device>::Resources as gfx::device::Resources>::Texture: 'a,
<<C as gfx::device::Device>::Resources as gfx::device::Resources>::Surface: 'a,
<<C as gfx::device::Device>::Resources as gfx::device::Resources>::FrameBuffer: 'a,
<<C as gfx::device::Device>::Resources as gfx::device::Resources>::Program: 'a,
<<C as gfx::device::Device>::Resources as gfx::device::Resources>::Shader: 'a,
<<C as gfx::device::Device>::Resources as gfx::device::Resources>::ArrayBuffer: 'a,
<<C as gfx::device::Device>::Resources as gfx::device::Resources>::Buffer: 'a
{
renderer: &'a mut gfx::Renderer<C>,
frame: &'a gfx::Frame,
renderer: &'a mut gfx::Renderer<C::CommandBuffer>,
frame: &'a gfx::Frame<C::Resources>,
g2d: &'a mut G2D,
}

impl<'a, C: gfx::Device> GraphicsBackEnd<'a, C> {
impl<'a, C> GraphicsBackEnd<'a, C>
where
C: gfx::Device<Resources = GlResources>
{
/// Creates a new object for rendering 2D graphics.
pub fn new(renderer: &'a mut gfx::Renderer<C>,
frame: &'a gfx::Frame,
pub fn new(renderer: &'a mut gfx::Renderer<C::CommandBuffer>,
frame: &'a gfx::Frame<C::Resources>,
g2d: &'a mut G2D) -> GraphicsBackEnd<'a, C> {
GraphicsBackEnd {
renderer: renderer,
Expand All @@ -295,7 +310,7 @@ impl<'a, C: gfx::Device> GraphicsBackEnd<'a, C> {
}

/// Returns true if texture has alpha channel.
pub fn has_texture_alpha(&self, texture: &Texture) -> bool {
pub fn has_texture_alpha(&self, texture: &Texture<C>) -> bool {
use gfx::tex::Components::RGBA;

texture.handle.get_info().format.get_components() == Some(RGBA)
Expand Down Expand Up @@ -329,9 +344,13 @@ impl<'a, C: gfx::Device> GraphicsBackEnd<'a, C> {
}
}

impl<'a, C: gfx::Device> BackEnd
for GraphicsBackEnd<'a, C> {
type Texture = Texture;
impl<'a, C> Graphics
for GraphicsBackEnd<'a, C>
where
C: gfx::Device<Resources = GlResources>,
C::Resources: 'a,
{
type Texture = Texture<C>;

fn clear(&mut self, color: [f32; 4]) {
let &mut GraphicsBackEnd {
Expand Down Expand Up @@ -379,7 +398,12 @@ for GraphicsBackEnd<'a, C> {
})
}

fn tri_list_uv<F>(&mut self, color: &[f32; 4], texture: &Texture, mut f: F)
fn tri_list_uv<F>(
&mut self,
color: &[f32; 4],
texture: &<Self as Graphics>::Texture,
mut f: F
)
where F: FnMut(&mut FnMut(&[f32], &[f32]))
{
let &mut GraphicsBackEnd {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! The implementation of a Rust-Graphics back-end using gfx-rs.
extern crate gfx;
extern crate gfx_device_gl;
extern crate gfx_texture;
extern crate graphics;
extern crate image;
Expand Down

0 comments on commit f2ed4bf

Please sign in to comment.