double-hit-balls/game/android_api.cpp

56 lines
1.8 KiB
C++
Raw Normal View History

2017-01-10 12:43:06 +00:00
#include "android_api.h"
#include "main_code.h"
JavaVM* localJavaVirtualMachine = 0;
JNIEXPORT void JNICALL Java_fishrungames_doublehitballs_JniWrapper_SetJavaVM(JNIEnv * env, jobject obj)
{
env->GetJavaVM(&localJavaVirtualMachine);
}
2017-01-10 12:43:06 +00:00
JNIEXPORT void JNICALL Java_fishrungames_doublehitballs_JniWrapper_Init(JNIEnv * env, jobject obj, jint width, jint height)
{
2018-07-14 11:53:59 +00:00
JniInitApp<TMyApplication>(width, height, width, height);
}
std::stringstream LoadFileFromAndroid(const std::string& fileName)
{
GetConsole() << "LoadFileFromAndroid called \n";
JNIEnv* jEnv;
localJavaVirtualMachine->GetEnv((void**)&jEnv, JNI_VERSION_1_4);
jclass jniWrapper = jEnv->FindClass("fishrungames/doublehitballs/JniWrapper");
jstring jFileName = jEnv->NewStringUTF(fileName.c_str());
jmethodID jLoadFile = jEnv->GetStaticMethodID(jniWrapper, "LoadFile", "(Ljava/lang/String;)Ljava/lang/String;");
jstring jFileContent = (jstring)jEnv->CallStaticObjectMethod(jniWrapper, jLoadFile, jFileName);
jboolean jCopy = false;
std::stringstream ss;
ss << jEnv->GetStringUTFChars(jFileContent, &jCopy);
GetConsole() << ss.str();
return ss;
}
void SaveFileToAndroid(const std::string& fileName, const std::string& fileContent)
{
GetConsole() << "SaveFileToAndroid called \n";
JNIEnv* jEnv;
localJavaVirtualMachine->GetEnv((void**)&jEnv, JNI_VERSION_1_4);
jclass jniWrapper = jEnv->FindClass("fishrungames/doublehitballs/JniWrapper");
jstring jFileName = jEnv->NewStringUTF(fileName.c_str());
jstring jFileContent = jEnv->NewStringUTF(fileContent.c_str());
2017-01-10 12:43:06 +00:00
jmethodID jSaveFile = jEnv->GetStaticMethodID(jniWrapper, "SaveFile", "(Ljava/lang/String;Ljava/lang/String;)V");
2017-01-10 12:43:06 +00:00
jEnv->CallStaticVoidMethod(jniWrapper, jSaveFile, jFileName, jFileContent);
}