How can the resolution of a PistonWindow be changed after creation? - rust

I'd like to change the resolution of a PistonWindow after it has been created. This would allow a user to change the resolution of the window while playing the game.
WindowSettings has a set_size method, but it doesn't seem to be accessible after the PistonWindow type is created.
A simplified example:
extern crate piston_window;
use piston_window::*;
fn main() {
let mut window: PistonWindow = WindowSettings::new("Game Title", [200, 200])
.exit_on_esc(true)
.build()
.unwrap();
// Do some logic here, and change resolution to 400 x 400.
while let Some(e) = window.next() {
window.draw_2d(&e, |c, g| {
ellipse([1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 100.0, 100.0], c.transform, g)
});
}
}

As far as I know, you can't. At least not of an existing window.
However, you can store the WindowSettings and re-create your window based on it when the user changes the resolution.

Related

How can I manually create meshes in bevy with vertices?

What do I have to do to to create a mesh for bevy with the following vertices:
let mut vertices : Vec<[f32; 3]> = Vec::new();
vertices.push([0.0, 0.0, 0.0]);
vertices.push([1.0, 2.0, 1.0]);
vertices.push([2.0, 0.0, 0.0]);
I then want to spawn a MeshBundle like so
commands
.spawn(MeshBundle {
mesh: mesh,
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..Default::default()
});
This answer has been updated for the latest bevy = "0.9.1" and uses the default shaders.
The code below demonstrates how to:
Define vertex positions for a bevy::render::pipeline::PrimitiveTopology::TriangleList
Assign vertex normals and uv coordinates to the vertices
Create a triangle using the 3 vertices we defined
It is based on the built in shapes in bevy, which can be found here.
use bevy::prelude::*;
use bevy::render::mesh::{self, PrimitiveTopology};
fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
// Positions of the vertices
// See https://bevy-cheatbook.github.io/features/coords.html
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![[0., 0., 0.], [1., 2., 1.], [2., 0., 0.]],
);
// In this example, normals and UVs don't matter,
// so we just use the same value for all of them
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vec![[0., 1., 0.]; 3]);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, vec![[0., 0.]; 3]);
// A triangle using vertices 0, 2, and 1.
// Note: order matters. [0, 1, 2] will be flipped upside down, and you won't see it from behind!
mesh.set_indices(Some(mesh::Indices::U32(vec![0, 2, 1])));
commands.spawn(PbrBundle {
mesh: meshes.add(mesh),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
You will have to define your own positions, uvs and normals according to your use case. Some shaders won't need all of these mesh attributes.
#frankenapps answer updated for bevy = 0.7
use bevy::prelude::*;
use bevy::render::mesh::{self, PrimitiveTopology};
fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
}
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let vertices = [
([0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0]),
([1.0, 2.0, 1.0], [0.0, 1.0, 0.0], [1.0, 1.0]),
([2.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0]),
];
let indices = mesh::Indices::U32(vec![0, 2, 1, 0, 3, 2]);
let mut positions = Vec::new();
let mut normals = Vec::new();
let mut uvs = Vec::new();
for (position, normal, uv) in vertices.iter() {
positions.push(*position);
normals.push(*normal);
uvs.push(*uv);
}
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.set_indices(Some(indices));
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
// add entities to the world
// plane
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(mesh),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
// light
commands.spawn_bundle(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0)
.looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}

Attempted to leave type `PointerState` uninitialized

