gpt4 book ai didi

c++ - SDL 在没有焦点时不会停止移动鼠标 - SDL C++

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:54 38 4
gpt4 key购买 nike

我希望我的游戏引擎停止将鼠标移动到中心(用于偏航和俯仰相机计算)。我写了一些应该处理它的代码,但鼠标在最小化时仍然移动。

void mainLoop()
{
// This is the main logic portion of the engine
bool done = false;
bool visible = true;
SDL_Event event;

// Check to make sure we are supposed to quit
while(! done)
{
// Check for SDL events
while( SDL_PollEvent(& event) )
{
// Figure out which event the user has triggered
switch ( event.type )
{
case SDL_QUIT :
done = true;
break;
case SDL_ACTIVEEVENT:
if( event.active.state & SDL_APPACTIVE )
{
//If the application is no longer active
if(event.active.gain == 0)
{
visible = false;
}
else
{
visible = true;
}
}
case SDL_KEYDOWN :

// Check for user input
switch(event.key.keysym.sym)
{
// Escape - end the program
case SDLK_ESCAPE :
done = true;
break;
// Plus key - increase the speed of the camera
case SDLK_PLUS :
case SDLK_KP_PLUS :
if(camera.walkSpeed < 20.0f) { camera.walkSpeed += 1.0f; }
break;
// Minus key - decrease the speed of the camera
case SDLK_KP_MINUS :
case SDLK_MINUS :
if(camera.walkSpeed > 2.0f) {camera.walkSpeed -= 1.0f; }
break;
// F1 - save a TGA screenshot
case SDLK_F1:
saveScreenshot();
break;
// All other unassigned keys
default:
break;
}
}
}
// All events have been handled, now handle logic and rendering
if(visible)
{
updateFrame();
renderFrame();
}
}
}

当应用程序失去焦点时,它应该将可见标志设置为 false,从而停止更新和渲染功能。有什么想法吗?

最佳答案

试试这个:

#include <SDL.h>
#include <iostream>

using namespace std;

int main( int argc, char** argv )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_SetVideoMode( 640, 480, 32, SDL_ANYFORMAT );

bool visible = true;
bool done = false;
while( !done )
{
SDL_Event event;
while( SDL_PollEvent(& event) )
{
switch ( event.type )
{
case SDL_QUIT :
done = true;
break;
case SDL_ACTIVEEVENT:
if( event.active.state & SDL_APPACTIVE ||
event.active.state & SDL_APPINPUTFOCUS )
{
visible = (event.active.gain == 1);
}
}
}

if(visible)
{
static unsigned int frame = 0;
frame++;
cout << "frame: " << frame << endl;
}

SDL_Delay( 10 );
}

SDL_Quit();
return 0;
}

除了 SDL_APPACTIVE 之外,您还需要检查 SDL_APPINPUTFOCUS

关于c++ - SDL 在没有焦点时不会停止移动鼠标 - SDL C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9439903/

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