SDL Problem - Please Help!

Started by
8 comments, last by NUCLEAR RABBIT 16 years, 6 months ago
Hello, Im trying to load a PNG file for lesson 3 of the lazy foo series, and when I compile my project (VC8), there are no errors, but when the program runs, the screen is blank. I compared my code the the websites and it doesnt seem like there is much of a difference. Note: I placed the image (poop.png) in every directory of the projects folder, but it still will not show.

// 9/24/07
// SDL Lesson 3 - Loading different image types

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

// screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

// Surfaces
SDL_Surface * background = NULL;
SDL_Surface * screen = NULL;

// loads an image
SDL_Surface * LoadImage( std::string file_name )
{
	SDL_Surface * loaded_image = NULL;
	SDL_Surface * optimized_image = NULL;

	loaded_image = IMG_Load( file_name.c_str() );

	// if image is loaded ok
	if( loaded_image != NULL )
	{
		optimized_image = SDL_DisplayFormat( loaded_image );
		SDL_FreeSurface( loaded_image );
	}

	return optimized_image;
}

// draws an immage to the screen
void DrawImage( int x, int y, SDL_Surface * background, SDL_Surface * screen )
{
	// creates a rect
	SDL_Rect offset;

	// assignes  coordinates
	offset.x = x;
	offset.y = y;

	SDL_BlitSurface( screen, NULL, background, &offset );
}

int main(int argc, char* args[])
{
	// initializes SDL
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
	{
		return 1;
	}

	// set up the screen
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

	// if the screen loaded ok
	if( screen == NULL )
	{
		return 1;
	}

	// set the windows caption
	SDL_WM_SetCaption( "SDL LESSON 3 - Loading A PNG File ", NULL );

	// load the image
	background = LoadImage( "poop.png" );

	// draws the immage
	DrawImage( 10, 3, background, screen );

	//update the screen
	if( SDL_Flip( screen ) == -1 )
	{
		return 1;
	}

	SDL_Delay(4000);

	SDL_FreeSurface( background );
	SDL_FreeSurface( screen );

	SDL_Quit();

	return 0;
}

Advertisement
What are the return values for all the SDL functions? Figure out which one is failing.
Quote:Original post by cshowe
What are the return values for all the SDL functions? Figure out which one is failing.


I found which one is failing (LoadImage), but I don't know why [sad]. optimized_image is an SDL_Surface* which is what the return value of the SDL function being called at that time is.
Quote:Original post by NUCLEAR RABBIT
Quote:Original post by cshowe
What are the return values for all the SDL functions? Figure out which one is failing.


I found which one is failing (LoadImage), but I don't know why [sad]. optimized_image is an SDL_Surface* which is what the return value of the SDL function being called at that time is.


So, is IMG_Load() returning NULL?

If it is, is file_name.c_str() failing?

Quote:Original post by aCynic2
Quote:Original post by NUCLEAR RABBIT
Quote:Original post by cshowe
What are the return values for all the SDL functions? Figure out which one is failing.


I found which one is failing (LoadImage), but I don't know why [sad]. optimized_image is an SDL_Surface* which is what the return value of the SDL function being called at that time is.


So, is IMG_Load() returning NULL?

If it is, is file_name.c_str() failing?


I dont think IMG_Load is returning NULL because what I did to test it was I pit SDL_Delay(9000) after SD:_FreeSurface( loaded_image) in the if statement
Quote:Original post by NUCLEAR RABBIT
Quote:Original post by aCynic2
Quote:Original post by NUCLEAR RABBIT
Quote:Original post by cshowe
What are the return values for all the SDL functions? Figure out which one is failing.


I found which one is failing (LoadImage), but I don't know why [sad]. optimized_image is an SDL_Surface* which is what the return value of the SDL function being called at that time is.


So, is IMG_Load() returning NULL?

If it is, is file_name.c_str() failing?


I dont think IMG_Load is returning NULL because what I did to test it was I pit SDL_Delay(9000) after SD:_FreeSurface( loaded_image) in the if statement


Ok, so it's executing that portion of the code.

What's the return value of:

optimized_image = SDL_DisplayFormat( loaded_image );

Is it good?
Quote:Original post by aCynic2
Quote:Original post by NUCLEAR RABBIT
Quote:Original post by aCynic2
Quote:Original post by NUCLEAR RABBIT
Quote:Original post by cshowe
What are the return values for all the SDL functions? Figure out which one is failing.


I found which one is failing (LoadImage), but I don't know why [sad]. optimized_image is an SDL_Surface* which is what the return value of the SDL function being called at that time is.


So, is IMG_Load() returning NULL?

If it is, is file_name.c_str() failing?


I dont think IMG_Load is returning NULL because what I did to test it was I pit SDL_Delay(9000) after SD:_FreeSurface( loaded_image) in the if statement


Ok, so it's executing that portion of the code.

What's the return value of:

optimized_image = SDL_DisplayFormat( loaded_image );

Is it good?


I dont see anything wrong with it. its the same return value of SDL_DisplayFormat. isnt it? Im confused :(
Quote:
Quote:Original post by NUCLEAR RABBIT
Ok, so it's executing that portion of the code.

What's the return value of:

optimized_image = SDL_DisplayFormat( loaded_image );

Is it good?


I dont see anything wrong with it. its the same return value of SDL_DisplayFormat. isnt it? Im confused :(



I'm not asking you what its specification says it returns, I'm asking what is it actually returning. Is it returning NULL?

You load the image, then you put it through an optimization function. If it loads fine, then what is optimization returning?

It sounds to me, you need to be doing more error checking and running this through a debugger.

The LoadImage function is obviously returning a NULL -- you determined that, right? --, so it must either be failing at the IMG_Load and bypassing the optimization section or the SDL_DisplayFormat is failing to return a valid result.
If you run it from the IDE, it'll be unable to find your image files, and will just display a black surface instead. If you want it to use the images, either run it from the debug folder for the project after building the executable, or set the "Working Directory" under "Debugging" in the Configuration properties to the folder with your images. Either should work.
ok, i found the poblem. :]

in the SDL_BlitSurface function I mixed up the screen and background in the params. haha

This topic is closed to new replies.

Advertisement