【问题标题】:SDL Image Not Supported : cute2.png is not a PNG file, or PNG support is not available不支持 SDL 图像:cute2.png 不是 PNG 文件,或者 PNG 支持不可用
【发布时间】:2013-05-24 09:53:13
【问题描述】:

我只是在尝试一个教程,因为:

并尝试通过简单的代码 sn-p 在应用程序中加载和显示我的便携式网络图形 (.png) 文件:

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"

#include <stdio.h>
#include <string>

//The attributes of the screen
const int screen_width = 640;
const int screen_height = 480;
const int screen_bpp = 32;              

//The surfaces that will be used
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *message = NULL;

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

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

    SDL_RWops *rwop;
    rwop=SDL_RWFromFile(filename.c_str(), "rb");
    if(IMG_isPNG(rwop))
        printf("%s is a PNG file.\n", filename.c_str());
    else
        printf("%s is not a PNG file, or PNG support is not available.\n", filename.c_str());

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

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

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }

    //Return the optimized image
    return optimizedImage;
}


void apply_surface(int x, int y, SDL_Surface *source_surface, SDL_Surface *destintion_Surface)
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect rectangle;

    //Give the offsets to the rectangle
    rectangle.x = x;
    rectangle.y = y;

    //Blit the surface
    SDL_BlitSurface(source_surface, NULL, destintion_Surface, &rectangle);
}

int main(int argc, char** argv)
{
    //Initialize all SDL subsystems
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
        return 1;

    //Set up the screen
    screen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_SWSURFACE);

    //If there was an error in setting up the screen
    if(screen == NULL)
        return 1;

    //Set the window caption
    SDL_WM_SetCaption("Surface Bliting", NULL);

    //Load the images
    background = load_image("cute2.png");
    message = load_image("cute4.png");

    //Apply the background to the screen
    apply_surface(0, 0, background, screen);
    apply_surface(320, 0, background, screen);
    apply_surface(0, 240, background, screen);              
    apply_surface(320, 240, background, screen);

    //Apply the message to the screen
     apply_surface( 180, 140, message, screen );    

    //Update the screen
    if(SDL_Flip(screen) == -1)
        return 1;

    SDL_Delay(12000);

    SDL_FreeSurface(background);
    SDL_FreeSurface(message);

    //Quit SDL
     SDL_Quit();

    return 0;
}

现在在 Visual Stdio 2008 中,应用程序运行良好。

但是当我尝试直接从我的应用程序运行我的.exe 时:

E:\SDL_sample\SDL Image Extension Libraries\Release\"SDL Image Extension Libraries.exe"

stdout.txt 正在显示消息:

cute2.png 不是 PNG 文件,或者 PNG 支持不可用。
cute4.png 不是 PNG 文件,或者 PNG 支持不可用。

然后窗口关闭,甚至不显示/渲染任何内容。

当我在 Visual Studio 2008 中构建/运行应用程序时,我不明白图像是如何成功加载的,但是当我运行 .exe 时,图像没有加载,其中 图像文件、dll 和所有东西在它们的位置上都是一样的。

【问题讨论】:

    标签: winapi png sdl


    【解决方案1】:

    您的解决方案的Working Directory 似乎与您的Output Directory 不同。您的\Release 文件夹是您编译的可执行文件的输出位置,它似乎不包含DLL 文件SDL_Image 正在寻找加载对PNG 文件格式的支持(可能是libpng##.dll)。

    一个简单的解决方法是将您依赖的所有动态链接库复制到您的Output Directory,无论它是什么,这样当您启动程序时,它会自动找到所有这些。

    【讨论】:

      【解决方案2】:

      作为对 emartel 所说您需要更改 工作目录 的补充回答。转到您的项目 > 属性 > 配置属性 > 调试 > 工作目录。将工作目录更改为类似于您的输出目录,然后将所有媒体(例如图像、声音、.etc)文件复制到那里。

      您可以在应用程序上看到图像的原因是因为 VS 默认使用项目目录作为当前工作目录,我假设您将媒体/资产文件放在其中。作为替代方案,您可以使用 WinAPI SetCurrentDirectory 在代码中设置工作目录。

      【讨论】:

      • 感谢 Fitz Abucay 的指导,要获得默认的 VS 工作流程真的很难……我没有找到任何关于这些的文档,因此,根据您的经验,我们都需要指导... :)
      猜你喜欢
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2015-09-24
      • 2011-06-14
      • 2014-02-14
      相关资源
      最近更新 更多