Page 1 of 2

[Solved] SDL image error?

Posted: Tue Mar 01, 2011 8:13 pm
by ParticleGames
I am currently in the process of making a SHMUP. I have a background ready to blit, but when I apply the background to the screen, it doesn't show up. I am flipping the screen, in case you were wondering. Also, when I test the image I am trying to use isn't NULL. I am using Code::Blocks on windows vista. My images are pngs and are in a folder titled "SHMUP." Directly inside SHMUP are my images, my project, and a SHMUP.exe file. Also in the SHMUP folder are the "bin" and "obj" folders. Inside bin is Debug, and inside Debug is another SHMUP.exe! There are also other dlls and files of those sort(I am on my iPod I can't completely remember) in bin/Debug. When I put my images in the directory of the other exe (bin/Debug) my program crashes and returns 3. I am so confused, I have done so much googling it's ridiculous. Does anyone think they can help?

Code: Select all

//classes.h (only the code that applies to my problem)
class ImageManager //loads and manages images
{
    private:

    SDL_Surface* playerShip, *miniShip, *medShip, *bigShip; //player and enemy
    SDL_Surface* background;
    SDL_Surface* miniBullet, *medBullet, *bigBullet;

    public:

    ImageManager();
    ~ImageManager();
    SDL_Surface * get_image(imageType type);

};


//classes.cpp (part of code that applies to my error)
ImageManager::ImageManager()
{
    //ships
    playerShip = load_image("playerShip.png");
    miniShip = load_image("miniShip.png");
    medShip = load_image("medShip.png");
    bigShip = load_image("bigShip.png");
    //bullets
    miniBullet = load_image("miniBullet.png");
    medBullet = load_image("medBullet.png");
    bigBullet = load_image("bigBullet.png");
    //other
    background = load_image("background.png");
}

SDL_Surface* ImageManager::get_image(imageType type)
{
    switch(type)
    {
        case EplayerShip:
        return playerShip;
        break;

        case EminiShip:
        return miniShip;
        break;

        case EmedShip:
        return medShip;
        break;

        case EbigShip:
        return bigShip;
        break;

        case Ebackground:
        return background;
        break;

        case EminiBullet:
        return miniBullet;
        break;

        case EmedBullet:
        return medBullet;
        break;

        case EbigBullet:
        return bigBullet;
        break;

        default: break;
    }
    return NULL;
}

ImageManager::~ImageManager()
{
    //ships
    SDL_FreeSurface(playerShip);
    SDL_FreeSurface(miniShip);
    SDL_FreeSurface(medShip);
    SDL_FreeSurface(bigShip);
    //bullets
    SDL_FreeSurface(miniBullet);
    SDL_FreeSurface(medBullet);
    SDL_FreeSurface(bigBullet);
    //other
    SDL_FreeSurface(background);
}




//main.cpp
int SDL_main(int argc, char *argv[])
{
    set_up();

    ImageManager imageMan;
    SDL_Surface * background = imageMan.get_image(Ebackground);

    bool quit = false;

    while(quit==false)
    {
        //background's area
        SDL_Rect backgroundClips;
        backgroundClips.x = 0;
        backgroundClips.y = 0;
        backgroundClips.w = 640;
        backgroundClips.h = 480;

        //banner
        SDL_Rect banner;
        banner.x = 0;
        banner.y = 0;
        banner.w = 640;
        banner.h = 30;

        apply_surface(0,30,background,screen,&backgroundClips);
        SDL_FillRect(screen,&banner,SDL_MapRGB(screen->format,40,60,180));
        SDL_Flip(screen);
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                quit = true;
            }
        }
    }

    clean_up();
    return 0;
}

Re: SDL image error?

Posted: Tue Mar 01, 2011 8:39 pm
by Ginto8
a few things:
1) post some code relevant to the issue
2) IIRC, Code::Blocks runs the program from the directory where the project file is, so make sure the file path is correct from that.
3) Image loading and drawing code both seem to be relevant here.

Re: SDL image error?

