Android: Upgraded the example to work with Gradle

This commit is contained in:
pjmlp 2017-10-31 11:26:03 +01:00 committed by Lukas Dürrenberger
parent 0adde249ec
commit 3a96024f6b
25 changed files with 490 additions and 1 deletions

View file

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sfml"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:glEsVersion="0x00010001" />
<uses-sdk android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.VIBRATE" />
<application android:label="@string/app_name"
android:icon="@drawable/sfml_logo"
android:hasCode="false"
android:allowBackup="true">
<activity android:name="android.app.NativeActivity"
android:label="@string/app_name"
android:icon="@drawable/sfml_logo"
android:configChanges="keyboardHidden|orientation|screenSize">
<meta-data android:name="android.app.lib_name" android:value="sfml-activity" />
<meta-data android:name="sfml.app.lib_name" android:value="sfml-example" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,20 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sfml-example
LOCAL_SRC_FILES := main.cpp
LOCAL_SHARED_LIBRARIES := sfml-system
LOCAL_SHARED_LIBRARIES += sfml-window
LOCAL_SHARED_LIBRARIES += sfml-graphics
LOCAL_SHARED_LIBRARIES += sfml-audio
LOCAL_SHARED_LIBRARIES += sfml-network
LOCAL_SHARED_LIBRARIES += sfml-activity
LOCAL_SHARED_LIBRARIES += openal
LOCAL_WHOLE_STATIC_LIBRARIES := sfml-main
include $(BUILD_SHARED_LIBRARY)
$(call import-module,sfml)

View file

@ -0,0 +1,5 @@
NDK_TOOLCHAIN_VERSION := 4.9
APP_PLATFORM := android-14
APP_STL := stlport_shared
APP_ABI := armeabi-v7a
APP_MODULES := sfml-activity sfml-example

View file

@ -0,0 +1,129 @@
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#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(), "");
sf::Texture texture;
if(!texture.loadFromFile("image.png"))
return EXIT_FAILURE;
sf::Sprite image(texture);
image.setPosition(0, 0);
image.setOrigin(texture.getSize().x/2, texture.getSize().y/2);
sf::Music music;
if(!music.openFromFile("canary.wav"))
return EXIT_FAILURE;
music.play();
sf::View view = window.getDefaultView();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
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;
}
}
window.clear(sf::Color::White);
window.draw(image);
window.display();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SFML</string>
</resources>