SDL School

API reference / Application

SDL_AppInit

Function, SDL3

You write this function, and SDL calls it one time at the start of the program. Use it for setup.

To use the main callbacks, define SDL_MAIN_USE_CALLBACKS before you include SDL_main.h in one source file.

Declaration

#include <SDL3/SDL.h>

SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]);

Parameters

appstateThe place to store a pointer to your own data. SDL passes that pointer to your other callbacks.
argcThe number of command line arguments.
argvThe command line arguments.

Return value

SDL_APP_CONTINUE to run the program, SDL_APP_SUCCESS to end it with no error, or SDL_APP_FAILURE to end it with an error.

Example

Hold state in a struct
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
    App *app = SDL_calloc(1, sizeof(App));

    if (app == NULL) {
        return SDL_APP_FAILURE;
    }
    *appstate = app; /* SDL gives this pointer to the other functions. */

    if (!SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("SDL_Init failed: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }

    if (!SDL_CreateWindowAndRenderer("Hold state in a struct", WINDOW_WIDTH, WINDOW_HEIGHT, 0,
                                     &app->window, &app->renderer)) {
        SDL_Log("SDL_CreateWindowAndRenderer failed: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }

    SDL_SetRenderVSync(app->renderer, 1);

    app->square = (SDL_FRect){ 40.0f, 216.0f, 48.0f, 48.0f };
    app->direction = 1.0f;
    app->last_ticks = SDL_GetTicksNS();
    return SDL_APP_CONTINUE;
}

See this code run in Hold state in a struct. Download the full program (.zip)

Notes

In SDL2

SDL2 has no main callbacks. You write main and your own loop.

See also