Skip to content

Commit

Permalink
prepare 5.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jan 12, 2019
1 parent cebb7cb commit ff5c46b
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 67 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ categories = [
]
description = '''A buffer which can be used as a render target for Piston's graphics library. This buffer can be loaded from and/or saved to a file on disk. This allows for things like screenshots in games.'''
documentation = 'https://docs.rs/graphics_buffer/'
edition = '2018'
keywords = [
'piston',
'image',
Expand All @@ -44,4 +45,4 @@ license = 'MIT'
name = 'graphics_buffer'
readme = 'readme.md'
repository = 'https://github.com/kaikalii/graphics_buffer'
version = '0.4.4'
version = '0.5.0'
9 changes: 3 additions & 6 deletions examples/circles.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
extern crate graphics;
extern crate graphics_buffer;

use graphics::ellipse;
use graphics_buffer::*;

Expand All @@ -13,21 +10,21 @@ fn main() {
ellipse(
[1.0, 0.0, 0.0, 0.7],
[0.0, 0.0, 100.0, 100.0],
identity(),
IDENTITY,
&mut buffer,
);
// Small blue circle
ellipse(
[0.0, 0.0, 1.0, 0.7],
[0.0, 0.0, 50.0, 50.0],
identity(),
IDENTITY,
&mut buffer,
);
// Small green circle
ellipse(
[0.0, 1.0, 0.0, 0.7],
[50.0, 50.0, 50.0, 50.0],
identity(),
IDENTITY,
&mut buffer,
);

Expand Down
10 changes: 4 additions & 6 deletions examples/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
extern crate graphics;
extern crate graphics_buffer;

use graphics::{text, Transformed};
use graphics_buffer::*;

Expand All @@ -14,13 +11,14 @@ fn main() {

// Draw text
text(
[1.0, 1.0, 1.0, 1.0],
[1.0; 4],
30,
"Oh boy!",
&mut glyphs,
identity().trans(10.0, 30.0),
IDENTITY.trans(10.0, 30.0),
&mut buffer,
).unwrap();
)
.unwrap();

// Save the image
buffer.save("text.png").unwrap();
Expand Down
5 changes: 1 addition & 4 deletions examples/tiled.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
extern crate graphics;
extern crate graphics_buffer;

use graphics::{Image, Transformed};
use graphics_buffer::*;

Expand All @@ -25,7 +22,7 @@ fn main() {
Image::new_color(*color).draw(
&matt,
&Default::default(),
identity().trans(*x, *y),
IDENTITY.trans(*x, *y),
&mut buffer,
);
}
Expand Down
35 changes: 17 additions & 18 deletions examples/window.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
extern crate graphics;
extern crate graphics_buffer;
extern crate piston_window;

use graphics::{ellipse, image, text, Transformed};
use graphics_buffer::*;
use piston_window::{
Expand All @@ -20,21 +16,21 @@ fn main() {
buffer.clear([0.0, 0.0, 0.0, 1.0]);

// Draw Matt to the buffer
image(&matt, identity(), &mut buffer);
image(&matt, IDENTITY, &mut buffer);

// Give Matt red eyes
let red = [1.0, 0.0, 0.0, 0.7];
let diameter = 40.0;
const RED: [f32; 4] = [1.0, 0.0, 0.0, 0.7];
const DIAMETER: f64 = 40.0;
ellipse(
red,
[115.0, 175.0, diameter, diameter],
identity(),
RED,
[115.0, 175.0, DIAMETER, DIAMETER],
IDENTITY,
&mut buffer,
);
ellipse(
red,
[210.0, 195.0, diameter, diameter],
identity(),
RED,
[210.0, 195.0, DIAMETER, DIAMETER],
IDENTITY,
&mut buffer,
);

Expand All @@ -44,17 +40,19 @@ fn main() {
70,
"# w o k e",
&mut glyphs,
identity().trans(40.0, 70.0),
IDENTITY.trans(40.0, 70.0),
&mut buffer,
).unwrap();
)
.unwrap();

// Create a window
let mut window: PistonWindow = WindowSettings::new(
"piston_window texture example",
(matt.height(), matt.height()),
).exit_on_esc(true)
.build()
.unwrap();
)
.exit_on_esc(true)
.build()
.unwrap();

// Create a texture from red-eyed Matt
let matt_texture = buffer
Expand All @@ -64,6 +62,7 @@ fn main() {
// Initialize a rotation
let mut rot = 0.0;

// Run the event loop
while let Some(event) = window.next() {
match event {
Event::Loop(Loop::Render(..)) => {
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ There is also an optional feature for `RenderBuffer` that allows it to be conver
Add this to your `cargo.toml` :

```toml
graphics_buffer = "0.4.4"
graphics_buffer = "0.5.0"
piston2d-graphics = "0.26.0"
```

or, if you want to be able to draw the texture to a window using [`piston_window`](https://github.com/PistonDevelopers/piston_window) :

```toml
graphics_buffer = { version = "0.4.4", features = ["piston_window_texture"] }
graphics_buffer = { version = "0.5.0", features = ["piston_window_texture"] }
piston2d-graphics = "0.26.0"
piston_window = "0.82.0"
```
Expand All @@ -41,21 +41,21 @@ fn main() {
ellipse(
[1.0, 0.0, 0.0, 0.7],
[0.0, 0.0, 100.0, 100.0],
identity(),
IDENTITY,
&mut buffer,
);
// Small blue circle
ellipse(
[0.0, 0.0, 1.0, 0.7],
[0.0, 0.0, 50.0, 50.0],
identity(),
IDENTITY,
&mut buffer,
);
// Small green circle
ellipse(
[0.0, 1.0, 0.0, 0.7],
[50.0, 50.0, 50.0, 50.0],
identity(),
IDENTITY,
&mut buffer,
);

Expand Down
19 changes: 10 additions & 9 deletions src/glyphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct CharacterDef {
}

impl CharacterDef {
fn as_character<'a>(&'a self) -> Character<'a, RenderBuffer> {
fn as_character(&self) -> Character<'_, RenderBuffer> {
Character {
offset: self.offset,
size: self.size,
Expand Down Expand Up @@ -51,12 +51,12 @@ impl<'f> BufferGlyphs<'f> {

impl<'f> CharacterCache for BufferGlyphs<'f> {
type Texture = RenderBuffer;
type Error = Box<error::Error>;
fn character<'a>(
&'a mut self,
type Error = Box<dyn error::Error>;
fn character(
&mut self,
font_size: FontSize,
ch: char,
) -> Result<Character<'a, Self::Texture>, Self::Error> {
) -> Result<Character<'_, Self::Texture>, Self::Error> {
let font = &self.font;
Ok(self
.characters
Expand Down Expand Up @@ -88,12 +88,13 @@ impl<'f> CharacterCache for BufferGlyphs<'f> {
});
CharacterDef {
offset: [
bounding_box.min.x as Scalar - 1.0,
-pixel_bounding_box.min.y as Scalar + 1.0,
Scalar::from(bounding_box.min.x) - 1.0,
Scalar::from(-pixel_bounding_box.min.y) + 1.0,
],
size: [h_metrics.advance_width as Scalar, 0 as Scalar],
size: [Scalar::from(h_metrics.advance_width), Scalar::from(0)],
texture,
}
}).as_character())
})
.as_character())
}
}
26 changes: 8 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@

// !There is also an optional feature for `RenderBuffer` that allows it to be converted into a `G2dTexture` so that it can be rendered with [`piston_window`](https://github.com/PistonDevelopers/piston_window). To enable this, add `features = ["piston_window_texture"]` to the `graphics_buffer` dependency in your `cargo.toml`.

extern crate bit_vec;
extern crate graphics;
extern crate image;
#[cfg(feature = "piston_window_texture")]
extern crate piston_window;
extern crate rayon;
extern crate rusttype;

mod glyphs;
pub use glyphs::*;
pub use crate::glyphs::*;

use std::{error, fmt, ops, path::Path};

Expand All @@ -25,10 +17,8 @@ use piston_window::{
};
use rayon::prelude::*;

/// Returns the identity matrix: `[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]`.
pub fn identity() -> Matrix2d {
[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
}
/// The identity matrix: `[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]`.
pub const IDENTITY: Matrix2d = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];

/// An Error type for `RenderBuffer`.
#[derive(Debug, Clone)]
Expand All @@ -37,7 +27,7 @@ pub enum Error {
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::ContainerTooSmall(len, area) => write!(
f,
Expand Down Expand Up @@ -71,11 +61,11 @@ impl RenderBuffer {
}
/// Creates a new `RenderBuffer` by opening it from a file.
pub fn open<P: AsRef<Path>>(path: P) -> ImageResult<RenderBuffer> {
image::open(path).map(|di| RenderBuffer::from(di))
image::open(path).map(RenderBuffer::from)
}
/// Creates a new `RenderBuffer` by decoding image data.
pub fn decode_from_bytes(bytes: &[u8]) -> ImageResult<RenderBuffer> {
image::load_from_memory(bytes).map(|di| RenderBuffer::from(di))
image::load_from_memory(bytes).map(RenderBuffer::from)
}
/// Clear the buffer with a color.
pub fn clear(&mut self, color: [f32; 4]) {
Expand Down Expand Up @@ -166,7 +156,7 @@ impl Graphics for RenderBuffer {
fn clear_stencil(&mut self, _value: u8) {}
fn tri_list<F>(&mut self, _draw_state: &DrawState, color: &[f32; 4], mut f: F)
where
F: FnMut(&mut FnMut(&[[f32; 2]])),
F: FnMut(&mut dyn FnMut(&[[f32; 2]])),
{
self.reset_used();
// Render Triangles
Expand Down Expand Up @@ -228,7 +218,7 @@ impl Graphics for RenderBuffer {
texture: &Self::Texture,
mut f: F,
) where
F: FnMut(&mut FnMut(&[[f32; 2]], &[[f32; 2]])),
F: FnMut(&mut dyn FnMut(&[[f32; 2]], &[[f32; 2]])),
{
self.reset_used();
// Render Triangles
Expand Down

0 comments on commit ff5c46b

Please sign in to comment.