Merged tangerinagames/love-android-sdl2/pixel_scale into master

This commit is contained in:
Martin Felis
2014-02-02 21:28:09 +01:00
16 changed files with 39 additions and 514 deletions
@@ -18,28 +18,31 @@
* 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 "android.h"
#include "common/runtime.h"
#include "DisplayMetrics.h"
#ifdef LOVE_ANDROID
// SDL
#include "core/android/SDL_android.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);
double getScreenScale()
{
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);
return env->GetFloatField(metrics, env->GetFieldID(metricsClass, "density", "F"));
}
} // android
} // love
#endif // LOVE_ANDROID_WRAP_DISPLAY_METRICS_H
#endif // LOVE_ANDROID
@@ -18,26 +18,26 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Android.h"
#ifndef LOVE_ANDROID_H
#define LOVE_ANDROID_H
#include "config.h"
#ifdef LOVE_ANDROID
namespace love
{
namespace android
{
DisplayMetrics* Android::getDisplayMetrics()
{
if (metrics == NULL)
{
metrics = new DisplayMetrics();
}
return metrics;
}
const char *Android::getName() const
{
return "love.android";
}
/**
* Gets the scale factor of the window's screen, e.g. on Retina displays this
* will return 2.0.
**/
double getScreenScale();
} // android
} // love
#endif // LOVE_ANDROID
#endif // LOVE_ANDROID_H
-1
View File
@@ -90,7 +90,6 @@
# define LOVE_LITTLE_ENDIAN 1
# endif
#else
# define LOVE_ENABLE_ANDROID
# define LOVE_ENABLE_AUDIO
# define LOVE_ENABLE_AUDIO_NULL
# define LOVE_ENABLE_AUDIO_OPENAL
-6
View File
@@ -35,9 +35,6 @@ enum Type
DATA_ID,
MODULE_ID,
// Android
ANDROID_DISPLAY_METRICS_ID,
// Filesystem.
FILESYSTEM_FILE_ID,
FILESYSTEM_FILE_DATA_ID,
@@ -124,9 +121,6 @@ const bits OBJECT_T = bits(1) << OBJECT_ID;
const bits DATA_T = (bits(1) << DATA_ID) | OBJECT_T;
const bits MODULE_T = (bits(1) << MODULE_ID) | OBJECT_T;
// Android
const bits ANDROID_DISPLAY_METRICS_T = (bits(1) << ANDROID_DISPLAY_METRICS_ID) | OBJECT_T;
// Filesystem.
const bits FILESYSTEM_FILE_T = (bits(1) << FILESYSTEM_FILE_ID) | OBJECT_T;
const bits FILESYSTEM_FILE_DATA_T = (bits(1) << FILESYSTEM_FILE_DATA_ID) | DATA_T;
-63
View File
@@ -1,63 +0,0 @@
/**
* 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
@@ -1,88 +0,0 @@
/**
* 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
@@ -1,98 +0,0 @@
/**
* 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
@@ -1,75 +0,0 @@
/**
* 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
@@ -1,40 +0,0 @@
/**
* 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
@@ -1,100 +0,0 @@
/**
* 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
-6
View File
@@ -52,9 +52,6 @@
// 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
@@ -110,9 +107,6 @@ 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
+7 -1
View File
@@ -23,6 +23,10 @@
#include "graphics/Graphics.h"
#include "Window.h"
#ifdef LOVE_ANDROID
#include "common/android.h"
#endif
// C++
#include <iostream>
#include <vector>
@@ -671,7 +675,9 @@ double Window::getPixelScale() const
{
double scale = 1.0;
#if SDL_VERSION_ATLEAST(2,0,1)
#ifdef LOVE_ANDROID
scale = love::android::getScreenScale();
#elif SDL_VERSION_ATLEAST(2,0,1)
if (window)
{
int wheight;
-2
View File
@@ -315,7 +315,6 @@ function love.init()
highdpi = false,
},
modules = {
android = true,
event = true,
keyboard = true,
mouse = true,
@@ -364,7 +363,6 @@ function love.init()
-- Gets desired modules.
for k,v in ipairs{
"android",
"thread",
"timer",
"event",
-2
View File
@@ -572,7 +572,6 @@ const unsigned char boot_lua[] =
0x2c, 0x0a,
0x09, 0x09, 0x7d, 0x2c, 0x0a,
0x09, 0x09, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x20, 0x3d, 0x20, 0x7b, 0x0a,
0x09, 0x09, 0x09, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a,
0x09, 0x09, 0x09, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a,
0x09, 0x09, 0x09, 0x6b, 0x65, 0x79, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65,
0x2c, 0x0a,
@@ -645,7 +644,6 @@ const unsigned char boot_lua[] =
0x6f, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x2e, 0x0a,
0x09, 0x66, 0x6f, 0x72, 0x20, 0x6b, 0x2c, 0x76, 0x20, 0x69, 0x6e, 0x20, 0x69, 0x70, 0x61, 0x69, 0x72, 0x73,
0x7b, 0x0a,
0x09, 0x09, 0x22, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x22, 0x2c, 0x0a,
0x09, 0x09, 0x22, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x22, 0x2c, 0x0a,
0x09, 0x09, 0x22, 0x74, 0x69, 0x6d, 0x65, 0x72, 0x22, 0x2c, 0x0a,
0x09, 0x09, 0x22, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x22, 0x2c, 0x0a,