简体   繁体   中英

SDL2 relative mouse movement limit

When I try to get the mouse relative motion, it casts to (signed char), because the maximum value is 127 and minimum is -127. I don't know how to fix it.

SDL_SetWindowGrab(window, SDL_TRUE);
SDL_SetRelativeMouseMode(SDL_TRUE);

signed short int mouseDX;
signed short int mouseDY;

while(true)
{
    mouseDX = eventSDL.motion.xrel;
    mouseDY = eventSDL.motion.yrel;

    if(logic->events->mouseDX != 0)
    {
        std::cout << logic->events->mouseDX << "\n";
    }
}

output:

控制台的屏幕截图

This might solve your problem. It calculates the relative motion by keeping track of previous and current absolute positions.

SDL_SetWindowGrab(window, SDL_TRUE);
SDL_SetRelativeMouseMode(SDL_TRUE);

int prev_x, curr_x, rel_x; // previous, current and relative x-coordinates
int prev_y, curr_y, rel_y; // previous, current and relative y-coordinates

// Load the current position
curr_x = eventSDL.motion.x;
curr_y = eventSDL.motion.y;

while(true)
{

    // Remember the last position
    prev_x = curr_x;
    prev_y = curr_y;

    // Update the current position
    curr_x = eventSDL.motion.x;
    curr_y = eventSDL.motion.y;

    // Calculate the relative movement
    rel_x = curr_x - prev_x;
    rel_y = curr_y - prev_y;

}

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