我正在学习教程,我认为我做的一切都是正确的,但它继续说“不支持的图像格式”。
代码:
SDL_Texture *LoadTexture(string filePath, SDL_Renderer *renderTarget)
{
SDL_Texture *texture = nullptr;
SDL_Surface *surface = IMG_Load(filePath.c_str());
if (surface == NULL)
{
cout << "Error: " << IMG_GetError() << endl;
}
else
{
texture = SDL_CreateTextureFromSurface(renderTarget, surface);
if (texture == NULL)
{
cout << "Error: " << SDL_GetError() << endl;
}
}
SDL_FreeSurface(surface);
return texture;
}
表面在接受 IMG_Load() 的结果后保持 NULL;
另外,我的包括:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL/SDL_image.h>
还有,我的初始化:
SDL_Init(SDL_INIT_VIDEO);
int image_flags = IMG_INIT_PNG;
if (IMG_Init(image_flags) != image_flags)
{
cout << "Error: " << IMG_GetError() << endl;
}
此外,以防万一,我在 Ubuntu 中执行此操作并且我最近从 Windows 切换过来,因此我可能无法正确使用这些库。
编辑:如果你会问,我正在尝试使用 PNG,所以我没有使用我尚未初始化的格式
最佳答案
改变这个:
#include <SDL/SDL_image.h>
到
#include <SDL2/SDL_image.h>
您当前正在将 SDL_image(来自 SDL1) header 与 SDL2 一起使用,我认为您的问题就在那里。如果未安装,您可能需要安装 SDL2_image-dev。
关于linux - SDL_Image IMG_Load() 表示不支持文件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28330486/