I wanted to try to make a game in Rust using Piston. This is the first time I have used this library. I took this code from the official doc to test it. However, when my mouse touches the application window it closes immediately and I don’t understand why.
extern crate glutin_window;
extern crate graphics;
extern crate opengl_graphics;
extern crate piston;
use glutin_window::GlutinWindow as Window;
use opengl_graphics::{GlGraphics, OpenGL};
use piston::event_loop::{EventSettings, Events};
use piston::input::{RenderArgs, RenderEvent, UpdateArgs, UpdateEvent};
use piston::window::WindowSettings;
pub struct App {
gl: GlGraphics, // OpenGL drawing backend.
rotation: f64, // Rotation for the square.
}
impl App {
fn render(&mut self, args: &RenderArgs) {
use graphics::*;
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
let square = rectangle::square(0.0, 0.0, 50.0);
let rotation = self.rotation;
let (x, y) = (args.window_size[0] / 2.0, args.window_size[1] / 2.0);
self.gl.draw(args.viewport(), |c, gl| {
// Clear the screen.
clear(GREEN, gl);
let transform = c
.transform
.trans(x, y)
.rot_rad(rotation)
.trans(-25.0, -25.0);
// Draw a box rotating around the middle of the screen.
rectangle(RED, square, transform, gl);
});
}
fn update(&mut self, args: &UpdateArgs) {
// Rotate 2 radians per second.
self.rotation += 2.0 * args.dt;
}
}
fn main() {
// Change this to OpenGL::V2_1 if not working.
let opengl = OpenGL::V3_2;
// Create an Glutin window.
let mut window: Window = WindowSettings::new("spinning-square", [200, 200])
.graphics_api(opengl)
.exit_on_esc(true)
.build()
.unwrap();
// Create a new game and run it.
let mut app = App {
gl: GlGraphics::new(opengl),
rotation: 0.0,
};
let mut events = Events::new(EventSettings::new());
while let Some(e) = events.next(&mut window) {
if let Some(args) = e.render_args() {
app.render(&args);
}
if let Some(args) = e.update_args() {
app.update(&args);
}
}
}
The error:
thread 'main' panicked at 'attempted to leave type `platform::platform::x11::util::input::PointerState` uninitialized, which is invalid', /home/zenmoa/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:658:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
This is apparently a bug in an old version of winit, which is fixed in the latest winit release. However, various crates e.g. amethyst, piston, ggez, etc. still use winit 0.19. In Rust 1.48.0 the issue has apparently manifested itself as a panic.
On the amethyst issue tracker a comment mentions, that for now a possible workaround is to revert back to Rust 1.47.0. If you're using rustup, then you can do that by executing the following command:
rustup default 1.47.0

Piston create a a block of multiple square

i want to create something like a square of square like a a square composed of 4 small squares (4x4) for a Tetris I know how to create a simple square and move it,
but I don't know how to create a more complex figure like a 4x4 square or an L and move it like a simple square do you have any idea ?
here the code if you need it =>
extern crate glutin_window;
extern crate graphics;
extern crate opengl_graphics;
extern crate piston;
use glutin_window::GlutinWindow;
use graphics::rectangle;
use opengl_graphics::{GlGraphics, OpenGL};
use piston::event_loop::{EventSettings, Events};
use piston::input::{
Button, Key, PressEvent, ReleaseEvent, RenderArgs, RenderEvent, UpdateArgs, UpdateEvent,
};
use piston::window::WindowSettings;
use piston_window::Transformed;
use std::process;
struct Game {
gl: GlGraphics,
pos_x: f64,
pos_y: f64,
}
impl Game {
fn render(&mut self, arg: &RenderArgs) {
use graphics;
let BACKGROUND: [f32; 4] = [0.349019608, 0.349019608, 0.290196078, 1.0];
let COO: [f32; 4] = [0.364705882, 0.717647059, 0.870588235, 0.8];
let square_c = rectangle::square(100.0, 100.0, 30.0);
let pos_x = self.pos_x as f64;
let pos_y = self.pos_y as f64;
println!("pos x = {}", pos_x);
println!("pos y = {}", pos_y);
self.gl.draw(arg.viewport(), |c, gl| {
graphics::clear(BACKGROUND, gl);
rectangle(COO, square_c, c.transform.trans(pos_x, pos_y), gl); // deplace le carre de -200 vers la gauche
});
}
fn press(&mut self, args: &Button) {
if let &Button::Keyboard(key) = args {
match key {
Key::Up => self.pos_y -= 10.0,
Key::Down => self.pos_y += 10.0,
Key::Left => self.pos_x -= 10.0,
Key::Right => self.pos_x += 10.0,
_ => {
println!("other1");
}
}
}
}
fn release(&mut self, args: &Button) {
if let &Button::Keyboard(key) = args {
match key {
Key::Up => {
println!("Up release");
}
Key::Down => {
println!("Down release");
}
Key::Left => {
println!("Left release");
}
Key::Right => {
println!("Right release");
}
_ => {
println!("other release");
}
}
}
}
}
fn main() {
let opengl = OpenGL::V3_2;
let mut window: GlutinWindow = WindowSettings::new("Tetris Game", [400, 800])
.graphics_api(opengl)
.exit_on_esc(true)
.build()
.unwrap();
let mut game = Game {
gl: GlGraphics::new(opengl),
pos_x: 200.0,
pos_y: 0.0,
};
let mut events = Events::new(EventSettings::new());
while let Some(e) = events.next(&mut window) {
if let Some(r) = e.render_args() {
game.render(&r);
}
if let Some(b) = e.press_args() {
game.press(&b);
}
if let Some(b) = e.release_args() {
game.release(&b);
}
}
}
If you want to create a square of 4 squares, then you just have to repeat what you already have, i.e. call rectangle 4 times and position the rectangles accordingly.
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
const YELLOW: [f32; 4] = [1.0, 1.0, 0.0, 1.0];
let (x, y) = (50.0, 50.0);
rectangle(RED, rectangle::square( 0.0, 0.0, 30.0), c.transform.trans(x, y), gl);
rectangle(GREEN, rectangle::square(30.0, 0.0, 30.0), c.transform.trans(x, y), gl);
rectangle(BLUE, rectangle::square( 0.0, 30.0, 30.0), c.transform.trans(x, y), gl);
rectangle(YELLOW, rectangle::square(30.0, 30.0, 30.0), c.transform.trans(x, y), gl);
The above renders a 4 rectangles that look like this:
If you want to create an L shape, then you can just position 2 rectangles to create that shape.
rectangle(RED, [0.0, 0.0, 15.0, 30.0], c.transform.trans(x, y), gl);
rectangle(GREEN, [0.0, 30.0, 30.0, 15.0], c.transform.trans(x, y), gl);
You could also bake the positions directly into the rectangle shape, instead of using .trans(x, y), i.e. for the last one it would be [x, y + 30.0, 30.0, 15.0].
If you instead want to create a single polygon from a series of points, then you can use polygon(). The following creates the same L shape as before, just this time it's completely red.
polygon(
RED,
&[
[ 0.0, 45.0],
[ 0.0, 0.0],
[15.0, 0.0],
[15.0, 30.0],
[30.0, 30.0],
[30.0, 45.0],
],
c.transform.trans(x, y),
gl,
);
In the piston2d-graphics crate's documentation you can see additional draw functions, such as ellipse(), image() and text()

