Jump to content
  • Advertisement
Sign in to follow this  
PtrN

Error using a function to initialize the screen in SDL. Could use help

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.  I'm new here so I apologise if this was not the right forum.  If it isn't I'd apreciate it if you could point me in the right direction.

 

So I've been following Lazyfoo's tutorials, making some modifications to mess around with the engine and ran into a bug.  I created a function to initialize everything.  I'm passing it the screen and the screen title.  For some reason it initializes correctly in the function but when I try to flip the screen the screen pointer is pointing to NULL and thus the program crashes.  I'm wondering what is causing it to do this.

 

I have found a fix and that is to change "SDL_Flip(screen);" to "SDL_Flip(SDL_GetVideoSurface());"  However I'm more curious as to why my code isn't working and how to fix it rather than finding a work around.

 

Here is the relevant code:

 

int main( int argc, char* args[] )
{
	//surfaces
	SDL_Surface *image = NULL;
	SDL_Surface *screen = NULL;

	SDL_Event event;

	bool quit = false;

	cout << "Initializing";
	//Initialize
	if( init(screen, "Press X to Quit") == false)
	{
		cout << "Failure to initialize \n";
		return 1;
	}

//...............
        if (screen == NULL)
	{
		cout << "Error: screen pointer is null \n";
		return 1;
	}
	if( SDL_Flip( screen ) == -1)
	{
		cout << "Error flipping the screen \n";
		return 1;
	}
// End of main function

bool init(SDL_Surface* screen, std::string windowTitle)
{
	//Initialize all the SDL systems
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
	{
		return false;
	}

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

	//If there was an error in setting up the screen
	if( screen == NULL )
	{
		return false;
	}

	//Set the window caption
	SDL_WM_SetCaption( windowTitle.c_str(), NULL);

	//If everything initialized properly
	return true;
}

 

 

 

By checking the STDOUT.txt file I see "InitializingError: screen pointer is null" which tells me that right before I call SDL_Flip and I check the pointer the function is seeing screen == NULL.

Share this post


Link to post
Share on other sites
Advertisement

You're passing the variable screen by value, so changes to it in the init function are only made to the local copy of it and not to the one in your main function. You need to pass it by reference (preferred), or by pointer.

bool init(SDL_Surface* &screen, std::string windowTitle)
{...}

Share this post


Link to post
Share on other sites

Thank you that worked!  I was thinking it was a pointer issue and was trying to use a double pointer but it still wasn't working.

 

Thanks a ton.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!