Improved the performance of love.window.getPixelScale / toPixels / fromPixels by caching some values. Made some coordinate space-sensitive code use the proper conversions to get to the correct coordinate space.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2015-02-23 17:55:30 -04:00
parent 182792de85
commit 52c4f2c119
14 changed files with 112 additions and 114 deletions
+30 -36
View File
@@ -470,18 +470,12 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
updateSettings(f);
if (gfx != nullptr)
{
int width = curMode.width;
int height = curMode.height;
SDL_GL_GetDrawableSize(window, &width, &height);
gfx->setMode(width, height, curMode.settings.sRGB);
}
gfx->setMode(curMode.pixelwidth, curMode.pixelheight, curMode.settings.sRGB);
return true;
}
bool Window::onWindowResize(int width, int height)
bool Window::onSizeChanged(int width, int height)
{
if (!window)
return false;
@@ -489,6 +483,12 @@ bool Window::onWindowResize(int width, int height)
curMode.width = width;
curMode.height = height;
SDL_GL_GetDrawableSize(window, &curMode.pixelwidth, &curMode.pixelheight);
graphics::Graphics *gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
if (gfx != nullptr)
gfx->setViewportSize(curMode.pixelwidth, curMode.pixelheight);
return true;
}
@@ -498,6 +498,7 @@ void Window::updateSettings(const WindowSettings &newsettings)
// Set the new display mode as the current display mode.
SDL_GetWindowSize(window, &curMode.width, &curMode.height);
SDL_GL_GetDrawableSize(window, &curMode.pixelwidth, &curMode.pixelheight);
if ((wflags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
@@ -604,13 +605,7 @@ bool Window::setFullscreen(bool fullscreen, Window::FullscreenType fstype)
// Update the viewport size now instead of waiting for event polling.
graphics::Graphics *gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
if (gfx != nullptr)
{
int width = curMode.width;
int height = curMode.height;
SDL_GL_GetDrawableSize(window, &width, &height);
gfx->setViewportSize(width, height);
}
gfx->setViewportSize(curMode.pixelwidth, curMode.pixelheight);
return true;
}
@@ -846,31 +841,30 @@ bool Window::isMouseGrabbed() const
void Window::getPixelDimensions(int &w, int &h) const
{
if (window)
SDL_GL_GetDrawableSize(window, &w, &h);
else
{
w = curMode.width;
h = curMode.height;
}
w = curMode.pixelwidth;
h = curMode.pixelheight;
}
void Window::windowToPixelCoords(double *x, double *y) const
{
if (x != nullptr)
*x = (*x) * ((double) curMode.pixelwidth / (double) curMode.width);
if (y != nullptr)
*y = (*y) * ((double) curMode.pixelheight / (double) curMode.height);
}
void Window::pixelToWindowCoords(double *x, double *y) const
{
if (x != nullptr)
*x = (*x) * ((double) curMode.width / (double) curMode.pixelwidth);
if (y != nullptr)
*y = (*y) * ((double) curMode.height / (double) curMode.pixelheight);
}
double Window::getPixelScale() const
{
double scale = 1.0;
if (window)
{
int wheight;
SDL_GetWindowSize(window, nullptr, &wheight);
int dheight = wheight;
SDL_GL_GetDrawableSize(window, nullptr, &dheight);
scale = (double) dheight / wheight;
}
return scale;
// TODO: Return the density display metric on Android.
return (double) curMode.pixelheight / (double) curMode.height;
}
double Window::toPixels(double x) const
+5 -1
View File
@@ -47,7 +47,7 @@ public:
bool setFullscreen(bool fullscreen, FullscreenType fstype);
bool setFullscreen(bool fullscreen);
bool onWindowResize(int width, int height);
bool onSizeChanged(int width, int height);
int getDisplayCount() const;
@@ -85,6 +85,8 @@ public:
bool isMouseGrabbed() const;
void getPixelDimensions(int &w, int &h) const;
void windowToPixelCoords(double *x, double *y) const;
void pixelToWindowCoords(double *x, double *y) const;
double getPixelScale() const;
@@ -129,6 +131,8 @@ private:
{
int width = 800;
int height = 600;
int pixelwidth = 800;
int pixelheight = 600;
WindowSettings settings;
StrongRef<love::image::ImageData> icon;