Posted: Wed Mar 02, 2011 7:26 pm
by ParticleGames
I added code. Also, the .exe that is in the location of the project file crashes when I run it. The other .exe in bin/Debug runs fine, but doesn't blit the images. And what do you mean by "make sure the file path is correct?" What file path?

And I know what's also coming about my code. I will try to implement error handling functions.

Re: SDL image error?

Posted: Thu Mar 03, 2011 6:21 am
by N64vSNES
I've had this issue countless times in the past.

The solution is always:

A- Your path is incorrect and your program can't find it
B- You've not got "libpng.dll" in your executables directory (You'll get no error message and the program will execute but you won't be able to use the SDL_Image library.

Re: SDL image error?

Posted: Thu Mar 03, 2011 7:29 pm
by ParticleGames
I've tried putting libpng12-0.dll basically everywhere, and it still doesn't work. I also checked to see if my images were NULL, and it appears that they aren't NULL. The build log says:

Checking for existence: ...\SHMUP\SHMUP.exe
Executing: ...\SHMUP\SHMUP.exe (in ...\SHMUP\bin\Debug)
:x :x

Re: SDL image error?

Posted: Thu Mar 03, 2011 7:43 pm
by xiphirx
You also need SDL_image.dll :P

Re: SDL image error?

Posted: Thu Mar 03, 2011 8:20 pm
by ParticleGames
That's there too. This is a frustrating error :x

Re: SDL image error?

Posted: Thu Mar 03, 2011 9:17 pm
by krilik
I think I've had this problem before, or a similar problem. I ended up putting all my SDL related DLL files in my windows/system32 folder because I was tired of it.

Re: SDL image error?

Posted: Fri Mar 04, 2011 9:06 am
by dandymcgee
krilik wrote:I think I've had this problem before, or a similar problem. I ended up putting all my SDL related DLL files in my windows/system32 folder because I was tired of it.
Which works great until you try to redistribute it. ;)

Re: SDL image error?

Posted: Fri Mar 04, 2011 11:58 am
by N64vSNES
Yeah SDL_image.dll is needed but I suggested libpng.dll because if you it will still run fine with it however SDL will refuse to load anything. Whereas with SDL_image.dll you get a pretty obvious error.

It needs to be in the same folder as the .exe you're running, if it still doesn't work you must be giving a incorrect filename.

Re: SDL image error?

Posted: Fri Mar 04, 2011 6:08 pm
by ParticleGames
I implemented error functions, and in stdout.txt it says:


Couldn't open playerShip.png
Couldn't open miniShip.png
Couldn't open medShip.png
Couldn't open bigShip.png
Couldn't open miniBullet.png
Couldn't open medBullet.png
Couldn't open bigBullet.png
Couldn't open background.png

I am 99% sure my filenames are correct. Could it have to do with the working directories and such? I am confused how those work, so I might be wrong.

Re: SDL image error?

Posted: Fri Mar 04, 2011 8:22 pm
by Ginto8
It's definitely problem of filepath. Your files should be in 1 of 2 places: where your executable is, or where your source files are. If one of them doesn't work, it's almost definitely the other.

Re: SDL image error?

Posted: Fri Mar 04, 2011 8:35 pm
by ParticleGames
Which exe? The one by my project or the one in bin/Debug? Also, my sourcefiles are in the same directory as the first exe.

Re: SDL image error?

Posted: Sat Mar 05, 2011 12:37 am
by xiphirx
ParticleGames wrote:Which exe? The one by my project or the one in bin/Debug? Also, my sourcefiles are in the same directory as the first exe.
How about you add simple temp code to create a file, then look for the file. Wherever it is, thats where your sprites go.

Re: SDL image error?

Posted: Sat Mar 05, 2011 5:19 am
by N64vSNES
Sounds like you're having trouble with the way your IDE has set up your folders.

The should be two folders in your project folder "debug" and "release" both will have a executable.

So make sure your *.DLL files and your image files are in that directory (debug or release depending on what mode you're running it in).

Other than that I'm not sure what to suggest.