[SDL] HiDPI support on Mac OS X

Alex Szpakowski aszpakowski at gmail.com
Fri Oct 4 09:36:34 PDT 2013


Why not use SDL_GL_GetDrawableSize / SDL_GetWindowSize instead of relying on a hard-coded 2x scale?

On 2013-10-04, at 1:33 PM, Aggelos Kolaitis <neoaggelos at gmail.com> wrote:

> Hey Stephanos,
> 
> You can overcome this issue by using an event watcher, a callback
> function that is automatically called when new events arrive. Use it
> to check if the window has SDL_WINDOW_ALLOW_HIDPI and edit mouse
> coordinates accordingly:
> 
> [code]
> 
> int MouseCoordinatesEventWatch(void *unused, SDL_Event* event) {
>  SDL_Window* window;
>  int window_flags;
> 
>  switch (event->type) {
>    case SDL_MOUSEBUTTONDOWN:
>    case SDL_MOUSEBUTTONUP:
>     window = SDL_GetWindowFromID(event->windowID);
>     if (SDL_GetWindowFlags(window) & SDL_WINDOW_ALLOW_HIDPI)) {
>       event->button.x *= 2;
>       event->button.y *= 2;
>    }
>    break;
>  case SDL_MOUSEMOTION:
>    window = SDL_GetWindowFromID(event->windowID);
>    if (SDL_GetWindowFlags(window) & SDL_WINDOW_ALLOW_HIDPI)) {
>      event->motion.x *= 2;
>      event->motion.y *= 2;
>    }
>  } /* end switch */
> 
> }
> 
> int main()
> {
>  /*** initialization code ****/
>  SDL_AddEventWatch(MouseCoordinatesEventWatch, NULL);
> 
> }
> 
> [/code]
> 
> This will automatically adjust the mouse coordinates every time a new
> mouse event arrives. Note two things:
> * This function handles cases where a window might not have the HiDPI
> flag. If you always use this flag, then
>   you could remove this conditional code.
> * The ' *= 2 ' code is based on my observation of the window sizes /
> event coordinates. It might be different, though
> * This may not work well if you use SDL_RenderSetLogicalSize() --- you
> need to test it to make sure it's okay
> 
> -- Aggelos Kolaitis
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org




More information about the SDL mailing list