Integer Pointers in SDL

Why does SDL require integer pointers in functions like SDL_GetWindowSize()?

SDL requires integer pointers in functions like SDL_GetWindowSize() to allow it to directly write the requested data back to your variables.

This is a common C-style approach for returning multiple values in functions that can't use a direct return value due to limitations in C.

How It Works

When you call SDL_GetWindowSize(window, &width, &height), SDL writes the current window width and height into the memory locations pointed to by &width and &height.

This avoids the need for dynamic memory allocation or returning a struct. Here's an example:

#include <SDL.h>
#include <iostream>

int main() {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* window =
    SDL_CreateWindow("Window Size Example",
                     SDL_WINDOWPOS_CENTERED,
                     SDL_WINDOWPOS_CENTERED,
                     400, 400,
                     SDL_WINDOW_RESIZABLE);

  int width, height;
  SDL_GetWindowSize(window, &width, &height);
  std::cout << "Window size: " << width << "x"
    << height << "\n";

  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}
Window size: 400x400

Advantages

  • Efficient: No temporary variables or heap allocations.
  • Simple: Pointers allow multiple outputs in a single function call.

Caveats

Always ensure the pointers are valid. Passing nullptr for either dimension skips updating that value, which can lead to bugs if misused.

Using pointers keeps SDL lightweight and efficient, consistent with its design philosophy.

Window Sizing

Learn how to resize, constrain, and manage SDL2 windows

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Handling Exceeded Minimum Window Size
What happens if the minimum window size exceeds the current window size?
Resizing and Graphical Content
How does resizing affect graphical content displayed inside the window?
Constraints in SDL_SetWindowSize()
Is SDL_SetWindowSize() affected by the maximum and minimum size constraints?
Size Changed vs Resized Events
What is the difference between SDL_WINDOWEVENT_SIZE_CHANGED and SDL_WINDOWEVENT_RESIZED?
Best Practices for Resize Events
What are the best practices for handling resize events in games?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant