When developing an SDL2 application for macOS, you may notice that the graphics appear blurry or pixelated on Retina displays. This is because Retina displays have a higher pixel density, and SDL2 needs to be configured to handle this properly.
Here's how you can ensure your SDL2 application looks sharp on Retina displays:
Step 1: Enable high DPI support in SDL2 by setting the SDL_HINT_VIDEO_HIGHDPI_DISABLED
 hint to "0"
 before creating the window:
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
Step 2: Create the SDL2 window with the SDL_WINDOW_ALLOW_HIGHDPI
 flag:
SDL_Window* window = SDL_CreateWindow(
"My Application",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800, 600,
SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
Step 3: After creating the window, get the actual drawable size of the window using SDL_GL_GetDrawableSize
 or SDL_GetRendererOutputSize
:
int drawableWidth, drawableHeight;
SDL_GL_GetDrawableSize(window,
&drawableWidth, &drawableHeight);
Step 4: Use the drawable size to create your rendering context (e.g., SDL_Renderer
 or OpenGL context) and scale your graphics accordingly.
Here's a complete example:
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow(
"My Application",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800, 600,
SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == nullptr) {
// Handle error
return 1;
}
int drawableWidth, drawableHeight;
SDL_GL_GetDrawableSize(window,
&drawableWidth, &drawableHeight);
SDL_Renderer* renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr) {
return 1;
}
SDL_RenderSetLogicalSize(renderer, 800, 600);
// Render your game content
// ...
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
In this example:
SDL_HINT_VIDEO_HIGHDPI_DISABLED
 hint to "0"
.SDL_WINDOW_ALLOW_HIGHDPI
 flag.SDL_GL_GetDrawableSize
.SDL_RenderSetLogicalSize
. This ensures that the rendering is scaled correctly for Retina displays.By following these steps, your SDL2 application will appear sharp and crisp on Retina displays, providing a better visual experience for users on macOS.
Answers to questions are automatically generated and may not have been reviewed.
This step-by-step guide shows you how to set up SDL2 in an Xcode or CMake project on macOS