graphics/
lib.rs

1#![crate_name = "graphics"]
2#![deny(
3    rust_2018_compatibility,
4    rust_2018_idioms,
5    unused,
6    nonstandard_style,
7    future_incompatible,
8    missing_docs,
9    missing_copy_implementations
10)]
11
12//! A library for 2D graphics that works with multiple back-ends.
13//!
14//! Piston-Graphics was started in 2014 by Sven Nilsen to test
15//! back-end agnostic design for 2D in Rust.
16//! This means generic code can be reused across projects and platforms.
17//!
18//! ### Design
19//!
20//! A graphics back-end implements the `Graphics` trait.
21//!
22//! This library uses immediate design for flexibility.
23//! By default, triangles are generated from 2D shapes and passed in chunks
24//! to the back-end. This behavior can be overridden by a back-end library.
25//!
26//! The structures used for drawing 2D shapes contains settings for rendering.
27//! The separation of shapes and settings allows more reuse and flexibility.
28//! For example, to render an image, you use an `Image` object.
29//!
30//! The `math` module contains useful methods for 2D geometry.
31//!
32//! `Context` stores settings that are commonly shared when rendering.
33//! It can be copied and changed without affecting any global state.
34//!
35//! At top level, there are some shortcut methods for common operations.
36//! For example, `ellipse` is a simplified version of `Ellipse`.
37
38pub use character::{Character, CharacterCache};
39pub use circle_arc::CircleArc;
40pub use colored::Colored;
41pub use context::Context;
42pub use draw_state::DrawState;
43pub use ellipse::Ellipse;
44pub use crate::graphics::Graphics;
45pub use image::Image;
46pub use line::Line;
47pub use polygon::Polygon;
48pub use radians::Radians;
49pub use rectangle::Rectangle;
50pub use rectangled::Rectangled;
51pub use source_rectangled::SourceRectangled;
52pub use text::Text;
53pub use texture::ImageSize;
54pub use transformed::Transformed;
55pub use viewport::Viewport;
56
57/// Any triangulation method called on the back-end
58/// never exceeds this number of vertices.
59/// This can be used to initialize buffers that fit the chunk size.
60///
61/// Must be a multiple of 3 because you need 3 vertices per triangle
62/// in a triangle list.
63pub const BACK_END_MAX_VERTEX_COUNT: usize = 1023;
64
65mod colored;
66mod graphics;
67mod rectangled;
68mod source_rectangled;
69mod transformed;
70
71pub mod character;
72pub mod circle_arc;
73pub mod color;
74pub mod context;
75pub mod draw_state;
76pub mod ellipse;
77pub mod glyph_cache;
78pub mod grid;
79pub mod image;
80pub mod line;
81pub mod math;
82pub mod modular_index;
83pub mod polygon;
84pub mod rectangle;
85pub mod text;
86pub mod texture_packer;
87pub mod triangulation;
88pub mod types;
89
90pub mod radians {
91    //! Reexport radians helper trait from vecmath
92
93    pub use vecmath::traits::Radians;
94}
95
96/// Clears the screen.
97pub fn clear<G>(color: types::Color, g: &mut G)
98where
99    G: Graphics,
100{
101    g.clear_color(color);
102    g.clear_stencil(0);
103}
104
105/// Draws image.
106pub fn image<G>(image: &<G as Graphics>::Texture, transform: math::Matrix2d, g: &mut G)
107where
108    G: Graphics,
109{
110    Image::new().draw(image, &Default::default(), transform, g);
111}
112
113/// Draws ellipse by corners.
114pub fn ellipse_from_to<P: Into<types::Vec2d>, G>(
115    color: types::Color,
116    from: P,
117    to: P,
118    transform: math::Matrix2d,
119    g: &mut G,
120) where
121    G: Graphics,
122{
123    Ellipse::new(color).draw_from_to(from, to, &Default::default(), transform, g);
124}
125
126/// Draws ellipse.
127pub fn ellipse<R: Into<types::Rectangle>, G>(
128    color: types::Color,
129    rect: R,
130    transform: math::Matrix2d,
131    g: &mut G,
132) where
133    G: Graphics,
134{
135    Ellipse::new(color).draw(rect, &Default::default(), transform, g);
136}
137
138/// Draws arc
139pub fn circle_arc<R: Into<types::Rectangle>, G>(
140    color: types::Color,
141    radius: types::Radius,
142    start: types::Scalar,
143    end: types::Scalar,
144    rect: R,
145    transform: math::Matrix2d,
146    g: &mut G,
147) where
148    G: Graphics,
149{
150    CircleArc::new(color, radius, start, end).draw(rect, &Default::default(), transform, g);
151}
152
153/// Draws rectangle.
154pub fn rectangle_from_to<P: Into<types::Vec2d>, G>(
155    color: types::Color,
156    from: P,
157    to: P,
158    transform: math::Matrix2d,
159    g: &mut G,
160) where
161    G: Graphics,
162{
163    Rectangle::new(color).draw_from_to(from, to, &Default::default(), transform, g);
164}
165
166/// Draws rectangle.
167pub fn rectangle<R: Into<types::Rectangle>, G>(
168    color: types::Color,
169    rect: R,
170    transform: math::Matrix2d,
171    g: &mut G,
172) where
173    G: Graphics,
174{
175    Rectangle::new(color).draw(rect, &Default::default(), transform, g);
176}
177
178/// Draws polygon.
179pub fn polygon<G>(
180    color: types::Color,
181    polygon: types::Polygon<'_>,
182    transform: math::Matrix2d,
183    g: &mut G,
184) where
185    G: Graphics,
186{
187    Polygon::new(color).draw(polygon, &Default::default(), transform, g);
188}
189
190/// Draws line between points.
191pub fn line_from_to<P: Into<types::Vec2d>, G>(
192    color: types::Color,
193    radius: types::Radius,
194    from: P,
195    to: P,
196    transform: math::Matrix2d,
197    g: &mut G,
198) where
199    G: Graphics,
200{
201    Line::new(color, radius).draw_from_to(from, to, &Default::default(), transform, g)
202}
203
204/// Draws line.
205pub fn line<L: Into<types::Line>, G>(
206    color: types::Color,
207    radius: types::Radius,
208    line: L,
209    transform: math::Matrix2d,
210    g: &mut G,
211) where
212    G: Graphics,
213{
214    Line::new(color, radius).draw(line, &Default::default(), transform, g)
215}
216
217/// Draws text.
218pub fn text<C, G>(
219    color: types::Color,
220    font_size: types::FontSize,
221    text: &str,
222    cache: &mut C,
223    transform: math::Matrix2d,
224    g: &mut G,
225) -> Result<(), C::Error>
226where
227    C: character::CharacterCache,
228    G: Graphics<Texture = <C as character::CharacterCache>::Texture>,
229{
230    Text::new_color(color, font_size).draw(text, cache, &Default::default(), transform, g)
231}