Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy event settings uses more cpu than non-lazy event settings #292

Open
jarbus opened this issue Mar 5, 2021 · 2 comments
Open

Lazy event settings uses more cpu than non-lazy event settings #292

jarbus opened this issue Mar 5, 2021 · 2 comments

Comments

@jarbus
Copy link

jarbus commented Mar 5, 2021

I have a small app that uses 1% of my CPU when I have normal rendering, but 12% when I enable rendering only on user input with let mut events = Events::new(EventSettings::new().lazy(true)). I was wondering if there might be a reason for this, given the script below:

extern crate piston;
extern crate graphics;
extern crate opengl_graphics;


use glutin_window::GlutinWindow;
use piston_window::*;

use opengl_graphics::{GlGraphics, OpenGL };

type Color = [f32; 4];

const RED: Color = [1.0, 0.0, 0.0, 1.0];
const GREEN: Color = [0.0, 1.0, 0.0, 1.0];
const BLUE: Color = [0.0, 0.0, 1.0, 1.0];
const WHITE: Color = [1.0, 1.0, 1.0, 1.0];
const BLACK: Color = [0.0, 0.0, 0.0, 1.0];


#[derive(Clone)]
enum Shape {
    Rect,
    Circle
}

#[derive(Clone)]
pub struct Node {
   shape: Shape,
   pos: [f64; 2],
   rot: f32,
   size: f64,
}

impl Node{
    fn draw(&self, c: graphics::context::Context, gl: &mut GlGraphics) {
        if let Some(v) = c.viewport{
            let x0 = (self.pos[0] * v.window_size[0]) - (self.size/2.0);
            let y0 = (self.pos[1] * v.window_size[1]) - (self.size/2.0);

            match self.shape {
                Shape::Rect => graphics::Rectangle::new(WHITE).draw([x0, y0,self.size, self.size], &c.draw_state, c.transform, gl),
                Shape::Circle => graphics::CircleArc::new(WHITE,100.0,0.0,2.0).draw([x0,y0,self.size,self.size], &c.draw_state, c.transform, gl),
            }
        }
    }
    fn new(start_shape: Shape, x:f64, y:f64) -> Node {
        Node {
            shape: start_shape,
            pos: [x, y],
            rot: 0.0,
            size: 100.0,
        }
    }
}

type Graph = Vec<Node>;

fn make_graph() -> Graph {
    let graph = vec![Node::new(Shape::Rect,0.5, 0.5); 1];
    graph
}

fn main() {
    let opengl = OpenGL::V3_2;
    let settings = WindowSettings::new("Roguelike", [512; 2]).exit_on_esc(true);
    let mut window: GlutinWindow = settings.build().expect("Could not create window");
    let mut gl = GlGraphics::new(opengl);
    let mut graph = make_graph();
    let mut events = Events::new(EventSettings::new());
    let mut cursor: [f64; 2] = [0.0, 0.0];
    let mut window_size: [f64; 2] = [0.0,0.0];
    while let Some(e) = events.next(&mut window) {
        if let Some(r) = e.render_args() {
            window_size = r.viewport().window_size;
            gl.draw(r.viewport(), |c, gl| {
                window_size = r.viewport().window_size;
                graphics::clear(BLACK, gl);
                for i in 0..graph.len() {
                    graph[i].draw(c,gl);
                }
            });
        }
        if let Some(pos) = e.mouse_cursor_args() {
            cursor = pos;
        }
        if let Some(button) = e.press_args() {
            match button {
                Button::Mouse(MouseButton::Left) => graph.push(Node::new(Shape::Rect, cursor[0]/window_size[0], cursor[1]/window_size[1])),
                Button::Mouse(MouseButton::Right) => graph.clear(),
                Button::Keyboard(Key::B) => println!("B has been pressed"),
                _ => {},
            }
        }
    }
}
@marcianx
Copy link

I'm running into the same thing. Here's a minimal repro:

use piston_window::*;

fn main() {
    let mut window: PistonWindow = WindowSettings::new("Test", [200, 200])
        .exit_on_esc(true)
        .build()
        .unwrap();
    let lazy = true; // true => 100% CPU, false => ~4% CPU
    let mut events = Events::new(EventSettings::new().lazy(lazy));
    while let Some(event) = events.next(&mut window) {
        window.draw_2d(&event, |_c, g, _d| clear([1.0; 4], g));
    }
}

@marcianx
Copy link

The issue is probably in the loop here since the issue is reproducible with lazy=false by setting ups=0 as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants