Trait piston_window::Graphics[][src]

pub trait Graphics {
    type Texture: ImageSize;
Show methods pub fn clear_color(&mut self, color: [f32; 4]);
pub fn clear_stencil(&mut self, value: u8);
pub fn tri_list<F>(
        &mut self,
        draw_state: &DrawState,
        color: &[f32; 4],
        f: F
    )
    where
        F: FnMut(&mut dyn FnMut(&[[f32; 2]]))
;
pub fn tri_list_c<F>(&mut self, draw_state: &DrawState, f: F)
    where
        F: FnMut(&mut dyn FnMut(&[[f32; 2]], &[[f32; 4]]))
;
pub fn tri_list_uv<F>(
        &mut self,
        draw_state: &DrawState,
        color: &[f32; 4],
        texture: &Self::Texture,
        f: F
    )
    where
        F: FnMut(&mut dyn FnMut(&[[f32; 2]], &[[f32; 2]]))
;
pub fn tri_list_uv_c<F>(
        &mut self,
        draw_state: &DrawState,
        texture: &Self::Texture,
        f: F
    )
    where
        F: FnMut(&mut dyn FnMut(&[[f32; 2]], &[[f32; 2]], &[[f32; 4]]))
; pub fn rectangle<R>(
        &mut self,
        r: &Rectangle,
        rectangle: R,
        draw_state: &DrawState,
        transform: [[f64; 3]; 2]
    )
    where
        R: Into<[f64; 4]>
, { ... }
pub fn polygon(
        &mut self,
        p: &Polygon,
        polygon: &[[f64; 2]],
        draw_state: &DrawState,
        transform: [[f64; 3]; 2]
    ) { ... }
pub fn polygon_tween_lerp(
        &mut self,
        p: &Polygon,
        polygons: &[&[[f64; 2]]],
        tween_factor: f64,
        draw_state: &DrawState,
        transform: [[f64; 3]; 2]
    ) { ... }
pub fn image(
        &mut self,
        image: &Image,
        texture: &Self::Texture,
        draw_state: &DrawState,
        transform: [[f64; 3]; 2]
    ) { ... }
pub fn ellipse<R>(
        &mut self,
        e: &Ellipse,
        rectangle: R,
        draw_state: &DrawState,
        transform: [[f64; 3]; 2]
    )
    where
        R: Into<[f64; 4]>
, { ... }
pub fn line<L>(
        &mut self,
        l: &Line,
        line: L,
        draw_state: &DrawState,
        transform: [[f64; 3]; 2]
    )
    where
        L: Into<[f64; 4]>
, { ... }
pub fn circle_arc<R>(
        &mut self,
        c: &CircleArc,
        rectangle: R,
        draw_state: &DrawState,
        transform: [[f64; 3]; 2]
    )
    where
        R: Into<[f64; 4]>
, { ... }
}

Implemented by all graphics back-ends.

An example back-end using raw OpenGL

By default, this design uses triangles as graphics primitives. This is supported by all GPUs and easy to implement in shader languages.

Default trait methods can be overridden for better performance or higher quality.

When drawing, use this trait as generic constraint:

use graphics::{Graphics, Context};

fn draw<G: Graphics>(c: &Context, g: &mut G) {
    //...
}

Color space is sRGB.

Notice for back-end authors

When sRGB is enabled for a back-end shader, the gamma must be converted to linear space when used as vertex color or uniform parameter. To convert gamma, use color::gamma_srgb_to_linear.

For more information, see https://github.com/PistonDevelopers/piston/issues/1014.

Associated Types

type Texture: ImageSize[src]

The texture type associated with the back-end.

In generic code, this type is often unknown. This might lead to more boilerplate code:

use graphics::{Graphics, Context, ImageSize};

fn draw_texture<G, T>(c: &Context, g: &mut G)
    where G: Graphics<Texture = T>, T: ImageSize {
    //...
}

Code written specifically for one back-end can be easier to write. Later, when the code is done, it can be refactored into generic code.

Loading content...

Required methods

pub fn clear_color(&mut self, color: [f32; 4])[src]

Clears background with a color.

The color should replace the values in the buffer.

Color space is sRGB.

pub fn clear_stencil(&mut self, value: u8)[src]

Clears stencil buffer with a value, usually 0.

A stencil buffer contains values that are not visible on the screen. These values are used to test against the pixel to paint.

If you are drawing a shape for clipping and forgot to clear the stencil buffer, then the clipping shape will carry over in next frame and cause artifacts.

pub fn tri_list<F>(&mut self, draw_state: &DrawState, color: &[f32; 4], f: F) where
    F: FnMut(&mut dyn FnMut(&[[f32; 2]])), 
[src]

Renders list of 2d triangles using a solid color.

All vertices share the same color.

The back-end calls the closure with a closure to receive vertices. First, the back-end sets up shaders and such to prepare. Then it calls the closure, which calls back with chunks of vertices. The number of vertices per chunk never exceeds BACK_END_MAX_VERTEX_COUNT. Vertex positions are encoded [[x0, y0], [x1, y1], ...].

