• ### Popular Now

• 12
• 12
• 9
• 10
• 13

This topic is 3831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

#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;

SDL_Surface * LoadImage( std::string file_name )
{
SDL_Surface * optimized_image = NULL;

// if image is loaded ok
{
}

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 );

// 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;
}



##### Share on other sites
What are the return values for all the SDL functions? Figure out which one is failing.

##### Share on other sites
Quote:
 Original post by cshoweWhat 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.

##### Share on other sites
Quote:
Original post by NUCLEAR RABBIT
Quote:
 Original post by cshoweWhat 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.

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

##### Share on other sites
Quote:
Original post by aCynic2
Quote:
Original post by NUCLEAR RABBIT
Quote:
 Original post by cshoweWhat 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.

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

##### Share on other sites
Quote:
Original post by NUCLEAR RABBIT
Quote:
Original post by aCynic2
Quote:
Original post by NUCLEAR RABBIT
Quote:
 Original post by cshoweWhat 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.

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:

Is it good?

##### Share on other sites
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 cshoweWhat 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.

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:

Is it good?

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

##### Share on other sites
Quote:
Quote:
 Original post by NUCLEAR RABBITOk, 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.

##### Share on other sites
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.