2012-03-09 49 views
0

我的脚本无法运行,因为它会打开一个窗口并关闭它。它在加载文件时失败。该脚本适用于我的笔记本电脑,但不适用于我的台式电脑,并且所有的neccecary文件都在那里。这是关于这个功能的;在SDL中使用自定义函数加载图像失败

这是我的功能load_image();

SDL_Surface *load_image(std::string filename) 
{ 
    //The image that's loaded 
    SDL_Surface* loadedImage = NULL; 

    //The optimized surface that will be used 
    SDL_Surface* optimizedImage = NULL; 

    //Load the image 
    loadedImage = IMG_Load(filename.c_str()); 

    //If the image loaded 
    if(loadedImage != NULL) 
    { 
     //Create an optimized surface 
     optimizedImage = SDL_DisplayFormatAlpha(loadedImage); 

     //Free the old surface 
     SDL_FreeSurface(loadedImage); 

     //If the surface was optimized 
     if(optimizedImage != NULL) 
     { 
      //Color key surface 
      SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF)); 
     } 
    } 

    //Return the optimized surface 
    return optimizedImage; 

} 

我这样称呼它;

sprite = load_image("sprites.png"); 

if(sprite == NULL) 
{ 
    return false; 
} 

唯一的问题是,它始终返回false,即使该文件是在那里。问题是,这个代码不会在我的笔记本电脑上返回false!

+0

两台计算机上的文件是否位于相同路径中? – 2012-03-09 22:33:47

+0

不,但我在两台机器上完全重新编译它,所以它应该没有关系? – 2012-03-09 22:39:19

回答

2

一切似乎都很好,请尝试使用IMG_GetError()来诊断此问题。以下是SDL文档中的代码,其中显示如何:

// load sample.png into image 
SDL_Surface *image; 
image=IMG_Load("sample.png"); 
if(!image) { 
    printf("IMG_Load: %s\n", IMG_GetError()); 
    // handle error 
} 
+0

没有libPNG> _ < – 2012-03-10 10:20:43

+0

IMG_Load:加载失败libpng15-15.dll:%1不是有效的Win32应用程序。 – 2012-03-10 10:26:57