Browse Source

Minor convention and code clean up.

Yao Wei Tjong 姚伟忠 11 years ago
parent
commit
e076d037c6

+ 3 - 1
Source/Urho3D/Core/ProcessUtils.cpp

@@ -270,7 +270,7 @@ String GetConsoleInput()
     // When we are running automated tests, reading the console may block. Just return empty in that case
     return ret;
     #endif
-    
+
     #ifdef WIN32
     HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
     HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -348,6 +348,8 @@ String GetPlatform()
     return "Mac OS X";
     #elif defined(RPI)
     return "Raspberry Pi";
+    #elif defined(EMSCRIPTEN)
+    return "HTML5";
     #elif defined(__linux__)
     return "Linux";
     #else

+ 12 - 25
Source/Urho3D/Engine/Application.cpp

@@ -37,8 +37,11 @@
 namespace Urho3D
 {
 
-#ifdef IOS
-// Code for supporting SDL_iPhoneSetAnimationCallback
+#if defined(IOS) || defined(EMSCRIPTEN)
+// Code for supporting SDL_iPhoneSetAnimationCallback() and emscripten_set_main_loop_arg()
+#if defined(EMSCRIPTEN)
+#include <emscripten.h>
+#endif
 void RunFrame(void* data)
 {
     Application* instance = reinterpret_cast<Application*>(data);
@@ -46,19 +49,6 @@ void RunFrame(void* data)
 }
 #endif
 
-#if defined(EMSCRIPTEN)
-#include <emscripten.h>
-// REVISIT: it seems there should only be one instance of Application
-// and it's needed here as there's no way to pass it along with a callback
-// for emscripten_set_main_loop() so just store a static pointer here for now
-static Application *emInstance = 0;
-void RunFrame()
-{
-    if (emInstance)
-        emInstance->GetSubsystem<Engine>()->RunFrame();
-}
-#endif
-    
 Application::Application(Context* context) :
     Object(context),
     exitCode_(EXIT_SUCCESS)
@@ -67,7 +57,7 @@ Application::Application(Context* context) :
 
     // Create the Engine, but do not initialize it yet. Subsystems except Graphics & Renderer are registered at this point
     engine_ = new Engine(context);
-    
+
     // Subscribe to log messages so that can show errors if ErrorExit() is called with empty message
     SubscribeToEvent(E_LOGMESSAGE, HANDLER(Application, HandleLogMessage));
 }
@@ -81,7 +71,7 @@ int Application::Run()
         Setup();
         if (exitCode_)
             return exitCode_;
-		
+
         if (!engine_->Initialize(engineParameters_))
         {
             ErrorExit();
@@ -103,14 +93,11 @@ int Application::Run()
         #else
         #if defined(IOS)
         SDL_iPhoneSetAnimationCallback(GetSubsystem<Graphics>()->GetImpl()->GetWindow(), 1, &RunFrame, this);
-        #else
-        // EMSCRIPTEN
-        emInstance = this; 
-        emscripten_set_main_loop(RunFrame, 0, 1);
-	
+        #elif defined(EMSCRIPTEN)
+        emscripten_set_main_loop_arg(RunFrame, this, 0, 1);
         #endif
         #endif
-        
+
         return exitCode_;
 #if !defined(EMSCRIPTEN)
     }
@@ -142,7 +129,7 @@ void Application::ErrorExit(const String& message)
 void Application::HandleLogMessage(StringHash eventType, VariantMap& eventData)
 {
     using namespace LogMessage;
-    
+
     if (eventData[P_LEVEL].GetInt() == LOG_ERROR)
     {
         // Strip the timestamp if necessary
@@ -150,7 +137,7 @@ void Application::HandleLogMessage(StringHash eventType, VariantMap& eventData)
         unsigned bracketPos = error.Find(']');
         if (bracketPos != String::NPOS)
             error = error.Substring(bracketPos + 2);
-        
+
         startupErrors_ += error + "\n";
     }
 }

+ 6 - 6
Source/Urho3D/Engine/Engine.cpp

@@ -83,7 +83,7 @@ Engine::Engine(Context* context) :
     timeStep_(0.0f),
     timeStepSmoothing_(2),
     minFps_(10),
-#if defined(ANDROID) || defined(IOS) || defined(RPI) || defined(EMSCRIPTEN)
+    #if defined(ANDROID) || defined(IOS) || defined(RPI) || defined(EMSCRIPTEN)
     maxFps_(60),
     maxInactiveFps_(10),
     pauseMinimized_(true),
@@ -128,7 +128,7 @@ Engine::Engine(Context* context) :
 #ifdef URHO3D_PHYSICS
     RegisterPhysicsLibrary(context_);
 #endif
-    
+
 #ifdef URHO3D_NAVIGATION
     RegisterNavigationLibrary(context_);
 #endif
@@ -254,7 +254,7 @@ bool Engine::Initialize(const VariantMap& parameters)
         else
             LOGDEBUGF("Skip specified resource package '%s' as it does not exist, check the documentation on how to set the 'resource prefix path'", resourcePackages[i].CString());
     }
