Added opt-in support for high-dpi mode in OS X when on a retina display (resolves issue #761).

Added a ‘highdpi’ boolean flag to t.window/love.window.setMode (defaults to false.)
When the window is actually in high-dpi mode on a supported display, the graphics width and height and the mouse position are in pixels, rather than ‘window coordinates’.

Added love.window.getPixelScale. Returns the scale factor of the window from user-space points to pixels (e.g. it will be 1 normally, and 2 on a retina display in OS X with high-dpi mode enabled.)
This commit is contained in:
Alex Szpakowski
2014-01-17 21:11:20 -04:00
parent 29f47d9a10
commit 309193895a
10 changed files with 308 additions and 148 deletions
+47 -4
View File
@@ -32,6 +32,39 @@ namespace mouse
namespace sdl
{
// SDL reports mouse coordinates in the window coordinate system in OS X, but
// we want them in pixel coordinates (may be different with high-DPI enabled.)
static void windowToPixelCoords(int *x, int *y)
{
double scale = 1.0;
love::window::Window *window = love::window::sdl::Window::getSingleton();
if (window != nullptr)
scale = window->getPixelScale();
if (x != nullptr)
*x = int(double(*x) * scale);
if (y != nullptr)
*y = int(double(*x) * scale);
}
// And vice versa for setting mouse coordinates.
static void pixelToWindowCoords(int *x, int *y)
{
double scale = 1.0;
love::window::Window *window = love::window::sdl::Window::getSingleton();
if (window != nullptr)
scale = window->getPixelScale();
if (x != nullptr)
*x = int(double(*x) / scale);
if (y != nullptr)
*y = int(double(*x) / scale);
}
const char *Mouse::getName() const
{
return "love.mouse.sdl";
@@ -98,30 +131,40 @@ love::mouse::Cursor *Mouse::getCursor() const
int Mouse::getX() const
{
int x;
SDL_GetMouseState(&x, 0);
SDL_GetMouseState(&x, nullptr);
windowToPixelCoords(&x, nullptr);
return x;
}
int Mouse::getY() const
{
int y;
SDL_GetMouseState(0, &y);
SDL_GetMouseState(nullptr, &y);
windowToPixelCoords(nullptr, &y);
return y;
}
void Mouse::getPosition(int &x, int &y) const
{
SDL_GetMouseState(&x, &y);
int mx, my;
SDL_GetMouseState(&mx, &my);
windowToPixelCoords(&mx, &my);
x = mx;
y = my;
}
void Mouse::setPosition(int x, int y)
{
love::window::Window *window = love::window::sdl::Window::getSingleton();
SDL_Window *handle = NULL;
SDL_Window *handle = nullptr;
if (window)
handle = (SDL_Window *) window->getHandle();
pixelToWindowCoords(&x, &y);
SDL_WarpMouseInWindow(handle, x, y);
}