Piston GenericEvent not implemented for Event

I am trying to write a game in Rust using piston_window(0.77.0) library. Starting from their hello world example I thought I would start by separating of rendering logic into a method using Event as parameter since according to documentation it is returned by window.next().
use piston_window::*;
pub struct UI<'a> {
window: &'a mut PistonWindow,
}
impl <'a> UI<'a> {
pub fn new(window: &mut PistonWindow) -> UI {
UI {
window,
}
}
fn render(&self, event: &Event) {
self.window.draw_2d(&event, |context, graphics| {
clear([1.0; 4], graphics);
rectangle(
[1.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 100.0, 100.0],
context.transform,
graphics);
});
}
pub fn run(&mut self) {
use Loop::Render;
use Event::Loop;
while let Some(event) = self.window.next() {
match event {
Loop(Render(_)) => self.render(&event),
_ => {}
}
}
}
}
However this ends with an error:
self.window.draw_2d(&event, |context, graphics| {
the trait piston_window::GenericEvent is not implemented for &piston_window::Event
Code without extracted render method works as expected.
pub fn run(&mut self) {
use Loop::Render;
use Event::Loop;
while let Some(event) = self.window.next() {
match event {
Loop(Render(_)) => {
self.window.draw_2d(&event, |context, graphics| {
clear([1.0; 4], graphics);
rectangle(
[1.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 100.0, 100.0],
context.transform,
graphics);
});
},
_ => {}
}
}
}
How can I extract this? Is there something I am overlooking?
The event variable has type &Event, not Event, so you are actually trying to pass an &&Event to window.draw_2d. Event implements GenericEvent but &Event does not, which is why you see that error.
You just need to do:
self.window.draw_2d(event, |context, graphics| {
...
}
instead of:
self.window.draw_2d(&event, |context, graphics| {
...
}
To be fair on the Rust compiler, it couldn't have done much more to point you in the right direction. When I compile your code, the full error message is:
error[E0277]: the trait bound `&piston_window::Event: piston_window::GenericEvent` is not satisfied
--> src/main.rs:34:21
|
34 | self.window.draw_2d(&event, |context, graphics| {
| ^^^^^^^ the trait `piston_window::GenericEvent` is not implemented for `&piston_window::Event`
|
= help: the following implementations were found:
<piston_window::Event as piston_window::GenericEvent>
That last "help" part is telling you that piston_window::Event does have the right implementation, while the preceding error is saying that &piston_window::Event does not.

How do I stop Piston from making the screen flash when I don't call `graphics::clear` every time the screen is rendered?

Consider two programs, and the difference between them:
$ diff flashes/src/main.rs doesnt_flash/src/main.rs
22,23c22
<
< let mut i = 0;
---
> let mut cursor_poses: Vec<(f64, f64)> = Vec::new();
28c27
< mx = x; my = y;
---
> cursor_poses.push((x,y));
32,33c31,33
< if i == 0 {
< graphics::clear([1.0; 4], g);
---
> graphics::clear([1.0; 4], g);
> for &(x, y) in cursor_poses.iter() {
> draw_cursor_pos([x, y], &c, g);
35,36d34
< draw_cursor_pos([mx, my], &c, g);
< i+=1;
Video demonstration of the two programs.
The program is an extremely basic paint program, with only one brush width, brush stroke color, canvas size, no saving, etc; oh and to stop drawing, move your mouse out of the window, since every time you go over the window this counts as drawing ;-)
flashes.rs does not draw every pixel every time e.render_args() is reached, except the first time. doesnt_flash.rs does draw every pixel every time e.render_args() is reached. This is the only difference between the two programs.
While it does not take long to generate the content in this program, so it is acceptable to re-generate it hundreds of times as the mouse moves over the window, this seems inefficient. In theory, as more and more points are added to the screen, each iteration of gl.draw takes longer and longer. In practice, the difference between calling graphics::ellipse one time vs. ten thousand times is not significant on modern hardware.
Other programs I'd want to write won't have that luxury as it will take longer to generate the result to put on the screen.
While perusing the API, I came up with no obvious way to just "do nothing". I assume that I would have to write my screen changes to some buffer object, then feed GlGraphics back this buffer object if e.render_args() is called but I don't need to update the screen.
The problem is, I can't seem to find this buffer object. :-(
How can I "do nothing" without getting screen flashing? If my theory is correct, how can I draw to a GlGraphics buffer instead of the screen, then feed my buffer back to the screen when I don't have anything new to draw?
Cargo.toml
[package]
name = "stackoverflow-piston-example"
version = "0.0.0"
authors = ["Fred"]
description = "Note: This program can be used for both of the programs below. Simply use `cargo new` and save either of the below files as `src/main.rs`"
keywords = []
[dependencies]
piston = "0.35.0"
piston2d-opengl_graphics = "0.50.0"
piston2d-graphics = "0.24.0"
piston2d-touch_visualizer = "0.8.0"
pistoncore-sdl2_window = "0.47.0"
doesnt_flash.rs
extern crate piston;
extern crate opengl_graphics;
extern crate graphics;
extern crate touch_visualizer;
extern crate sdl2_window;
use opengl_graphics::{ GlGraphics, OpenGL };
use graphics::{ Context, Graphics };
use piston::input::*;
use piston::event_loop::*;
use sdl2_window::Sdl2Window as AppWindow;
static CURSOR_POS_COLOR: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
fn main() {
let opengl = OpenGL::V3_2;
let mut window: AppWindow = piston::window::WindowSettings::new("Example for StackOverflow", [600, 600])
.exit_on_esc(true).opengl(opengl).build().unwrap();
let ref mut gl = GlGraphics::new(opengl);
let (mut mx, mut my) = (0., 0.);
let mut cursor_poses: Vec<(f64, f64)> = Vec::new();
let mut events = Events::new(EventSettings::new().lazy(true));
while let Some(e) = events.next(&mut window) {
e.mouse_cursor(|x, y| {
cursor_poses.push((x,y));
});
if let Some(args) = e.render_args() {
gl.draw(args.viewport(), |c, g| {
graphics::clear([1.0; 4], g);
for &(x, y) in cursor_poses.iter() {
draw_cursor_pos([x, y], &c, g);
}
}
);
}
}
}
fn draw_cursor_pos<G: Graphics>(
cursor: [f64; 2],
c: &Context,
g: &mut G,
) {
graphics::ellipse(
CURSOR_POS_COLOR,
graphics::ellipse::circle(cursor[0], cursor[1], 4.0),
c.transform,
g
);
}
flashes.rs
extern crate piston;
extern crate opengl_graphics;
extern crate graphics;
extern crate touch_visualizer;
extern crate sdl2_window;
use opengl_graphics::{ GlGraphics, OpenGL };
use graphics::{ Context, Graphics };
use piston::input::*;
use piston::event_loop::*;
use sdl2_window::Sdl2Window as AppWindow;
static CURSOR_POS_COLOR: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
fn main() {
let opengl = OpenGL::V3_2;
let mut window: AppWindow = piston::window::WindowSettings::new("Example for StackOverflow", [600, 600])
.exit_on_esc(true).opengl(opengl).build().unwrap();
let ref mut gl = GlGraphics::new(opengl);
let (mut mx, mut my) = (0., 0.);
let mut i = 0;
let mut events = Events::new(EventSettings::new().lazy(true));
while let Some(e) = events.next(&mut window) {
e.mouse_cursor(|x, y| {
mx = x; my = y;
});
if let Some(args) = e.render_args() {
gl.draw(args.viewport(), |c, g| {
if i == 0 {
graphics::clear([1.0; 4], g);
}
draw_cursor_pos([mx, my], &c, g);
i+=1;
}
);
}
}
}
fn draw_cursor_pos<G: Graphics>(
cursor: [f64; 2],
c: &Context,
g: &mut G,
) {
graphics::ellipse(
CURSOR_POS_COLOR,
graphics::ellipse::circle(cursor[0], cursor[1], 4.0),
c.transform,
g
);
}
I think the flashing is caused by buffer swapping: in flashes.rs only the first buffer to be drawn into is cleared. The second one will be all zeros, or leftover gpu memory if you're unlucky. According to the OpenGL wiki there's no good way around calling graphics::clear:
A modern OpenGL program should always use double buffering. . .
The buffers should always be cleared. On much older hardware, there
was a technique to get away without clearing the scene, but on even
semi-recent hardware, this will actually make things slower. So always
do the clear.
Instead, the usual method is to accumulate your changes to a texture or renderbuffer, and then draw that to the screen, exactly as you described.
I couldn't find any way to do this from within opengl_graphics either (there are no calls to gl::GenFramebuffers anywhere in it) but it's relatively straightforward to set up using raw gl calls. (I've used textures instead of renderbuffers because they have the significant advantage of being supported by high-level methods like Image::draw.)
extern crate piston;
extern crate opengl_graphics;
extern crate graphics;
extern crate sdl2_window;
extern crate gl;
use opengl_graphics::{ GlGraphics, OpenGL, Texture, TextureSettings };
use graphics::{ Context, Graphics, Transformed };
use graphics::image::Image;
use piston::input::*;
use piston::event_loop::*;
use piston::window::Window;
use sdl2_window::Sdl2Window as AppWindow;
use gl::types::GLuint;
static CURSOR_POS_COLOR: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
fn main() {
let opengl = OpenGL::V3_2;
let mut window: AppWindow = piston::window::WindowSettings::new("Example for StackOverflow", [600, 600])
.exit_on_esc(true).opengl(opengl).build().expect("window");
let ref mut gl = GlGraphics::new(opengl);
let (mut mx, mut my) = (0., 0.);
let draw_size = window.draw_size();
// It would also be possible to create a texture by hand using gl::GenTextures and call
// gl::TexImage2D with a null pointer for the data argument, which would require another unsafe
// block but would save this allocation
let texture_buf = vec![0u8; draw_size.width as usize * draw_size.height as usize];
let texture = Texture::from_memory_alpha(&texture_buf, draw_size.width, draw_size.height,
&TextureSettings::new()).expect("texture");
let fbo;
unsafe {
let mut fbos: [GLuint; 1] = [0];
// Create a Framebuffer Object that we can draw to later
gl::GenFramebuffers(1, fbos.as_mut_ptr());
fbo = fbos[0];
// Switch to it as the active framebuffer
gl::BindFramebuffer(gl::FRAMEBUFFER, fbo);
// Set up the framebuffer object so that draws to it will go to the texture
gl::FramebufferTexture2D(gl::FRAMEBUFFER,
gl::COLOR_ATTACHMENT0, // draw colors, not depth or stencil data
gl::TEXTURE_2D, // the texture's type
texture.get_id(),
0); // mipmap level
}
let mut events = Events::new(EventSettings::new().lazy(true));
while let Some(e) = events.next(&mut window) {
e.mouse_cursor(|x, y| {
mx = x; my = y;
});
e.render(|args| {
// Switch to the texture framebuffer and draw the cursor
unsafe {
gl::BindFramebuffer(gl::FRAMEBUFFER, fbo);
}
gl.draw(args.viewport(), |c, g| {
draw_cursor_pos([mx, my], &c, g);
});
// Switch to the window framebuffer and draw the texture
unsafe {
gl::BindFramebuffer(gl::FRAMEBUFFER, 0);
}
gl.draw(args.viewport(), |c, g| {
graphics::clear([1f32, 1f32, 1f32, 0f32], g);
// I can't entirely explain this. We already applied the viewport transform when
// we were rendering the cursor, so I think the texture is right-side-up for GL,
// but piston::Image is expecting an image laid out in screen coordinates.
// Since there is an offset in the viewport transform, the flip has to be applied
// first, otherwise it would flip across the origin.
let flipped = c.transform.prepend_transform(graphics::math::scale(1., -1.));
Image::new().draw(&texture, &c.draw_state, flipped, g);
});
});
}
}
fn draw_cursor_pos<G: Graphics>(
cursor: [f64; 2],
c: &Context,
g: &mut G,
) {
graphics::ellipse(
CURSOR_POS_COLOR,
graphics::ellipse::circle(cursor[0], cursor[1], 4.0),
c.transform,
g
);
}
Alternatively, the gfx backend has the promising-sounding Factory::CreateRenderTarget method. My hardware doesn't support it, but I believe using it would look approximately like this:
extern crate piston;
extern crate graphics;
extern crate piston_window;
extern crate gfx_core;
use graphics::{ Context, Graphics, Transformed };
use graphics::image::Image;
use piston::input::*;
use piston::event_loop::*;
use piston::window::Window;
use piston_window::{ PistonWindow, OpenGL, G2dTexture };
use gfx_core::factory::Factory;
use gfx_core::texture::{ SamplerInfo, FilterMethod, WrapMode, Size };
static CURSOR_POS_COLOR: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
fn main() {
let opengl = OpenGL::V2_1;
let window_settings =
piston::window::WindowSettings::new("Example for StackOverflow", [600, 600])
.opengl(opengl)
.exit_on_esc(true);
let mut window: PistonWindow = window_settings.build().expect("window");
window.set_lazy(true);
let size = window.draw_size();
let (texture_handle, shader_view, target) = window.factory.create_render_target(size.width as Size, size.height as Size)
.expect("render target");
let sampler = window.factory.create_sampler(SamplerInfo::new(FilterMethod::Scale, WrapMode::Tile));
let texture = G2dTexture {
surface: texture_handle,
sampler: sampler,
view: shader_view,
};
let stencil = window.factory.create_depth_stencil_view_only(size.width as Size, size.height as Size)
.expect("stencil");
let (mut mx, mut my) = (0., 0.);
while let Some(e) = window.next() {
e.mouse_cursor(|x, y| {
mx = x; my = y;
});
if let Some(args) = e.render_args() {
window.g2d.draw(&mut window.encoder, &target, &stencil, args.viewport(), |c, g| {
draw_cursor_pos([mx, my], &c, g);
});
window.draw_2d(&e, |c, g| {
graphics::clear([1f32, 1f32, 1f32, 0f32], g);
let flipped = c.transform.prepend_transform(graphics::math::scale(1., -1.));
Image::new().draw(&texture, &c.draw_state, flipped, g);
});
}
}
}
fn draw_cursor_pos<G: Graphics>(
cursor: [f64; 2],
c: &Context,
g: &mut G,
) {
graphics::ellipse(
CURSOR_POS_COLOR,
graphics::ellipse::circle(cursor[0], cursor[1], 4.0),
c.transform,
g
);
}

Resources