smhk

Pixel Perfect Rendering in SDL2

TL;DR: To ensure SDL2 rendering is pixel perfect, you must use “nearest pixel sampling” for the render quality, and mark the process as “DPI aware” on Windows.

Enable nearest pixel sampling §

The scale quality must be set to "0", which is nearest pixel sampling.

SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");

If set to "1" / "linear" or "2" / "best", SDL2 will use linear filtering when scaling textures, resulting in antialiasing.

Set DPI aware on Windows §

Windows 10 may automatically apply a scaling of 125% to processes which do not declare themselves “DPI aware”. This will break any attempt at pixel perfect rendering.

The quick fix is to call SetProcessDPIAware() to tell Windows the process is “DPI aware”, and it will no longer apply any automatic scaling.

#ifdef _WIN32
#include <windows.h>  // SetProcessDPIAware
#endif

// ...

int main(int argc, char *argv[])
{
	// ...

#ifdef _WIN32
    // Prevent Windows from applying scaling.
    SetProcessDPIAware();
#endif

}

The official documentation recommends “that you set the process-default DPI awareness via application manifest, not an API call” and to see here for an example. But the API call works just fine.

Ideally, your application should then take the DPI into account.