Skip to content

Commit

Permalink
Updated to latest Piston-Graphics
Browse files Browse the repository at this point in the history
- Bumped to 0.72.0
  • Loading branch information
bvssvni committed Nov 12, 2020
1 parent 4ff965f commit 8396cd9
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "piston2d-gfx_graphics"
version = "0.71.1"
version = "0.72.0"
authors = ["bvssvni <[email protected]>"]
keywords = ["graphics", "2d", "gfx", "piston"]
description = "A Gfx 2D back-end for the Piston game engine"
Expand All @@ -20,16 +20,16 @@ name = "gfx_graphics"
[dependencies]
gfx = "0.18.1"
draw_state = "0.8.0"
piston-shaders_graphics2d = "0.3.1"
piston-shaders_graphics2d = "0.4.0"
piston-gfx_texture = "0.41.0"
shader_version = "0.7.0"

[dependencies.piston2d-graphics]
version = "0.38.0"
version = "0.39.0"
features = ["glyph_cache_rusttype"]

[dev-dependencies]
pistoncore-glutin_window = "0.66.0"
pistoncore-glutin_window = "0.67.0"
piston = "0.52.0"
find_folder = "0.3.0"
gfx_device_gl = "0.16.2"
Expand Down
Binary file added assets/rust-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions examples/colored_image_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
extern crate piston;
extern crate graphics;
extern crate gfx;
extern crate gfx_graphics;
extern crate glutin_window;
extern crate image as im;

use std::path::Path;
use piston::event_loop::*;
use piston::input::*;
use piston::window::{OpenGLWindow, Window, WindowSettings};
use gfx::format::{DepthStencil, Formatted, Srgba8};
use gfx::memory::Typed;
use gfx::Device;
use gfx_graphics::*;
use glutin_window::{OpenGL, GlutinWindow};

fn main() {
let opengl = OpenGL::V3_2;
let samples = 4;
let mut window: GlutinWindow = WindowSettings::new("gfx_graphics: colored_image_test", [300, 300])
.exit_on_esc(true)
.graphics_api(opengl)
.samples(samples)
.build()
.unwrap();
let (mut device, mut factory) =
gfx_device_gl::create(|s| window.get_proc_address(s) as *const std::os::raw::c_void);

// Set up texture
let path = Path::new("./assets/rust-white.png");
let img = match im::open(path) {
Ok(img) => img,
Err(e) => {
panic!("Could not load '{:?}': {:?}", path.file_name().unwrap(), e);
}
};
let img = match img {
im::DynamicImage::ImageRgba8(img) => img,
x => x.to_rgba(),
};
let mut texture_context = TextureContext {
factory: factory.clone(),
encoder: factory.create_command_buffer().into(),
};
let rust_logo = Texture::from_image(&mut texture_context, &img, &TextureSettings::new()).unwrap();

let mut encoder = factory.create_command_buffer().into();
let draw_size = window.draw_size();
let dim = (
draw_size.width as u16,
draw_size.height as u16,
1,
(samples as gfx::texture::NumSamples).into(),
);
let color_format = <Srgba8 as Formatted>::get_format();
let depth_format = <DepthStencil as Formatted>::get_format();
let (output_color, output_stencil) =
gfx_device_gl::create_main_targets_raw(dim, color_format.0, depth_format.0);
let output_color = Typed::new(output_color);
let output_stencil = Typed::new(output_stencil);

let mut g2d = Gfx2d::new(opengl, &mut factory);
let mut events = Events::new(EventSettings::new());
while let Some(e) = events.next(&mut window) {
use graphics::*;

if let Some(args) = e.render_args() {
g2d.draw(
&mut encoder,
&output_color,
&output_stencil,
args.viewport(),
|c, g|
{
use graphics::triangulation::{tx, ty};

let transform = c.transform.trans(0.0, 0.0);

let tr = |p: [f64; 2]| [tx(transform, p[0], p[1]), ty(transform, p[0], p[1])];

clear([1.0; 4], g);
Rectangle::new([1.0, 0.0, 0.0, 1.0])
.draw([0.0, 0.0, 100.0, 100.0], &c.draw_state, c.transform, g);
Rectangle::new([0.0, 1.0, 0.0, 0.3])
.draw([50.0, 50.0, 100.0, 100.0], &c.draw_state, c.transform, g);
g.tri_list_uv_c(&c.draw_state, &rust_logo, |f| {
(f)(
&[
tr([0.0, 0.0]),
tr([300.0, 0.0]),
tr([0.0, 300.0]),

tr([300.0, 0.0]),
tr([0.0, 300.0]),
tr([300.0, 300.0]),
],
&[
[0.0, 0.0],
[1.0, 0.0],
[0.0, 1.0],

[1.0, 0.0],
[0.0, 1.0],
[1.0, 1.0],
],
&[
[1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 1.0],

[0.0, 00.0, 0.0, 1.0],
[0.0, 00.0, 0.0, 1.0],
[0.0, 00.0, 0.0, 1.0],
]
)
});
});
encoder.flush(&mut device);
}
if let Some(_) = e.after_render_args() {
device.cleanup();
}
}
}
Loading

0 comments on commit 8396cd9

Please sign in to comment.