From 888b973bf33283effc77a51f1f84efdb154c95ad Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Tue, 10 Apr 2012 15:44:58 +0200 Subject: [PATCH] Apps can now request portrait mode if desired --- android/app-android.cpp | 15 +++++++++++++-- .../com/turboviking/libnative/NativeActivity.java | 6 +++++- .../src/com/turboviking/libnative/NativeApp.java | 1 + base/NativeApp.h | 7 ++++--- base/PCMain.cpp | 14 ++++++++++---- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/android/app-android.cpp b/android/app-android.cpp index ba33634a5e..b6fb8f0a0d 100644 --- a/android/app-android.cpp +++ b/android/app-android.cpp @@ -77,6 +77,13 @@ InputState input_state; static bool renderer_inited = false; static bool first_lost = true; +extern "C" jboolean Java_com_turboviking_libnative_NativeApp_isLandscape(JNIEnv *env, jclass) { + std::string app_name, app_nice_name; + bool landscape; + NativeGetAppInfo(&app_name, &app_nice_name, &landscape); + return landscape; +} + extern "C" void Java_com_turboviking_libnative_NativeApp_init (JNIEnv *env, jclass, jint xxres, jint yyres, jstring apkpath, jstring dataDir, jstring externalDir, jstring jinstallID) { @@ -105,8 +112,12 @@ extern "C" void Java_com_turboviking_libnative_NativeApp_init str = env->GetStringUTFChars(jinstallID, &isCopy); std::string installID = std::string(str); - const char *app_name = "rollerball"; - NativeInit(1, &app_name, user_data_path.c_str(), installID.c_str()); + std::string app_name, app_nice_name; + bool landscape; + + NativeGetAppInfo(&app_name, &app_nice_name, &landscape); + const char *argv[2] = {app_name.c_str(), 0}; + NativeInit(1, argv, user_data_path.c_str(), installID.c_str()); } extern "C" void Java_com_turboviking_libnative_NativeApp_shutdown diff --git a/android/src/com/turboviking/libnative/NativeActivity.java b/android/src/com/turboviking/libnative/NativeActivity.java index 701a73e2d2..767bb6e275 100644 --- a/android/src/com/turboviking/libnative/NativeActivity.java +++ b/android/src/com/turboviking/libnative/NativeActivity.java @@ -288,7 +288,11 @@ public class NativeActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + if (NativeApp.isLandscape()) { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + } else { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } super.onCreate(savedInstanceState); Log.i(TAG, "onCreate"); installID = Installation.id(this); diff --git a/android/src/com/turboviking/libnative/NativeApp.java b/android/src/com/turboviking/libnative/NativeApp.java index 5366a22c14..51b9654a59 100644 --- a/android/src/com/turboviking/libnative/NativeApp.java +++ b/android/src/com/turboviking/libnative/NativeApp.java @@ -1,6 +1,7 @@ package com.turboviking.libnative; public class NativeApp { + public static native boolean isLandscape(); public static native void init( int xxres, int yyres, String apkPath, String dataDir, String externalDir, String installID); diff --git a/base/NativeApp.h b/base/NativeApp.h index 72ffc952c0..d19eee64bb 100644 --- a/base/NativeApp.h +++ b/base/NativeApp.h @@ -5,14 +5,15 @@ // The Native App API. // // Implement these functions and you've got a native app. These are called -// from app-android, which exposes the native JNI api which is a bit +// from the framework, which exposes the native JNI api which is a bit // more complicated. // This is defined in input/input_state.h. struct InputState; // You must implement this. The first function to get called, just write strings to the two pointers. -void NativeGetAppInfo(std::string *app_dir_name, std::string *app_nice_name); +// This might get called multiple times in some implementations, you must be able to handle that. +void NativeGetAppInfo(std::string *app_dir_name, std::string *app_nice_name, bool *landscape); // The very first function to be called after NativeGetAppInfo. Even NativeMix is not called // before this, although it may be called at any point in time afterwards (on any thread!) @@ -38,7 +39,7 @@ void NativeRender(); // Try not to make too many assumptions on the granularity // of num_samples. // This function may be called from a totally separate thread from -// the rest of the game. +// the rest of the game, so be careful with synchronization. void NativeMix(short *audio, int num_samples); // Called when it's time to shutdown. After this has been called, diff --git a/base/PCMain.cpp b/base/PCMain.cpp index 0e4f1a7147..6353922a50 100644 --- a/base/PCMain.cpp +++ b/base/PCMain.cpp @@ -112,12 +112,18 @@ int main(int argc, char *argv[]) { g_xres = 1280; g_yres = 800; */ - g_xres = 800; - g_yres = 480; - std::string app_name; std::string app_name_nice; - NativeGetAppInfo(&app_name, &app_name_nice); + bool landscape; + NativeGetAppInfo(&app_name, &app_name_nice, &landscape); + + if (landscape) { + g_xres = 800; + g_yres = 480; + } else { + g_xres = 480; + g_yres = 800; + } if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());