SDL_GetWindowPosition returns null

Hello all,

As the title suggests, I’m trying to get the window position so that mouse events are adjusted to the position of the window. I have a piece of code that works like this:

int* window_x = nullptr;
int* window_y = nullptr;
SDL_GetWindowPosition(window, window_x, window_y);

But the window_x and window_y are always null. Why is that?

Also, is there a way to get mouse events (event.motion.x and event.motion.y) relative to the window position? (This way I wont need window position)

Thanks ! :slight_smile:

You either has to new the integers before passing them into the SDL_GetWindowPosition() function or use local variables and pass them in as references.
If the passed in pointers are nullptr, the function won’t do anything with those parameters.

Example code:

Pass in pointers:

int* pWindowWidth	= new int;
int* pWindowHeight	= new int;

SDL_GetWindowPosition(pWindow, pWindowWidth, pWindowHeight);

Pass by reference:

int WindowWidth		= 0;
int WindowHeight	= 0;

SDL_GetWindowPosition(pWindow, &WindowWidth, &WindowHeight);
1 Like

Ah thank you! I thought SDL would allocate memory dynamically but I guess it’s our job to do that.

Thanks a lot, works like a charm.