int main( int argc, char* args[] )
{
if( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
std::cout<<"unable to init sdl";
SDL_Surface *screen = SDL_SetVideoMode(800,600,32,SDL_DOUBLEBUF);
std::cout<<"before while\n";
SDL_Event event;
while(SDL_PollEvent(&event))
{
std::cout<<"in while\n";
if(event.type == SDL_QUIT)
std::cout<<"SDL_QUIT\n";
}
std::cout<<"after while\n";
SDL_Quit();
}
由于某些未知原因,此SDL应用程序在while循环中运行4次后退出而没有我杀死/关闭/等它并且没有将“SDL_QUIT”打印到stdout。 是否有一个原因?我该如何解决?
答案 0 :(得分:4)
您需要通过为其创建主循环来保持应用程序的活跃性。截至目前,您的应用程序只是在您轮询所有初始事件后退出:
int main( int argc, char* args[] )
{
if( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
std::cout<<"unable to init sdl";
SDL_Surface *screen = SDL_SetVideoMode(800,600,32,SDL_DOUBLEBUF);
SDL_Event event;
bool active = true;
while(active)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
std::cout<<"SDL_QUIT\n";
active = false;
}
}
// TODO: add drawing to screen
SDL_Flip(screen);
}
SDL_Quit();
}
答案 1 :(得分:4)
SDL_PollEvent
将返回false,这在应用程序启动后就是正确的。
您需要将poll循环嵌套在另一个使应用程序保持活动状态的循环中:
int running = 1;
while (running)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
running = 0;
}
// Update and draw here usually
}