2015-01-26 12 views
-1

我正在做關於SDL遊戲開發的教程。找到一個輸入時,程序應該離開主循環,但它並不:SDL_PollEvent不起作用

SDL_Window* gWindow = NULL; 
SDL_Surface* gScreenSurface = NULL; 
SDL_Surface* gXOut = NULL; 

int main(int argc, char* argv[]) { 

    if (!init()){ 
     printf("Failed to initialize.\n"); 
    } 
    else { 
     //Load media 
     if (!loadMedia()){ 
      printf("Failed to load media.\n"); 
     } 
     else { 
      //Main loop flag 
      bool quit = false; 

      //Event handler 
      SDL_Event e; 

      //While application is runnig 
      while (!quit){ 
       //Handles events on queue 
       while (SDL_PollEvent(&e) != 0){ 
        //User requests quit 
        if (e.type == SDL_QUIT){ 
         quit = true; 
        } 
       } 

       //Aply the image 
       SDL_BlitSurface(gXOut, NULL, gScreenSurface, NULL); 

       //Update the surface 
       SDL_UpdateWindowSurface(gWindow); 

      } 
     } 
    } 

    //Free resources and close SDL 
    close(); 

    return 0; 

我試圖改變SDL_QUIT爲SDL_KEYPRESSED,但它也不起作用。任何想法爲什麼?

回答

0

SDL_KEYPRESSED在SDL2中不存在。如果你想使用鍵盤輸入退出程序,使用SDL_KEYDOWN或SDL_KEYUP在這樣的循環:

請拿上wiki page看看。

+0

我拼錯了,我的意思是KEYDOWN,這也沒有奏效。現在解決了。但那麼我的問題是,究竟是什麼處理SDL_QUIT? – vandermies 2015-01-29 13:03:44

+0

看看這個頁面 - https://wiki.libsdl.org/SDL_EventType#Remarks – Gumichan01 2015-02-05 11:16:33

0

這是怎樣的代碼查找你要實現的目標:

//Using SDL and standard IO 
#include <SDL.h> 
#include <stdio.h> 

//Screen dimension constants 
const int SCREEN_WIDTH = 640; 
const int SCREEN_HEIGHT = 480; 



int main(int argc, char* args[]) 
{ 
    //Main loop flag 
    bool quit = false; 

    //Event handler 
    SDL_Event e; 

    SDL_Surface* gXOut = NULL; 

    //The window we'll be rendering to 
    SDL_Window* window = NULL; 

    //The surface contained by the window 
    SDL_Surface* screenSurface = NULL; 

    //Initialize SDL 
    if (SDL_Init(SDL_INIT_VIDEO) < 0) 
    { 
     printf("SDL could not initialize! SDL_Error: %s\n",     SDL_GetError()); 
    } 
    else 
    { 
     //Create window 
     window = SDL_CreateWindow("SDL Tutorial", 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()); 
     } 
     else 
     { 
      //Get window surface 
      screenSurface = SDL_GetWindowSurface(window); 

      //Fill the surface white 
      SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); 

      //Update the surface 
      SDL_UpdateWindowSurface(window); 

      //Wait two seconds 
      SDL_Delay(2000); 
     } 
    } 

     //While application is running 
     while (!quit) 
     { 
       //Handle events on queue 
       while (SDL_PollEvent(&e) != 0) 
       { 
        //User requests quit 
        if (e.type == SDL_QUIT) //can change the SD_QUIT event to whatever you want to customize with. 
        { 
         quit = true; 
        } 
       } 
       //Apply the image 
       SDL_BlitSurface(gXOut, NULL, screenSurface, NULL); 

      //Update the surface 
      SDL_UpdateWindowSurface(window); 
     } 

    //Destroy window 
    SDL_DestroyWindow(window); 

    //Quit SDL subsystems 
    SDL_Quit(); 

    return 0; 
} 

的SDL_QUIT需要時按下窗口上的X輸入,或者如果你想改變的關鍵是別的東西在應用程序中使用不同的密鑰退出。