[SDL] HiDPI support on Mac OS X

Aggelos Kolaitis neoaggelos at gmail.com
Fri Oct 4 09:33:57 PDT 2013


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

On 10/4/13, Stefanos A. <stapostol at gmail.com> wrote:
> Hmm, this fixes the glViewport() issue. However, now mouse coordinates no
> longer correspond to the drawable size...
>
> I guess it is by design that the mouse coordinates are specified in points
> (as are the window coordinates). That said, is there any public API to map
> mouse coordinates from points to pixels? There are applications that rely
> on pixel-precise mouse input (e.g. for painting).
>
>
> 2013/10/4 Alex Szpakowski <aszpakowski at gmail.com>
>
>> I think this is the expected behaviour - check out the bugzilla
>> discussion
>> where the feature was created:
>> https://bugzilla.libsdl.org/show_bug.cgi?id=1934
>> Use SDL_GL_GetDrawableSize to get the pixel dimensions of the OpenGL
>> drawable (for viewport setting, etc.)
>>
>> On 2013-10-03, at 7:35 PM, "Stefanos A." <stapostol at gmail.com> wrote:
>>
>> Hi,
>>
>> I'm facing a strange issue when testing the hg tip with the new
>> SDL_WINDOW_ALLOW_HIDPI support on my rMBP: although the window is sized
>> correctly, all SDL properties return incorrect values.
>>
>> For instance, if I create a 800x600 window with the HIDPI flag, I receive
>> a 1600x1200 window (which is expected), but SDL_GetWindowSize() still
>> returns 800x600; mouse coordinates are limited to 800x600;
>> SDL_GetDisplayBounds() returns 1440x900 (instead of 2880x1800).
>>
>> Is this a known issue? Is there anything I can do to help improve the
>> situation?
>>
>> Thanks,
>> Stefanos
>> _______________________________________________
>> SDL mailing list
>> SDL at lists.libsdl.org
>> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>>
>>
>>
>> _______________________________________________
>> SDL mailing list
>> SDL at lists.libsdl.org
>> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>>
>>
>



More information about the SDL mailing list