From e28f606f38ed50cc9067968e37ddc77dae1b5dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Fri, 29 Dec 2017 22:06:36 -0300 Subject: [PATCH] Added PlatformHelper ( moved all the Android platform helpers there ). Fixed a couple of minor bugs. --HG-- branch : dev --- include/eepp/window.hpp | 1 + include/eepp/window/engine.hpp | 9 +-- include/eepp/window/platformhelper.hpp | 44 +++++++++++ include/eepp/window/window.hpp | 41 ----------- projects/linux/ee.files | 5 ++ src/eepp/system/sys.cpp | 10 +-- src/eepp/ui/uiwidget.cpp | 8 +- .../backend/SDL2/platformhelpersdl2.cpp | 73 +++++++++++++++++++ .../backend/SDL2/platformhelpersdl2.hpp | 33 +++++++++ src/eepp/window/backend/SDL2/windowsdl2.cpp | 72 ------------------ src/eepp/window/backend/SDL2/windowsdl2.hpp | 24 ------ .../backend/SFML/platformhelpersfml.cpp | 39 ++++++++++ .../backend/SFML/platformhelpersfml.hpp | 33 +++++++++ src/eepp/window/engine.cpp | 40 +++++----- src/eepp/window/window.cpp | 26 ------- 15 files changed, 258 insertions(+), 200 deletions(-) create mode 100644 include/eepp/window/platformhelper.hpp create mode 100644 src/eepp/window/backend/SDL2/platformhelpersdl2.cpp create mode 100644 src/eepp/window/backend/SDL2/platformhelpersdl2.hpp create mode 100644 src/eepp/window/backend/SFML/platformhelpersfml.cpp create mode 100644 src/eepp/window/backend/SFML/platformhelpersfml.hpp diff --git a/include/eepp/window.hpp b/include/eepp/window.hpp index 31e8f9544..5f0f7d99c 100644 --- a/include/eepp/window.hpp +++ b/include/eepp/window.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #endif diff --git a/include/eepp/window/engine.hpp b/include/eepp/window/engine.hpp index 150265c18..ba4ad6934 100755 --- a/include/eepp/window/engine.hpp +++ b/include/eepp/window/engine.hpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace EE { namespace System { class IniFile; class Pack; } } @@ -117,11 +118,8 @@ class EE_API Engine { /** @return The id of the thread that was used to initialize the OpenGL Context. */ Uint32 getMainThreadId(); -#if EE_PLATFORM == EE_PLATFORM_ANDROID - std::string getExternalStoragePath(); - - std::string getInternalStoragePath(); -#endif + /** @return The instance of platform class that provides some helpers for some platforms */ + PlatformHelper * getPlatformHelper(); protected: friend class Window; @@ -130,6 +128,7 @@ class EE_API Engine { EE::Window::Window * mWindow; bool mSharedGLContext; Uint32 mMainThreadId; + PlatformHelper * mPlatformHelper; Pack * mZip; Engine(); diff --git a/include/eepp/window/platformhelper.hpp b/include/eepp/window/platformhelper.hpp new file mode 100644 index 000000000..9b577b5d4 --- /dev/null +++ b/include/eepp/window/platformhelper.hpp @@ -0,0 +1,44 @@ +#ifndef EE_WINDOW_PLATFORM_HPP +#define EE_WINDOW_PLATFORM_HPP + +#include +#include + +namespace EE { namespace Window { + +class PlatformHelper +{ + public: +#if EE_PLATFORM == EE_PLATFORM_ANDROID + /** @return The Activity object for the application */ + virtual void * getActivity() = 0; + + /** @return The JNI environment for the current thread */ + virtual void * getJNIEnv() = 0; + + /** @return The path used for external storage for this application. + * This path is unique to your application, but is public and can be + * written to by other applications. + */ + virtual std::string getExternalStoragePath() = 0; + + /** @return The path used for internal storage for this application. + * This path is unique to your application and cannot be written to + * by other applications. + */ + virtual std::string getInternalStoragePath() = 0; + + /** @return The application APK file path */ + virtual std::string getApkPath() = 0; + + /** @return True if the external storage is readable. */ + virtual bool isExternalStorageReadable() = 0; + + /** @return True if the external storage is writeable. */ + virtual bool isExternalStorageWritable() = 0; +#endif +}; + +}} + +#endif diff --git a/include/eepp/window/window.hpp b/include/eepp/window/window.hpp index 6914b54df..b0c5cadd1 100644 --- a/include/eepp/window/window.hpp +++ b/include/eepp/window/window.hpp @@ -148,13 +148,6 @@ class DisplayMode { Uint32 ScreenIndex; }; - -/* See the official Android developer guide for more information: - http://developer.android.com/guide/topics/data/data-storage.html -*/ -#define EE_ANDROID_EXTERNAL_STORAGE_READ 0x01 -#define EE_ANDROID_EXTERNAL_STORAGE_WRITE 0x02 - class EE_API Window { public: typedef cb::Callback1 WindowResizeCallback; @@ -397,40 +390,6 @@ class EE_API Window { */ virtual bool isScreenKeyboardShown(); -#if EE_PLATFORM == EE_PLATFORM_ANDROID - /** @return The JNI environment for the current thread - * This returns JNIEnv*, but the prototype is void* so we don't need jni.h - */ - virtual void * getJNIEnv(); - - /** @return The SDL Activity object for the application - * This returns jobject, but the prototype is void* so we don't need jni.h - */ - virtual void * getActivity(); - - /** @return The current state of external storage, a bitmask of these values: - * EE_ANDROID_EXTERNAL_STORAGE_READ - * EE_ANDROID_EXTERNAL_STORAGE_WRITE - * If external storage is currently unavailable, this will return 0. - */ - virtual int getExternalStorageState(); - - /** @return The path used for internal storage for this application. - * This path is unique to your application and cannot be written to - * by other applications. - */ - virtual std::string getInternalStoragePath(); - - /** @return The path used for external storage for this application. - * This path is unique to your application, but is public and can be - * written to by other applications. - */ - virtual std::string getExternalStoragePath(); - - /** @return The application APK file path */ - virtual std::string getApkPath(); -#endif - /** @return True if the current window support a threaded GL Context. This means that supports OpenGL Shared Contexts ( multithreaded opengl contexts ). ** Only supported with SDL2 backend.*/ virtual bool isThreadedGLContext(); diff --git a/projects/linux/ee.files b/projects/linux/ee.files index c66a76ad1..fbd5b6ac1 100644 --- a/projects/linux/ee.files +++ b/projects/linux/ee.files @@ -436,7 +436,12 @@ ../../include/eepp/window/cursor.hpp ../../include/eepp/window/clipboard.hpp ../../include/eepp/window/base.hpp +../../src/eepp/window/backend/SDL2/platformhelpersdl2.cpp +../../src/eepp/window/backend/SDL2/platformhelpersdl2.hpp +../../src/eepp/window/backend/SFML/platformhelpersfml.cpp +../../src/eepp/window/backend/SFML/platformhelpersfml.hpp ../../src/eepp/window/inputhelper.cpp +../../src/eepp/window/platformhelper.hpp ../../src/eepp/window/window.cpp ../../src/eepp/window/view.cpp ../../src/eepp/window/platformimpl.cpp diff --git a/src/eepp/system/sys.cpp b/src/eepp/system/sys.cpp index 333190d38..a46c12547 100644 --- a/src/eepp/system/sys.cpp +++ b/src/eepp/system/sys.cpp @@ -515,7 +515,7 @@ static std::string sGetProcessPath() { return FileSystem::fileRemoveFileName( std::string( info.name ) ); #elif EE_PLATFORM == EE_PLATFORM_ANDROID - return Window::Engine::instance()->getExternalStoragePath() + "/"; + return Window::Engine::instance()->getPlatformHelper()->getExternalStoragePath() + "/"; #else #warning Sys::GetProcessPath() not implemented on this platform. ( will return "./" ) return "./"; @@ -623,7 +623,7 @@ std::string Sys::getConfigPath( std::string appname ) { #elif EE_PLATFORM == EE_PLATFORM_IOS return GetProcessPath() + "config"; #elif EE_PLATFORM == EE_PLATFORM_ANDROID - return Window::Engine::instance()->getInternalStoragePath() + "/"; + return Window::Engine::instance()->getPlatformHelper()->getInternalStoragePath() + "/"; #else #warning Sys::GetConfigPath not implemented for this platform ( it will use HOME directory + /.appname ) @@ -649,11 +649,7 @@ std::string Sys::getTempPath() { return std::string( "C:\\WINDOWS\\TEMP\\" ); } #elif EE_PLATFORM == EE_PLATFORM_ANDROID - if ( NULL != Window::Engine::instance() ) { - String::strCopy( path, Window::Engine::instance()->getCurrentWindow()->getInternalStoragePath().c_str(), EE_MAX_CFG_PATH_LEN ); - } else { - String::strCopy( path, "/tmp", EE_MAX_CFG_PATH_LEN ); - } + String::strCopy( path, std::string( Window::Engine::instance()->getPlatformHelper()->getInternalStoragePath() + "/tmp/" ).c_str(), EE_MAX_CFG_PATH_LEN ); #else char * tmpdir = getenv("TMPDIR"); diff --git a/src/eepp/ui/uiwidget.cpp b/src/eepp/ui/uiwidget.cpp index 7055c741c..528a61770 100644 --- a/src/eepp/ui/uiwidget.cpp +++ b/src/eepp/ui/uiwidget.cpp @@ -432,10 +432,10 @@ static BlendMode toBlendMode( std::string val ) { BlendMode blendMode; - if ( val == "add" ) blendMode == BlendAdd; - else if ( val == "alpha" ) blendMode == BlendAlpha; - else if ( val == "multiply" ) blendMode == BlendMultiply; - else if ( val == "none" ) blendMode == BlendNone; + if ( val == "add" ) blendMode = BlendAdd; + else if ( val == "alpha" ) blendMode = BlendAlpha; + else if ( val == "multiply" ) blendMode = BlendMultiply; + else if ( val == "none" ) blendMode = BlendNone; return blendMode; } diff --git a/src/eepp/window/backend/SDL2/platformhelpersdl2.cpp b/src/eepp/window/backend/SDL2/platformhelpersdl2.cpp new file mode 100644 index 000000000..b07dc1a0e --- /dev/null +++ b/src/eepp/window/backend/SDL2/platformhelpersdl2.cpp @@ -0,0 +1,73 @@ +#include +#include + +#if EE_PLATFORM == EE_PLATFORM_ANDROID +#include +#endif + +namespace EE { namespace Window { namespace Backend { namespace SDL2 { + +PlatformHelperSDL2::PlatformHelperSDL2() +{ +} + +#if EE_PLATFORM == EE_PLATFORM_ANDROID +void * PlatformHelperSDL2::getActivity() { + return SDL_AndroidGetActivity(); +} + +void * PlatformHelperSDL2::getJNIEnv() { + return SDL_AndroidGetJNIEnv(); +} + +std::string PlatformHelperSDL2::getExternalStoragePath() { + return std::string( SDL_AndroidGetExternalStoragePath() ); +} + +std::string PlatformHelperSDL2::getInternalStoragePath() { + return std::string( SDL_AndroidGetInternalStoragePath() ); +} + +std::string PlatformHelperSDL2::getApkPath() { + static std::string apkPath = ""; + + if ( "" == apkPath ) { + jmethodID mid; + jobject context; + jobject fileObject; + const char *path; + + JNIEnv *env = (JNIEnv*)getJNIEnv(); + + jclass ActivityClass = env->GetObjectClass((jobject)getActivity()); + + // context = SDLActivity.getContext(); + mid = env->GetStaticMethodID(ActivityClass,"getContext","()Landroid/content/Context;"); + + context = env->CallStaticObjectMethod(ActivityClass, mid); + + // fileObj = context.getFilesDir(); + mid = env->GetMethodID(env->GetObjectClass(context),"getPackageCodePath", "()Ljava/lang/String;"); + + fileObject = env->CallObjectMethod(context, mid); + + jboolean isCopy; + path = env->GetStringUTFChars((jstring)fileObject, &isCopy); + + apkPath = std::string( path ); + } + + return apkPath; +} + +bool PlatformHelperSDL2::isExternalStorageReadable() { + return 0 != ( SDL_AndroidGetExternalStorageState() & SDL_ANDROID_EXTERNAL_STORAGE_READ ); +} + +bool PlatformHelperSDL2::isExternalStorageWritable() { + return 0 != ( SDL_AndroidGetExternalStorageState() & SDL_ANDROID_EXTERNAL_STORAGE_WRITE ); +} + +#endif + +}}}} diff --git a/src/eepp/window/backend/SDL2/platformhelpersdl2.hpp b/src/eepp/window/backend/SDL2/platformhelpersdl2.hpp new file mode 100644 index 000000000..058842864 --- /dev/null +++ b/src/eepp/window/backend/SDL2/platformhelpersdl2.hpp @@ -0,0 +1,33 @@ +#ifndef EE_PLATFORMHELPERSDL2_HPP +#define EE_PLATFORMHELPERSDL2_HPP + +#include +#include + +namespace EE { namespace Window { namespace Backend { namespace SDL2 { + +class PlatformHelperSDL2 : public PlatformHelper +{ + public: + PlatformHelperSDL2(); + +#if EE_PLATFORM == EE_PLATFORM_ANDROID + void * getActivity(); + + void * getJNIEnv(); + + std::string getExternalStoragePath(); + + std::string getInternalStoragePath(); + + std::string getApkPath(); + + bool isExternalStorageReadable(); + + bool isExternalStorageWritable(); +#endif +}; + +}}}} + +#endif diff --git a/src/eepp/window/backend/SDL2/windowsdl2.cpp b/src/eepp/window/backend/SDL2/windowsdl2.cpp index bcc910bf8..2e01a2257 100644 --- a/src/eepp/window/backend/SDL2/windowsdl2.cpp +++ b/src/eepp/window/backend/SDL2/windowsdl2.cpp @@ -23,52 +23,6 @@ namespace EE { namespace Window { namespace Backend { namespace SDL2 { -#if EE_PLATFORM == EE_PLATFORM_ANDROID -#include -#include - -std::string WindowSDL::SDL_AndroidGetApkPath() { - static std::string apkPath = ""; - - if ( "" == apkPath ) { - jmethodID mid; - jobject context; - jobject fileObject; - const char *path; - - JNIEnv *env = (JNIEnv*)SDL_AndroidGetJNIEnv(); - - jclass ActivityClass = env->GetObjectClass((jobject)SDL_AndroidGetActivity()); - - // context = SDLActivity.getContext(); - mid = env->GetStaticMethodID(ActivityClass,"getContext","()Landroid/content/Context;"); - - context = env->CallStaticObjectMethod(ActivityClass, mid); - - // fileObj = context.getFilesDir(); - mid = env->GetMethodID(env->GetObjectClass(context),"getPackageCodePath", "()Ljava/lang/String;"); - - fileObject = env->CallObjectMethod(context, mid); - - jboolean isCopy; - path = env->GetStringUTFChars((jstring)fileObject, &isCopy); - - apkPath = std::string( path ); - } - - return apkPath; -} - -std::string WindowSDL::SDL_AndroidGetExternalStoragePath() { - return std::string( SDL_AndroidGetExternalStoragePath() ); -} - -std::string WindowSDL::SDL_AndroidGetInternalStoragePath() { - return std::string( SDL_AndroidGetInternalStoragePath() ); -} - -#endif - WindowSDL::WindowSDL( WindowSettings Settings, ContextSettings Context ) : Window( Settings, Context, eeNew( ClipboardSDL, ( this ) ), eeNew( InputSDL, ( this ) ), eeNew( CursorManagerSDL, ( this ) ) ), mSDLWindow( NULL ), @@ -687,32 +641,6 @@ bool WindowSDL::isScreenKeyboardShown() { #endif } -#if EE_PLATFORM == EE_PLATFORM_ANDROID -void * WindowSDL::getJNIEnv() { - return SDL_AndroidGetJNIEnv(); -} - -void * WindowSDL::getActivity() { - return SDL_AndroidGetActivity(); -} - -int WindowSDL::getExternalStorageState() { - return SDL_AndroidGetExternalStorageState(); -} - -std::string WindowSDL::getInternalStoragePath() { - return std::string( SDL_AndroidGetInternalStoragePath() ); -} - -std::string WindowSDL::getExternalStoragePath() { - return std::string( SDL_AndroidGetExternalStoragePath() ); -} - -std::string WindowSDL::getApkPath() { - return SDL_AndroidGetApkPath(); -} -#endif - }}}} #endif diff --git a/src/eepp/window/backend/SDL2/windowsdl2.hpp b/src/eepp/window/backend/SDL2/windowsdl2.hpp index bdd9b6fa0..8d53f68cb 100644 --- a/src/eepp/window/backend/SDL2/windowsdl2.hpp +++ b/src/eepp/window/backend/SDL2/windowsdl2.hpp @@ -13,20 +13,10 @@ #define EE_USE_WMINFO #endif -namespace EE { namespace System { class Zip; } } - namespace EE { namespace Window { namespace Backend { namespace SDL2 { class EE_API WindowSDL : public Window { public: - #if EE_PLATFORM == EE_PLATFORM_ANDROID - static std::string SDL_AndroidGetApkPath(); - - static std::string SDL_AndroidGetExternalStoragePath(); - - static std::string SDL_AndroidGetInternalStoragePath(); - #endif - WindowSDL( WindowSettings Settings, ContextSettings Context ); virtual ~WindowSDL(); @@ -81,20 +71,6 @@ class EE_API WindowSDL : public Window { bool isScreenKeyboardShown(); -#if EE_PLATFORM == EE_PLATFORM_ANDROID - void * getJNIEnv(); - - void * getActivity(); - - int getExternalStorageState(); - - std::string getInternalStoragePath(); - - std::string getExternalStoragePath(); - - std::string getApkPath(); -#endif - bool isThreadedGLContext(); void setGLContextThread(); diff --git a/src/eepp/window/backend/SFML/platformhelpersfml.cpp b/src/eepp/window/backend/SFML/platformhelpersfml.cpp new file mode 100644 index 000000000..5185e43ba --- /dev/null +++ b/src/eepp/window/backend/SFML/platformhelpersfml.cpp @@ -0,0 +1,39 @@ +#include + +namespace EE { namespace Window { namespace Backend { namespace SFML { + +PlatformHelperSFML::PlatformHelperSFML() +{ +} + +#if EE_PLATFORM == EE_PLATFORM_ANDROID +void * PlatformHelperSFML::getActivity() { + return NULL; +} + +void * PlatformHelperSFML::getJNIEnv() { + return NULL; +} + +std::string PlatformHelperSFML::getExternalStoragePath() { + return std::string(); +} + +std::string PlatformHelperSFML::getInternalStoragePath() { + return std::string(); +} + +std::string PlatformHelperSFML::getApkPath() { + return std::string(); +} + +bool PlatformHelperSFML::isExternalStorageReadable() { + return true; +} + +bool PlatformHelperSFML::isExternalStorageWritable() { + return true; +} +#endif + +}}}} diff --git a/src/eepp/window/backend/SFML/platformhelpersfml.hpp b/src/eepp/window/backend/SFML/platformhelpersfml.hpp new file mode 100644 index 000000000..f0d30e15b --- /dev/null +++ b/src/eepp/window/backend/SFML/platformhelpersfml.hpp @@ -0,0 +1,33 @@ +#ifndef EE_PLATFORMHELPERSFML_HPP +#define EE_PLATFORMHELPERSFML_HPP + +#include +#include + +namespace EE { namespace Window { namespace Backend { namespace SFML { + +class PlatformHelperSFML : public PlatformHelper +{ + public: + PlatformHelperSFML(); + +#if EE_PLATFORM == EE_PLATFORM_ANDROID + void * getActivity(); + + void * getJNIEnv(); + + std::string getExternalStoragePath(); + + std::string getInternalStoragePath(); + + std::string getApkPath(); + + bool isExternalStorageReadable(); + + bool isExternalStorageWritable(); +#endif +}; + +}}}} + +#endif diff --git a/src/eepp/window/engine.cpp b/src/eepp/window/engine.cpp index 96a0ff139..d25f1ce7a 100755 --- a/src/eepp/window/engine.cpp +++ b/src/eepp/window/engine.cpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #if EE_PLATFORM == EE_PLATFORM_ANDROID @@ -43,11 +45,14 @@ Engine::Engine() : mBackend( NULL ), mWindow( NULL ), mSharedGLContext( false ), - mMainThreadId( 0 ) -#if EE_PLATFORM == EE_PLATFORM_ANDROID - , mZip( NULL ) -#endif + mMainThreadId( 0 ), + mPlatformHelper( NULL ) { +#if EE_PLATFORM == EE_PLATFORM_ANDROID + mZip = eeNew( Zip, () ); + mZip->open( getPlatformHelper()->getApkPath() ); +#endif + TextureAtlasManager::createSingleton(); } @@ -88,6 +93,8 @@ Engine::~Engine() { eeSAFE_DELETE( mZip ); #endif + eeSAFE_DELETE( mPlatformHelper ); + eeSAFE_DELETE( mBackend ); } @@ -334,25 +341,16 @@ Uint32 Engine::getMainThreadId() { return mMainThreadId; } -#if EE_PLATFORM == EE_PLATFORM_ANDROID -std::string Engine::getExternalStoragePath() { - #if defined( EE_BACKEND_SDL2 ) - if ( NULL == mZip ) { - mZip = eeNew( Zip, () ); - mZip->open( Backend::SDL2::WindowSDL::SDL_AndroidGetApkPath() ); +PlatformHelper * Engine::getPlatformHelper() { + if ( NULL == mPlatformHelper ) { + #if DEFAULT_BACKEND == BACKEND_SDL2 + mPlatformHelper = eeNew( Backend::SDL2::PlatformHelperSDL2, () ); + #elif DEFAULT_BACKEND == BACKEND_SFML + mPlatform = eeNew( Backend::SFML::PlatformHelperSFML, () ); + #endif } - return SDL_AndroidGetExternalStoragePath(); - #endif - return ""; + return mPlatformHelper; } -std::string Engine::getInternalStoragePath() { - #if defined( EE_BACKEND_SDL2 ) - return SDL_AndroidGetExternalStoragePath(); - #endif - return ""; -} -#endif - }} diff --git a/src/eepp/window/window.cpp b/src/eepp/window/window.cpp index 08a69454a..4d20edb24 100644 --- a/src/eepp/window/window.cpp +++ b/src/eepp/window/window.cpp @@ -520,30 +520,4 @@ void Window::runMainLoop( void (*func)(), int fps ) { #endif } -#if EE_PLATFORM == EE_PLATFORM_ANDROID -void * Window::getJNIEnv() { - return NULL; -} - -void * Window::getActivity() { - return NULL; -} - -int Window::getExternalStorageState() { - return 0; -} - -std::string Window::getInternalStoragePath() { - return std::string(""); -} - -std::string Window::getExternalStoragePath() { - return std::string(""); -} - -std::string Window::getApkPath() { - return std::string(""); -} -#endif - }}