【问题标题】:IMG_Load returns SDL_image error "unsupported image format"IMG_Load 返回 SDL_image 错误“不支持的图像格式”
【发布时间】:2016-08-10 19:46:08
【问题描述】:

当我尝试加载 png 文件时,返回错误“不支持的图像格式”。 人们之前曾在 StackOverflow 上发布过有关此主题的帖子,但似乎没有一个解决方案适合我。

我在一台Linux机器上工作,用g++编译

这是我的图书馆...

#include <SDL2/SDL.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
#include <string>
#include <zlib.h>
#include <png.h>

当我编译时,我包含 -lSDL2 和 -lSDL_image 标志。 我不使用 SDL2/SDL_image.h,因为它没有安装在我正在使用的机器上。此外,我的 png 文件肯定与尝试加载它的代码位于同一目录中,我确实调用了 IMG_Init(IMG_INIT_PNG) 和 IMG_INIT_PNG。

这是我的代码,错误发生在 loadSurface 函数中(我相信)。

//Using SDL, SDL_image, standard IO, and strings
#include <SDL2/SDL.h>
//#include <SDL/SDL_version.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
#include <string>
#include <zlib.h>
#include <png.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//Starts up SDL and creates window
bool init();

//Loads media
bool loadMedia();

//Frees media and shuts down SDL
void close();

//Loads individual image
SDL_Surface* loadSurface( std::string path );

//The window we'll be rendering to
SDL_Window* gWindow = NULL;

//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;

//Current displayed PNG image
SDL_Surface* gPNGSurface = NULL;

bool init()
{
        //Initialization flag
 bool success = true;

        //Initialize SDL
        if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
        {
                printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
                success = false;
        }
        else
        {
                //Create window
                gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
                if( gWindow == NULL )
                {
                        printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
                        success = false;
                }
                else
                {
                        //Initialize PNG loading
                        int imgFlags = IMG_INIT_PNG;
                      if( !( IMG_Init( imgFlags ) & imgFlags ) )
                      {
                              printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
                              success = false;
                      }
                      else
                        {
                                //Get window surface
                                gScreenSurface = SDL_GetWindowSurface( gWindow );
                        }
                }
        }

        return success;
}

bool loadMedia()
{
        //Loading success flag
        bool success = true;

        //Load PNG surface
        gPNGSurface = loadSurface( "loaded.png" );
        if( gPNGSurface == NULL )
        {
                printf( "Failed to load PNG image!\n" );
                success = false;
        }

        return success;
}

void close()
{
        //Free loaded image
        SDL_FreeSurface( gPNGSurface );
        gPNGSurface = NULL;

        //Destroy window
        SDL_DestroyWindow( gWindow );
        gWindow = NULL;

        //Quit SDL subsystems
        IMG_Quit();
        SDL_Quit();
}

SDL_Surface* loadSurface( std::string path )
{
        //The final optimized image
        SDL_Surface* optimizedSurface = NULL;
 //Load image at specified path
        SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
        if( loadedSurface == NULL )
        {
                printf( "1Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
        }
        else
        {
                //Convert surface to screen format
                optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );
                if( optimizedSurface == NULL )
                {
                        printf( "2Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
                }

                //Get rid of old loaded surface
                SDL_FreeSurface( loadedSurface );
        }

        return optimizedSurface;
}

int main( int argc, char* args[] )
{
        //Start up SDL and create window
        if( !init() )
        {
                printf( "Failed to initialize!\n" );
        }
        else
        {
                //Load media
                if( !loadMedia() )
                {
                        printf( "Failed to load media!\n" );
                }
                else
                {
                       //Main loop flag
                        bool quit = false;

                        //Event handler
                        SDL_Event e;

                        //While application is running
                        while( !quit )
                        {
                                //Handle events on queue
                                while( SDL_PollEvent( &e ) != 0 )
                                {
                                        //User requests quit
                                        if( e.type == SDL_QUIT )
                                        {
                                                quit = true;
                                        }
                                }

                                //Apply the PNG image
                                SDL_BlitSurface( gPNGSurface, NULL, gScreenSurface, NULL );

                                //Update the surface
                                SDL_UpdateWindowSurface( gWindow );
                        }
                }
        }

        //Free resources and close SDL
        close();

        return 0;
}

编译时,我使用 g++、编译器标志 -w 和链接器标志 -lSDL2 -lSDL_image

当我运行时,输出如图所示......

1无法加载图片loaded.png! SDL_image 错误:不支持的图像格式

PNG图片加载失败!

加载媒体失败!

【问题讨论】:

  • 你有编译并显示错误的最小示例吗?
  • //在指定路径加载图片\n SDL_Surface* loadedSurface = IMG_Load( path.c_str() ); \n if( loadedSurface == NULL ) { \n printf( "无法加载图像 %s!SDL_image 错误:%s\n", path.c_str(), IMG_GetError() ); \n } 然后输出显示...“无法加载加载的图像.png!SDL_image 错误:不支持的图像格式”
  • 不,他们的解决方案是链接 SDL2_image 而不是 SDL_image。但是,我无权访问 SDL2_image。
  • 我在 Ubuntu 16.04 中使用从 APT 安装的软件包时遇到了同样的问题:libsdl2-image-2.0-0、libsdl2-image-dev、libsdl2-image-2.0-0、libsdl2-dev,但是在我的其他操作系统它只适用于从包管理器安装的默认包。

标签: c++ linux sdl


【解决方案1】:

编译器标志-lSDL_image应该是-lSDL2_image

【讨论】:

  • 我遇到了类似的问题,这很有帮助。谢谢!
【解决方案2】:

首先,编译代码时有几个步骤。编译、链接和执行。如果你的程序通过了前两个步骤,它就可以工作了——可以执行了。如果它通过了前两个步骤,您就知道没有语法错误或链接错误(例如,您将正确的标志传递给链接器)。但是,如果您在运行时收到错误(这正是发生的情况),则说明环境或程序逻辑有问题。

这就是说,您根本没有提供任何代码,因此很难为您提供帮助。我不确定您是否知道如何加载图像——您似乎为此操作包含了许多不必要的库。我建议您阅读this tutorial 以了解如何使用 SDL 加载图像。可能代码有问题(标志,也许?),但您最好自己编译代码并且缺少一些依赖项。

【讨论】:

  • 我继续编辑原始帖子,为您提供更多详细信息。实际上,我一直在使用 Lazy Foo 教程来学习 SDL,并被困在那个教程页面上。任何帮助将不胜感激!
  • 我编译了你的代码,并成功打开了文件。代码没问题。确保您有有效的文件,检查其名称。我在项目目录中,而不是 bin 目录中。您可能想要添加完整路径。顺便说一句,我正在使用 Code::Blocks。编辑:实际上,如果我是你,如果它无法加载 png 文件,我会尝试其他 png 文件。之后,寻找依赖项(也许重新安装库?)。
猜你喜欢
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多