gpt4 book ai didi

c++ - SDL 鼠标事件的处理速度不够快

转载 作者:行者123 更新时间:2023-11-30 01:55:33 25 4
gpt4 key购买 nike

我已经使用 SDL 好几天了,虽然我认为我掌握了它的窍门,但我在菜单方面遇到了一个小问题。更具体地说,菜单中的按钮。当鼠标移动得不是那么快时,它们可以完美地工作,但是当我稍微加快速度时(在按钮之间移动光标,不断使按钮从“正常” Sprite 重绘为“鼠标悬停 Sprite ”),它就会滞后落后,有时根本不更新,小错误开始出现。

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

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

和:

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(Screen); 移到 while(SDL_PollEvent(&Ev)) 循环之外。通过像这样调用 SDL_RenderPresent,您可能每秒仅轮询 60 个事件(如果启用了垂直同步,则每帧一个),从而导致您看到的延迟。如果将 SDL_RenderPresent 移出事件循环,您将能够处理队列中可用的事件,完成后呈现帧并等待垂直同步。

关于c++ - SDL 鼠标事件的处理速度不够快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20620780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com