SDL Screen flashes then goes away:

Hi all,

New to C++ and SDL.
Have been taking LazyFoo tutorial.If I copy and paste LazyFoo code into VS2008 Express, it builds and debugs and I get screen even using my own image and background files.
When I try to replicate LazyFoo's code by typing it all in by hand it builds without error and when starting without debugging screen flashes for a second then is gone. Have verified all image file names are correct but still just a flash then poof.

Below is code I ran that build without errors. Can send image files if needed by both mine and LazyFoo's image and background files run on his code but not mine using same exact code.

// First real test of SDL image with background 031313

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

//Constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//SurfaCES
SDL_Surface *background = NULL;
SDL_Surface *car = NULL;
SDL_Surface *screen = NULL;

// Event structure
SDL_Event event;

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

//the optimized image to be used
SDL_Surface* optimizedImage = NULL;

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

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

// free the old image
SDL_FreeSurface( loadedImage );

//If the optimized image loaded fine
if( optimizedImage != NULL)
{
//Map the colorkey
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 0xFF, 255);

//Set all pixels of color R 0, G 0xFF, B 0, to be transparent
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}



}



//Return optimized image
return optimizedImage;

}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )

{

//temporary rectangle to hold the offsets
SDL_Rect offset;

//Get the offsets
offset.x = x;
offset.y = y;

//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}

bool init()

{

// initialize SDL subsystems
if(SDL_Init(SDL_INIT_EVERYTHING ) == -1)
{
return 1;

}

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

// If there was an error in setting up screen
if( screen == NULL)
{

return 1;

}

//Set the window Caption
SDL_WM_SetCaption( " Working screen and image ", NULL);

//If everything initialized fine
return true;
}

bool load_files()

{

//load the background image
background = load_image( " gameBkgroundS11.png ");

// Check if background loaded fine
if( background = NULL)

{

return false;
}


//Load what ever image you want
car = load_image( " Porsche944yellow8.png ");

//Check if image loaded fine
if( car = NULL)

{

return false;

}

return true;

}

void clean_up()

{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface( car );

//Quit SDL
SDL_Quit();

}

int main( int argc, char* args[])

{

//quit flag
bool quit = false;

//Initialize
if( init() == false)

{

return 1;
}

//Load the files
if(load_files() == false)

{

return 1;
}

//Apply the surfaces to the screen
apply_surface( 0, 0, background, screen);
apply_surface( 200, 100, car, screen);

//Update the creen
if( SDL_Flip (screen ) == -1)

{

return 1;

}

//While the user has not quit
if( SDL_Quit == false)

{

//While there are events to handle
if( SDL_PollEvent( &event) )

{
//If the user has X'ed of of the window
if( event.type == SDL_QUIT)

{


//Quit the program
quit = true;

}
}


}

//Free the surfaces and quit SDL
clean_up();

return 0;

}

////////


Thanks for any help, let me know if somehow I sent code wrong format or something. Will try to fix on next post...

Yes, I'm sure there will be another post, and another post...only way to learn.

Thanks again
Topic archived. No new replies allowed.