2016-03-06 181 views
-1

這是我在SDL中的第二個程序。我嘗試加載位置圖像(「C:/Users/user/Desktop/learning_sdl/bpn.bmp」)並將其顯示在SDL Window上。該程序編譯沒有錯誤。但是,我只能看到控制檯屏幕。 plzz幫助。不顯示SDL窗口。

#include<iostream> 
#include<sdl.h> 
using namespace std; 

bool init(); //initializes window and surface. 
bool loadimage(); //loads image 
void close(); //free surface and destroy pointer 

SDL_Window* w; //window pointer 
SDL_Surface*img; //image pointter 
SDL_Surface*surf; //surface pointer 
const int scrWidth=640; 
const int scrLength=400; 

int main(int argc, char*args[]) 
{ 
    if(!init) 
    { 
     cout<<"Sorry, Cannot Initialize the window."<<SDL_GetError(); 
    } 
    else 
    { 
     if(!loadimage()) 
     { 
      cout<<"Cannot load images."<<SDL_GetError(); 
     } 
     else 
     { 
      SDL_BlitSurface(img, NULL, surf, NULL); 
      SDL_UpdateWindowSurface(w); 
      SDL_Delay(10000); 
     } 
    } 

    close(); 
} 



bool init() 
{ 
    bool status=true; 
    if(SDL_Init(SDL_INIT_VIDEO) < 0) 
     { 
      cout<<"Cannot initialize SDL"<<SDL_GetError(); 
      status=false; 
     } 
    else 
    { 
     w=SDL_CreateWindow("bipin",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,scrWidth,scrLength,SDL_WINDOW_SHOWN); 
     if (w==NULL) 
     { 
      cout<<"Cannot create sdl window"<<SDL_GetError(); 
      status=false; 
     } 
     else 
     { 
      surf=SDL_GetWindowSurface(w); 
      if (surf==NULL) status=false; 
     } 
    } 
    return status; 
} 



bool loadimage() 
{ 
    bool status=true; 
    img=SDL_LoadBMP("C:/Users/user/Desktop/learning_sdl/bpn.bmp"); 
    if (img==NULL) 
    { 
     cout<<"Unable to find image."<<SDL_GetError(); 
     status=false; 
    } 
    return status; 
} 




void close() 
{ 
    SDL_FreeSurface(img); 
    SDL_FreeSurface(surf); 
    img=NULL; 
    surf=NULL; 
    SDL_DestroyWindow(w); 
    w=NULL; 
    SDL_Quit(); 
} 
+1

你沒有稱爲你的'init'函數,只是檢查它的地址。不應該是'if(!init())'嗎? – keltar

+0

@keltar感謝的人,它的工作。 :) – paradox

回答

0

INT你的代碼,在主要功能:

if(!init) 
{ 
    cout<<"Sorry, Cannot Initialize the window."<<SDL_GetError(); 
} 

注意INIT是一個函數,而不是一個函數調用,而不是使用方法:

if(!init()) 
{ 
    cout<<"Sorry, Cannot Initialize the window."<<SDL_GetError(); 
}