-    
+
     // Add auto load folders. Prioritize these (if exist) before the default folders
     for (unsigned i = 0; i < autoLoadPaths.Size(); ++i)
     {
@@ -343,7 +343,7 @@ bool Engine::Initialize(const VariantMap& parameters)
         renderer->SetTextureQuality(GetParameter(parameters, "TextureQuality", QUALITY_HIGH).GetInt());
         renderer->SetTextureFilterMode((TextureFilterMode)GetParameter(parameters, "TextureFilterMode", FILTER_TRILINEAR).GetInt());
         renderer->SetTextureAnisotropy(GetParameter(parameters, "TextureAnisotropy", 4).GetInt());
-        
+
         if (GetParameter(parameters, "Sound", true).GetBool())
         {
             GetSubsystem<Audio>()->SetMode(
@@ -523,12 +523,12 @@ void Engine::DumpResources(bool dumpFileName)
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     const HashMap<StringHash, ResourceGroup>& resourceGroups = cache->GetAllResources();
     LOGRAW("\n");
-    
+
     if (dumpFileName)
     {
         LOGRAW("Used resources:\n");
     }
-    
+
     for (HashMap<StringHash, ResourceGroup>::ConstIterator i = resourceGroups.Begin();
         i != resourceGroups.End(); ++i)
     {

+ 10 - 15
Source/Urho3D/Input/Input.cpp

@@ -43,6 +43,7 @@
 #ifdef EMSCRIPTEN
 #include <emscripten/html5.h>
 #endif
+
 #include "../DebugNew.h"
 
 extern "C" int SDL_AddTouch(SDL_TouchID touchID, const char *name);
@@ -85,9 +86,9 @@ UIElement* TouchState::GetTouchedElement()
  * Mouse Input:
  * - The OS mouse cursor position can't be set.
  * - The mouse can be trapped within play area via 'PointerLock API', which requires a request and interaction between the user and browser.
- *   - To request mouse lock, call SetMouseMode(MM_RELATIVE). The E_MOUSEMODECHANGED event will be sent if/when the user confirms the request.
- *   - The user can press 'escape' key and browser will force user out of pointer lock. Urho will send the E_MOUSEMODECHANGED event.
- *   - SetMouseMode(MM_ABSOLUTE) will leave pointer lock.
+ * - To request mouse lock, call SetMouseMode(MM_RELATIVE). The E_MOUSEMODECHANGED event will be sent if/when the user confirms the request.
+ * - The user can press 'escape' key and browser will force user out of pointer lock. Urho will send the E_MOUSEMODECHANGED event.
+ * - SetMouseMode(MM_ABSOLUTE) will leave pointer lock.
  * - MM_WRAP is unsupported.
  *
  * Touch Input:
@@ -109,22 +110,15 @@ public:
     /// Send request to user to gain pointer lock.
     void RequestPointerLock();
     void ExitPointerLock();
-
-private:
-    /// Instance of Input subsystem that constructed this instance.
-    Input* inputInst;
 };
 
 EmscriptenInput::EmscriptenInput(Input* inputInst)
 {
-    inputInst = inputInst;
-    emscripten_set_pointerlockchange_callback(
-        NULL, (void*)inputInst, false, EmscriptenInput::PointerLockCallback);
+    emscripten_set_pointerlockchange_callback(NULL, (void*)inputInst, false, EmscriptenInput::PointerLockCallback);
 
     // Handle focus changes:
     emscripten_set_blur_callback(NULL, (void*)inputInst, false, EmscriptenInput::LoseFocus);
     emscripten_set_focus_callback(NULL, (void*)inputInst, false, EmscriptenInput::GainFocus);
-
 }
 
 void EmscriptenInput::RequestPointerLock()
@@ -220,7 +214,7 @@ Input::Input(Context* context) :
     SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
 
     #ifdef EMSCRIPTEN
-    emscriptenInput = new EmscriptenInput(this);
+    emscriptenInput_ = new EmscriptenInput(this);
     #endif
 
     // Try to initialize right now, but skip if screen mode is not yet set
@@ -230,7 +224,8 @@ Input::Input(Context* context) :
 Input::~Input()
 {
     #ifdef EMSCRIPTEN
-    delete emscriptenInput;
+    delete emscriptenInput_;
+    emscriptenInput_ = 0;
     #endif
 }
 
@@ -518,7 +513,7 @@ void Input::SetMouseMode(MouseMode mode)
             ResetMouseVisible();
 
             #ifdef EMSCRIPTEN
-            emscriptenInput->ExitPointerLock();
+            emscriptenInput_->ExitPointerLock();
             #endif
 
             SDL_SetWindowGrab(window, SDL_FALSE);
@@ -558,7 +553,7 @@ void Input::SetMouseMode(MouseMode mode)
                 #else
                 // Defer mouse mode change to PointerLock callback
                 mouseMode_ = previousMode;
-                emscriptenInput->RequestPointerLock();
+                emscriptenInput_->RequestPointerLock();
                 #endif
 
             }

+ 1 - 1
Source/Urho3D/Input/Input.h

@@ -370,7 +370,7 @@ private:
     bool initialized_;
 #ifdef EMSCRIPTEN
     /// Emscripten Input glue instance
-    EmscriptenInput* emscriptenInput;
+    EmscriptenInput* emscriptenInput_;
 #endif
 };