Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 PtrN   Members   -  Reputation: 101

Like
0Likes
Like

Posted 17 May 2013 - 12:05 PM

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.



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7787

Like
0Likes
Like

Posted 17 May 2013 - 12:40 PM

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)
{...}


#3 PtrN   Members   -  Reputation: 101

Like
0Likes
Like

Posted 17 May 2013 - 12:46 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS