diff --git a/.hgignore b/.hgignore index 7d7931aa4..788b3f18f 100644 --- a/.hgignore +++ b/.hgignore @@ -1,5 +1,6 @@ syntax: regexp ^fonts$ +^assets/temp$ ^obj$ ^libs$ ^make$ diff --git a/include/eepp/graphics/csprite.hpp b/include/eepp/graphics/csprite.hpp index 006d4b719..820327bea 100755 --- a/include/eepp/graphics/csprite.hpp +++ b/include/eepp/graphics/csprite.hpp @@ -32,7 +32,7 @@ class EE_API cSprite { * @param SearchInTextureAtlas If you want only to search in a especific atlas ( NULL if you want to search in all atlases ) * @note Texture atlases saves the SubTextures names without extension by default. * @see cTextureAtlasManager::GetSubTexturesByPattern -*/ + */ cSprite( const std::string& name, const std::string& extension = "", cTextureAtlas * SearchInTextureAtlas = NULL ); /** Creates a Sprite from a SubTexture diff --git a/include/eepp/graphics/csubtexture.hpp b/include/eepp/graphics/csubtexture.hpp index 466b3e6cb..27b473825 100644 --- a/include/eepp/graphics/csubtexture.hpp +++ b/include/eepp/graphics/csubtexture.hpp @@ -79,9 +79,9 @@ class EE_API cSubTexture { /** Set the SubTexture offset. */ void Offset( const eeVector2i& offset ); - void Draw( const eeFloat& X, const eeFloat& Y, const eeColorA& Color = eeColorA(), const eeFloat& Angle = 0.f, const eeFloat& Scale = 1.f, const EE_BLEND_MODE& Blend = ALPHA_NORMAL, const EE_RENDER_MODE& Effect = RN_NORMAL, const bool& ScaleRendered = true ); + void Draw( const eeFloat& X, const eeFloat& Y, const eeColorA& Color = eeColorA(), const eeFloat& Angle = 0.f, const eeFloat& Scale = 1.f, const EE_BLEND_MODE& Blend = ALPHA_NORMAL, const EE_RENDER_MODE& Effect = RN_NORMAL, const bool& ScaleCentered = true ); - void Draw( const eeFloat& X, const eeFloat& Y, const eeFloat& Angle, const eeFloat& Scale, const eeColorA& Color0 = eeColorA(), const eeColorA& Color1 = eeColorA(), const eeColorA& Color2 = eeColorA(), const eeColorA& Color3 = eeColorA(), const EE_BLEND_MODE& Blend = ALPHA_NORMAL, const EE_RENDER_MODE& Effect = RN_NORMAL, const bool& ScaleRendered = true ); + void Draw( const eeFloat& X, const eeFloat& Y, const eeFloat& Angle, const eeFloat& Scale, const eeColorA& Color0 = eeColorA(), const eeColorA& Color1 = eeColorA(), const eeColorA& Color2 = eeColorA(), const eeColorA& Color3 = eeColorA(), const EE_BLEND_MODE& Blend = ALPHA_NORMAL, const EE_RENDER_MODE& Effect = RN_NORMAL, const bool& ScaleCentered = true ); void Draw( const eeQuad2f Q, const eeFloat& X, const eeFloat& Y, const eeFloat& Angle = 0.f, const eeFloat& Scale = 1.f, const eeColorA& Color0 = eeColorA(), const eeColorA& Color1 = eeColorA(), const eeColorA& Color2 = eeColorA(), const eeColorA& Color3 = eeColorA(), const EE_BLEND_MODE& Blend = ALPHA_NORMAL ); diff --git a/include/eepp/window/cengine.hpp b/include/eepp/window/cengine.hpp index 3994e1d47..f641da650 100755 --- a/include/eepp/window/cengine.hpp +++ b/include/eepp/window/cengine.hpp @@ -108,12 +108,28 @@ class EE_API cEngine { @param iniKeyName The ini key name to search the properties */ ContextSettings CreateContextSettings( cIniFile * ini, std::string iniKeyName = "EEPP" ); + + /** Enabling Shared GL Context allows asynchronous OpenGL resource loading ( only if is supported by the backend, SDL 2 backend is the only one supported ). + ** If the cTextureLoader is threaded, will upload the texture un another thread to the GPU. So, it will not block the main rendering thread. + ** Shared GL Context is disabled by default. + */ + void EnableSharedGLContext(); + + /** Disable the Shared GL Context + ** @see EnableSharedGLContext() + */ + void DisableSharedGLContext(); + + /** @return If the Shared GL Context is enabled and ready to use. */ + bool IsSharedGLContextEnabled(); protected: friend class cWindow; Backend::cBackend * mBackend; std::list mWindows; cWindow * mWindow; + cMutex mGLThreadMutex; + bool mSharedGLContext; cEngine(); diff --git a/include/eepp/window/cwindow.hpp b/include/eepp/window/cwindow.hpp index a801091e7..d37f30b8f 100644 --- a/include/eepp/window/cwindow.hpp +++ b/include/eepp/window/cwindow.hpp @@ -416,6 +416,12 @@ class EE_API cWindow { /** @return The application APK file path */ virtual std::string GetApkPath(); #endif + + virtual bool IsThreadedGLContext(); + + virtual void SetGLContextThread(); + + virtual void UnsetGLContextThread(); protected: friend class cEngine; @@ -485,6 +491,10 @@ class EE_API cWindow { void LogSuccessfulInit( const std::string& BackendName, const std::string& ProcessPath = "" ); void LogFailureInit( const std::string& ClassName, const std::string& BackendName ); + + void GLThreadMutexLock(); + + void GLThreadMutexUnlock(); }; }} diff --git a/projects/linux/ee.creator.user b/projects/linux/ee.creator.user index e938dd047..f8cf489e8 100644 --- a/projects/linux/ee.creator.user +++ b/projects/linux/ee.creator.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget diff --git a/projects/linux/ee.files b/projects/linux/ee.files index a10f3461d..0c4f5f37e 100644 --- a/projects/linux/ee.files +++ b/projects/linux/ee.files @@ -614,3 +614,4 @@ ../../src/examples/physics/physics.cpp ../../src/eepp/physics/cshapepoint.cpp ../../include/eepp/physics/cshapepoint.hpp +../../include/eepp/math/originpoint.hpp diff --git a/src/eepp/graphics/csubtexture.cpp b/src/eepp/graphics/csubtexture.cpp index ba520e468..d3d4718df 100644 --- a/src/eepp/graphics/csubtexture.cpp +++ b/src/eepp/graphics/csubtexture.cpp @@ -136,14 +136,14 @@ void cSubTexture::Offset( const eeVector2i& offset ) { mOffset = offset; } -void cSubTexture::Draw( const eeFloat& X, const eeFloat& Y, const eeColorA& Color, const eeFloat& Angle, const eeFloat& Scale, const EE_BLEND_MODE& Blend, const EE_RENDER_MODE& Effect, const bool& ScaleRendered ) { +void cSubTexture::Draw( const eeFloat& X, const eeFloat& Y, const eeColorA& Color, const eeFloat& Angle, const eeFloat& Scale, const EE_BLEND_MODE& Blend, const EE_RENDER_MODE& Effect, const bool& ScaleCentered ) { if ( NULL != mTexture ) - mTexture->DrawEx( X + mOffset.x, Y + mOffset.y, mDestSize.x, mDestSize.y, Angle, Scale, Color, Color, Color, Color, Blend, Effect, ScaleRendered, mSrcRect ); + mTexture->DrawEx( X + mOffset.x, Y + mOffset.y, mDestSize.x, mDestSize.y, Angle, Scale, Color, Color, Color, Color, Blend, Effect, ScaleCentered, mSrcRect ); } -void cSubTexture::Draw( const eeFloat& X, const eeFloat& Y, const eeFloat& Angle, const eeFloat& Scale, const eeColorA& Color0, const eeColorA& Color1, const eeColorA& Color2, const eeColorA& Color3, const EE_BLEND_MODE& Blend, const EE_RENDER_MODE& Effect, const bool& ScaleRendered ) { +void cSubTexture::Draw( const eeFloat& X, const eeFloat& Y, const eeFloat& Angle, const eeFloat& Scale, const eeColorA& Color0, const eeColorA& Color1, const eeColorA& Color2, const eeColorA& Color3, const EE_BLEND_MODE& Blend, const EE_RENDER_MODE& Effect, const bool& ScaleCentered ) { if ( NULL != mTexture ) - mTexture->DrawEx( X + mOffset.x, Y + mOffset.y, mDestSize.x, mDestSize.y, Angle, Scale, Color0, Color1, Color2, Color3, Blend, Effect, ScaleRendered, mSrcRect ); + mTexture->DrawEx( X + mOffset.x, Y + mOffset.y, mDestSize.x, mDestSize.y, Angle, Scale, Color0, Color1, Color2, Color3, Blend, Effect, ScaleCentered, mSrcRect ); } void cSubTexture::Draw( const eeQuad2f Q, const eeFloat& X, const eeFloat& Y, const eeFloat& Angle, const eeFloat& Scale, const eeColorA& Color0, const eeColorA& Color1, const eeColorA& Color2, const eeColorA& Color3, const EE_BLEND_MODE& Blend ) { diff --git a/src/eepp/graphics/ctextureloader.cpp b/src/eepp/graphics/ctextureloader.cpp index c89fa4fd2..79bc5ffe1 100644 --- a/src/eepp/graphics/ctextureloader.cpp +++ b/src/eepp/graphics/ctextureloader.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +using namespace EE::Window; #define TEX_LT_PATH (1) #define TEX_LT_MEM (2) @@ -238,8 +240,9 @@ void cTextureLoader::Start() { mTexLoaded = true; - if ( !mThreaded ) + if ( !mThreaded || ( cEngine::instance()->IsSharedGLContextEnabled() && cEngine::instance()->GetCurrentWindow()->IsThreadedGLContext() ) ) { LoadFromPixels(); + } } void cTextureLoader::LoadFile() { @@ -415,6 +418,10 @@ void cTextureLoader::LoadFromPixels() { flags = ( mClampMode == CLAMP_REPEAT) ? (flags | SOIL_FLAG_TEXTURE_REPEATS) : flags; flags = ( mCompressTexture ) ? ( flags | SOIL_FLAG_COMPRESS_TO_DXT ) : flags; + if ( mThreaded && cEngine::instance()->IsSharedGLContextEnabled() && cEngine::instance()->GetCurrentWindow()->IsThreadedGLContext() ) { + cEngine::instance()->GetCurrentWindow()->SetGLContextThread(); + } + GLint PreviousTexture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &PreviousTexture); @@ -444,6 +451,10 @@ void cTextureLoader::LoadFromPixels() { glBindTexture( GL_TEXTURE_2D, PreviousTexture ); + if ( mThreaded && cEngine::instance()->IsSharedGLContextEnabled() && cEngine::instance()->GetCurrentWindow()->IsThreadedGLContext() ) { + cEngine::instance()->GetCurrentWindow()->UnsetGLContextThread(); + } + if ( tTexId ) { mWidth = width; mHeight = height; @@ -474,7 +485,6 @@ void cTextureLoader::LoadFromPixels() { } mPixels = NULL; - } else { if ( NULL != stbi_failure_reason() ) { cLog::instance()->Write( stbi_failure_reason() ); @@ -492,7 +502,9 @@ void cTextureLoader::LoadFromPixels() { } void cTextureLoader::Update() { - LoadFromPixels(); + if ( !( cEngine::instance()->IsSharedGLContextEnabled() && cEngine::instance()->GetCurrentWindow()->IsThreadedGLContext() ) ) { + LoadFromPixels(); + } } const Uint32& cTextureLoader::Id() const { diff --git a/src/eepp/window/backend/SDL2/cwindowsdl2.cpp b/src/eepp/window/backend/SDL2/cwindowsdl2.cpp index 1122cac0e..569d5f8f4 100644 --- a/src/eepp/window/backend/SDL2/cwindowsdl2.cpp +++ b/src/eepp/window/backend/SDL2/cwindowsdl2.cpp @@ -6,6 +6,10 @@ #include #endif +#include + +#include +#include #include #include #include @@ -16,11 +20,8 @@ #include #include #include -#include -#include #include - #if EE_PLATFORM == EE_PLATFORM_ANDROID #include #include @@ -191,7 +192,10 @@ bool cWindowSDL::Create( WindowSettings Settings, ContextSettings Context ) { } #endif - mGLContext = SDL_GL_CreateContext( mSDLWindow ); + SDL_GL_SetAttribute( SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1 ); + + mGLContext = SDL_GL_CreateContext( mSDLWindow ); + mGLContextThread = SDL_GL_CreateContext( mSDLWindow ); if ( NULL == mGLContext ) { cLog::instance()->Write( "Unable to create context: " + std::string( SDL_GetError() ) ); @@ -252,6 +256,20 @@ bool cWindowSDL::Create( WindowSettings Settings, ContextSettings Context ) { return true; } +bool cWindowSDL::IsThreadedGLContext() { + return true; +} + +void cWindowSDL::SetGLContextThread() { + GLThreadMutexLock(); + SDL_GL_MakeCurrent( mSDLWindow, mGLContextThread ); +} + +void cWindowSDL::UnsetGLContextThread() { + SDL_GL_MakeCurrent( mSDLWindow, NULL ); + GLThreadMutexUnlock(); +} + std::string cWindowSDL::GetVersion() { SDL_version ver; diff --git a/src/eepp/window/backend/SDL2/cwindowsdl2.hpp b/src/eepp/window/backend/SDL2/cwindowsdl2.hpp index 8b2e7bb6a..9e2422dd1 100644 --- a/src/eepp/window/backend/SDL2/cwindowsdl2.hpp +++ b/src/eepp/window/backend/SDL2/cwindowsdl2.hpp @@ -87,11 +87,18 @@ class EE_API cWindowSDL : public cWindow { std::string GetApkPath(); #endif + + bool IsThreadedGLContext(); + + void SetGLContextThread(); + + void UnsetGLContextThread(); protected: friend class cClipboardSDL; SDL_Window * mSDLWindow; SDL_GLContext mGLContext; + SDL_GLContext mGLContextThread; #ifdef EE_USE_WMINFO SDL_SysWMinfo * mWMinfo; diff --git a/src/eepp/window/cengine.cpp b/src/eepp/window/cengine.cpp index 1fc07b248..d916a0481 100755 --- a/src/eepp/window/cengine.cpp +++ b/src/eepp/window/cengine.cpp @@ -45,7 +45,8 @@ SINGLETON_DECLARE_IMPLEMENTATION(cEngine) cEngine::cEngine() : mBackend( NULL ), - mWindow( NULL ) + mWindow( NULL ), + mSharedGLContext( false ) { cTextureAtlasManager::CreateSingleton(); } @@ -366,4 +367,16 @@ ContextSettings cEngine::CreateContextSettings( std::string iniPath, std::string return CreateContextSettings( &Ini ); } +void cEngine::EnableSharedGLContext() { + mSharedGLContext = true; +} + +void cEngine::DisableSharedGLContext() { + mSharedGLContext = false; +} + +bool cEngine::IsSharedGLContextEnabled() { + return mSharedGLContext; +} + }} diff --git a/src/eepp/window/cwindow.cpp b/src/eepp/window/cwindow.cpp index 807a36ab0..c3c7d3f9f 100644 --- a/src/eepp/window/cwindow.cpp +++ b/src/eepp/window/cwindow.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -522,6 +523,24 @@ bool cWindow::IsScreenKeyboardShown() { return false; } +bool cWindow::IsThreadedGLContext() { + return false; +} + +void cWindow::SetGLContextThread() { +} + +void cWindow::UnsetGLContextThread() { +} + +void cWindow::GLThreadMutexLock() { + cEngine::instance()->mGLThreadMutex.Lock(); +} + +void cWindow::GLThreadMutexUnlock() { + cEngine::instance()->mGLThreadMutex.Unlock(); +} + #if EE_PLATFORM == EE_PLATFORM_ANDROID void * cWindow::GetJNIEnv() { return NULL; diff --git a/src/test/eetest.cpp b/src/test/eetest.cpp index 954d532fa..1f829a167 100644 --- a/src/test/eetest.cpp +++ b/src/test/eetest.cpp @@ -1212,8 +1212,6 @@ void cEETest::Screen5() { } void cEETest::Render() { - mResLoad.Update(); - HWidth = mWindow->GetWidth() * 0.5f; HHeight = mWindow->GetHeight() * 0.5f; @@ -1576,6 +1574,8 @@ void cEETest::Process() { Input(); + mResLoad.Update(); + if ( mFontLoader.IsLoaded() ) { Render(); } else {