Skip to content

Commit

Permalink
Simplify management of ActivityStates
Browse files Browse the repository at this point in the history
  • Loading branch information
vittorioromeo authored and eXpl0it3r committed Nov 23, 2021
1 parent dc88cbd commit ef8fee5
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 85 deletions.
17 changes: 10 additions & 7 deletions src/SFML/Main/MainAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <android/window.h>
#include <android/native_activity.h>
#include <cstring>
#include <cassert>

#define SF_GLAD_EGL_IMPLEMENTATION
#include <glad/egl.h>
Expand All @@ -70,17 +71,19 @@ int getAndroidApiLevel(ANativeActivity* activity)
jfieldID sdkIntFieldID = lJNIEnv->GetStaticFieldID(versionClass, "SDK_INT", "I");
if (sdkIntFieldID == NULL)
return 0;

jint sdkInt = 0;
sdkInt = lJNIEnv->GetStaticIntField(versionClass, sdkIntFieldID);

return sdkInt;
}


////////////////////////////////////////////////////////////
ActivityStates* retrieveStates(ANativeActivity* activity)
{
assert(activity != NULL);

// Hide the ugly cast we find in each callback function
return (ActivityStates*)activity->instance;
}
Expand All @@ -97,8 +100,8 @@ static void initializeMain(ActivityStates* states)
states->looper = looper;

/**
* Acquire increments a reference counter on the looper. This keeps android
* from collecting it before the activity thread has a chance to detach its
* Acquire increments a reference counter on the looper. This keeps android
* from collecting it before the activity thread has a chance to detach its
* input queue.
*/
ALooper_acquire(states->looper);
Expand Down Expand Up @@ -175,7 +178,7 @@ void goToFullscreenMode(ANativeActivity* activity)

// Default flags
jint flags = 0;

// API Level 14
if (apiLevel >= 14)
{
Expand Down Expand Up @@ -327,7 +330,7 @@ static void onDestroy(ANativeActivity* activity)
delete states;

// Reset the activity pointer for all modules
sf::priv::getActivity(NULL, true);
sf::priv::resetActivity(NULL);

// The application should now terminate
}
Expand Down Expand Up @@ -509,7 +512,7 @@ JNIEXPORT void ANativeActivity_onCreate(ANativeActivity* activity, void* savedSt
states->terminated = false;

// Share it across the SFML modules
sf::priv::getActivity(states, true);
sf::priv::resetActivity(states);

// These functions will update the activity states and therefore, will allow
// SFML to be kept in the know
Expand Down
19 changes: 15 additions & 4 deletions src/SFML/System/Android/Activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
////////////////////////////////////////////////////////////
#include <SFML/System/Android/Activity.hpp>
#include <android/log.h>
#include <cassert>

#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_INFO, "sfml-error", __VA_ARGS__))

Expand Down Expand Up @@ -56,14 +57,24 @@ namespace sf
{
namespace priv
{
ActivityStates* getActivity(ActivityStates* initializedStates, bool reset)

ActivityStates*& getActivityStatesPtr()
{
static ActivityStates* states = NULL;
return states;
}

if (!states || reset)
states = initializedStates;
void resetActivity(ActivityStates* initializedStates)
{
getActivityStatesPtr() = initializedStates;
}

return states;
ActivityStates& getActivity()
{
ActivityStates* const states = getActivityStatesPtr();
assert(states != NULL);
return *states;
}

}
}
4 changes: 3 additions & 1 deletion src/SFML/System/Android/Activity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ struct ActivityStates
LogcatStream logcat;
};

SFML_SYSTEM_API ActivityStates* getActivity(ActivityStates* initializedStates=NULL, bool reset=false);
SFML_SYSTEM_API ActivityStates*& getActivityStatesPtr();
SFML_SYSTEM_API void resetActivity(ActivityStates* initializedStates);
SFML_SYSTEM_API ActivityStates& getActivity();

} // namespace priv
} // namespace sf
Expand Down
2 changes: 1 addition & 1 deletion src/SFML/System/Android/NativeActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace sf
////////////////////////////////////////////////////////////
ANativeActivity* getNativeActivity()
{
return priv::getActivity()->activity;
return priv::getActivity().activity;
}

} // namespace sf
6 changes: 3 additions & 3 deletions src/SFML/System/Android/ResourceStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ namespace priv
ResourceStream::ResourceStream(const std::string& filename) :
m_file (NULL)
{
ActivityStates* states = getActivity(NULL);
Lock lock(states->mutex);
m_file = AAssetManager_open(states->activity->assetManager, filename.c_str(), AASSET_MODE_UNKNOWN);
ActivityStates& states = getActivity();
Lock lock(states.mutex);
m_file = AAssetManager_open(states.activity->assetManager, filename.c_str(), AASSET_MODE_UNKNOWN);
}


