Browse Source

In Application class, use SDL_iPhoneSetAnimationCallback on iOS instead of running a blocking main loop. Closes #381.

Lasse Öörni 11 years ago
parent
commit
f3b537d11b
2 changed files with 34 additions and 2 deletions
  1. 21 1
      Source/Engine/Engine/Application.cpp
  2. 13 1
      Source/Engine/Engine/Application.h

+ 21 - 1
Source/Engine/Engine/Application.cpp

@@ -23,6 +23,10 @@
 #include "Precompiled.h"
 #include "Precompiled.h"
 #include "Application.h"
 #include "Application.h"
 #include "Engine.h"
 #include "Engine.h"
+#ifdef IOS
+#include "Graphics.h"
+#include "GraphicsImpl.h"
+#endif
 #include "IOEvents.h"
 #include "IOEvents.h"
 #include "Log.h"
 #include "Log.h"
 #include "ProcessUtils.h"
 #include "ProcessUtils.h"
@@ -34,6 +38,15 @@
 namespace Urho3D
 namespace Urho3D
 {
 {
 
 
+#ifdef IOS
+// Code for supporting SDL_iPhoneSetAnimationCallback
+void RunFrame(void* data)
+{
+    Application* instance = reinterpret_cast<Application*>(data);
+    instance->GetSubsystem<Engine>()->RunFrame();
+}
+#endif
+    
 Application::Application(Context* context) :
 Application::Application(Context* context) :
     Object(context),
     Object(context),
     exitCode_(EXIT_SUCCESS)
     exitCode_(EXIT_SUCCESS)
@@ -65,11 +78,18 @@ int Application::Run()
         if (exitCode_)
         if (exitCode_)
             return exitCode_;
             return exitCode_;
 
 
+        // Platforms other than iOS run a blocking main loop
+        #ifndef IOS
         while (!engine_->IsExiting())
         while (!engine_->IsExiting())
             engine_->RunFrame();
             engine_->RunFrame();
 
 
         Stop();
         Stop();
-
+        // iOS will setup a timer for running animation frames so eg. Game Center can run. In this case we do not
+        // support calling the Stop() function, as the application will never stop manually
+        #else
+        SDL_iPhoneSetAnimationCallback(GetSubsystem<Graphics>()->GetImpl()->GetWindow(), 1, &RunFrame, this);
+        #endif
+        
         return exitCode_;
         return exitCode_;
     }
     }
     catch (std::bad_alloc&)
     catch (std::bad_alloc&)

+ 13 - 1
Source/Engine/Engine/Application.h

@@ -67,6 +67,7 @@ protected:
 };
 };
 
 
 // Macro for defining a main function which creates a Context and the application, then runs it
 // Macro for defining a main function which creates a Context and the application, then runs it
+#ifndef IOS
 #define DEFINE_APPLICATION_MAIN(className) \
 #define DEFINE_APPLICATION_MAIN(className) \
 int RunApplication() \
 int RunApplication() \
 { \
 { \
@@ -75,5 +76,16 @@ int RunApplication() \
     return application->Run(); \
     return application->Run(); \
 } \
 } \
 DEFINE_MAIN(RunApplication());
 DEFINE_MAIN(RunApplication());
-
+#else
+// On iOS we will let this function exit, so do not hold the context and application in SharedPtr's
+#define DEFINE_APPLICATION_MAIN(className) \
+int RunApplication() \
+{ \
+    Urho3D::Context* context = new Urho3D::Context(); \
+    className* application = new className(context); \
+    return application->Run(); \
+} \
+DEFINE_MAIN(RunApplication());
+#endif
+    
 }
 }