Drawing shapesSDL 3.2

SDL_SetRenderDrawColor

Chooses the colour for the next clear, rectangle, line or point.

Signature

bool SDL_SetRenderDrawColor(SDL_Renderer *renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

How it works

The renderer remembers one current colour. SDL_RenderClear, SDL_RenderFillRect, SDL_RenderRect, SDL_RenderLine and SDL_RenderPoint all use it. Set it right before each thing you draw, because the last value stays.

Values go from 0 to 255. Alpha (a) only has an effect once blending is on (SDL_SetRenderDrawBlendMode with SDL_BLENDMODE_BLEND). SDL_SetRenderDrawColorFloat takes 0–1 floats instead.

Parameters

rendererSDL_Renderer *

The renderer.

rUint8

Red, 0–255.

gUint8

Green, 0–255.

bUint8

Blue, 0–255.

aUint8

Alpha (opacity), 0–255. 255 is opaque.

Return value

true on success, false on failure.

Example

example.cpp
1SDL_SetRenderDrawColor(renderer, 16, 18, 26, 255);    // background
2SDL_RenderClear(renderer);
3SDL_SetRenderDrawColor(renderer, 59, 130, 246, 255);   // blue box
4SDL_RenderFillRect(renderer, &box);
Official SDL wiki