Question

I tried to load a png image using SDL_Image:

The code

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);

The project file

LIBS += -L/usr/lib -lSDL2 -lSDL_image

When i run it, it shows an error:

Failed to load '/tmp/mygame/i/avatar.png': Error reading the PNG file.
libpng error: Not a PNG file

when it try to run file command, it does exists:

# file /tmp/mygame/i/avatar.png
/tmp/mygame/i/avatar.png: PNG image data, 34 x 24, 8-bit/color RGBA, non-interlaced

the result of ldd command are:

# 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)

installed and installable libpng* library on my computer:

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)

Which part did I do wrong?

Was it helpful?

Solution

Converted from a comment:

The problem is that you have linked against SDL2 and SDL_image. The latter is the 1.2 version of the library, meant to be compatible with SDL 1.2.

When using SDL2, always use the SDL2 versions of its libraries, e.g. SDL2_image, SDL2_mixer and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top