Crate piston_window

Source
Expand description

The official Piston window wrapper for the Piston game engine

Notice! If this is your first time visiting Piston, start here.

The purpose of this library is to provide an easy-to-use, simple-to-get-started and convenient-for-applications API for Piston.

Sets up:

  • Gfx with an OpenGL back-end.
  • gfx_graphics for 2D rendering.
  • glutin_window as default window back-end, but this can be swapped (see below).

§Example

extern crate piston_window;

use piston_window::*;

fn main() {
    let mut window: PistonWindow =
        WindowSettings::new("Hello World!", [512; 2])
            .build().unwrap();
    while let Some(e) = window.next() {
        window.draw_2d(&e, |c, g, _| {
            clear([0.5, 0.5, 0.5, 1.0], g);
            rectangle([1.0, 0.0, 0.0, 1.0], // red
                      [0.0, 0.0, 100.0, 100.0], // rectangle
                      c.transform, g);
        });
    }
}

The draw_2d function calls the closure on render events. There is no need to filter events manually, and there is no overhead.

§Swap to another window back-end

Change the generic parameter to the window back-end you want to use.

extern crate piston_window;
extern crate sdl2_window;

use piston_window::*;
use sdl2_window::Sdl2Window;


let window: PistonWindow<Sdl2Window> =
    WindowSettings::new("title", [512; 2])
        .build().unwrap();

§sRGB

The impl of BuildFromWindowSettings in this library turns on WindowSettings::srgb, because it is required by gfx_graphics.

Most images such as those found on the internet uses sRGB, that has a non-linear gamma corrected space. When rendering 3D, make sure textures and colors are in linear gamma space. Alternative is to use Srgb8 and Srgba8 formats for textures.

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

§Library dependencies

This library is meant to be used in applications only. It is not meant to be depended on by generic libraries. Instead, libraries should depend on the lower abstractions, such as the Piston core.

Re-exports§

pub extern crate graphics;
pub extern crate texture;
pub use prelude::*;

Modules§

character
A text character
circle_arc
Draw an arc
color
Helper methods for colors
context
Transformation context
draw_state
Graphics draw state.
ellipse
Draw ellipse
glyph_cache
Implementations of the CharacterCache trait.
grid
A flat grid with square cells.
image
Draw an image
line
Draw Line
math
Various methods for computing with vectors.
modular_index
Helper functions for computing modular index safely.
polygon
Draw polygon
prelude
Exports all of the types exposed by this module, except for graphics.
radians
Reexport radians helper trait from vecmath
rectangle
Draw rectangle
text
Draw text
texture_packer
Texture packing.
triangulation
Methods for converting shapes into triangles.
types
Contains type aliases used in this library

Structs§

Character
Holds rendered character data.
CircleArc
A curved line
Context
Drawing 2d context.
DrawState
Graphics draw state used for blending, clipping and stencil rendering.
Ellipse
An ellipse with filled color
Image
An image
Line
A colored line with a default border radius
Polygon
A polygon
Rectangle
A filled rectangle
Text
Renders text
Viewport
Stores viewport information.

Constants§

BACK_END_MAX_VERTEX_COUNT
Any triangulation method called on the back-end never exceeds this number of vertices. This can be used to initialize buffers that fit the chunk size.

Traits§

CharacterCache
Stores characters in a buffer and loads them by demand.
Colored
Implemented by contexts that contains color.
Graphics
Implemented by all graphics back-ends.
ImageSize
Implemented by all images to be used with generic algorithms.
Radians
Useful constants for radians.
Rectangled
Should be implemented by contexts that have rectangle information.
SourceRectangled
Should be implemented by contexts that have source rectangle information.
Transformed
Implemented by contexts that can transform.

Functions§

circle_arc
Draws arc
clear
Clears the screen.
ellipse
Draws ellipse.
ellipse_from_to
Draws ellipse by corners.
image
Draws image.
line
Draws line.
line_from_to
Draws line between points.
polygon
Draws polygon.
rectangle
Draws rectangle.
rectangle_from_to
Draws rectangle.
text
Draws text.