Code terminates at if( player == NULL ) in the loadMedia function

The code terminates once it reaches the if( player == NULL ) statement in the loadMedia function. No errors are given, just ends.

loadMedia function

bool loadMedia()
{
  std::cout << "Loading media" << std::endl;

  player = SDL_LoadBMP( "player.bmp" );

  if( player == NULL )
  {
    std::cout << "Media could not be loaded " << static_cast< std::string >( SDL_GetError() );
    return false;
  }

  return true;
}

Entire code

//Johnathan Regha-Dodge: CS_version1

#include <iostream>
#include <limits>
#include "src/include/SDL2/SDL.h"

//Screen dimensions
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//Starts up SDL and creates Window
bool init();

//Loads Media
bool loadMedia();

//Close SDL window
void close();

//window to render to
SDL_Window* window = NULL;

SDL_Surface *screen = NULL;

SDL_Surface *player = NULL;

SDL_Rect *playerPos;

bool init()
{
  std::cout << "Initializing" << std::endl;

  //SDL_Init returns -1 if an error occurs, function below to check for error
  if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  {
    std::cout << "Initialization failed " << static_cast< std::string >( SDL_GetError() );
    return false;
  }

    //Assigning parameters to create window
    window = SDL_CreateWindow( "CS_version1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

    if( window == NULL )
    {
      std::cout << "Window could not be created " << static_cast< std::string >( SDL_GetError() );
    }

    //Get window surface
    screen = SDL_GetWindowSurface( window );

    return true;
}


bool loadMedia()
{
  std::cout << "Loading media" << std::endl;

  player = SDL_LoadBMP( "player.bmp" );

  if( player == NULL )
  {
    std::cout << "Media could not be loaded " << static_cast< std::string >( SDL_GetError() );
    return false;
  }

  return true;
}

void close()
{
  std::cout << "Closing" << std::endl;

  //Deallocate surface
  SDL_FreeSurface( player );

  //Destroy window
  SDL_DestroyWindow( window );
  window = NULL;

  //Quits SDL
  SDL_Quit();
}

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

  if( !init() )
  {
    std::cout << "Initialization failed " << static_cast< std::string >( SDL_GetError() );

    return false;
  }
  else
  {
    //Load Media
    if( !loadMedia() )
    {
      std::cout << "media failed " << static_cast< std::string >( SDL_GetError() );

      return false;
    }
    else
    {
      playerPos->x = 0;
      playerPos->y = 0;
      playerPos->w = 20;
      playerPos->h = 40;

      SDL_BlitSurface(player, NULL, screen, playerPos);

      SDL_UpdateWindowSurface( window );

      SDL_Delay(2000);

    }
  }

  close();

  //Console closes once program ends or error occurs, code used to keep it open
  std::cout << "Enter to continue..." << std::endl;
  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

  return 0;
}

1 answer

  • answered 2022-05-03 01:58 Fantastic Mr Fox

    here is how you call loadMedia:

       if( !loadMedia() )
        {
          std::cout << "media failed " << static_cast< std::string >( SDL_GetError() );
    
          return false;
        }
    

    You have indicated that when player is null, it fails, but why? If player is null, the function loadMedia will return false, putting you in the above if statement.

          std::cout << "media failed " << static_cast< std::string >( SDL_GetError() );
    
          return false;
    

    The first line, prints, but you don't see it. This is likely because you don't flush the output stream, and your program ends soon after. Try adding a force flush with << std::endl.

    Then the next statement, you return. You are returning from the main function, so your program will exit. Since it is int main and you are doing return false the value returned is converted from false to an int which happens to be the value 0. Returning 0 from main indicates that the program has completed successfully. You could return an error code like -1 or even throw an exception to indicate to the user that it has failed.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum