Ticket #2349: hidpi.patch

File hidpi.patch, 6.3 KB (added by historic_bruno, 8 years ago)

patch by orangy from #3692

  • source/gui/CGUI.cpp

     
    7979        // Yes the mouse position is stored as float to avoid
    8080        //  constant conversions when operating in a
    8181        //  float-based environment.
    82         m_MousePos = CPos((float)ev->ev.motion.x * g_GuiScale, (float)ev->ev.motion.y * g_GuiScale);
     82        m_MousePos = CPos((float)ev->ev.motion.x * g_GuiScale * g_DpiScale, (float)ev->ev.motion.y * g_GuiScale * g_DpiScale);
    8383
    8484        SGUIMessage msg(GUIM_MOUSE_MOTION);
    8585        GUI<SGUIMessage>::RecurseObject(GUIRR_HIDDEN | GUIRR_GHOST, m_BaseObject,
     
    106106    CPos oldMousePos = m_MousePos;
    107107    if (ev->ev.type == SDL_MOUSEBUTTONDOWN || ev->ev.type == SDL_MOUSEBUTTONUP)
    108108    {
    109         m_MousePos = CPos((float)ev->ev.button.x * g_GuiScale, (float)ev->ev.button.y * g_GuiScale);
     109        m_MousePos = CPos((float)ev->ev.button.x * g_GuiScale * g_DpiScale, (float)ev->ev.button.y * g_GuiScale * g_DpiScale);
    110110    }
    111111
    112112    // Only one object can be hovered
  • source/gui/scripting/GuiScriptConversions.cpp

     
    109109    {
    110110        // SET(obj, "which", (int)val.ev.motion.which); // (not in wsdl.h)
    111111        // SET(obj, "state", (int)val.ev.motion.state); // (not in wsdl.h)
    112         SET(obj, "x", (int)val.ev.motion.x);
    113         SET(obj, "y", (int)val.ev.motion.y);
     112        SET(obj, "x", (int)val.ev.motion.x * g_DpiScale);
     113        SET(obj, "y", (int)val.ev.motion.y * g_DpiScale);
    114114        // SET(obj, "xrel", (int)val.ev.motion.xrel); // (not in wsdl.h)
    115115        // SET(obj, "yrel", (int)val.ev.motion.yrel); // (not in wsdl.h)
    116116        break;
     
    121121        // SET(obj, "which", (int)val.ev.button.which); // (not in wsdl.h)
    122122        SET(obj, "button", (int)val.ev.button.button);
    123123        SET(obj, "state", (int)val.ev.button.state);
    124         SET(obj, "x", (int)val.ev.button.x);
    125         SET(obj, "y", (int)val.ev.button.y);
     124        SET(obj, "x", (int)val.ev.button.x * g_DpiScale);
     125        SET(obj, "y", (int)val.ev.button.y * g_DpiScale);
    126126        break;
    127127    }
    128128    case SDL_HOTKEYDOWN:
  • source/ps/GameSetup/Config.cpp

     
    5959
    6060int g_xres, g_yres;
    6161float g_GuiScale = 1.0f;
     62float g_DpiScale = 1.0f;
    6263bool g_VSync = false;
    6364
    6465bool g_Quickstart = false;
  • source/ps/GameSetup/Config.h

     
    8282
    8383extern int g_xres, g_yres;
    8484extern float g_GuiScale;
     85extern float g_DpiScale;
    8586extern bool g_VSync;
    8687
    8788extern bool g_Quickstart;
  • source/ps/Globals.cpp

     
    9090#endif
    9191
    9292    case SDL_MOUSEMOTION:
    93         g_mouse_x = ev->ev.motion.x;
    94         g_mouse_y = ev->ev.motion.y;
     93        g_mouse_x = ev->ev.motion.x * g_DpiScale;
     94        g_mouse_y = ev->ev.motion.y * g_DpiScale;
    9595        return IN_PASS;
    9696
    9797    case SDL_MOUSEBUTTONDOWN:
  • source/ps/VideoMode.cpp

     
    7979    if (!m_Window)
    8080    {
    8181        // Note: these flags only take affect in SDL_CreateWindow
    82         flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
     82        flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
    8383        m_WindowedX = m_WindowedY = SDL_WINDOWPOS_CENTERED_DISPLAY(m_ConfigDisplay);
    8484
    8585        m_Window = SDL_CreateWindow("0 A.D.", m_WindowedX, m_WindowedY, w, h, flags);
     
    148148    // Grab the current video settings
    149149    SDL_GetWindowSize(m_Window, &m_CurrentW, &m_CurrentH);
    150150    m_CurrentBPP = bpp;
     151    LOGMESSAGE("Window Size: %dx%d", m_CurrentW, m_CurrentH);
     152   
     153    // This may differ from SDL_GetWindowSize() if we're rendering to a high-DPI drawable
     154    int drawingWidth, drawingHeight;
     155    SDL_GL_GetDrawableSize(m_Window, &drawingWidth, &drawingHeight);
     156    LOGMESSAGE("Drawable Size: %dx%d", drawingWidth, drawingHeight);
    151157
     158    UpdateRenderer();
     159
    152160    if (fullscreen)
    153161        SDL_SetWindowGrab(m_Window, SDL_TRUE);
    154162    else
     
    197205
    198206    m_IsFullscreen = fullscreen;
    199207
    200     g_xres = m_CurrentW;
    201     g_yres = m_CurrentH;
    202 
    203208    return true;
    204209}
    205210
     
    389394
    390395    m_WindowedW = w;
    391396    m_WindowedH = h;
    392 
    393     UpdateRenderer(w, h);
    394 
     397   
    395398    return true;
    396399}
    397400
     
    434437        if (!SetVideoMode(w, h, bpp, fullscreen))
    435438            return false;
    436439
    437         UpdateRenderer(m_CurrentW, m_CurrentH);
    438 
    439440        return true;
    440441    }
    441442    else
     
    450451        if (!SetVideoMode(w, h, bpp, fullscreen))
    451452            return false;
    452453
    453         UpdateRenderer(w, h);
    454 
    455454        return true;
    456455    }
    457456}
     
    470469    }
    471470}
    472471
     472void CVideoMode::UpdateRenderer() {
     473    // This may differ from SDL_GetWindowSize() if we're rendering to a high-DPI drawable
     474    int drawingWidth, drawingHeight;
     475    SDL_GL_GetDrawableSize(m_Window, &drawingWidth, &drawingHeight);
     476    UpdateRenderer(drawingWidth, drawingHeight);
     477
     478    g_xres = drawingWidth;
     479    g_yres = drawingHeight;
     480    g_GuiScale = m_CurrentW / ((float)drawingWidth);
     481    g_DpiScale = ((float)drawingWidth) / m_CurrentW;
     482}
     483
    473484void CVideoMode::UpdateRenderer(int w, int h)
    474485{
    475486    if (w < 2) w = 2; // avoid GL errors caused by invalid sizes
  • source/ps/VideoMode.h

     
    6262     */
    6363    void UpdatePosition(int x, int y);
    6464
     65    /**
     66     * Update the graphics code to start drawing to the new size set for the SDL context.
     67     * This should be called after the GL context has been resized.
     68     */
     69    void UpdateRenderer();
     70
    6571    /**
    66      * Update the graphics code to start drawing to the new size.
     72     * Update the graphics code to start drawing to the specified size.
    6773     * This should be called after the GL context has been resized.
    6874     * This can also be used when the GL context is managed externally, not via SDL.
    6975     */