Ticket #2349: highdpi2.patch

File highdpi2.patch, 8.7 KB (added by wraitii, 7 years ago)
  • binaries/data/config/default.cfg

    diff --git a/binaries/data/config/default.cfg b/binaries/data/config/default.cfg
    index 5df2129..ee92943 100644
    a b yres = 0  
    5656; Force a non-standard bit depth (if 0 then use the current desktop bit depth)
    5757bpp = 0
    5858
     59; Render at high-dpi resolution if screen supports it. Will be much slower.
     60high_dpi = false
     61
    5962; Preferred display (for multidisplay setups, only works with SDL 2.0)
    6063display = 0
    6164
  • source/gui/CGUI.cpp

    diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp
    index 09c3386..a0bf996 100644
    a b InReaction CGUI::HandleEvent(const SDL_Event_* ev)  
    8080        // Yes the mouse position is stored as float to avoid
    8181        //  constant conversions when operating in a
    8282        //  float-based environment.
    83         m_MousePos = CPos((float)ev->ev.motion.x * g_GuiScale, (float)ev->ev.motion.y * g_GuiScale);
     83        m_MousePos = CPos((float)ev->ev.motion.x * g_GuiScale * g_DpiScale, (float)ev->ev.motion.y * g_GuiScale * g_DpiScale);
    8484
    8585        SGUIMessage msg(GUIM_MOUSE_MOTION);
    8686        GUI<SGUIMessage>::RecurseObject(GUIRR_HIDDEN | GUIRR_GHOST, m_BaseObject,
    InReaction CGUI::HandleEvent(const SDL_Event_* ev)  
    107107    CPos oldMousePos = m_MousePos;
    108108    if (ev->ev.type == SDL_MOUSEBUTTONDOWN || ev->ev.type == SDL_MOUSEBUTTONUP)
    109109    {
    110         m_MousePos = CPos((float)ev->ev.button.x * g_GuiScale, (float)ev->ev.button.y * g_GuiScale);
     110        m_MousePos = CPos((float)ev->ev.button.x * g_GuiScale * g_DpiScale, (float)ev->ev.button.y * g_GuiScale * g_DpiScale);
    111111    }
    112112
    113113    // Only one object can be hovered
  • source/gui/scripting/GuiScriptConversions.cpp

    diff --git a/source/gui/scripting/GuiScriptConversions.cpp b/source/gui/scripting/GuiScriptConversions.cpp
    index b2a83c1..f817d0e 100644
    a b  
    2727    // ignore JS_SetProperty return value, because errors should be impossible
    2828    // and we can't do anything useful in the case of errors anyway
    2929
     30extern float g_DpiScale;
     31
    3032template<> void ScriptInterface::ToJSVal<SDL_Event_>(JSContext* cx, JS::MutableHandleValue ret, SDL_Event_ const& val)
    3133{
    3234    JSAutoRequest rq(cx);
    template<> void ScriptInterface::ToJSVal<SDL_Event_>(JSContext* cx, JS::MutableH  
    8789    {
    8890        // SET(obj, "which", (int)val.ev.motion.which); // (not in wsdl.h)
    8991        // SET(obj, "state", (int)val.ev.motion.state); // (not in wsdl.h)
    90         SET(obj, "x", (int)val.ev.motion.x);
    91         SET(obj, "y", (int)val.ev.motion.y);
     92        SET(obj, "x", (int)val.ev.motion.x * g_DpiScale);
     93        SET(obj, "y", (int)val.ev.motion.y * g_DpiScale);
    9294        // SET(obj, "xrel", (int)val.ev.motion.xrel); // (not in wsdl.h)
    9395        // SET(obj, "yrel", (int)val.ev.motion.yrel); // (not in wsdl.h)
    9496        break;
    template<> void ScriptInterface::ToJSVal<SDL_Event_>(JSContext* cx, JS::MutableH  
    99101        // SET(obj, "which", (int)val.ev.button.which); // (not in wsdl.h)
    100102        SET(obj, "button", (int)val.ev.button.button);
    101103        SET(obj, "state", (int)val.ev.button.state);
    102         SET(obj, "x", (int)val.ev.button.x);
    103         SET(obj, "y", (int)val.ev.button.y);
     104        SET(obj, "x", (int)val.ev.button.x * g_DpiScale);
     105        SET(obj, "y", (int)val.ev.button.y * g_DpiScale);
    104106        break;
    105107    }
    106108    case SDL_HOTKEYDOWN:
  • source/ps/GameSetup/Config.cpp

    diff --git a/source/ps/GameSetup/Config.cpp b/source/ps/GameSetup/Config.cpp
    index d27cf8e..e0d0e85 100644
    a b CStr g_RenderPath = "default";  
    6161
    6262int g_xres, g_yres;
    6363float g_GuiScale = 1.0f;
     64float g_DpiScale = 1.0f;
    6465bool g_VSync = false;
    6566
    6667bool g_Quickstart = false;
  • source/ps/GameSetup/Config.h

    diff --git a/source/ps/GameSetup/Config.h b/source/ps/GameSetup/Config.h
    index 9173fb2..f02dac0 100644
    a b extern CStr g_RenderPath;  
    8686
    8787extern int g_xres, g_yres;
    8888extern float g_GuiScale;
     89extern float g_DpiScale;
    8990extern bool g_VSync;
    9091
    9192extern bool g_Quickstart;
  • source/ps/Globals.cpp

    diff --git a/source/ps/Globals.cpp b/source/ps/Globals.cpp
    index 3c6e639..d6a3153 100644
    a b InReaction GlobalsInputHandler(const SDL_Event_* ev)  
    6969        return IN_PASS;
    7070
    7171    case SDL_MOUSEMOTION:
    72         g_mouse_x = ev->ev.motion.x;
    73         g_mouse_y = ev->ev.motion.y;
     72        g_mouse_x = ev->ev.motion.x * g_DpiScale;
     73        g_mouse_y = ev->ev.motion.y * g_DpiScale;
    7474        return IN_PASS;
    7575
    7676    case SDL_MOUSEBUTTONDOWN:
  • source/ps/VideoMode.cpp

    diff --git a/source/ps/VideoMode.cpp b/source/ps/VideoMode.cpp
    index c1aa2cb..e2b4d71 100644
    a b CVideoMode g_VideoMode;  
    4949CVideoMode::CVideoMode() :
    5050    m_IsFullscreen(false), m_IsInitialised(false), m_Window(NULL),
    5151    m_PreferredW(0), m_PreferredH(0), m_PreferredBPP(0), m_PreferredFreq(0),
    52     m_ConfigW(0), m_ConfigH(0), m_ConfigBPP(0), m_ConfigFullscreen(false), m_ConfigForceS3TCEnable(true),
     52    m_ConfigW(0), m_ConfigH(0), m_ConfigBPP(0), m_ConfigHighDPI(false), m_ConfigFullscreen(false), m_ConfigForceS3TCEnable(true),
    5353    m_WindowedW(DEFAULT_WINDOW_W), m_WindowedH(DEFAULT_WINDOW_H), m_WindowedX(0), m_WindowedY(0)
    5454{
    5555    // (m_ConfigFullscreen defaults to false, so users don't get stuck if
    void CVideoMode::ReadConfig()  
    6464
    6565    CFG_GET_VAL("xres", m_ConfigW);
    6666    CFG_GET_VAL("yres", m_ConfigH);
     67    CFG_GET_VAL("high_dpi", m_ConfigHighDPI);
    6768    CFG_GET_VAL("bpp", m_ConfigBPP);
    6869    CFG_GET_VAL("display", m_ConfigDisplay);
    6970    CFG_GET_VAL("force_s3tc_enable", m_ConfigForceS3TCEnable);
    bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen)  
    7980    {
    8081        // Note: these flags only take affect in SDL_CreateWindow
    8182        flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
     83        if (m_ConfigHighDPI)
     84            flags |= SDL_WINDOW_ALLOW_HIGHDPI;
    8285        m_WindowedX = m_WindowedY = SDL_WINDOWPOS_CENTERED_DISPLAY(m_ConfigDisplay);
    8386
    8487        m_Window = SDL_CreateWindow("0 A.D.", m_WindowedX, m_WindowedY, w, h, flags);
    bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen)  
    147150    // Grab the current video settings
    148151    SDL_GetWindowSize(m_Window, &m_CurrentW, &m_CurrentH);
    149152    m_CurrentBPP = bpp;
     153    LOGMESSAGE("Window Size: %dx%d", m_CurrentW, m_CurrentH);
     154   
     155    // This may differ from SDL_GetWindowSize() if we're rendering to a high-DPI drawable
     156    int drawingWidth, drawingHeight;
     157    SDL_GL_GetDrawableSize(m_Window, &drawingWidth, &drawingHeight);
     158    LOGMESSAGE("Drawable Size: %dx%d", drawingWidth, drawingHeight);
     159
     160    UpdateRenderer();
    150161
    151162    if (fullscreen)
    152163        SDL_SetWindowGrab(m_Window, SDL_TRUE);
    bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen)  
    155166
    156167    m_IsFullscreen = fullscreen;
    157168
    158     g_xres = m_CurrentW;
    159     g_yres = m_CurrentH;
    160 
    161169    return true;
    162170}
    163171
    bool CVideoMode::ResizeWindow(int w, int h)  
    325333
    326334    m_WindowedW = w;
    327335    m_WindowedH = h;
    328 
    329     UpdateRenderer(w, h);
    330 
     336   
    331337    return true;
    332338}
    333339
    bool CVideoMode::SetFullscreen(bool fullscreen)  
    370376        if (!SetVideoMode(w, h, bpp, fullscreen))
    371377            return false;
    372378
    373         UpdateRenderer(m_CurrentW, m_CurrentH);
    374 
    375379        return true;
    376380    }
    377381    else
    bool CVideoMode::SetFullscreen(bool fullscreen)  
    386390        if (!SetVideoMode(w, h, bpp, fullscreen))
    387391            return false;
    388392
    389         UpdateRenderer(w, h);
    390 
    391393        return true;
    392394    }
    393395}
    void CVideoMode::UpdatePosition(int x, int y)  
    406408    }
    407409}
    408410
     411void CVideoMode::UpdateRenderer() {
     412    // This may differ from SDL_GetWindowSize() if we're rendering to a high-DPI drawable
     413    int drawingWidth, drawingHeight;
     414    SDL_GL_GetDrawableSize(m_Window, &drawingWidth, &drawingHeight);
     415    UpdateRenderer(drawingWidth, drawingHeight);
     416
     417    g_xres = drawingWidth;
     418    g_yres = drawingHeight;
     419    g_GuiScale = m_CurrentW / ((float)drawingWidth);
     420    g_DpiScale = ((float)drawingWidth) / m_CurrentW;
     421}
     422
    409423void CVideoMode::UpdateRenderer(int w, int h)
    410424{
    411425    if (w < 2) w = 2; // avoid GL errors caused by invalid sizes
  • source/ps/VideoMode.h

    diff --git a/source/ps/VideoMode.h b/source/ps/VideoMode.h
    index 0a820da..2cc2685 100644
    a b public:  
    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     */
    private:  
    105111    int m_ConfigW;
    106112    int m_ConfigH;
    107113    int m_ConfigBPP;
     114    bool m_ConfigHighDPI;
    108115    int m_ConfigDisplay;
    109116    bool m_ConfigFullscreen;
    110117    bool m_ConfigForceS3TCEnable;