2

I've got a problem with the SDL_image library. I wrote a simple program loading a spritesheet and animating it on left-click. Here's the code:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

const int sw=800;
const int sh=450;

int main(){
    SDL_Init(SDL_INIT_EVERYTHING);
    IMG_Init(IMG_INIT_PNG);

    bool quit=false;
    bool shoot=false;
    SDL_Event event;
    Uint32 time;
    unsigned  char frame=0;


    SDL_Window* window = SDL_CreateWindow("Sprite",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,sw,sh,0);
    SDL_Renderer* render = SDL_CreateRenderer(window,-1,0);

    SDL_Surface* image = IMG_Load("p_pt_12.png");
    printf("IMG_Load: %s\n", IMG_GetError());

    SDL_Texture* texture = SDL_CreateTextureFromSurface(render, image);

    while(!quit){
        if(shoot){
            time=SDL_GetTicks();
            time/=(750/5);
            frame=time%5;
            if(frame==4){
                frame = 0;
                shoot=false;
            }
        }
            SDL_Rect srcrect = { frame*96,0,96,96};
            SDL_Rect dstrect = { (sw-96)/2, (sh-96)/2, 96, 96 };


        SDL_PollEvent(&event);
        switch (event.type){
            case SDL_QUIT:
                quit = true;
                break;
            case SDL_MOUSEBUTTONDOWN:
                if(event.button.button == SDL_BUTTON_LEFT) shoot = true;
                break;
        }

        SDL_RenderClear(render);
        SDL_RenderCopy(render, texture,&srcrect,&dstrect);
        SDL_RenderPresent(render);
    }

    SDL_DestroyTexture(texture);
    SDL_FreeSurface(image);
    SDL_DestroyRenderer(render);
    SDL_DestroyWindow(window);
    IMG_Quit();
    SDL_Quit();

    return 0;
}    

The problem is when I try to run it, IMG_Load() returns NULL pointer and IMG_GetError() says "Unsupported file format". The spritesheet is a .png file, but the same happens with .jpg spritesheet. I'm sure, that the code is correct, because it works on other device with exactly the same image file like a charm. I've tried reinstalling SDL and SDL_image, but it doesn't work. SDL_RWops and IMG_LoadPNG_RW doesn't help neither. I'm using Eclipse Kepler SR2 (but I've also tried to run it with Code::Blocks 12.11 with the same result) on 64-bit Linux Mint.

I will appreciate any kind of help, thanks!

Lisu
  • 35
  • 1
  • 4

1 Answers1

2

Your code is correct and works for me. Probably your system is missing some libraries needed by sdl_image to handle images, such as libpng and zlib. Install them and let us know if it worked.

user3556781
  • 128
  • 6
  • I've installed SDL_image via package manager, so libpng and zlib were installed automatically. – Lisu Apr 27 '14 at 14:55
  • *Most* of the time that error is caused by missing libpng and zlib, try to include them as png.h and zlib.h. – user3556781 Apr 28 '14 at 11:02
  • Tried that too, no effect. – Lisu Apr 28 '14 at 19:03
  • That's pretty strange. I got that error only when someone else tried to compile some code of mine without the needed libraries or when the image was not in the working directory (check that too, especially if you launch the program inside an IDE in a project tree, try launching from terminal in the same directory), so this time I'm not sure. Try compiling with -ggdb and run valgrind to see where the error is. I'm interested in getting this fixed, as a SDL/OpenGL developer I don't want this to happen, so if you can give more informations and do some more tests it'll be helpful for both. – user3556781 Apr 28 '14 at 22:20
  • Ok, I think I got it. It's a problem with your linker. Copy here your g++ full command line please – user3556781 Apr 29 '14 at 20:50
  • I'm building it with Eclipse IDE, so here's the build log I found: http://pastebin.com/qJXLCyLM – Lisu Apr 29 '14 at 22:12
  • 2
    Just as I thought, link SDL2_image instead of SDL_image. – user3556781 Apr 30 '14 at 20:14
  • Wow, so stupid of me... It works now of course, thanks a lot! – Lisu Apr 30 '14 at 21:16