SDL鼠标事件处理速度不够快 [英] SDL mouse events are not being handled quick enough

查看:473
本文介绍了SDL鼠标事件处理速度不够快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在SDL玩了好几天了,而且当我想到了它的时候,我遇到了一个小菜单问题。更具体地,菜单中的按钮。当鼠标不快速移动时,它们工作无瑕疵,但当我加快速度(在按钮之间移动光标时,不断地将按钮从正常精灵重新绘制到鼠标悬停精灵),它滞后后面,有时根本就不会更新,小错误开始出现。

I've been playing around with SDL for a few days now, and while I think I got the hang of it, I've run into a small problem with the menu. More specifically, the buttons in the menu. They work flawlessly when the mouse isn't moving that fast, but when I speed it up a bit (moving the cursor between the buttons, constantly making the button redraw from "normal" sprite to "mouse-over sprite"), it lags behind, sometimes doesn't update at all and small bugs start appearing.

这是我关于一般按钮和事件管理的所有代码:

This is all my code regarding the management of buttons and events in general:

while(!Exit)
{
while(SDL_PollEvent(&Ev))
    {

        curTime = SDL_GetTicks();
        if((ControlVar == 1 && Ev.type == SDL_MOUSEMOTION) || (ControlVar == 1 && Ev.type == SDL_MOUSEBUTTONDOWN) || (ControlVar == 1 && Ev.type == SDL_MOUSEBUTTONUP)) {
            But1.doAction();
            But2.doAction();
            But3.doAction();

                if(curTime > lastTime + 25) {
                SDL_RenderClear(Screen);
                ApplyImage("Menu.png");

                But1.Draw();
                But2.Draw();
                But3.Draw();

                SDL_RenderPresent(Screen);
                lastTime = SDL_GetTicks();
            }

        }

        if(Ev.type == SDL_QUIT)
            Exit = true;
    }
SDL_Delay(10);
}

And:

class Button {
int Id;
int Clip;
SDL_Rect box;
std::string Filepath;

public:

    Button::Button(int number, int X, int Y, std::string filename)
    {
    Id = number;
    Clip = 0;
    box.x = X;
    box.y = Y;
    box.w = 300;
    box.h = 40;

    Filepath = filename;
    }

    void Draw()
    {
    SDL_Texture *tex = nullptr;
    SDL_Rect targetRec;
    tex = IMG_LoadTexture(Screen, Filepath.c_str());
    targetRec.h = 40;
    targetRec.w = 300;
    targetRec.x = 0;
    targetRec.y = Clip * 40;

    SDL_RenderCopy(Screen, tex, &targetRec, &box);

    SDL_DestroyTexture(tex);
    }

    void doAction()
    {
        if(Ev.motion.x > box.x && Ev.motion.x < box.x+box.w && Ev.motion.y > box.y && Ev.motion.y < box.y+box.h)
        {
            if(Ev.type == SDL_MOUSEMOTION && Clip != 2)
                Clip = 1;

            if(Ev.type == SDL_MOUSEBUTTONDOWN && Ev.button.button == SDL_BUTTON_LEFT)
                Clip = 2;

            if(Ev.type == SDL_MOUSEBUTTONUP && Ev.button.button == SDL_BUTTON_LEFT)
                Clip = 1;
        }
        else if(Clip != 0)
            Clip = 0;
    }
};


推荐答案

尝试移动 SDL_RenderPresent ); 之外的(SDL_PollEvent(& Ev))循环。通过调用SDL_RenderPresent,您可能每秒仅轮询60个事件(如果启用vsync,则每帧一个),从而导致您看到的延迟。
如果您在事件循环之外移动SDL_RenderPresent,您将能够处理队列中可用的事件数量,完成后可以呈现帧并等待vsync。

Try moving SDL_RenderPresent(Screen); outside of the while(SDL_PollEvent(&Ev)) loop. By calling SDL_RenderPresent like that you are likely polling only 60 events per second (one per frame if vsync is enabled), causing the delay you see. If you move SDL_RenderPresent outside of your event loop, you'll be able to process as many events are there are available in the queue, and when you are done you present the frame and wait for vsync.

这篇关于SDL鼠标事件处理速度不够快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