2013-03-06 53 views
1

我正在使用SDL加載圖像並將其分配給OpenGL四邊形。我找到了一個教程,告訴我如何去做,但是當我去運行程序時,它說SDL Surface變量(代碼中的曲面)正在被使用而不被初始化。我不知道是不是因爲我正在使用類,因爲我已經使用其他語言的知識來尋找解決方案,但沒有運氣。SDL表面正在使用而未被初始化錯誤

下面是一些代碼: 實體類(到目前爲止):

//d2_entity.h 
class d2Entity 
{ 
public: 
    bool LoadImage(const char* fileName); 
    void DrawImage(); 
    void Clear(); 

private: 
    GLuint *texture; 
    GLenum textureFormat; 
    GLint noColours; 
}; 

//d2_entity.cpp 
bool d2Entity::LoadImageW(const char* fileName) 
{ 
SDL_Surface *surface; 

if((surface == IMG_Load(fileName))) 
{ 
    // Check if image size is a power of 2 
    if((surface->w & (surface->w - 1)) != 0) 
     cout << "Opps! '" << fileName << "'s' width is not a power of 2!"; 
    if((surface->h & (surface->h -1)) != 0) 
     cout << "Opps! '" << fileName << "'s' height is not a power of 2!"; 

    // Get the No. of channels 
    d2Entity::noColours = surface->format->BitsPerPixel; 
    // Contains a alpha channel 
    if(d2Entity::noColours == 4) 
    { 
     if(surface->format->Rmask == 0x000000ff) 
      d2Entity::textureFormat = GL_RGBA; 
     else 
      d2Entity::textureFormat = GL_BGRA; 
    } 
    // No alpha channel 
    else if(d2Entity::noColours == 3) 
    { 
     if(surface->format->Rmask == 0x000000ff) 
      d2Entity::textureFormat = GL_RGB; 
     else 
      d2Entity::textureFormat = GL_BGR; 
    } 
    else 
     cout << "Opps! The image '" << fileName << "' is not truecolour!" << endl; 

    // Create an OpenGL texture 
    glGenTextures(1, d2Entity::texture); 
    glBindTexture(GL_TEXTURE_2D, *d2Entity::texture); 
    // Set texture streching properties 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexImage2D(GL_TEXTURE_2D, 0, d2Entity::noColours, surface->w, surface->h, 0, d2Entity::textureFormat, GL_UNSIGNED_BYTE, surface->pixels); 
} 
else 
{ 
    cout << "Opps! The entity image could not be loaded!" << endl; 
    SDL_Quit(); 
    return false; 
} 

if(surface) 
    SDL_FreeSurface(surface); 

return true; 
} 

回答

3
SDL_Surface *surface;  
if((surface == IMG_Load(fileName))) 

你永遠不會初始化surface,然後你試圖把它比作的IMG_Load結果。我很確定你的意思是分配它。

SDL_Surface *surface; 
if((surface = IMG_Load(fileName))) 
+0

謝謝,我不知道爲什麼我沒有注意到。這可能是因爲我通常會做阿明發布的內容。 – Antony 2013-03-06 16:31:25

2

你做了一個對比,而不是轉讓的這裏

if((surface == IMG_Load(fileName))) 
{ 

代碼應該是:

SDL_Surface surface; 
surface = IMG_Load("image.bmp"); 

if(surface != NULL) 
{ 
    //do stuff with surface