Expose Android Display Metrics to Löve

This commit is contained in:
Jairo Luiz
2014-01-23 18:44:06 -03:00
committed by Jairo Pereira
parent c8c7bc5853
commit 2b99a488d3
15 changed files with 588 additions and 26 deletions
+43
View File
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Android.h"
namespace love
{
namespace android
{
DisplayMetrics* Android::getDisplayMetrics()
{
if (metrics == NULL)
{
metrics = new DisplayMetrics();
}
return metrics;
}
const char *Android::getName() const
{
return "love.android";
}
} // android
} // love
+63
View File
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_ANDROID_ANDROID_H
#define LOVE_ANDROID_ANDROID_H
// LOVE
#include "common/Module.h"
#include "DisplayMetrics.h"
namespace love
{
namespace android
{
/**
* The Android module provide access to some device features, like display metrics and camera.
**/
class Android : public Module
{
public:
/**
* Destructor.
**/
virtual ~Android() {}
/**
* Gets display metrics that describe the size and density of this display.
* @return A DisplayMetrics object containing the metrics.
**/
DisplayMetrics* getDisplayMetrics();
// Implements Module.
virtual const char *getName() const;
private:
DisplayMetrics *metrics;
}; // Android
} // android
} // love
#endif // LOVE_ANDROID_ANDROID_H
@@ -0,0 +1,88 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "DisplayMetrics.h"
#include "core/android/SDL_android.h"
namespace love
{
namespace android
{
DisplayMetrics::DisplayMetrics()
{
JNIEnv *env = Android_JNI_GetEnv();
jclass activity = env->FindClass("org/love2d/android/GameActivity");
jmethodID getMetrics = env->GetStaticMethodID(activity, "getMetrics", "()Landroid/util/DisplayMetrics;");
jobject metrics = env->CallStaticObjectMethod(activity, getMetrics);
jclass metricsClass = env->GetObjectClass(metrics);
// Density
density = env->GetFloatField(metrics, env->GetFieldID(metricsClass, "density", "F"));
scaledDensity = env->GetFloatField(metrics, env->GetFieldID(metricsClass, "scaledDensity", "F"));
densityDpi = env->GetIntField(metrics, env->GetFieldID(metricsClass, "densityDpi", "I"));
// Size
widthPixels = env->GetIntField(metrics, env->GetFieldID(metricsClass, "widthPixels", "I"));
heightPixels = env->GetIntField(metrics, env->GetFieldID(metricsClass, "heightPixels", "I"));
// DPI
xdpi = env->GetFloatField(metrics, env->GetFieldID(metricsClass, "xdpi", "F"));
ydpi = env->GetFloatField(metrics, env->GetFieldID(metricsClass, "ydpi", "F"));
}
float DisplayMetrics::getDensity()
{
return density;
}
float DisplayMetrics::getScaledDensity()
{
return scaledDensity;
}
int DisplayMetrics::getDensityDpi()
{
return densityDpi;
}
int DisplayMetrics::getWidthPixels()
{
return widthPixels;
}
int DisplayMetrics::getHeightPixels()
{
return heightPixels;
}
float DisplayMetrics::getXdpi()
{
return xdpi;
}
float DisplayMetrics::getYdpi()
{
return ydpi;
}
} // android
} // love
@@ -0,0 +1,98 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_ANDROID_DISPLAY_METRICS_H
#define LOVE_ANDROID_DISPLAY_METRICS_H
// LOVE
#include "common/Object.h"
namespace love
{
namespace android
{
/**
* A DisplayMetrics describe a general information about a display, such as its size, density, and font scaling.
**/
class DisplayMetrics : public Object
{
public:
/**
* Constructor.
**/
DisplayMetrics();
/**
* Destructor.
**/
virtual ~DisplayMetrics() {}
/**
* The logical density of the display.
**/
float getDensity();
/**
* A scaling factor for fonts displayed on the display.
**/
float getScaledDensity();
/**
* The screen density expressed as dots-per-inch.
**/
int getDensityDpi();
/**
* The absolute width of the display in pixels.
**/
int getWidthPixels();
/**
* The absolute height of the display in pixels.
**/
int getHeightPixels();
/**
* The exact physical pixels per inch of the screen in the X dimension.
**/
float getXdpi();
/**
* The exact physical pixels per inch of the screen in the X dimension.
**/
float getYdpi();
private:
float density;
float scaledDensity;
int densityDpi;
int widthPixels;
int heightPixels;
float xdpi;
float ydpi;
}; // DisplayMetrics
} // android
} // love
#endif // LOVE_ANDROID_DISPLAY_METRICS_H
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_Android.h"
#include "wrap_DisplayMetrics.h"
namespace love
{
namespace android
{
static Android *instance = nullptr;
int w_getDisplayMetrics(lua_State *L)
{
DisplayMetrics *metrics = 0;
EXCEPT_GUARD(metrics = instance->getDisplayMetrics();)
luax_pushtype(L, "DisplayMetrics", ANDROID_DISPLAY_METRICS_T, metrics);
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] =
{
{ "getDisplayMetrics", w_getDisplayMetrics },
{ 0, 0 }
};
// Types for this module.
static const lua_CFunction types[] =
{
luaopen_displaymetrics,
0
};
extern "C" int luaopen_love_android(lua_State *L)
{
if (instance == 0)
{
EXCEPT_GUARD(instance = new Android();)
}
else
instance->retain();
WrappedModule w;
w.module = instance;
w.name = "android";
w.flags = MODULE_T;
w.functions = functions;
w.types = types;
int n = luax_register_module(L, w);
return n;
}
} // android
} // love
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_ANDROID_WRAP_ANDROID_H
#define LOVE_ANDROID_WRAP_ANDROID_H
// LOVE
#include "common/config.h"
#include "common/runtime.h"
#include "Android.h"
namespace love
{
namespace android
{
int w_getDisplayMetrics(lua_State *L);
extern "C" LOVE_EXPORT int luaopen_love_android(lua_State *L);
} // android
} // love
#endif // LOVE_ANDROID_WRAP_ANDROID_H
@@ -0,0 +1,100 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_DisplayMetrics.h"
namespace love
{
namespace android
{
DisplayMetrics *luax_checkdisplaymetrics(lua_State *L, int idx)
{
return luax_checktype<DisplayMetrics>(L, idx, "DisplayMetrics", ANDROID_DISPLAY_METRICS_T);
}
int w_getDensity(lua_State *L)
{
DisplayMetrics *metrics = luax_checkdisplaymetrics(L, 1);
lua_pushnumber(L, metrics->getDensity());
return 1;
}
int w_getScaledDensity(lua_State *L)
{
DisplayMetrics *metrics = luax_checkdisplaymetrics(L, 1);
lua_pushnumber(L, metrics->getScaledDensity());
return 1;
}
int w_getDensityDpi(lua_State *L)
{
DisplayMetrics *metrics = luax_checkdisplaymetrics(L, 1);
lua_pushnumber(L, metrics->getDensityDpi());
return 1;
}
int w_getWidthPixels(lua_State *L)
{
DisplayMetrics *metrics = luax_checkdisplaymetrics(L, 1);
lua_pushnumber(L, metrics->getWidthPixels());
return 1;
}
int w_getHeightPixels(lua_State *L)
{
DisplayMetrics *metrics = luax_checkdisplaymetrics(L, 1);
lua_pushnumber(L, metrics->getHeightPixels());
return 1;
}
int w_getXdpi(lua_State *L)
{
DisplayMetrics *metrics = luax_checkdisplaymetrics(L, 1);
lua_pushnumber(L, metrics->getXdpi());
return 1;
}
int w_getYdpi(lua_State *L)
{
DisplayMetrics *metrics = luax_checkdisplaymetrics(L, 1);
lua_pushnumber(L, metrics->getYdpi());
return 1;
}
static const luaL_Reg functions[] =
{
{ "getDensity", w_getDensity },
{ "getScaledDensity", w_getScaledDensity },
{ "getDensityDpi", w_getDensityDpi },
{ "getWidthPixels", w_getWidthPixels },
{ "getHeightPixels", w_getHeightPixels },
{ "getXdpi", w_getXdpi },
{ "getYdpi", w_getYdpi },
{ 0, 0 },
};
extern "C" int luaopen_displaymetrics(lua_State *L)
{
return luax_register_type(L, "DisplayMetrics", functions);
}
} // android
} // love
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_ANDROID_WRAP_DISPLAY_METRICS_H
#define LOVE_ANDROID_WRAP_DISPLAY_METRICS_H
#include "common/runtime.h"
#include "DisplayMetrics.h"
namespace love
{
namespace android
{
DisplayMetrics *luax_checkdisplaymetrics(lua_State *L, int idx);
int w_getDensity(lua_State *L);
int w_getScaledDensity(lua_State *L);
int w_getDensityDpi(lua_State *L);
int w_getWidthPixels(lua_State *L);
int w_getHeightPixels(lua_State *L);
int w_getXdpi(lua_State *L);
int w_getYdpi(lua_State *L);
extern "C" int luaopen_displaymetrics(lua_State *L);
} // android
} // love
#endif // LOVE_ANDROID_WRAP_DISPLAY_METRICS_H
+6
View File
@@ -52,6 +52,9 @@
// of addressing implementations directly.
extern "C"
{
#if defined(LOVE_ENABLE_ANDROID)
extern int luaopen_love_android(lua_State*);
#endif
#if defined(LOVE_ENABLE_AUDIO)
extern int luaopen_love_audio(lua_State*);
#endif
@@ -107,6 +110,9 @@ extern "C"
}
static const luaL_Reg modules[] = {
#if defined(LOVE_ENABLE_ANDROID)
{ "love.android", luaopen_love_android },
#endif
#if defined(LOVE_ENABLE_AUDIO)
{ "love.audio", luaopen_love_audio },
#endif