Forráskód Böngészése

WebGL up and running

Josh Engebretson 9 éve
szülő
commit
028bd49732

+ 16 - 2
Build/CMake/Modules/AtomicWeb.cmake

@@ -4,8 +4,22 @@ include(AtomicCommon)
 set (JAVASCRIPT_BINDINGS_PLATFORM "WEB")
 
 add_definitions(-DATOMIC_PLATFORM_WEB)
-add_definitions(-DATOMIC_OPENGL -Wno-warn-absolute-paths -DATOMIC_TBUI)
+add_definitions(-DATOMIC_OPENGL -Wno-warn-absolute-paths -DATOMIC_TBUI -DNO_POPEN)
+
+set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-warn-absolute-paths -Wno-unknown-warning-option")
+set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof -std=gnu++0x -Wno-warn-absolute-paths -Wno-unknown-warning-option")
+
+set (CMAKE_EXECUTABLE_SUFFIX_C .html)
+set (CMAKE_EXECUTABLE_SUFFIX_CXX .html)
+
+# Linker flags
+set (MEMORY_LINKER_FLAGS "-s ALLOW_MEMORY_GROWTH=1")
+#set (MEMORY_LINKER_FLAGS "-s TOTAL_MEMORY=${EMSCRIPTEN_TOTAL_MEMORY}")
+
+set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MEMORY_LINKER_FLAGS} -s NO_EXIT_RUNTIME=1")
+set (CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -O3 -s AGGRESSIVE_VARIABLE_ELIMINATION=1")     # Remove variables to make the -O3 regalloc easier
+set (CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g4")     # Preserve LLVM debug information, show line number debug comments, and generate source maps
+
 
-set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof -std=gnu++0x")
 
 set (ATOMIC_LINK_LIBRARIES ${ATOMIC_LINK_LIBRARIES})

+ 66 - 40
Build/CMake/Toolchains/emscripten.toolchain.cmake

@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2008-2015 the Urho3D project.
+# Copyright (c) 2008-2016 the Urho3D project.
 #
 # Permission is hereby granted, free of charge, to any person obtaining a copy
 # of this software and associated documentation files (the "Software"), to deal
@@ -20,9 +20,9 @@
 # THE SOFTWARE.
 #
 
-# Based on cmake/Modules/Platform/Emscripten.cmake from https://github.com/kripken/emscripten
-
-cmake_minimum_required (VERSION 2.6.3)
+if (DEFINED CMAKE_CROSSCOMPILING)
+    return ()
+endif ()
 
 if (CMAKE_TOOLCHAIN_FILE)
     # Reference toolchain variable to suppress "unused variable" warning
@@ -35,20 +35,59 @@ set (CMAKE_SYSTEM_NAME Linux)
 set (CMAKE_SYSTEM_VERSION 1)
 
 # specify the cross compiler
-if (NOT EMSCRIPTEN_ROOT_PATH AND DEFINED ENV{EMSCRIPTEN})
-    file (TO_CMAKE_PATH $ENV{EMSCRIPTEN} EMSCRIPTEN_ROOT_PATH)
+if (CMAKE_HOST_WIN32)
+    set (TOOL_EXT .bat)
 endif ()
-if (NOT EXISTS ${EMSCRIPTEN_ROOT_PATH}/emcc)
+if (NOT EMSCRIPTEN_ROOT_PATH)
+    if (DEFINED ENV{EMSCRIPTEN_ROOT_PATH})
+        file (TO_CMAKE_PATH $ENV{EMSCRIPTEN_ROOT_PATH} EMSCRIPTEN_ROOT_PATH)
+    elseif (DEFINED ENV{EMSCRIPTEN})
+        file (TO_CMAKE_PATH $ENV{EMSCRIPTEN} EMSCRIPTEN_ROOT_PATH)
+    endif ()
+endif ()
+if (NOT EXISTS ${EMSCRIPTEN_ROOT_PATH}/emcc${TOOL_EXT})
     message (FATAL_ERROR "Could not find Emscripten cross compilation tool. "
-        "Use EMSCRIPTEN_ROOT_PATH environment variable or build option to specify the location of the toolchain.")
+        "Use EMSCRIPTEN_ROOT_PATH environment variable or build option to specify the location of the toolchain. "
+        "Or use the canonical EMSCRIPTEN environment variable by calling emsdk_env script.")
 endif ()
-if (CMAKE_HOST_WIN32)
-    set (TOOL_EXT .bat)
+if (NOT EMCC_VERSION)
+    execute_process (COMMAND ${EMSCRIPTEN_ROOT_PATH}/emcc${TOOL_EXT} --version RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE EMCC_VERSION ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
+    if (EXIT_CODE EQUAL 0)
+        string (REGEX MATCH "[^ .]+\\.[^.]+\\.[^ ]+" EMCC_VERSION "${EMCC_VERSION}")    # Stringify as it could be empty when an error has occured
+    else ()
+        message (FATAL_ERROR "Could not determine the emcc version. Make sure you have installed and activated the Emscripten SDK correctly.")
+    endif ()
+    set (EMCC_VERSION ${EMCC_VERSION} CACHE STRING "emcc version being used in this build tree")    # Cache the result even when there was error in determining the version
+endif ()
+set (COMPILER_PATH ${EMSCRIPTEN_ROOT_PATH})
+# ccache support could only be enabled for emcc prior to 1.31.3 when the CCACHE_CPP2 env var is also set to 1, newer emcc version could enable ccache support without this caveat (see https://github.com/kripken/emscripten/issues/3365 for more detail)
+# The CCACHE_CPP2 env var tells ccache to fallback to use original input source file instead of preprocessed one when passing on the compilation task to the compiler proper
+if (NOT CMAKE_C_COMPILER AND "$ENV{USE_CCACHE}" AND NOT CMAKE_HOST_WIN32 AND ("$ENV{CCACHE_CPP2}" OR NOT EMCC_VERSION VERSION_LESS 1.31.3))
+    if (NOT $ENV{PATH} MATCHES ${EMSCRIPTEN_ROOT_PATH})
+        message (FATAL_ERROR "The bin directory containing the compiler toolchain (${EMSCRIPTEN_ROOT_PATH}) has not been added in the PATH environment variable. "
+            "This is required to enable ccache support for Emscripten compiler toolchain.")
+    endif ()
+    execute_process (COMMAND which ccache RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE CCACHE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
+    if (EXIT_CODE EQUAL 0)
+        foreach (name emcc em++)
+            execute_process (COMMAND ${CMAKE_COMMAND} -E create_symlink ${CCACHE} ${CMAKE_BINARY_DIR}/${name})
+        endforeach ()
+        set (COMPILER_PATH ${CMAKE_BINARY_DIR})
+    else ()
+        message (WARNING "ccache may not have been installed on this host system. "
+            "This is required to enable ccache support for Emscripten compiler toolchain. CMake has been configured to use the actual compiler toolchain instead of ccache. "
+            "In order to rectify this, the build tree must be regenerated after installing ccache.")
+    endif ()
 endif ()
-set (CMAKE_C_COMPILER   ${EMSCRIPTEN_ROOT_PATH}/emcc${TOOL_EXT}     CACHE PATH "C compiler")
-set (CMAKE_CXX_COMPILER ${EMSCRIPTEN_ROOT_PATH}/em++${TOOL_EXT}     CACHE PATH "C++ compiler")
+set (CMAKE_C_COMPILER   ${COMPILER_PATH}/emcc${TOOL_EXT}            CACHE PATH "C compiler")
+set (CMAKE_CXX_COMPILER ${COMPILER_PATH}/em++${TOOL_EXT}            CACHE PATH "C++ compiler")
 set (CMAKE_AR           ${EMSCRIPTEN_ROOT_PATH}/emar${TOOL_EXT}     CACHE PATH "archive")
 set (CMAKE_RANLIB       ${EMSCRIPTEN_ROOT_PATH}/emranlib${TOOL_EXT} CACHE PATH "ranlib")
+set (CMAKE_LINKER       ${EMSCRIPTEN_ROOT_PATH}/emlink.py           CACHE PATH "linker")
+# Specific to Emscripten
+set (EMRUN              ${EMSCRIPTEN_ROOT_PATH}/emrun${TOOL_EXT}    CACHE PATH "emrun")
+set (EMPACKAGER         python ${EMSCRIPTEN_ROOT_PATH}/tools/file_packager.py CACHE PATH "file_packager.py")
+set (EMBUILDER          python ${EMSCRIPTEN_ROOT_PATH}/embuilder.py CACHE PATH "embuilder.py")
 
 # specify the system root
 if (NOT EMSCRIPTEN_SYSROOT)
@@ -71,35 +110,21 @@ set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
 set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
 set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
 
-# Don't do compiler autodetection, since we are cross-compiling.
-include (CMakeForceCompiler)
-CMAKE_FORCE_C_COMPILER ("${CMAKE_C_COMPILER}" Clang)
-CMAKE_FORCE_CXX_COMPILER ("${CMAKE_CXX_COMPILER}" Clang)
-
-# Hardwire support for cmake-2.8/Modules/CMakeBackwardsCompatibilityC.cmake without having CMake to try complex things
-# to autodetect these:
-set (CMAKE_SKIP_COMPATIBILITY_TESTS 1)
-set (CMAKE_SIZEOF_CHAR 1)
-set (CMAKE_SIZEOF_UNSIGNED_SHORT 2)
-set (CMAKE_SIZEOF_SHORT 2)
-set (CMAKE_SIZEOF_INT 4)
-set (CMAKE_SIZEOF_UNSIGNED_LONG 4)
-set (CMAKE_SIZEOF_UNSIGNED_INT 4)
-set (CMAKE_SIZEOF_LONG 4)
-set (CMAKE_SIZEOF_VOID_P 4)
-set (CMAKE_SIZEOF_FLOAT 4)
-set (CMAKE_SIZEOF_DOUBLE 8)
-set (CMAKE_C_SIZEOF_DATA_PTR 4)
-set (CMAKE_CXX_SIZEOF_DATA_PTR 4)
-set (CMAKE_HAVE_LIMITS_H 1)
-set (CMAKE_HAVE_UNISTD_H 1)
-set (CMAKE_HAVE_PTHREAD_H 1)
-set (CMAKE_HAVE_SYS_PRCTL_H 1)
-set (CMAKE_WORDS_BIGENDIAN 0)
-set (CMAKE_DL_LIBS)
+# Still perform the compiler checks except for those stated otherwise below
+foreach (LANG C CXX)
+    # Since currently CMake does not able to identify Emscripten compiler toolchain, set the compiler identification explicitly
+    set (CMAKE_${LANG}_COMPILER_ID_RUN TRUE)
+    set (CMAKE_${LANG}_COMPILER_ID Clang)
+    # The ABI info could not be checked as per normal as CMake does not understand the test build output from Emscripten, so bypass it also
+    set (CMAKE_${LANG}_ABI_COMPILED TRUE)
+    set (CMAKE_${LANG}_SIZEOF_DATA_PTR 4)   # Assume it is always 32-bit for now (we could have used our CheckCompilerToolChains.cmake module here)
+    # There could be a bug in CMake itself where setting CMAKE_EXECUTABLE_SUFFIX variable outside of the scope, where it processes the platform configuration files, does not being honored by try_compile() command and as a result all the check macros that depend on try_compile() do not work properly when the CMAKE_EXECUTABLE_SUFFIX variable is only being set later futher down the road; At least one of the CMake devs has the opinion that this is the intended behavior but it is an unconvincing explanation because setting CMAKE_EXECUTABLE_SUFFIX variable later does have the desired effect everywhere else EXCEPT the try_compile() command
+    # We are forced to set CMAKE_EXECUTABLE_SUFFIX_C and CMAKE_EXECUTABLE_SUFFIX_CXX here as a workaround; we could not just set CMAKE_EXECUTABLE_SUFFIX directly because CMake processes platform configuration files after the toolchain file and since we tell CMake that we are cross-compiling for 'Linux' platform via CMAKE_SYSTEM_NAME variable, CMake initializes the CMAKE_EXECUTABLE_SUFFIX to empty string (as expected for Linux platform); the workaround avoids our setting from being overwritten by platform configuration files by using the C and CXX language variants of the variable
+    # The executable suffix needs to be .js for the below Emscripten-specific compiler setting to be effective
+    set (CMAKE_EXECUTABLE_SUFFIX_${LANG} .js)
+endforeach ()
 
-# In order for check_function_exists() detection to work, we must signal it to pass an additional flag, which causes the compilation
-# to abort if linking results in any undefined symbols. The CMake detection mechanism depends on the undefined symbol error to be raised.
+# Set required compiler flags for internal CMake various check_xxx() macros which rely on the error to occur for the check to be performed correctly
 set (CMAKE_REQUIRED_FLAGS "-s ERROR_ON_UNDEFINED_SYMBOLS=1")
 
 # Use response files on Windows host
@@ -112,4 +137,5 @@ if (CMAKE_HOST_WIN32)
     endforeach ()
 endif ()
 
+set (WEB 1)
 set (EMSCRIPTEN 1)

+ 4 - 4
CMakeLists.txt

@@ -9,7 +9,7 @@ include(AtomicUtils)
 
 add_definitions(-DATOMIC_ROOT_SOURCE_DIR="${CMAKE_SOURCE_DIR}" -DATOMIC_ROOT_BUILD_DIR="${CMAKE_BINARY_DIR}")
 
-add_definitions( -DATOMIC_API= -DATOMIC_STATIC_DEFINE -DATOMIC_ATOMIC2D -DATOMIC_LOGGING -DATOMIC_PROFILING -DATOMIC_THREADING)
+add_definitions( -DATOMIC_API= -DATOMIC_STATIC_DEFINE -DATOMIC_ATOMIC2D -DATOMIC_LOGGING -DATOMIC_PROFILING)
 
 if (NOT DEFINED ATOMIC_DEV_BUILD)
     set(ATOMIC_DEV_BUILD 1)
@@ -25,7 +25,7 @@ endif()
 # this is here as QtCreator is having trouble picking up #include <Atomic/*> without it
 include_directories(${CMAKE_SOURCE_DIR}/Source ${CMAKE_SOURCE_DIR}/Source/AtomicEditor/Source)
 
-set (ATOMIC_LINK_LIBRARIES Atomic Box2D Duktape TurboBadger FreeType JO LZ4 PugiXml STB)
+set (ATOMIC_LINK_LIBRARIES Atomic Box2D Duktape TurboBadger FreeType JO LZ4 PugiXml STB SDL)
 
 if (NOT ATOMIC_BUILD_2D)
     add_definitions( -DATOMIC_PHYSICS -DATOMIC_3D)
@@ -33,8 +33,8 @@ if (NOT ATOMIC_BUILD_2D)
 endif()
 
 if (NOT EMSCRIPTEN)
-    add_definitions( -DATOMIC_NETWORK)
-    set (ATOMIC_LINK_LIBRARIES ${ATOMIC_LINK_LIBRARIES} SDL Civetweb Recast Detour DetourCrowd DetourTileCache kNet libcurl )
+    add_definitions( -DATOMIC_THREADING -DATOMIC_NETWORK)
+    set (ATOMIC_LINK_LIBRARIES ${ATOMIC_LINK_LIBRARIES} Civetweb Recast Detour DetourCrowd DetourTileCache kNet libcurl )
 endif()
 
 add_definitions( -DATOMIC_WEB )

+ 5 - 11
Source/Atomic/Input/Input.cpp

@@ -696,9 +696,7 @@ void Input::SetMouseModeEmscriptenFinal(MouseMode mode, bool suppressEvent)
             SetMouseVisibleEmscripten(true, suppressEvent);
         }
 
-        UI* const ui = GetSubsystem<UI>();
-        Cursor* const cursor = ui->GetCursor();
-        SetMouseGrabbed(!(mouseVisible_ || (cursor && cursor->IsVisible())), suppressEvent);
+        SetMouseGrabbed(!mouseVisible_, suppressEvent);
     }
     else if (mode == MM_RELATIVE && emscriptenPointerLock_)
     {
@@ -729,9 +727,6 @@ void Input::SetMouseModeEmscripten(MouseMode mode, bool suppressEvent)
     const MouseMode previousMode = mouseMode_;
     mouseMode_ = mode;
 
-    UI* const ui = GetSubsystem<UI>();
-    Cursor* const cursor = ui->GetCursor();
-
     // Handle changing from previous mode
     if (previousMode == MM_RELATIVE)
         ResetMouseVisible();
@@ -741,7 +736,7 @@ void Input::SetMouseModeEmscripten(MouseMode mode, bool suppressEvent)
     {
         // Attempt to cancel pending pointer-lock requests
         emscriptenInput_->ExitPointerLock(suppressEvent);
-        SetMouseGrabbed(!(mouseVisible_ || (cursor && cursor->IsVisible())), suppressEvent);
+        SetMouseGrabbed(!mouseVisible_, suppressEvent);
     }
     else if (mode == MM_ABSOLUTE)
     {
@@ -753,14 +748,13 @@ void Input::SetMouseModeEmscripten(MouseMode mode, bool suppressEvent)
             }
             else
             {
-                if (!cursor)
-                    SetMouseVisible(true, suppressEvent);
+                SetMouseVisible(true, suppressEvent);
                 // Deferred mouse mode change to pointer-lock callback
                 mouseMode_ = previousMode;
                 emscriptenInput_->RequestPointerLock(MM_ABSOLUTE, suppressEvent);
             }
 
-            SetMouseGrabbed(!(mouseVisible_ || (cursor && cursor->IsVisible())), suppressEvent);
+            SetMouseGrabbed(!(mouseVisible_), suppressEvent);
         }
     }
     else if (mode == MM_RELATIVE)
@@ -768,7 +762,7 @@ void Input::SetMouseModeEmscripten(MouseMode mode, bool suppressEvent)
         if (emscriptenPointerLock_)
         {
             SetMouseVisibleEmscripten(false, true);
-            SetMouseGrabbed(!(cursor && cursor->IsVisible()), suppressEvent);
+            SetMouseGrabbed(true, suppressEvent);
         }
         else
         {

+ 2 - 1
Source/ThirdParty/CMakeLists.txt

@@ -14,8 +14,9 @@ if (NOT ATOMIC_BUILD_2D)
     add_subdirectory(StanHull)
 endif()
 
+add_subdirectory(SDL)
+
 if (NOT EMSCRIPTEN)
-    add_subdirectory(SDL)
     add_subdirectory(Recast)
     add_subdirectory(Civetweb)
     add_subdirectory(Detour)