2015-05-10 26 views
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 documentationSDL_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语句,所以我不认为这是问题所在。

回答

0

一个常见的错误在这里:

if(ev.type = SDL_QUIT) 

- 这是一个任务,而不是一个比较。那么你的代码的第一个版本应该可以工作。