Color space is sRGB.

pub fn tri_list_c<F>(&mut self, draw_state: &DrawState, f: F) where
    F: FnMut(&mut dyn FnMut(&[[f32; 2]], &[[f32; 4]])), 
[src]

Same as tri_list, but with individual vertex colors.

Argument are |vertices: &[[f32; 2], colors: &[[f32; 4]]]|.

pub fn tri_list_uv<F>(
    &mut self,
    draw_state: &DrawState,
    color: &[f32; 4],
    texture: &Self::Texture,
    f: F
) where
    F: FnMut(&mut dyn FnMut(&[[f32; 2]], &[[f32; 2]])), 
[src]

Renders list of 2d triangles using a color and a texture.

All vertices share the same color.

Tip: For objects of different colors, use grayscale textures. The texture color gets multiplied with the color.

A texture coordinate is assigned per vertex (from [0, 0] to [1, 1]).

The back-end calls the closure with a closure to receive vertices. First, the back-end sets up shaders and such to prepare. Then it calls the closure, which calls back with chunks of vertices. The number of vertices per chunk never exceeds BACK_END_MAX_VERTEX_COUNT. Vertex positions are encoded [[x0, y0], [x1, y1], ...]. Texture coordinates are encoded [[u0, v0], [u1, v1], ...].

Chunks uses separate buffer for vertex positions and texture coordinates. Arguments are |vertices: &[[f32; 2]], texture_coords: &[[f32; 2]]|.

Color space is sRGB.

pub fn tri_list_uv_c<F>(
    &mut self,
    draw_state: &DrawState,
    texture: &Self::Texture,
    f: F
) where
    F: FnMut(&mut dyn FnMut(&[[f32; 2]], &[[f32; 2]], &[[f32; 4]])), 
[src]

Same as tri_list_uv, but with individual vertex colors.

Argument are |vertices: &[[f32; 2], texture_coors: &[[f32; 2]], colors: &[[f32; 4]]]|.

Loading content...

Provided methods

pub fn rectangle<R>(
    &mut self,
    r: &Rectangle,
    rectangle: R,
    draw_state: &DrawState,
    transform: [[f64; 3]; 2]
) where
    R: Into<[f64; 4]>, 
[src]

Draws a rectangle.

Can be overriden in the back-end for higher performance.

Instead of calling this directly, use Rectangle::draw.

pub fn polygon(
    &mut self,
    p: &Polygon,
    polygon: &[[f64; 2]],
    draw_state: &DrawState,
    transform: [[f64; 3]; 2]
)
[src]

Draws a polygon.

Can be overridden in the back-end for higher performance.

Instead of calling this directly, use Polygon::draw.

pub fn polygon_tween_lerp(
    &mut self,
    p: &Polygon,
    polygons: &[&[[f64; 2]]],
    tween_factor: f64,
    draw_state: &DrawState,
    transform: [[f64; 3]; 2]
)
[src]

Draws a tweened polygon using linear interpolation.

Can be overridden in the back-end for higher performance.

Instead of calling this directly, use Polygon::draw_tween_lerp.

pub fn image(
    &mut self,
    image: &Image,
    texture: &Self::Texture,
    draw_state: &DrawState,
    transform: [[f64; 3]; 2]
)
[src]

Draws image.

Can be overridden in the back-end for higher performance.

Instead of calling this directly, use Image::draw.

pub fn ellipse<R>(
    &mut self,
    e: &Ellipse,
    rectangle: R,
    draw_state: &DrawState,
    transform: [[f64; 3]; 2]
) where
    R: Into<[f64; 4]>, 
[src]

Draws ellipse.

Can be overridden in the back-end for higher performance.

Instead of calling this directly, use Ellipse::draw.

pub fn line<L>(
    &mut self,
    l: &Line,
    line: L,
    draw_state: &DrawState,
    transform: [[f64; 3]; 2]
) where
    L: Into<[f64; 4]>, 
[src]

Draws line.

Can be overridden in the back-end for higher performance.

Instead of calling this directly, use Line::draw.

pub fn circle_arc<R>(
    &mut self,
    c: &CircleArc,
    rectangle: R,
    draw_state: &DrawState,
    transform: [[f64; 3]; 2]
) where
    R: Into<[f64; 4]>, 
[src]

Draws circle arc.

Can be overriden in the back-end for higher performance.

Instead of calling this directly, use CircleArc::draw.

Loading content...

Implementations on Foreign Types

impl<'a, R, C> Graphics for GfxGraphics<'a, R, C> where
    C: Buffer<R>,
    R: Resources,
    <R as Resources>::Buffer: 'a,
    <R as Resources>::Shader: 'a,
    <R as Resources>::Program: 'a,
    <R as Resources>::Texture: 'a,
    <R as Resources>::Sampler: 'a, 
[src]

type Texture = Texture<R>

Loading content...

Implementors

Loading content...