IMG_Load返回错误SDL_image "unsupported image format"

IMG_Load returns SDL_image error "unsupported image format"

本文关键字:image format unsupported 错误 Load 返回 IMG SDL      更新时间:2023-10-16

当我尝试加载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: %sn", 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: %sn", 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: %sn", 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: %sn", 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: %sn", 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无法加载加载的图像.png!SDL_image错误:不支持的图像格式

加载PNG图像失败!

加载媒体失败!

编译器标志-lSDL_image应为-lSDL2_image

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

也就是说,你根本没有提供任何代码,所以这很难帮助你。我不确定您是否知道如何加载图像——您似乎为这个操作包含了许多不必要的lib。我建议您阅读本教程,了解如何使用SDL加载图像。代码可能有问题(可能是标志?),但您也可以自己编译代码,这样会丢失一些依赖项。