summaryrefslogtreecommitdiffstats
path: root/NativeApp/src/Android
diff options
context:
space:
mode:
authorEgor Yusov <egor.yusov@gmail.com>2019-11-25 03:00:55 +0000
committerEgor Yusov <egor.yusov@gmail.com>2019-11-25 03:00:55 +0000
commit6699190149464be566b55e7726c04fb10010f788 (patch)
treed63af48bbd827310f4dc991d5df389a3c9bf82d2 /NativeApp/src/Android
parentclang-formatted ImGuiImpl (diff)
downloadDiligentTools-6699190149464be566b55e7726c04fb10010f788.tar.gz
DiligentTools-6699190149464be566b55e7726c04fb10010f788.zip
clang-formatted NativeApp
Diffstat (limited to 'NativeApp/src/Android')
-rw-r--r--NativeApp/src/Android/AndroidAppBase.cpp177
-rw-r--r--NativeApp/src/Android/AndroidMain.cpp34
2 files changed, 107 insertions, 104 deletions
diff --git a/NativeApp/src/Android/AndroidAppBase.cpp b/NativeApp/src/Android/AndroidAppBase.cpp
index f3e341c..05386fd 100644
--- a/NativeApp/src/Android/AndroidAppBase.cpp
+++ b/NativeApp/src/Android/AndroidAppBase.cpp
@@ -35,7 +35,7 @@ namespace Diligent
int AndroidAppBase::InitDisplay()
{
- if( !initialized_resources_ )
+ if (!initialized_resources_)
{
Initialize();
@@ -45,7 +45,7 @@ int AndroidAppBase::InitDisplay()
else
{
// initialize OpenGL ES and EGL
- if( EGL_SUCCESS != Resume( app_->window ) )
+ if (EGL_SUCCESS != Resume(app_->window))
{
UnloadResources();
LoadResources();
@@ -57,14 +57,14 @@ int AndroidAppBase::InitDisplay()
//tap_camera_.SetFlip( 1.f, -1.f, -1.f );
//tap_camera_.SetPinchTransformFactor( 2.f, 2.f, 8.f );
- return 0;
+ return 0;
}
void AndroidAppBase::InitSensors()
{
- sensor_manager_ = ASensorManager_getInstance();
- accelerometer_sensor_ = ASensorManager_getDefaultSensor( sensor_manager_, ASENSOR_TYPE_ACCELEROMETER );
- sensor_event_queue_ = ASensorManager_createEventQueue( sensor_manager_, app_->looper, LOOPER_ID_USER, NULL, NULL );
+ sensor_manager_ = ASensorManager_getInstance();
+ accelerometer_sensor_ = ASensorManager_getDefaultSensor(sensor_manager_, ASENSOR_TYPE_ACCELEROMETER);
+ sensor_event_queue_ = ASensorManager_createEventQueue(sensor_manager_, app_->looper, LOOPER_ID_USER, NULL, NULL);
}
//
@@ -75,19 +75,21 @@ void AndroidAppBase::DrawFrame()
// APP_CMD_CONFIG_CHANGED event is generated seveal frames
// before the screen is actually resized. The only robust way
// to detect window resize is to check it very frame
- if(CheckWindowSizeChanged())
- WindowResize(0,0);
+ if (CheckWindowSizeChanged())
+ WindowResize(0, 0);
float fFPS;
- if( monitor_.Update( fFPS ) )
+ if (monitor_.Update(fFPS))
{
- UpdateFPS( fFPS );
+ UpdateFPS(fFPS);
}
static Diligent::Timer Timer;
- static double PrevTime = Timer.GetElapsedTime();
- auto CurrTime = Timer.GetElapsedTime();
- auto ElapsedTime = CurrTime - PrevTime;
+
+ static double PrevTime = Timer.GetElapsedTime();
+ auto CurrTime = Timer.GetElapsedTime();
+ auto ElapsedTime = CurrTime - PrevTime;
+
PrevTime = CurrTime;
Update(CurrTime, ElapsedTime);
@@ -106,80 +108,80 @@ void AndroidAppBase::DrawFrame()
//
// Process the next input event.
//
-int32_t AndroidAppBase::HandleInput( android_app* app, AInputEvent* event )
+int32_t AndroidAppBase::HandleInput(android_app* app, AInputEvent* event)
{
AndroidAppBase* eng = (AndroidAppBase*)app->userData;
- return eng->HandleInput( event );
+ return eng->HandleInput(event);
}
//
// Process the next main command.
//
-void AndroidAppBase::HandleCmd( struct android_app* app, int32_t cmd )
+void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd)
{
AndroidAppBase* eng = (AndroidAppBase*)app->userData;
- switch( cmd )
+ switch (cmd)
{
- case APP_CMD_SAVE_STATE:
- break;
+ case APP_CMD_SAVE_STATE:
+ break;
- case APP_CMD_INIT_WINDOW:
- // The window is being shown, get it ready.
- if( app->window != NULL )
- {
- eng->InitDisplay();
+ case APP_CMD_INIT_WINDOW:
+ // The window is being shown, get it ready.
+ if (app->window != NULL)
+ {
+ eng->InitDisplay();
+ eng->DrawFrame();
+ }
+ break;
+
+ case APP_CMD_CONFIG_CHANGED:
+ case APP_CMD_WINDOW_RESIZED:
+ // This does not work as the screen resizes few frames
+ // after the event has been received
+ // eng->WindowResize(0,0);
+ break;
+
+ case APP_CMD_TERM_WINDOW:
+ // The window is being hidden or closed, clean it up.
+ eng->TermDisplay();
+ eng->has_focus_ = false;
+ break;
+
+ case APP_CMD_STOP:
+ break;
+
+ case APP_CMD_GAINED_FOCUS:
+ eng->ResumeSensors();
+ //Start animation
+ eng->has_focus_ = true;
+ break;
+
+ case APP_CMD_LOST_FOCUS:
+ eng->SuspendSensors();
+ // Also stop animating.
+ eng->has_focus_ = false;
eng->DrawFrame();
- }
- break;
-
- case APP_CMD_CONFIG_CHANGED:
- case APP_CMD_WINDOW_RESIZED:
- // This does not work as the screen resizes few frames
- // after the event has been received
- // eng->WindowResize(0,0);
- break;
-
- case APP_CMD_TERM_WINDOW:
- // The window is being hidden or closed, clean it up.
- eng->TermDisplay();
- eng->has_focus_ = false;
- break;
-
- case APP_CMD_STOP:
- break;
-
- case APP_CMD_GAINED_FOCUS:
- eng->ResumeSensors();
- //Start animation
- eng->has_focus_ = true;
- break;
-
- case APP_CMD_LOST_FOCUS:
- eng->SuspendSensors();
- // Also stop animating.
- eng->has_focus_ = false;
- eng->DrawFrame();
- break;
-
- case APP_CMD_LOW_MEMORY:
- //Free up GL resources
- eng->TrimMemory();
- break;
+ break;
+
+ case APP_CMD_LOW_MEMORY:
+ //Free up GL resources
+ eng->TrimMemory();
+ break;
}
}
//-------------------------------------------------------------------------
//Sensor handlers
//-------------------------------------------------------------------------
-void AndroidAppBase::ProcessSensors( int32_t id )
+void AndroidAppBase::ProcessSensors(int32_t id)
{
// If a sensor has data, process it now.
- if( id == LOOPER_ID_USER )
+ if (id == LOOPER_ID_USER)
{
- if( accelerometer_sensor_ != NULL )
+ if (accelerometer_sensor_ != NULL)
{
ASensorEvent event;
- while( ASensorEventQueue_getEvents( sensor_event_queue_, &event, 1 ) > 0 )
+ while (ASensorEventQueue_getEvents(sensor_event_queue_, &event, 1) > 0)
{
}
}
@@ -189,12 +191,12 @@ void AndroidAppBase::ProcessSensors( int32_t id )
void AndroidAppBase::ResumeSensors()
{
// When our app gains focus, we start monitoring the accelerometer.
- if( accelerometer_sensor_ != NULL )
+ if (accelerometer_sensor_ != NULL)
{
- ASensorEventQueue_enableSensor( sensor_event_queue_, accelerometer_sensor_ );
+ ASensorEventQueue_enableSensor(sensor_event_queue_, accelerometer_sensor_);
// We'd like to get 60 events per second (in us).
- ASensorEventQueue_setEventRate( sensor_event_queue_, accelerometer_sensor_,
- (1000L / 60) * 1000 );
+ ASensorEventQueue_setEventRate(sensor_event_queue_, accelerometer_sensor_,
+ (1000L / 60) * 1000);
}
}
@@ -202,27 +204,28 @@ void AndroidAppBase::SuspendSensors()
{
// When our app loses focus, we stop monitoring the accelerometer.
// This is to avoid consuming battery while not being used.
- if( accelerometer_sensor_ != NULL )
+ if (accelerometer_sensor_ != NULL)
{
- ASensorEventQueue_disableSensor( sensor_event_queue_, accelerometer_sensor_ );
+ ASensorEventQueue_disableSensor(sensor_event_queue_, accelerometer_sensor_);
}
}
//-------------------------------------------------------------------------
//Misc
//-------------------------------------------------------------------------
-void AndroidAppBase::SetState( android_app* state, const char* native_activity_class_name )
+void AndroidAppBase::SetState(android_app* state, const char* native_activity_class_name)
{
app_ = state;
+
native_activity_class_name_ = native_activity_class_name;
- doubletap_detector_.SetConfiguration( app_->config );
- drag_detector_.SetConfiguration( app_->config );
- pinch_detector_.SetConfiguration( app_->config );
+ doubletap_detector_.SetConfiguration(app_->config);
+ drag_detector_.SetConfiguration(app_->config);
+ pinch_detector_.SetConfiguration(app_->config);
}
bool AndroidAppBase::IsReady()
{
- if( has_focus_ )
+ if (has_focus_)
return true;
return false;
@@ -237,30 +240,30 @@ bool AndroidAppBase::IsReady()
void AndroidAppBase::ShowUI()
{
- JNIEnv *jni;
- app_->activity->vm->AttachCurrentThread( &jni, NULL );
+ JNIEnv* jni;
+ app_->activity->vm->AttachCurrentThread(&jni, NULL);
//Default class retrieval
- jclass clazz = jni->GetObjectClass( app_->activity->clazz );
- jmethodID methodID = jni->GetMethodID( clazz, "showUI", "()V" );
- jni->CallVoidMethod( app_->activity->clazz, methodID );
+ jclass clazz = jni->GetObjectClass(app_->activity->clazz);
+ jmethodID methodID = jni->GetMethodID(clazz, "showUI", "()V");
+ jni->CallVoidMethod(app_->activity->clazz, methodID);
app_->activity->vm->DetachCurrentThread();
return;
}
-void AndroidAppBase::UpdateFPS( float fFPS )
+void AndroidAppBase::UpdateFPS(float fFPS)
{
- JNIEnv *jni;
- app_->activity->vm->AttachCurrentThread( &jni, NULL );
+ JNIEnv* jni;
+ app_->activity->vm->AttachCurrentThread(&jni, NULL);
//Default class retrieval
- jclass clazz = jni->GetObjectClass( app_->activity->clazz );
- jmethodID methodID = jni->GetMethodID( clazz, "updateFPS", "(F)V" );
- jni->CallVoidMethod( app_->activity->clazz, methodID, fFPS );
+ jclass clazz = jni->GetObjectClass(app_->activity->clazz);
+ jmethodID methodID = jni->GetMethodID(clazz, "updateFPS", "(F)V");
+ jni->CallVoidMethod(app_->activity->clazz, methodID, fFPS);
app_->activity->vm->DetachCurrentThread();
return;
}
-}
+} // namespace Diligent
diff --git a/NativeApp/src/Android/AndroidMain.cpp b/NativeApp/src/Android/AndroidMain.cpp
index 6e65b8a..b37bf88 100644
--- a/NativeApp/src/Android/AndroidMain.cpp
+++ b/NativeApp/src/Android/AndroidMain.cpp
@@ -1,4 +1,4 @@
- /* Copyright 2015-2018 Egor Yusov
+/* Copyright 2015-2018 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@
#include "NativeAppBase.h"
-#define HELPER_CLASS_NAME "com/android/helper/NDKHelper" //Class name of helper function
+#define HELPER_CLASS_NAME "com/android/helper/NDKHelper" //Class name of helper function
#define NATIVEACTIVITY_CLASS_NAME "android/app/NativeActivity"
using namespace Diligent;
@@ -37,53 +37,53 @@ using namespace Diligent;
// This is the main entry point of a native application that is using
// android_native_app_glue. It runs in its own thread, with its own
// event loop for receiving input events and doing other things.
-void android_main( android_app* state )
+void android_main(android_app* state)
{
std::unique_ptr<AndroidAppBase> theApp(CreateApplication());
- theApp->SetState( state, NATIVEACTIVITY_CLASS_NAME );
+ theApp->SetState(state, NATIVEACTIVITY_CLASS_NAME);
//Init helper functions
- ndk_helper::JNIHelper::Init( state->activity, HELPER_CLASS_NAME );
+ ndk_helper::JNIHelper::Init(state->activity, HELPER_CLASS_NAME);
- state->userData = theApp.get();
- state->onAppCmd = AndroidAppBase::HandleCmd;
+ state->userData = theApp.get();
+ state->onAppCmd = AndroidAppBase::HandleCmd;
state->onInputEvent = AndroidAppBase::HandleInput;
#ifdef USE_NDK_PROFILER
- monstartup( "libEngineSandbox.so" );
+ monstartup("libEngineSandbox.so");
#endif
// Prepare to monitor accelerometer
theApp->InitSensors();
// loop waiting for stuff to do.
- while( 1 )
+ while (true)
{
// Read all pending events.
- int id;
- int events;
+ int id;
+ int events;
android_poll_source* source;
// If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
- while( (id = ALooper_pollAll( theApp->IsReady() ? 0 : -1, NULL, &events, (void**)&source )) >= 0 )
+ while ((id = ALooper_pollAll(theApp->IsReady() ? 0 : -1, NULL, &events, (void**)&source)) >= 0)
{
// Process this event.
- if( source != NULL )
- source->process( state, source );
+ if (source != NULL)
+ source->process(state, source);
- theApp->ProcessSensors( id );
+ theApp->ProcessSensors(id);
// Check if we are exiting.
- if( state->destroyRequested != 0 )
+ if (state->destroyRequested != 0)
{
theApp->TermDisplay();
return;
}
}
- if( theApp->IsReady() )
+ if (theApp->IsReady())
{
// Drawing is throttled to the screen update rate, so there
// is no need to do timing here.