William Barnes William Barnes - 3 months ago 17
C++ Question

Passing Pointer To Function C++ SDL2

I have recently been breaking up a large source file into headers and smaller source files. I have never done this before, and largely because of scope, I had to rewrite some of the functions to accept pointers and then call them from main().

This is still a work in progress, however, I've ran into a problem that I have been trying for hours to fix. The code is developed from Lazy Foos excellent SDL tutorial series.

I have moved this function into a header file. Ingeniously titled misc.cpp/misc.h.

bool init(SDL_Window *gWindow, SDL_Renderer *gRenderer)
{
//Initialization flag
bool success = true;

//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "Warning: Linear texture filtering not enabled!" );
}

//Create window
gWindow = SDL_CreateWindow( "Roulette Testing", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );

//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
success = false;
}
std::cout << gRenderer << "renderer not NULL" << std::endl; //This is where I am checking the value of gRenderer within the function.
}
}
}
std::cout << "initialization success" << std::endl;
return success;
}


Now I am trying to call this function from my main() after having included the header and compiled the cpp into object code. This worked fine for the texture header file.

Unfortunately, now when I'm passing a SDL_Renderer pointer to this init function, it's becoming local, and I'm left with a NULL pointer when I return. I have tried the "&" address of operator, but it won't accept it!

init(gWindow, gRenderer);


I'm unsure why, but I'm guessing it's to do with the type.

Any ideas guy??

In response to comments... In my main I fire off a

cout << gRenderer << endl;


and it comes back NULL both before and after the function call.

I have highlighted in the function where I fire off another cout.

Answer

Make the function signature a reference to a pointer and it will return the value returned from SDL_CreateRenderer to the calling function (assuming gRenderer is a pointer when passed in):

bool init(SDL_Window *gWindow, SDL_Renderer *&gRenderer)

Looks like you should also pass gWindow in the same way.

Comments