The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_Image won't load my PNG
He4TS3eKeR


Joined: 23 Feb 2013
Posts: 5
Hi everyone, this is my first time being here so i hope i can become a helpful part in this community soon enough.

So, I've gotten through the first 5 SDL tutorials and i followed them slowly step by step making sure i make no mistakes. Up until this point whenever i debugged my project it would just flash and exit with code 1. I didn't know what that was all about since I'm new to this so i tried running the .exe in the debug directory and it worked perfectly.
Realizing something has to be wrong if the debugger doesn't work(it made no sense) i started looking into it, but not to bore you with more things i'm just gonna get right into it.

So this is from the 4th tutorial: Event driven programming. I used breakpoints and the logger to pinpoint the error.

Code:

int main( int argc, char* args[] )
{
    //Make sure the program waits for a quit
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }
   log ( "Loading files..." );
    //Load the files
    if( load_files() == false )
    {
        return 1;
    }
   log ( "Blitting... " );
    //Apply the surface to the screen
    apply_surface( 0, 0, image, screen );

The main function ^ it doesn't even get to blitting.
Code:

bool load_files()
{
    //Load the image
   log ( "Loading image..." );
    image = load_image( "x.png" );

    //If there was an error in loading the image
    if( image == NULL )
    {
      log ( SDL_GetError() );
      log ( IMG_GetError() );
        return false;
    }

    //If everything loaded fine
    return true;
}

^ the function for loading files as you can see, with the logger in it
Code:

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }
   else
   {
      log ( "Error loading image" );
   }

    //Return the optimized image
    return optimizedImage;
}

this is the part where the image gets loaded, also with the logger

As you can see all of this code is pretty much correct, but this is what the logger returns:
Quote:

Loading files...
Loading image...
Error loading image
Couldn't open x.png
Couldn't open x.png

As you can see loadedImage returns NULL therefore the optimized image returns NULL as well.
Before you ask, my x.png is in the same folder as the .exe, and it is the one LazyFoo used in his tutorial, extracted from the .rar.
Is it possible that it cant read it because its the old version of PNG or something? This file is from 2006.

I've been searching for a solution on google to no avail and i've been stuck on it for a few days now.

Help would be really appreciated Smile.
He4TS3eKeR


Joined: 23 Feb 2013
Posts: 5
Tried making a new 8bit PNG with the same name and resolution in photoshop cs6. Doesn't work Sad
pmacfarlane


Joined: 28 Jul 2012
Posts: 21
Location: Scotland
Well, I don't know which tutorials you're talking about... can you elaborate? (Provide a link?)

Not sure how it works on Windows, but on Linux, you'll need to install libpng for SDL_image to be able to read .png files. Maybe this is true on Windows too.

Try making a .bmp file instead, and loading that. SDL_image can load those with no extra library support required.
He4TS3eKeR


Joined: 23 Feb 2013
Posts: 5
Eh, maybe i didnt make myself clear enough. Sorry i forgot this isnt Lazyfoo's forum, but the general SDL one.
http://lazyfoo.net/SDL_tutorials/lesson04/index.php
It's not just this lesson btw, none of my lessons can debug because of this problem.
All the code is correct, the only problem is that IMG_load can't open x.png, and ive got everything laid out correctly already, like said. Also like i said the logger returns "Couldn't open x.png". All the code is there and I'm using the SDL_image.lib in the linker and i have all the .dll's in my core folder as well. I really don't know what else to try.
He4TS3eKeR


Joined: 23 Feb 2013
Posts: 5
bump
He4TS3eKeR


Joined: 23 Feb 2013
Posts: 5
Okay, I'm sorry but for anyone who encounters a problem like this, you don't have to put the image next to the .exe. I put the image in the folder it took me to when i right clicked my project in visual studio and clicked "Open windows explorer", which was in this format Projects/ProjectName/ProjectName. I have no idea why this is the case and if anyone can help me configure my Visual c++ to point to my Debug folder where the .exe is i would appreciate it, otherwise ill keep using this folder since the problem is solved.