没有鼠标移动,PollEvent 无法工作[SDL]
PollEvent not working without mouse movement[SDL]
如果鼠标未在 window 范围内移动或在本应 运行 动画时未按下任何键,则绕过 PollEvent。这是代码:
SDL_Init(SDL_INIT_EVERYTHING);
int imgFlags = IMG_INIT_PNG | IMG_INIT_JPG;
if (IMG_Init(imgFlags) != imgFlags)
{
std::cout << IMG_GetError() << std::endl;
}
window = SDL_CreateWindow("NRG", 200, 200, 800, 600, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED| SDL_RENDERER_PRESENTVSYNC);
isRunning = true;
while (isRunning)
{
while (SDL_PollEvent(&ev))
{
heroImg = LoadTxt("image.png", renderer);
SDL_QueryTexture(heroImg, NULL, NULL, &textureWidth, &textureHeight);
frameWidth = textureWidth / 3;
frameHeight = textureHeight / 4;
heroRct.x = 0;
heroRct.y = 0;
heroRct.h = frameHeight;
heroRct.w = frameWidth;
frameTime++;
if (60 / frameTime == 4)
{
frameTime = 0;
heroRct.x += frameWidth;
if (heroRct.x >= textureWidth)
heroRct.x = 0;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, heroImg, &heroRct, NULL);
SDL_RenderPresent(renderer);
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
window = NULL;
windowSurface = NULL;
renderer = NULL;
SDL_Quit();
其他东西,例如 SDL_Event ev;包含在 .h 文件中
对于有同样问题的人:
将渲染等放在 while (SDL_PollEvent(&ev)) 循环之外 :)
愚蠢的错误,初学者很难解决:)
如果鼠标未在 window 范围内移动或在本应 运行 动画时未按下任何键,则绕过 PollEvent。这是代码:
SDL_Init(SDL_INIT_EVERYTHING);
int imgFlags = IMG_INIT_PNG | IMG_INIT_JPG;
if (IMG_Init(imgFlags) != imgFlags)
{
std::cout << IMG_GetError() << std::endl;
}
window = SDL_CreateWindow("NRG", 200, 200, 800, 600, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED| SDL_RENDERER_PRESENTVSYNC);
isRunning = true;
while (isRunning)
{
while (SDL_PollEvent(&ev))
{
heroImg = LoadTxt("image.png", renderer);
SDL_QueryTexture(heroImg, NULL, NULL, &textureWidth, &textureHeight);
frameWidth = textureWidth / 3;
frameHeight = textureHeight / 4;
heroRct.x = 0;
heroRct.y = 0;
heroRct.h = frameHeight;
heroRct.w = frameWidth;
frameTime++;
if (60 / frameTime == 4)
{
frameTime = 0;
heroRct.x += frameWidth;
if (heroRct.x >= textureWidth)
heroRct.x = 0;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, heroImg, &heroRct, NULL);
SDL_RenderPresent(renderer);
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
window = NULL;
windowSurface = NULL;
renderer = NULL;
SDL_Quit();
其他东西,例如 SDL_Event ev;包含在 .h 文件中
对于有同样问题的人: 将渲染等放在 while (SDL_PollEvent(&ev)) 循环之外 :)
愚蠢的错误,初学者很难解决:)