Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


PtrN

Member Since 17 May 2013
Offline Last Active May 17 2013 12:43 PM

Topics I've Started

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

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.


PARTNERS