Setting up SDL2 in macOS (Xcode or CMake)

Handling Retina Displays in SDL2 on macOS

How can I ensure my SDL2 application looks sharp on Retina displays on macOS?

Abstract art representing computer programming

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:

  1. We enable high DPI support by setting the SDL_HINT_VIDEO_HIGHDPI_DISABLED hint to "0".
  2. We create the window with the SDL_WINDOW_ALLOW_HIGHDPI flag.
  3. We get the actual drawable size of the window using SDL_GL_GetDrawableSize.
  4. We create the renderer using the drawable size and set the logical size to the original window size using 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.

This Question is from the Lesson:

Setting up SDL2 in macOS (Xcode or CMake)

This step-by-step guide shows you how to set up SDL2 in an Xcode or CMake project on macOS

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Setting up SDL2 in macOS (Xcode or CMake)

This step-by-step guide shows you how to set up SDL2 in an Xcode or CMake project on macOS

3D art representing computer programming
Part of the course:

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved