Search code examples
rustglium

How can I get the mouse X and Y in Glium + Glutin?


I use Glium to write a game where I have to render an image in a specific position when a click is detected. To do so, I need to get the X and Y position. So how do I get them?

My current code looks like this:

/* ... */
event.run(move |event, _, control_flow| {
    match event {
            glutin::event::Event::WindowEvent { event, .. } => match event {
                glutin::event::WindowEvent::CloseRequested => {
                   println!("Received termination signal.");
                  *control_flow = glutin::event_loop::ControlFlow::Exit;
                    return;
                },
                _ => return,
            },
            glutin::event::Event::NewEvents(cause) => match cause {
                glutin::event::StartCause::ResumeTimeReached { .. } => (),
                glutin::event::StartCause::Init => (),
                _ => return,
            },
            _ => return,
        }

If possible, I wouldn't mind getting OpenGL Coordinates as X and Y (-1, 1), but I think it will be a hassle to pass it to a vertex buffer.


Solution

  • This is the answer I was looking for, it's a WindowEvent:

    match event {
                glutin::event::Event::WindowEvent { event, .. } => match event {
                    glutin::event::WindowEvent::CloseRequested => {
                        println!("Received termination signal.");
                        *control_flow = glutin::event_loop::ControlFlow::Exit;
                        return;
                    },
                    /* The code to get the mouse position (And print it to the console) */
                    glutin::event::WindowEvent::CursorMoved { position, .. } => {
                        println!("Mouse position: {:?}x{:?}", position.x as u16, position.y as u16);
                    }
                    _ => return,
                },
                /* Handle the rest of events */