2
只要我启动程序,窗口就会关闭。这是主要的功能:SDL窗口在启动时关闭并返回0
int main(int argc, char* argv[])
{
if (!init())
{
printf("Could not initialize!");
}
else
{
bool quit = false;
SDL_Event ev;
while(!quit)
{
while(SDL_PollEvent(&ev))
{
if(ev.type = SDL_QUIT)
{
quit = true;
}
}
}
}
close();
return 0;
}
添加的printf()语句,尽快把范围缩小到这部分
while(SDL_PollEvent(&ev))
{
if(ev.type = SDL_QUIT)
{
quit = true;
}
}
如果我改变while(SDL_PollEvent(&ev))
到while(!SDL_PollEvent(&ev))
或while(SDL_PollEvent(&ev) != 0)
的窗口保持打开状态,但关闭,因为我将鼠标悬停在上面或尝试移动它。
the SDL documentation 说SDL_PollEvent
只返回1(真),如果存在未决的事件,因为该程序返回0,好像SDL_PollEvent
必须以某种方式返回1,也即ev.type
设置为SDL_QUIT
无需点击X按钮,我觉得这不太可能。所以我可能做了错误的事情,但我无法弄清楚它是什么,我一直在努力寻找解决方案。
此外,这里是init()函数。
bool init()
{
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL failed to initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
window = SDL_CreateWindow("Image Encrypter", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
screenSurface = SDL_GetWindowSurface(window);
if(screenSurface == NULL)
{
printf("Screen surface could not be created! SDL Error: %s\n", SDL_GetError());
}
}
}
return success;
}
控制台不会在init()函数中输出任何printf语句,所以我不认为这是问题所在。