简体   繁体   中英

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.

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 */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM