not-fl3 / macroquad

Cross-platform game engine in Rust.
Apache License 2.0
3.04k stars 297 forks source link

set_cursor_grab works as expected in example code but not on my own #731

Open AntCursor opened 1 month ago

AntCursor commented 1 month ago

the set_cursor_grab function works totally fine in the first_person.rs example (not letting the mouse go out, and "looping" the cursor), but in my pratically equal code it doesn't: (Ignore my bad skill)

mod camera;

use camera::FirstPerson;
use macroquad::{
    miniquad::window::{set_cursor_grab, show_mouse},
    prelude::*,
};

const MOVE_SPEED: f32 = 0.2;
const LOOK_SPEED: f32 = 10.;

#[macroquad::main("3D")]
async fn main() {
    let mut cam: FirstPerson = FirstPerson {
        ..Default::default()
    };

    let mut grabbed = true;
    set_cursor_grab(grabbed);
    show_mouse(false);

    loop {
        clear_background(LIGHTGRAY);

        let delta = get_frame_time();

        if is_key_pressed(KeyCode::Escape) {
            break;
        }
        if is_key_pressed(KeyCode::Tab) {
            grabbed = !grabbed;
            set_cursor_grab(grabbed);
            show_mouse(!grabbed);
        }
        cam.movement(MOVE_SPEED);

        let mouse_delta = -mouse_delta_position();
        cam.mouse_direction_change(mouse_delta, LOOK_SPEED, delta);

        set_camera(&cam.get_camera());

        draw_grid(100, 1., BLACK, GRAY);

        set_default_camera();
        draw_text(
            &format!("{:?}  {:?}", mouse_position_local(), mouse_delta),
            15.,
            15.,
            20.,
            RED,
        );

        next_frame().await;
    }
}