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.
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
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.
Answers to questions are automatically generated and may not have been reviewed.
Learn how to resize, constrain, and manage SDL2 windows