2

I'm hitting what I assume is a pretty basic misunderstanding of how to draw things using the Rust Piston library. Here is the main function I'm working with, mostly copied from beginner tutorials.

extern crate piston_window;

use piston_window::*;

fn main() {
    let dims = (640.0, 480.0);
    let mut window: PistonWindow =
        WindowSettings::new("Hello, Piston!", (dims.0 as u32, dims.1 as u32))
            .exit_on_esc(true)
            .build()
            .unwrap();
    while let Some(e) = window.next() {
        match e {
            Event::Loop(Loop::Render(_)) => {
                window.draw_2d(&e, |c, g| {
                    clear([1.0; 4], g);
                    rectangle(
                        [1.0, 0.0, 0.0, 1.0],
                        [0.0, 0.0, dims.0, dims.1],
                        c.transform,
                        g,
                    );
                });
            }
            _ => {}
        }
    }
}

I'm trying to draw a red rectangle the size of the window, but when I run this function, I get a red box drawn on the lower-left corner of the frame, not occupying the whole window as I might expect.

red rectangle appears to the lower left corner of the frame

Oddly enough, if I change the dimensions to (480.0, 480.0) so that the window is a square, the box fills up the window like I would expect. I imagine this is simply me misunderstanding how Piston treats coordinates. What exactly am I missing here, and why does it seem like the position (0, 0) is halfway down the window on the left?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116

0 Answers0