Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ IMG“U Load返回SDL“U图像错误”;“不支持的图像格式”;_C++_Linux_Sdl - Fatal编程技术网

C++ IMG“U Load返回SDL“U图像错误”;“不支持的图像格式”;

C++ IMG“U Load返回SDL“U图像错误”;“不支持的图像格式”;,c++,linux,sdl,C++,Linux,Sdl,当我尝试加载png文件时,返回错误“不支持的图像格式”。 以前有人在StackOverflow上发布过关于这个主题的帖子,但似乎没有一个解决方案适合我 我在一台Linux机器上工作,用g++ 这是我的图书馆 #include <SDL2/SDL.h> #include <SDL/SDL_image.h> #include <stdio.h> #include <string> #include <zlib.h> #include <

当我尝试加载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_图像标志。 我不使用SDL2/SDL_image.h,因为我正在使用的机器上没有安装它。此外,我的png文件肯定与尝试加载它的代码位于同一目录中,我确实调用了IMG_Init(IMG_Init_png)和IMG_Init_png

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

//使用SDL、SDL_映像、标准IO和字符串
#包括
//#包括
#包括
#包括
#包括
#包括
#包括
//屏幕尺寸常数
屏幕宽度=640;
屏幕上的常数=480;
//启动SDL并创建窗口
bool init();
//加载媒体
bool loadMedia();
//释放媒体并关闭SDL
无效关闭();
//加载单个图像
SDL_表面*加载表面(标准::字符串路径);
//我们将渲染到的窗口
SDL_窗口*gWindow=NULL;
//窗户所包含的表面
SDL_表面*gScreenSurface=NULL;
//当前显示的PNG图像
SDL_表面*gPNGSurface=NULL;
boolinit()
{
//初始化标志
布尔成功=真;
//初始化SDL
if(SDL_Init(SDL_Init_视频)<0)
{
printf(“SDL无法初始化!SDL错误:%s\n”,SDL_GetError());
成功=错误;
}
其他的
{
//创建窗口
gWindow=SDL_CreateWindow(“SDL教程”,SDL_WINDOWPOS_未定义,SDL_WINDOWPOS_未定义,屏幕宽度,屏幕高度,显示SDL_窗口);
if(gWindow==NULL)
{
printf(“无法创建窗口!SDL错误:%s\n”,SDL_GetError());
成功=错误;
}
其他的
{
//初始化PNG加载
int imgFlags=IMG_INIT_PNG;
if(!(IMG_Init(imgFlags)和imgFlags))
{
printf(“SDL_映像无法初始化!SDL_映像错误:%s\n”,IMG_GetError());
成功=错误;
}
其他的
{
//获取窗口表面
gScreenSurface=SDL_GetWindowSurface(gWindow);
}
}
}
回归成功;
}
bool loadMedia()
{
//加载成功标志
布尔成功=真;
//加载PNG曲面
gPNGSurface=loadSurface(“loaded.png”);
if(gPNGSurface==NULL)
{
printf(“加载PNG图像失败!\n”);
成功=错误;
}
回归成功;
}
无效关闭()
{
//自由加载图像
SDL_自由曲面(gPNGSurface);
gPNGSurface=NULL;
//破坏窗口
SDL_(格温多);
gWindow=NULL;
//退出SDL子系统
IMG_Quit();
SDL_退出();
}
SDL_表面*加载表面(标准::字符串路径)
{
//最终优化的图像
SDL_曲面*优化曲面=空;

//在指定路径加载图像 SDL_Surface*loadedSurface=IMG_Load(path.c_str()); 如果(loadedSurface==NULL) { printf(“1无法加载图像%s!SDL_图像错误:%s\n”,path.c_str(),IMG_GetError()); } 其他的 { //将曲面转换为屏幕格式 optimizedSurface=SDL_ConvertSurface(loadedSurface,GSCreensSurface->格式,NULL); 如果(optimizedSurface==NULL) { printf(“2无法优化映像%s!SDL错误:%s\n”,path.c_str(),SDL_GetError()); } //去除旧的加载曲面 SDL_自由表面(加载表面); } 返回优化曲面; } int main(int argc,char*args[] { //启动SDL并创建窗口 如果(!init()) { printf(“初始化失败!\n”); } 其他的 { //加载媒体 如果(!loadMedia()) { printf(“加载媒体失败!\n”); } 其他的 { //主循环标志 bool-quit=false; //事件处理程序 SDL_事件e; //当应用程序运行时 而(!退出) { //处理队列上的事件 while(SDL_PollEvent(&e)!=0) { //用户请求退出 如果(e.type==SDL\u退出) { 退出=真; } } //应用PNG图像 SDL_BlitSurface(gPNGSurface,NULL,gScreenSurface,NULL); //更新曲面 SDL_更新内表面(gWindow); } } } //释放资源并关闭SDL close(); 返回0; }
编译时,我使用g++、编译器标志-w和链接器标志-lSDL2-lSDL_image

当我运行时,输出是一个
//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;
}