Skip to content

Commit

Permalink
[Android] Add Swappy support to Samples
Browse files Browse the repository at this point in the history
  • Loading branch information
darksylinc committed Apr 12, 2024
1 parent a188ce0 commit 5e79187
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Samples/2.0/AndroidAppTemplate/Template/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ endif()
file( READ "${OGRE_BINARIES}/include/OgreBuildSettings.h" OGRE_BUILD_SETTINGS_STR )
string( FIND "${OGRE_BUILD_SETTINGS_STR}" "#define OGRE_BUILD_COMPONENT_ATMOSPHERE" OGRE_BUILD_COMPONENT_ATMOSPHERE )

# Read OgreVulkanBuildSettings.h to link against optional libraries (only if set)
file( READ "${OGRE_BINARIES}/include/OgreVulkanBuildSettings.h" OGRE_VK_BUILD_SETTINGS_STR )
string( FIND "${OGRE_VK_BUILD_SETTINGS_STR}" "#define OGRE_VULKAN_USE_SWAPPY" OGRE_VULKAN_USE_SWAPPY )

include_directories( ${OGRE_SOURCE}/OgreMain/include )
include_directories( ${OGRE_SOURCE}/RenderSystems/Vulkan/include )
include_directories( ${OGRE_SOURCE}/Components/Overlay/include )
Expand Down Expand Up @@ -83,4 +87,8 @@ set( OGRE_LIBS
${OGRE_SOURCE}/DependenciesAndroid/lib/libzlib.a
${OGRE_SOURCE}/DependenciesAndroid/lib/libzziplib.a )

if( OGRE_VULKAN_USE_SWAPPY )
list( APPEND OGRE_LIBS ${OGRE_SOURCE}/DependenciesAndroid/lib/libswappy_static.a )
endif()

target_link_libraries( %%sampleName%% app-glue log android vulkan ${OGRE_LIBS} cpufeatures )
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,26 @@ namespace Demo
mAccumulator += timeSinceLast;
}
};

class DemoJniProvider final : public Ogre::AndroidJniProvider
{
void acquire( JNIEnv **env, jobject *activity ) override
{
android_app *app = AndroidSystems::getAndroidApp();
app->activity->vm->AttachCurrentThread( env, nullptr );
*activity = app->activity->clazz;
}

void release( JNIEnv * ) override
{
android_app *app = AndroidSystems::getAndroidApp();
app->activity->vm->DetachCurrentThread();
}
};
} // namespace Demo

static Demo::AndroidAppController g_appController;
static Demo::DemoJniProvider g_demoJniProvider;

// Process the next main command.
void handle_cmd( android_app *app, int32_t cmd )
Expand Down Expand Up @@ -179,6 +196,7 @@ void android_main( struct android_app *app )
app->onAppCmd = handle_cmd;

Demo::AndroidSystems::setAndroidApp( app );
Demo::AndroidSystems::setJniProvider( &g_demoJniProvider );

// Used to poll the events in the main loop
int events;
Expand Down
15 changes: 13 additions & 2 deletions Samples/2.0/Common/include/System/Android/AndroidSystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,35 @@ THE SOFTWARE.
struct android_app;
struct ANativeWindow;

namespace Ogre
{
class AndroidJniProvider;
}

namespace Demo
{
/// Utility class to load plugins statically
class AndroidSystems
{
#if OGRE_PLATFORM == OGRE_PLATFORM_ANDROID
android_app * mAndroidApp;
android_app *mAndroidApp;
ANativeWindow *mNativeWindow;

Ogre::AndroidJniProvider *mJniProvider;
#endif

public:
AndroidSystems();

static void setAndroidApp( android_app *androidApp );
static void setAndroidApp( android_app *androidApp );
static android_app *getAndroidApp();

static void setNativeWindow( ANativeWindow *nativeWindow );
static ANativeWindow *getNativeWindow();

static void setJniProvider( Ogre::AndroidJniProvider *provider );
static Ogre::AndroidJniProvider *getJniProvider();

/**
On Android platforms:
Opens a file in an APK and returns a DataStream smart pointer that can be read from
Expand Down
3 changes: 3 additions & 0 deletions Samples/2.0/Common/src/GraphicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ namespace Demo
params.insert( std::make_pair(
"ANativeWindow",
Ogre::StringConverter::toString( (uintptr_t)AndroidSystems::getNativeWindow() ) ) );
params.insert( std::make_pair(
"AndroidJniProvider",
Ogre::StringConverter::toString( (uintptr_t)AndroidSystems::getJniProvider() ) ) );
#endif

params.insert( std::make_pair( "title", windowTitle ) );
Expand Down
14 changes: 13 additions & 1 deletion Samples/2.0/Common/src/System/Android/AndroidSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,29 @@ namespace Demo
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_ANDROID
AndroidSystems::AndroidSystems() : mAndroidApp( 0 ), mNativeWindow( 0 ) {}
AndroidSystems::AndroidSystems() : mAndroidApp( 0 ), mNativeWindow( 0 ), mJniProvider( 0 ) {}
//-------------------------------------------------------------------------
void AndroidSystems::setAndroidApp( android_app *androidApp )
{
g_andrSystem.mAndroidApp = androidApp;
}
//-------------------------------------------------------------------------
android_app *AndroidSystems::getAndroidApp() { return g_andrSystem.mAndroidApp; }
//-------------------------------------------------------------------------
void AndroidSystems::setNativeWindow( ANativeWindow *nativeWindow )
{
g_andrSystem.mNativeWindow = nativeWindow;
}
//-------------------------------------------------------------------------
ANativeWindow *AndroidSystems::getNativeWindow() { return g_andrSystem.mNativeWindow; }
//-------------------------------------------------------------------------
void AndroidSystems::setJniProvider( Ogre::AndroidJniProvider *provider )
{
g_andrSystem.mJniProvider = provider;
}
//-------------------------------------------------------------------------
Ogre::AndroidJniProvider *AndroidSystems::getJniProvider() { return g_andrSystem.mJniProvider; }
//-------------------------------------------------------------------------
void AndroidSystems::registerArchiveFactories()
{
AAssetManager *assetMgr = g_andrSystem.mAndroidApp->activity->assetManager;
Expand Down Expand Up @@ -105,8 +114,11 @@ namespace Demo
//-------------------------------------------------------------------------
AndroidSystems::AndroidSystems() {}
void AndroidSystems::setAndroidApp( android_app * ) {}
android_app *AndroidSystems::getAndroidApp() { return 0; }
void AndroidSystems::setNativeWindow( ANativeWindow * ) {}
ANativeWindow *AndroidSystems::getNativeWindow() { return 0; }
void AndroidSystems::setJniProvider( Ogre::AndroidJniProvider *provider ) {}
Ogre::AndroidJniProvider *AndroidSystems::getJniProvider() { return 0; }
std::string AndroidSystems::getFilesDir( const bool /*bInternal*/ ) { return ""; }
bool AndroidSystems::isAndroid() { return false; }
void AndroidSystems::registerArchiveFactories() {}
Expand Down

0 comments on commit 5e79187

Please sign in to comment.