SDL School

API reference / Windows and drawing

SDL_CreateWindowAndRenderer

Function, SDL3

Makes a window and a renderer in one call. The window is the rectangle on the screen, and the renderer is the object that draws in it.

Declaration

#include <SDL3/SDL.h>

bool SDL_CreateWindowAndRenderer(const char *title, int width, int height, SDL_WindowFlags window_flags, SDL_Window **window, SDL_Renderer **renderer);

Parameters

titleThe text in the title bar.
widthThe width of the window in pixels.
heightThe height of the window in pixels.
window_flagsOptions for the window. Use 0 for a normal window.
windowThe address of your window pointer. SDL writes the new pointer there.
rendererThe address of your renderer pointer. SDL writes the new pointer there.

Return value

True on success. On failure it returns false, and SDL_GetError gives the reason.

Example

Open a window
if (!SDL_CreateWindowAndRenderer("Hello, window", 640, 480, 0,
                                 &window, &renderer)) {
    SDL_Log("SDL_CreateWindowAndRenderer failed: %s", SDL_GetError());
    return SDL_APP_FAILURE;
}

See this code run in Open a window. Download the full program (.zip)

In SDL2

The name is the same, but the title is not an argument, and it returns 0 on success. Set the title with SDL_SetWindowTitle.

See also