7 Replies - 9881 Views - Last Post: 30 September 2009 - 05:21 PM Rate Topic: -----

#1 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

SDL IMG_Load keeps returning NULL

Posted 30 September 2009 - 11:43 AM

Hello,
One again, I am am running into SDL problems in my new game that I am attempting to build. My proble is that the IMG_Load call (as included in SDL_image.h) keeps returning NULL even though the image is in the proper directory (I've even tried with it in the same directory). However, here is the code that I am using:

SDL_Surface* loadImage(std::string filename)
{
	SDL_Surface* image = NULL;
	SDL_Surface* changedImage = NULL;

	// Load the image
	image = IMG_Load(filename.c_str()); // ERROR occurs here!
	if (image != NULL)
	{
		// ... Change the image
	}

	return changedImage;
}


background = loadImage("images\\wall.png");
if (background == NULL)
{
	exit( 1 );
}



The program keeps returning an error code of 1 right here so I know that the call is returning NULL...
If i remove the exit code, then I get a wide array of even greater problems.

Thanks all.
~Dogstopper

This post has been edited by Dogstopper: 30 September 2009 - 11:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: SDL IMG_Load keeps returning NULL

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: SDL IMG_Load keeps returning NULL

Posted 30 September 2009 - 02:17 PM

Have you tried giving the full path to the file?
Was This Post Helpful? 0
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: SDL IMG_Load keeps returning NULL

Posted 30 September 2009 - 02:35 PM

Why not trying
SDL_Surface* loadImage(std::string filename)
{
	SDL_Surface* image = NULL;
	SDL_Surface* changedImage = NULL;

	// Load the image
	image = IMG_Load(filename.c_str()); // ERROR occurs here!
SDL_Surface * image - IMG_Load(filename.c_str());//try adding this line	
if (image != NULL)
	{
		// ... Change the image
	}

	return changedImage;
}


Read that here dunno if its your same problem :P
http://article.gmane...p.lib.sdl/34354
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: SDL IMG_Load keeps returning NULL

Posted 30 September 2009 - 02:47 PM

Thanks for trying guys! But BOTH suggestions failed. I have 2 other classes that I use. Here is the main game loop:
void start()
{
	// Set the initial state to the title
	currentState = new Title(); // ERROR OCCURS HERE
	stateID = STATE_TITLE;

	// Loop forever
	while (stateID != STATE_EXIT)
	{
		// Handle overridden events
		//currentState->handleInput();
		//currentState->logic();

		// Before printing the screen, change state if necessary
		//applyNextState();

		//if (stateID != STATE_EXIT)
			//currentState->render();

		if(SDL_Flip(screen) == -1) 
		{
			exit( 1 );
		}
	}
}



Here is the Title constructor:
Title::Title() 
{
	nextState = STATE_NULL;

	// Start out with our pointers at NULL
	background = NULL; // SDL_Surface pointer
	clickLabel = NULL;	// SDL_Surface pointer
	
	background = loadImage("E:\\SDL\\TSA_Project\\Release\\images\\wall.png");
	if (background == NULL)
	{
		exit( 1 );
	}

	SDL_Color textColor = {0, 0, 0};
	clickLabel = TTF_RenderText_Shaded(font, "Press Enter to Continue.", textColor, textColor);
	if (clickLabel == NULL)
	{
		exit( 1 );
	}

}



Thanks again!

This post has been edited by Dogstopper: 30 September 2009 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: SDL IMG_Load keeps returning NULL

Posted 30 September 2009 - 03:01 PM

I know nothing about SDL, so I guess I'll back out. I just realized paths often need to be fully-qualified to work.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: SDL IMG_Load keeps returning NULL

Posted 30 September 2009 - 03:02 PM

View PostJackOfAllTrades, on 30 Sep, 2009 - 02:01 PM, said:

I know nothing about SDL, so I guess I'll back out. I just realized paths often need to be fully-qualified to work.

Thanks for being honest...

Also if it helps, this line is screwing up too:
clickLabel = TTF_RenderText_Shaded(font, "Press Enter to Continue.", textColor, textColor);



Here is its loading function:
if (TTF_Init() == -1)
{
	exit( 1 );
}

// Setup the font
font = TTF_OpenFont("images\\lucon.ttf", 28); // A TTF_Font pointer


This post has been edited by Dogstopper: 30 September 2009 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: SDL IMG_Load keeps returning NULL

Posted 30 September 2009 - 04:55 PM

I forgot to include libpng12-0.dll which under-the-hood, loads ALL of my images. Thanks though!

SOLVED
Was This Post Helpful? 0
  • +
  • -

#8 BlackPhoenix  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

Re: SDL IMG_Load keeps returning NULL

Posted 30 September 2009 - 05:21 PM

Was gonna post the solution, but I'm glad you have already found it :)

Check out this website for amazing SDL tutorials.

This one in particular would have solved your problem

http://lazyfoo.net/S...son03/index.php
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1