移动鼠标时抖动 [英] Jittering when moving mouse
问题描述
我有一个 opengl 程序,我正在使用 SDL 进行窗口创建/事件处理等.现在,当我移动鼠标以重新定位摄像机偏航/俯仰时,我得到一个抖动的图像,旧位置闪烁在屏幕上一瞬间.
I have an opengl program and I am using SDL for the window creating / event handling etc. Now, when I move the mouse to re-position the cameras yaw/pitch, I get a jittering image, with the old position flashing on the screen for a split second.
现在我认为这可能是我重新定位鼠标的方式的问题.在重新定位结束时,我将其设置回屏幕的中心.这是上述代码:
Now i think this may be an issue with the way I re-position the mouse. I have it set back to the center of the screen at the end of the re-positioning. Here is the said code:
void handleMouseMove(int mouseX, int mouseY)
{
GLfloat vertMouseSensitivity = 10.0f;
GLfloat horizMouseSensitivity = 10.0f;
int horizMovement = mouseX - midWindowX;
int vertMovement = mouseY - midWindowY;
camXRot += vertMovement / vertMouseSensitivity;
camYRot += horizMovement / horizMouseSensitivity;
if (camXRot < -90.0f)
{
camXRot = -90.0f;
}
if (camXRot > 90.0f)
{
camXRot = 90.0f;
}
if (camYRot < -180.0f)
{
camYRot += 360.0f;
}
if (camYRot > 180.0f)
{
camYRot -= 360.0f;
}
// Reset the mouse to center of screen
SDL_WarpMouse(midWindowX, midWindowY);
}
现在,在我的主 while 循环中,这是调用此函数的 SDL 代码:
Now, Inside my main while loop, here is the SDL code for calling this function:
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_KEYDOWN )
{
if( event.key.keysym.sym == SDLK_ESCAPE )
{
Game.running = false;
}
}
if( event.type == SDL_MOUSEMOTION )
{
int mouse_x, mouse_y;
SDL_GetMouseState(&mouse_x, &mouse_y);
handleMouseMove(mouse_x, mouse_y);
}
我发现像这样获取坐标 event.motion.x
和 event.motion.x
造成的这种影响较小.
I found that getting the coordinates like this verses event.motion.x
and event.motion.x
caused less of this effect.
关于如何避免这种抖动"图像的任何想法?
Any thoughts on how I could avoid this 'jittering' image?
推荐答案
当您SDL_WarpMouse
时,会生成另一个鼠标移动事件,您正在再次处理该事件.你必须忽略第二个.
When you SDL_WarpMouse
, another mouse move event is generated that you are handling again. You must ignore the second one.
为了让事情变得更复杂,并不是每个运动事件都跟随着由 SDL_WarpMouse
引起的相反方向的一个事件.事件可能来自例如两个运动和 1 个由扭曲到屏幕中间产生的运动.我写了这段对我有用的代码:
To make things more complicated, it's not like every motion event is followed by one in the opposite direction caused by SDL_WarpMouse
. The events may come for example two motions and 1 generated by warp to the middle of the screen. I have written this code which works for me:
void motion(SDL_MouseMotionEvent *mme)
{
static bool initializing = true; // For the first time
static int warpMotionX = 0, warpMotionY = 0; // accumulated warp motion
if (initializing)
{
if (mme->x == midWindowX && mme->y == midWindowY)
initializing = false;
else
SDL_WarpMouse(midWindowX, midWindowY);
}
else if (mme->xrel != -warpMotionX || mme->yrel != -warpMotionY)
{
/****** Mouse moved by mme->xrel and mme->yrel ******/
warpMotionX += mme->xrel;
warpMotionY += mme->yrel;
SDL_WarpMouse(midWindowX, midWindowY);
}
else // if event motion was negative of accumulated previous moves,
// then it is generated by SDL_WarpMotion
warpMotionX = warpMotionY = 0;
}
void processEvents()
{
SDL_Event e;
while (SDL_PollEvent(&e))
{
switch (e.type)
{
case SDL_MOUSEMOTION:
motion(&e.motion);
break;
default:
break;
}
}
}
请注意,我自己对 -mme->yrel
比 mme->yrel
感兴趣,这就是为什么它在原始代码中我在任何地方都否定它我使用它(我在上面发布的代码中删除了它)
Note that, I myself am interested in -mme->yrel
than mme->yrel
and that's why it's in the original code I have it negated wherever I use it (I removed it in the code I posted above)
我写的地方 /****** Mouse move by mme->xrel and mme->yrel ******/
是你想要对鼠标进行操作的地方运动.其他代码是忽略由 SDL_WarpMouse
Where I wrote /****** Mouse moved by mme->xrel and mme->yrel ******/
is where you want to act on the mouse motion. Other code is to ignore motion generated by SDL_WarpMouse
这篇关于移动鼠标时抖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!