SDL Function in general

Hello

I’m currently learning about SDL and i want to take all my functions in an other file name functions.hpp and call the functions in my main.cpp but i don’t know how to do that i have trying different things but it doesn’t work :
An example of what i have in my main.cpp:

//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
//The music that will be played
Mix_Music *gMusic = NULL;
int main( int argc, char* args[] )
{
    //Start up SDL and create window
    if( !init() )
    {
        printf( "Failed to initialize!\n" );
    }
    else
    {
        // PARTIE MENU EN TEST
        
        
        //The level tiles
        Tile* tileSet[ TOTAL_TILES ];
        
        //Load media
        if( !loadMedia( tileSet ) )
        {
            printf( "Failed to load media!\n" );
        }
        else{}

}}

i want to move the init function in an other file but the gWindow and gRenderer make a problem

bool init()
{
//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( "Labyrinthe", 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;
            }
            //essai audio
            //Initialize SDL_mixer
            if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 )
            {
                printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
                success = false;
            }
            
        }
    }
}

return success;

}

Thanks

i want to move the init function in an other file but the gWindow and gRenderer make a problem

To be clear: you have a separate source file with your init() function, and the problem is that it can’t see the global variables in main.cpp (gWindow, gRenderer, etc)?

In the same file as init(), somewhere before it:

extern SDL_Window* gWindow;
extern SDL_Renderer* gRenderer;
extern Mix_Music *gMusic;

This tells the compiler that these variables exist somewhere, so it’s okay to use them. The linker will figure out where to find them when building the final executable.

(Ideally, this goes in a header and not at the top of a source file, so there’s one copy of this information that various source files can share, so when this list of variables change, you only have to update one place. But you still need the non-extern lines in main.cpp. One’s a declaration, the other is a definition, and the difference is important. One is a note in the card catalog, the other is the actual book on the shelf.)

(Also, if this wasn’t what you were asking, I apologize in advance!)

Thanks it was exactly what i m looking for! :slight_smile:
One thing: is not dangerous to use global variables ?
Thanks you debugged my code
Have a good day :slight_smile:

1 Like

One thing: is not dangerous to use global variables ?

It can get messy, and working carefully to manage that messiness is a skill programmers develop over time, but it’s extremely rare to see a large program that doesn’t use some globals.

Many programming courses will say things like “global variables are bad, never use them,” and that’s frankly wrong advice. Limit your use of globals, because it’s easy to get lazy and sloppy with them, but they are a useful tool, even in good clean code.

Okay thanks :slight_smile:

1 Like

Absolutely try global variables so that you learn them. Same with “forbidden” goto and friends :slight_smile:

Another way might be to use a some kind of a context variable and pass that to your functions.

For example:

typedef struct {
  SDL_Renderer *renderer;
  SDL_Window *window;
  Mix_Music *music;
} Context;

Then you can initialize the content somewhere near your main() function and after that you can pass it forward.

void MyAmazingFunction(Context *ctx) { ... }

int main(...) {
  Context ctx;
  ctx.renderer = SDL_CreateRenderer(...);
  // and so on

  MyAmazingFunction(&ctx);
}