diff --git a/cmake/Modules/AXBuildHelpers.cmake b/cmake/Modules/AXBuildHelpers.cmake index c3a2acb7a9..6ef8a263ed 100644 --- a/cmake/Modules/AXBuildHelpers.cmake +++ b/cmake/Modules/AXBuildHelpers.cmake @@ -531,9 +531,9 @@ set(AX_WASM_SHELL_FILE "${_AX_ROOT}/core/platform/wasm/shell_minimal.html" CACHE option(AX_WASM_ENABLE_DEVTOOLS "Enable wasm devtools" ON) -set(_AX_WASM_EXPORTS "_main") +set(_AX_WASM_EXPORTS "_main,_axmol_wgl_context_lost,_axmol_wgl_context_restored,_axmol_hdoc_visibilitychange") if(AX_WASM_ENABLE_DEVTOOLS) - set(_AX_WASM_EXPORTS "${_AX_WASM_EXPORTS},_axmol_director_pause,_axmol_director_resume,_axmol_director_step") + string(APPEND _AX_WASM_EXPORTS ",_axmol_dev_pause,_axmol_dev_resume,_axmol_dev_step") endif() set(AX_WASM_EXPORTS "${_AX_WASM_EXPORTS}" CACHE STRING "" FORCE) diff --git a/core/2d/RenderTexture.cpp b/core/2d/RenderTexture.cpp index b80701d8de..eec57afcbc 100644 --- a/core/2d/RenderTexture.cpp +++ b/core/2d/RenderTexture.cpp @@ -95,13 +95,6 @@ void RenderTexture::listenToBackground(EventCustom* /*event*/) void RenderTexture::listenToForeground(EventCustom* /*event*/) { #if AX_ENABLE_CACHE_TEXTURE_DATA - const Vec2& s = _texture2D->getContentSizeInPixels(); - // TODO new-renderer: field _depthAndStencilFormat removal - // if (_depthAndStencilFormat != 0) - // { - // setupDepthAndStencil(s.width, s.height); - // } - _texture2D->setAntiAliasTexParameters(); #endif } diff --git a/core/base/EventType.h b/core/base/EventType.h index bf1e8a6f0e..54fce25af9 100644 --- a/core/base/EventType.h +++ b/core/base/EventType.h @@ -23,8 +23,8 @@ THE SOFTWARE. ****************************************************************************/ -#ifndef __CCEVENT_TYPE_H__ -#define __CCEVENT_TYPE_H__ +#ifndef __AXEVENT_TYPE_H__ +#define __AXEVENT_TYPE_H__ /// @cond DO_NOT_SHOW /** diff --git a/core/base/Random.cpp b/core/base/Random.cpp index 6fc3420eca..3eb4f41769 100644 --- a/core/base/Random.cpp +++ b/core/base/Random.cpp @@ -27,11 +27,12 @@ THE SOFTWARE. #include "base/Random.h" #ifdef EMSCRIPTEN -#include -#include +# include +# include std::mt19937& ax::RandomHelper::getEngine() { - static std::mt19937 engine(emscripten_random() * (std::numeric_limits::max)()); + static std::mt19937 engine( + static_cast(emscripten_random() * static_cast((std::numeric_limits::max)()))); return engine; } #else diff --git a/core/network/Downloader-wasm.cpp b/core/network/Downloader-wasm.cpp index f281ff470d..415de0a6a4 100644 --- a/core/network/Downloader-wasm.cpp +++ b/core/network/Downloader-wasm.cpp @@ -172,7 +172,7 @@ namespace ax { namespace network { dir = storagePath.substr(0, found + 1); if (false == util->isDirectoryExist(dir)) { - if (false == util->createDirectory(dir)) + if (false == util->createDirectories(dir)) { errCode = DownloadTask::ERROR_CREATE_DIR_FAILED; errCodeInternal = 0; diff --git a/core/platform/PlatformMacros.h b/core/platform/PlatformMacros.h index e1b615f66a..8b628579e4 100644 --- a/core/platform/PlatformMacros.h +++ b/core/platform/PlatformMacros.h @@ -88,7 +88,7 @@ Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md). * * @since v0.99.5 */ -#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID) +#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID) || (AX_TARGET_PLATFORM == AX_PLATFORM_WASM) # if !defined(AX_ENABLE_CACHE_TEXTURE_DATA) # define AX_ENABLE_CACHE_TEXTURE_DATA 1 # endif diff --git a/core/platform/android/javaactivity-android.cpp b/core/platform/android/javaactivity-android.cpp index 1fd76a87dc..be598ffa98 100644 --- a/core/platform/android/javaactivity-android.cpp +++ b/core/platform/android/javaactivity-android.cpp @@ -88,7 +88,7 @@ JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeInit(JNIEnv*, jcla auto glView = director->getGLView(); if (!glView) { - glView = ax::GLViewImpl::create("Android app"); + glView = ax::GLViewImpl::create("axmol2"); glView->setFrameSize(w, h); director->setGLView(glView); @@ -97,7 +97,7 @@ JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeInit(JNIEnv*, jcla else { backend::DriverBase::getInstance()->resetState(); - ax::Director::getInstance()->resetMatrixStack(); + director->resetMatrixStack(); ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED); director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true); director->setGLDefaultValues(); diff --git a/core/platform/wasm/Application-wasm.cpp b/core/platform/wasm/Application-wasm.cpp index 4c373a9cc8..0a746fae71 100644 --- a/core/platform/wasm/Application-wasm.cpp +++ b/core/platform/wasm/Application-wasm.cpp @@ -28,35 +28,71 @@ THE SOFTWARE. #include "platform/PlatformConfig.h" #if AX_TARGET_PLATFORM == AX_PLATFORM_WASM -#include "platform/wasm/Application-wasm.h" -#include -#include -#include -#include "base/Director.h" -#include "base/Utils.h" -#include "platform/FileUtils.h" -#include +# include "platform/wasm/Application-wasm.h" +# include "platform/wasm/devtools-wasm.h" +# include +# include +# include +# include "base/Director.h" +# include "base/Utils.h" +# include "platform/FileUtils.h" +# include + +extern "C" { +// +void axmol_hdoc_visibilitychange(bool hidden) +{ + ax::EventCustom event(hidden ? EVENT_COME_TO_BACKGROUND : EVENT_COME_TO_FOREGROUND); + ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event, true); +} + +// webglcontextlost +void axmol_wgl_context_lost() +{ + AXLOGI("receive event: webglcontextlost"); +} + +// webglcontextrestored +void axmol_wgl_context_restored() +{ + AXLOGI("receive event: webglcontextrestored"); + + auto director = ax::Director::getInstance(); + ax::backend::DriverBase::getInstance()->resetState(); + director->resetMatrixStack(); + ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED); + director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true); + director->setGLDefaultValues(); +# if AX_ENABLE_CACHE_TEXTURE_DATA + ax::VolatileTextureMgr::reloadAllTextures(); +# endif +} + +void axmol_dev_pause() +{ + ax::DevToolsImpl::getInstance()->pause(); +} + +void axmol_dev_resume() +{ + ax::DevToolsImpl::getInstance()->resume(); +} + +void axmol_dev_step() +{ + ax::DevToolsImpl::getInstance()->step(); +} +} namespace ax { - // sharedApplication pointer -Application * Application::sm_pSharedApplication = nullptr; +Application* Application::sm_pSharedApplication = nullptr; -static long getCurrentMillSecond() { - long lLastTime; - struct timeval stCurrentTime; - - gettimeofday(&stCurrentTime,NULL); - lLastTime = stCurrentTime.tv_sec*1000+stCurrentTime.tv_usec*0.001; // milliseconds - return lLastTime; -} - -Application::Application() -: _animationSpeed(60) +Application::Application() : _animationSpeed(60) { - AX_ASSERT(! sm_pSharedApplication); + AX_ASSERT(!sm_pSharedApplication); sm_pSharedApplication = this; } @@ -69,7 +105,7 @@ Application::~Application() extern "C" void mainLoopIter(void) { auto director = Director::getInstance(); - auto glview = director->getGLView(); + auto glview = director->getGLView(); director->mainLoop(); glview->pollEvents(); @@ -79,25 +115,25 @@ int Application::run() { initGLContextAttrs(); // Initialize instance and cocos2d. - if (! applicationDidFinishLaunching()) + if (!applicationDidFinishLaunching()) { return 1; } auto director = Director::getInstance(); - auto glview = director->getGLView(); + auto glview = director->getGLView(); // Retain glview to avoid glview being released in the while loop glview->retain(); - //emscripten_set_main_loop(&mainLoopIter, 0, 1); + // emscripten_set_main_loop(&mainLoopIter, 0, 1); emscripten_set_main_loop(&mainLoopIter, _animationSpeed, 1); // TODO: ? does these cleanup really run? /* Only work on Desktop - * Director::mainLoop is really one frame logic - * when we want to close the window, we should call Director::end(); - * then call Director::mainLoop to do release of internal resources - */ + * Director::mainLoop is really one frame logic + * when we want to close the window, we should call Director::end(); + * then call Director::mainLoop to do release of internal resources + */ if (glview->isOpenGLReady()) { director->end(); @@ -120,7 +156,7 @@ void Application::setResourceRootPath(const std::string& rootResDir) { _resourceRootPath += '/'; } - FileUtils* pFileUtils = FileUtils::getInstance(); + FileUtils* pFileUtils = FileUtils::getInstance(); std::vector searchPaths = pFileUtils->getSearchPaths(); searchPaths.insert(searchPaths.begin(), _resourceRootPath); pFileUtils->setSearchPaths(searchPaths); @@ -143,9 +179,7 @@ std::string Application::getVersion() bool Application::openURL(std::string_view url) { - EM_ASM_ARGS({ - window.open(UTF8ToString($0)); - }, url.data()); + EM_ASM_ARGS({ window.open(UTF8ToString($0)); }, url.data()); return true; } @@ -165,21 +199,26 @@ Application* Application::sharedApplication() return Application::getInstance(); } -const char * Application::getCurrentLanguageCode() +const char* Application::getCurrentLanguageCode() { - static char code[3]={0}; + static char code[3] = {0}; char pLanguageName[16]; - EM_ASM_ARGS({ - var lang = localStorage.getItem('localization_language'); - if (lang == null) { - stringToUTF8(window.navigator.language.replace(/-.*/, ""), $0, 16); - } else { - stringToUTF8(lang, $0, 16); - } - }, pLanguageName); - strncpy(code,pLanguageName,2); - code[2]='\0'; + EM_ASM_ARGS( + { + var lang = localStorage.getItem('localization_language'); + if (lang == null) + { + stringToUTF8(window.navigator.language.replace(/ - .*/, ""), $0, 16); + } + else + { + stringToUTF8(lang, $0, 16); + } + }, + pLanguageName); + strncpy(code, pLanguageName, 2); + code[2] = '\0'; return code; } @@ -187,19 +226,23 @@ LanguageType Application::getCurrentLanguage() { char pLanguageName[16]; - EM_ASM_ARGS({ - var lang = localStorage.getItem('localization_language'); - if (lang == null) { - stringToUTF8(window.navigator.language.replace(/-.*/, ""), $0, 16); - } else { - stringToUTF8(lang, $0, 16); - } - }, pLanguageName); + EM_ASM_ARGS( + { + var lang = localStorage.getItem('localization_language'); + if (lang == null) + { + stringToUTF8(window.navigator.language.replace(/ - .*/, ""), $0, 16); + } + else + { + stringToUTF8(lang, $0, 16); + } + }, + pLanguageName); return utils::getLanguageTypeByISO2(pLanguageName); } -} - -#endif // AX_TARGET_PLATFORM == AX_PLATFORM_WASM +} // namespace ax +#endif // AX_TARGET_PLATFORM == AX_PLATFORM_WASM diff --git a/core/platform/wasm/devtools-wasm.cpp b/core/platform/wasm/devtools-wasm.cpp index 33a0c6bd3d..d476d80d38 100644 --- a/core/platform/wasm/devtools-wasm.cpp +++ b/core/platform/wasm/devtools-wasm.cpp @@ -78,22 +78,4 @@ DevToolsImpl* DevToolsImpl::getInstance() } -extern "C" -{ - void axmol_director_pause() - { - ax::DevToolsImpl::getInstance()->pause(); - } - - void axmol_director_resume() - { - ax::DevToolsImpl::getInstance()->resume(); - } - - void axmol_director_step() - { - ax::DevToolsImpl::getInstance()->step(); - } -} - #endif // AX_TARGET_PLATFORM == AX_PLATFORM_WASM diff --git a/core/platform/wasm/shell_minimal.html b/core/platform/wasm/shell_minimal.html index 569d6d2d72..47e9521526 100644 --- a/core/platform/wasm/shell_minimal.html +++ b/core/platform/wasm/shell_minimal.html @@ -46,9 +46,9 @@ | - - - + + +
@@ -140,7 +140,10 @@ // As a default initial behavior, pop up an alert when webgl context is lost. To make your // application robust, you may want to override this behavior before shipping! // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2 - canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false); + canvas.addEventListener("webglcontextlost", function(e) { Module.ccall('axmol_wgl_context_lost'); alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false); + canvas.addEventListener("webglcontextrestored", function(e) { Module.ccall('axmol_wgl_context_restored'); e.preventDefault(); }, false); + + document.addEventListener("visibilitychange", () => { Module.ccall('axmol_hdoc_visibilitychange', document.hidden); }); return canvas; })(), diff --git a/core/platform/winrt/xaml/AxmolRenderer.cpp b/core/platform/winrt/xaml/AxmolRenderer.cpp index 3de5048e7a..878809050a 100644 --- a/core/platform/winrt/xaml/AxmolRenderer.cpp +++ b/core/platform/winrt/xaml/AxmolRenderer.cpp @@ -97,15 +97,14 @@ void AxmolRenderer::DeviceLost() auto director = ax::Director::getInstance(); if (director->getGLView()) { - // TODO: - // ax::GL::invalidateStateCache(); - // ax::GLProgramCache::getInstance()->reloadDefaultGLPrograms(); - // ax::DrawPrimitives::init(); - // ax::VolatileTextureMgr::reloadAllTextures(); - + backend::DriverBase::getInstance()->resetState(); + ax::Director::getInstance()->resetMatrixStack(); ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED); director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true); director->setGLDefaultValues(); +#if AX_ENABLE_CACHE_TEXTURE_DATA + ax::VolatileTextureMgr::reloadAllTextures(); +#endif Application::getInstance()->applicationWillEnterForeground(); ax::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);