Expand Down
34 changes: 17 additions & 17 deletions src/SFML/Window/Android/InputImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ void InputImpl::setVirtualKeyboardVisible(bool visible)
{
// todo: Check if the window is active

ActivityStates* states = getActivity(NULL);
Lock lock(states->mutex);
ActivityStates& states = getActivity();
Lock lock(states.mutex);

// Initializes JNI
jint lResult;
jint lFlags = 0;

JavaVM* lJavaVM = states->activity->vm;
JNIEnv* lJNIEnv = states->activity->env;
JavaVM* lJavaVM = states.activity->vm;
JNIEnv* lJNIEnv = states.activity->env;

JavaVMAttachArgs lJavaVMAttachArgs;
lJavaVMAttachArgs.version = JNI_VERSION_1_6;
Expand All @@ -69,7 +69,7 @@ void InputImpl::setVirtualKeyboardVisible(bool visible)
err() << "Failed to initialize JNI, couldn't switch the keyboard visibility" << std::endl;

// Retrieves NativeActivity
jobject lNativeActivity = states->activity->clazz;
jobject lNativeActivity = states.activity->clazz;
jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity);

// Retrieves Context.INPUT_METHOD_SERVICE
Expand Down Expand Up @@ -138,10 +138,10 @@ bool InputImpl::isMouseButtonPressed(Mouse::Button button)
{
ALooper_pollAll(0, NULL, NULL, NULL);

priv::ActivityStates* states = priv::getActivity(NULL);
Lock lock(states->mutex);
priv::ActivityStates& states = priv::getActivity();
Lock lock(states.mutex);

return states->isButtonPressed[button];
return states.isButtonPressed[button];
}


Expand All @@ -150,10 +150,10 @@ Vector2i InputImpl::getMousePosition()
{
ALooper_pollAll(0, NULL, NULL, NULL);

priv::ActivityStates* states = priv::getActivity(NULL);
Lock lock(states->mutex);
priv::ActivityStates& states = priv::getActivity();
Lock lock(states.mutex);

return states->mousePosition;
return states.mousePosition;
}


Expand Down Expand Up @@ -183,10 +183,10 @@ bool InputImpl::isTouchDown(unsigned int finger)
{
ALooper_pollAll(0, NULL, NULL, NULL);

priv::ActivityStates* states = priv::getActivity(NULL);
Lock lock(states->mutex);
priv::ActivityStates& states = priv::getActivity();
Lock lock(states.mutex);

return states->touchEvents.find(finger) != states->touchEvents.end();
return states.touchEvents.find(finger) != states.touchEvents.end();
}


Expand All @@ -195,10 +195,10 @@ Vector2i InputImpl::getTouchPosition(unsigned int finger)
{
ALooper_pollAll(0, NULL, NULL, NULL);

priv::ActivityStates* states = priv::getActivity(NULL);
Lock lock(states->mutex);
priv::ActivityStates& states = priv::getActivity();
Lock lock(states.mutex);

return states->touchEvents.find(finger)->second;
return states.touchEvents.find(finger)->second;
}


Expand Down
6 changes: 3 additions & 3 deletions src/SFML/Window/Android/VideoModeImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ std::vector<VideoMode> VideoModeImpl::getFullscreenModes()
VideoMode VideoModeImpl::getDesktopMode()
{
// Get the activity states
priv::ActivityStates* states = priv::getActivity(NULL);
Lock lock(states->mutex);
priv::ActivityStates& states = priv::getActivity();
Lock lock(states.mutex);

return VideoMode(states->screenSize.x, states->screenSize.y);
return VideoMode(states.screenSize.x, states.screenSize.y);
}

} // namespace priv
Expand Down

0 comments on commit ef8fee5

Please sign in to comment.