SDL mouse events are not being handled quick enough

若如初见. 提交于 2019-12-11 05:38:43

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/20620780/sdl-mouse-events-are-not-being-handled-quick-enough

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!