Android: Added sf::getNativeActivity()
to retrieve a handle to the current activity
This is only implemented for Android builds and requires the user to include the sub-header specifically. Other platforms will trigger a compiler error, if they try to include this header file.
This commit is contained in:
parent
845c684ec8
commit
9f6f02f988
6 changed files with 210 additions and 15 deletions
|
@ -4,7 +4,76 @@
|
|||
#include <SFML/Audio.hpp>
|
||||
#include <SFML/Network.hpp>
|
||||
|
||||
// Do we want to showcase direct JNI/NDK interaction?
|
||||
// Undefine this to get real cross-platform code.
|
||||
#define USE_JNI
|
||||
|
||||
#if defined(USE_JNI)
|
||||
// These headers are only needed for direct NDK/JDK interaction
|
||||
#include <jni.h>
|
||||
#include <android/native_activity.h>
|
||||
|
||||
// Since we want to get the native activity from SFML, we'll have to use an
|
||||
// extra header here:
|
||||
#include <SFML/System/NativeActivity.hpp>
|
||||
|
||||
// NDK/JNI sub example - call Java code from native code
|
||||
int vibrate(sf::Time duration)
|
||||
{
|
||||
// First we'll need the native activity handle
|
||||
ANativeActivity *activity = sf::getNativeActivity();
|
||||
|
||||
// Retrieve the JVM and JNI environment
|
||||
JavaVM* vm = activity->vm;
|
||||
JNIEnv* env = activity->env;
|
||||
|
||||
// First, attach this thread to the main thread
|
||||
JavaVMAttachArgs attachargs;
|
||||
attachargs.version = JNI_VERSION_1_6;
|
||||
attachargs.name = "NativeThread";
|
||||
attachargs.group = NULL;
|
||||
jint res = vm->AttachCurrentThread(&env, &attachargs);
|
||||
|
||||
if (res == JNI_ERR)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Retrieve class information
|
||||
jclass natact = env->FindClass("android/app/NativeActivity");
|
||||
jclass context = env->FindClass("android/content/Context");
|
||||
|
||||
// Get the value of a constant
|
||||
jfieldID fid = env->GetStaticFieldID(context, "VIBRATOR_SERVICE", "Ljava/lang/String;");
|
||||
jobject svcstr = env->GetStaticObjectField(context, fid);
|
||||
|
||||
// Get the method 'getSystemService' and call it
|
||||
jmethodID getss = env->GetMethodID(natact, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
|
||||
jobject vib_obj = env->CallObjectMethod(activity->clazz, getss, svcstr);
|
||||
|
||||
// Get the object's class and retrieve the member name
|
||||
jclass vib_cls = env->GetObjectClass(vib_obj);
|
||||
jmethodID vibrate = env->GetMethodID(vib_cls, "vibrate", "(J)V");
|
||||
|
||||
// Determine the timeframe
|
||||
jlong length = duration.asMilliseconds();
|
||||
|
||||
// Bzzz!
|
||||
env->CallVoidMethod(vib_obj, vibrate, length);
|
||||
|
||||
// Free references
|
||||
env->DeleteLocalRef(vib_obj);
|
||||
env->DeleteLocalRef(vib_cls);
|
||||
env->DeleteLocalRef(svcstr);
|
||||
env->DeleteLocalRef(context);
|
||||
env->DeleteLocalRef(natact);
|
||||
|
||||
// Detach thread again
|
||||
vm->DetachCurrentThread();
|
||||
}
|
||||
#endif
|
||||
|
||||
// This is the actual Android example. You don't have to write any platform
|
||||
// specific code, unless you want to use things not directly exposed.
|
||||
// ('vibrate()' in this example; undefine 'USE_JNI' above to disable it)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "");
|
||||
|
@ -31,23 +100,26 @@ int main(int argc, char *argv[])
|
|||
|
||||
while (window.pollEvent(event))
|
||||
{
|
||||
if (event.type == sf::Event::Closed)
|
||||
switch (event.type)
|
||||
{
|
||||
window.close();
|
||||
case sf::Event::Closed:
|
||||
window.close();
|
||||
break;
|
||||
case sf::Event::Resized:
|
||||
view.setSize(event.size.width, event.size.height);
|
||||
view.setCenter(event.size.width/2, event.size.height/2);
|
||||
window.setView(view);
|
||||
break;
|
||||
case sf::Event::TouchBegan:
|
||||
if (event.touch.finger == 0)
|
||||
{
|
||||
image.setPosition(event.touch.x, event.touch.y);
|
||||
#if defined(USE_JNI)
|
||||
vibrate(sf::milliseconds(10));
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (event.type == sf::Event::Resized)
|
||||
{
|
||||
view.setSize(event.size.width, event.size.height);
|
||||
view.setCenter(event.size.width/2, event.size.height/2);
|
||||
window.setView(view);
|
||||
}
|
||||
}
|
||||
|
||||
if (sf::Touch::isDown(0))
|
||||
{
|
||||
sf::Vector2i position = sf::Touch::getPosition(0);
|
||||
image.setPosition(position.x, position.y);
|
||||
}
|
||||
|
||||
window.clear(sf::Color::White);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue