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.