2015-05-10 34 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) 

- 這是一個任務,而不是一個比較。那麼你的代碼的第一個版本應該可以工作。