2015-05-04 106 views
0

當我嘗試獲取鼠標相對運動時,它轉換爲(有符號字符),因爲最大值爲127,最小值爲-127。我不知道如何解決它。請幫忙。SDL2相對鼠標移動限制

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"; 
    } 
} 

輸出:http://i.imgur.com/0GM67Tu.png

回答

0

這可能會解決你的問題。它通過跟蹤以前和當前的絕對位置來計算相對運動。

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; 

}