SDL 2.0 - 鼠标移动是垃圾事件队列

Pat*_*iet 0 queue mouse events sdl sdl-2

正如小标题所说,鼠标移动是垃圾事件队列,所以当我摇晃鼠标时我不能走路。有什么办法吗?仅鼠标或其他东西的不同队列。

SDL_Event event_;
    CInputManager::Instance()->SetEvent(&event_);
    InitializeGameFiles();
    while (running)
    {
        if (SDL_PollEvent(&event_))
        {
            if (event_.type == SDL_QUIT)
            {
                running = false;
            }
            else if (event_.type == SDL_MOUSEMOTION)
            {
                CCrosshair::Instance()->OnUpdate();
            }
        }
        OnUpdate(SDL_GetTicks() - currentTime);
        currentTime = SDL_GetTicks();
        OnRender();
    }

void OnUpdate(unsigned int deltaTime)
{
    //Game logic here
    CInputManager* IM = CInputManager::Instance();
    CPlayer* player = &CPlayer::PlayerControl;
    IM->UpdateHeyHeld();
    if (IM->IsKeyDown(SDLK_w))
    {
        cam[1] += speed * (deltaTime / 1000.f ); 
    }
    else if (IM->IsKeyDown(SDLK_s))
    {
        cam[1] -= speed * (deltaTime / 1000.f ); 
    }

    if (IM->IsKeyDown(SDLK_a))
    {
        cam[0] += speed * (deltaTime / 1000.f );
    }
    else if (IM->IsKeyDown(SDLK_d))
    {
        cam[0] -= speed * (deltaTime / 1000.f ); 
    }

    if (IM->IsKeyDown(SDLK_q))
    {
        player->OnRotate(- (int) asp * (deltaTime / 1000.f) );
    }
    if (IM->IsKeyDown(SDLK_e))
    {
        player->OnRotate((int)asp * (deltaTime / 1000.f));
    }
    if (IM->IsKeyDown(SDLK_x))
    {
        ent->animation->SetAnimation(0);
    }
    else if (IM->IsKeyDown(SDLK_c))
    {
        ent->animation->StopAnimation();
    }

    ent->animation->OnUpdate();
}
Run Code Online (Sandbox Code Playgroud)

鼠标/十字准线处理在这里并不真正相关,即使没有它也会滞后

ole*_*ard 5

你的问题在这里:

if (SDL_PollEvent(&event_)) // <--- Change if to while
{
    if (event_.type == SDL_QUIT)
    {
        running = false;
    }
    else if (event_.type == SDL_MOUSEMOTION)
    {
        CCrosshair::Instance()->OnUpdate();
    }
}
Run Code Online (Sandbox Code Playgroud)

以一种if方式运行它意味着您每帧只会从事件队列中删除一个事件。即使您只是稍微移动鼠标,SDL 也倾向于发送垃圾邮件事件。而且由于您每帧只从队列中取出一个事件,队列中的鼠标事件数量只会不断增加,而 keydown/up 事件将在队列中“丢失”。远远超过鼠标事件。

您可以查看SDL2 wiki了解更多信息。