diff --git a/JGE/Dependencies/SDL/src/main/android/SDL_android_main.cpp b/JGE/Dependencies/SDL/src/main/android/SDL_android_main.cpp index 4058e6b9c..2b9749bff 100644 --- a/JGE/Dependencies/SDL/src/main/android/SDL_android_main.cpp +++ b/JGE/Dependencies/SDL/src/main/android/SDL_android_main.cpp @@ -24,10 +24,12 @@ extern "C" void Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass c /* Run the application code! */ int status; - char *argv[2]; + char *argv[4]; argv[0] = strdup("SDL_app"); - argv[1] = NULL; - status = SDL_main(1, argv); + argv[1] = (char *)env; + argv[2] = (char *)&cls; + argv[3] = NULL; + status = SDL_main(3, argv); /* We exit here for consistency with other platforms. */ exit(status); diff --git a/JGE/include/JGE.h b/JGE/include/JGE.h index 5fbf25ca3..c7167f1fd 100644 --- a/JGE/include/JGE.h +++ b/JGE/include/JGE.h @@ -55,6 +55,10 @@ typedef u32 LocalKeySym; #endif +#if defined(ANDROID) +#include +#endif + bool JGEGetButtonState(const JButton button); bool JGEGetButtonClick(const JButton button); void JGECreateDefaultBindings(); @@ -121,6 +125,12 @@ class JGE private: #endif +#if defined (ANDROID) + JNIEnv * mJNIEnv; + jclass mJNIClass; + jmethodID midSendCommand; +#endif + bool mDone; float mDeltaTime; bool mDebug; @@ -356,6 +366,16 @@ class JGE void Assert(const char *filename, long lineNumber); + /// Sends a message through JGE + /// Currently used only to communicate with the JNI Layer in Android + void SendCommand(std::string command); + + #if defined (ANDROID) + /// Access to JNI Environment + void SetJNIEnv(JNIEnv * env, jclass cls); + void sendJNICommand(std::string command); +#endif + protected: JGE(); ~JGE(); diff --git a/JGE/src/JGE.cpp b/JGE/src/JGE.cpp index c035d6d8c..361199f1c 100644 --- a/JGE/src/JGE.cpp +++ b/JGE/src/JGE.cpp @@ -315,6 +315,11 @@ JGE::JGE() strcpy(mDebuggingMsg, ""); mCurrentMusic = NULL; #endif + +#if defined (ANDROID) + mJNIEnv = NULL; +#endif + Init(); } @@ -567,5 +572,29 @@ void JGE::Scroll(int inXVelocity, int inYVelocity) } } +void JGE::SendCommand(string command) +{ + #if defined (ANDROID) + sendJNICommand(command); + #endif +} + + #if defined (ANDROID) + /// Access to JNI Environment + void JGE::SetJNIEnv(JNIEnv * env, jclass cls) + { + mJNIEnv = env; + mJNIClass = cls; + midSendCommand = mJNIEnv->GetStaticMethodID(mJNIClass,"jgeSendCommand","(Ljava/lang/String;)V"); + } + + void JGE::sendJNICommand(string command) + { + if (midSendCommand) { + mJNIEnv->CallStaticVoidMethod(mJNIClass, midSendCommand, mJNIEnv->NewStringUTF(command.c_str())); + } + } +#endif + std::queue< pair< pair, bool> > JGE::keyBuffer; std::multimap JGE::keyBinds; diff --git a/JGE/src/SDLmain.cpp b/JGE/src/SDLmain.cpp index 510029afc..e3a452cb6 100644 --- a/JGE/src/SDLmain.cpp +++ b/JGE/src/SDLmain.cpp @@ -50,12 +50,39 @@ uint64_t lastTickCount; JGE* g_engine = NULL; JApp* g_app = NULL; JGameLauncher* g_launcher = NULL; +#ifdef ANDROID +JNIEnv * mJNIEnv = NULL; +jclass * mJNIClass = NULL; +#endif class SdlApp; SdlApp *g_SdlApp = NULL; +#ifdef ANDROID +// Pause +extern "C" void Java_org_libsdl_app_SDLActivity_nativePause( + JNIEnv* env, jclass cls) +{ + DebugTrace("Attempt pause"); + if (!g_engine) + return; + g_engine->Pause(); + DebugTrace("Pause done"); +} + +// Resume +extern "C" void Java_org_libsdl_app_SDLActivity_nativeResume( + JNIEnv* env, jclass cls) +{ + if (!g_engine) + return; + g_engine->Resume(); +} +#endif + + class SdlApp { public: /* For easy interfacing with JGE static functions */ @@ -89,11 +116,14 @@ public: while(Running) { - while(SDL_WaitEventTimeout(&Event, 0)) + if (g_engine && !g_engine->IsPaused()) { - OnEvent(&Event); + while(SDL_WaitEventTimeout(&Event, 0)) + { + OnEvent(&Event); + } + OnUpdate(); } - OnUpdate(); } OnCleanup(); @@ -348,6 +378,16 @@ bool InitGame(void) g_app = g_launcher->GetGameApp(); g_app->Create(); g_engine->SetApp(g_app); +#ifdef ANDROID + DebugTrace("Can I Set JNI Params ?"); + if (mJNIEnv) + DebugTrace("mJNIEnv is ok"); + if (mJNIEnv && mJNIClass) + { + DebugTrace("Setting JNI Params"); + g_engine->SetJNIEnv(mJNIEnv, *mJNIClass); + } +#endif JRenderer::GetInstance()->Enable2D(); lastTickCount = JGEGetTime(); @@ -686,6 +726,15 @@ int SDL_main(int argc, char * argv[]) int main(int argc, char* argv[]) #endif //ANDROID { + +#if (defined ANDROID) + if (argc > 2) + { + mJNIEnv = (JNIEnv * )argv[1]; + mJNIClass = (jclass * )argv[2]; + } +#endif + DebugTrace("I R in da native"); g_launcher = new JGameLauncher(); diff --git a/projects/mtg/Android/AndroidManifest.xml b/projects/mtg/Android/AndroidManifest.xml index 5d131ad26..f8e3a0aef 100644 --- a/projects/mtg/Android/AndroidManifest.xml +++ b/projects/mtg/Android/AndroidManifest.xml @@ -3,16 +3,22 @@ package="org.libsdl.app" android:versionCode="1" android:versionName="1.0"> - + + + + + android:label="@string/app_name" > + + - + diff --git a/projects/mtg/Android/build.xml b/projects/mtg/Android/build.xml index c9c8a2042..73c1a0ac6 100644 --- a/projects/mtg/Android/build.xml +++ b/projects/mtg/Android/build.xml @@ -1,5 +1,5 @@ - +