我尝试使用SDL_Image加载png图像:
代码
int fname_len = 1024;
char fname[fname_len];
string pwd = getcwd(fname,fname_len);
strcat(fname,"/i/avatar.png");
struct stat buf;
if(stat(fname, &buf)) error("File does not exists",fname);
SDL_RWops *rwop = SDL_RWFromFile(fname, "rb");
SDL_Surface *img = 0;
img = IMG_LoadPNG_RW(rwop); // 6
if(!img) error("Failed to load",fname);
SDL_Texture *tx = tex[z] = SDL_CreateTextureFromSurface(ren, img); // 5
SDL_FreeSurface(img); // ~6
if(!tx) error("Failed to create texture from",fname);
项目文件
LIBS += -L/usr/lib -lSDL2 -lSDL_image
当我运行它时,它显示错误:
Failed to load '/tmp/mygame/i/avatar.png': Error reading the PNG file.
libpng error: Not a PNG file
当它尝试运行file
命令时,它确实存在:
# file /tmp/mygame/i/avatar.png
/tmp/mygame/i/avatar.png: PNG image data, 34 x 24, 8-bit/color RGBA, non-interlaced
ldd
命令的结果是:
# ldd /tmp/mygame/mygame
linux-vdso.so.1 (0x00007fffe9400000)
libSDL2-2.0.so.0 => /usr/lib/libSDL2-2.0.so.0 (0x00007f0dd39b0000)
libSDL_image-1.2.so.0 => /usr/lib/libSDL_image-1.2.so.0 (0x00007f0dd3790000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f0dd3488000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f0dd3270000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007f0dd2ec8000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007f0dd2bc0000)
libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f0dd29b8000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f0dd2798000)
librt.so.1 => /usr/lib/librt.so.1 (0x00007f0dd2590000)
libSDL-1.2.so.0 => /usr/lib/libSDL-1.2.so.0 (0x00007f0dd22f8000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0dd3c98000)
在我的计算机上安装并安装了libpng *库:
1 extra/libpng 1.6.9-1 [installed]
2 community/libpng12 1.2.50-4 [installed]
3 multilib/lib32-libpng 1.6.8-1 [installed]
4 multilib/lib32-libpng12 1.2.50-2 [installed]
5 catalyst/libpng12 1.2.50-1
6 aur/devkitppc-portlibs-libpng 1.5.4-2 (6)
7 aur/k3d-libpng.patch 0.8.0.1-1 (2)
8 aur/lib32-libpng14 1.4.12-1 (93)
9 aur/lib32-libpng15 1.5.18-1 (34)
10 aur/libpng-old 1.0.2-2 (8)
11 aur/libpng14 1.4.13-2 [installed] (251)
12 aur/libpng15 1.5.18-1 (121)
我做错了哪一部分?
答案 0 :(得分:3)
从评论中转换:
问题在于您已与SDL2
和SDL_image
相关联。后者是1.2 version of the library,意味着与SDL 1.2兼容。
使用SDL2时,请始终使用其库的SDL2版本,例如: SDL2_image,SDL2_mixer等等。