[SDL] HiDPI support on Mac OS X

neoaggelos at gmail.com neoaggelos at gmail.com
Fri Oct 4 10:40:20 PDT 2013


I added a note about this [2nd note]. Of course in a project environment SDL_GL_GetDrawableSize / SDL_GetWindowSize should be used. I didn't do this for the sake of clarity of how event watchers work.

-- Aggelos Kolaitis

On Oct 4, 2013, at 7:36 PM, Alex Szpakowski <aszpakowski at gmail.com> wrote:

> 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
> 
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



More information about the SDL mailing list