Winapi 不支持SDL图像:cute2.png不是png文件,或者png支持不可用

Winapi 不支持SDL图像:cute2.png不是png文件,或者png支持不可用,winapi,png,sdl,Winapi,Png,Sdl,我只是在尝试一个教程,如: 并尝试通过简单的代码段加载和显示我的便携式网络图形(.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

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

并尝试通过简单的代码段加载和显示我的便携式网络图形(
.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;
}
stdout.txt
正在显示消息:

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

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


我不明白当我在Visual Studio 2008中构建/运行应用程序时,图像是如何成功加载的,但是当我运行
.exe
时,图像没有加载,其中图像文件,dll和所有内容在其位置上都是相同的

看来解决方案的
工作目录
输出目录
不同。您的
\Release
文件夹是您已编译的可执行文件的输出位置,它似乎不包含
DLL
文件
SDL_Image
希望加载对PNG文件格式的支持(可能是
libpng##.DLL


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

您的解决方案的
工作目录
似乎与
输出目录
不同。您的
\Release
文件夹是您已编译的可执行文件的输出位置,它似乎不包含
DLL
文件
SDL_Image
希望加载对PNG文件格式的支持(可能是
libpng##.DLL


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

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


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

作为对emartel所说需要更改工作目录的补充回答。转到项目>属性>配置属性>调试>工作目录。更改与输出目录类似的工作目录,并将所有媒体(如图像、声音等)文件复制到该目录

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

感谢Fitz Abucay提供的指导原则,要获得默认的VS工作流程确实很困难。。。我没有找到任何关于这些的文档,所以根据您的经验,我们都需要指导……:)感谢Fitz Abucay提供的指导原则,要获得VS的默认工作流程真的很难,因为它确实默认了。。。我没有找到任何关于这些的文档,所以根据您的经验,我们都需要指导……:)
E:\SDL_sample\SDL Image Extension Libraries\Release\"SDL Image Extension Libraries.exe"