فهرست منبع

Fixed life cycle events on iOS when using main callbacks

Sam Lantinga 9 ماه پیش
والد
کامیت
fff783de6c
3فایلهای تغییر یافته به همراه27 افزوده شده و 15 حذف شده
  1. 4 0
      src/main/ios/SDL_sysmain_callbacks.m
  2. 2 0
      src/video/uikit/SDL_uikitevents.h
  3. 21 15
      src/video/uikit/SDL_uikitevents.m

+ 4 - 0
src/main/ios/SDL_sysmain_callbacks.m

@@ -26,6 +26,9 @@
 
 #import <UIKit/UIKit.h>
 
+#include "../../video/uikit/SDL_uikitevents.h"  // For SDL_UpdateLifecycleObserver()
+
+
 @interface SDLIosMainCallbacksDisplayLink : NSObject
 @property(nonatomic, retain) CADisplayLink *displayLink;
 - (void)appIteration:(CADisplayLink *)sender;
@@ -53,6 +56,7 @@ static SDLIosMainCallbacksDisplayLink *globalDisplayLink;
         self.displayLink = nil;
         globalDisplayLink = nil;
         SDL_QuitMainCallbacks();
+        SDL_UpdateLifecycleObserver();
         exit((rc < 0) ? 1 : 0);
     }
 }

+ 2 - 0
src/video/uikit/SDL_uikitevents.h

@@ -25,6 +25,8 @@
 
 #include "../SDL_sysvideo.h"
 
+extern void SDL_UpdateLifecycleObserver(void);
+
 extern Uint64 UIKit_GetEventTimestamp(NSTimeInterval nsTimestamp);
 extern void UIKit_PumpEvents(SDL_VideoDevice *_this);
 

+ 21 - 15
src/video/uikit/SDL_uikitevents.m

@@ -23,6 +23,7 @@
 #ifdef SDL_VIDEO_DRIVER_UIKIT
 
 #include "../../events/SDL_events_c.h"
+#include "../../main/SDL_main_callbacks.h"
 
 #include "SDL_uikitevents.h"
 #include "SDL_uikitopengles.h"
@@ -46,10 +47,10 @@ static BOOL UIKit_EventPumpEnabled = YES;
 
 @implementation SDL_LifecycleObserver
 
-- (void)eventPumpChanged
+- (void)update
 {
     NSNotificationCenter *notificationCenter = NSNotificationCenter.defaultCenter;
-    if (UIKit_EventPumpEnabled && !self.isObservingNotifications) {
+    if ((UIKit_EventPumpEnabled || SDL_HasMainCallbacks()) && !self.isObservingNotifications) {
         self.isObservingNotifications = YES;
         [notificationCenter addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
         [notificationCenter addObserver:self selector:@selector(applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
@@ -63,7 +64,7 @@ static BOOL UIKit_EventPumpEnabled = YES;
                                    name:UIApplicationDidChangeStatusBarOrientationNotification
                                  object:nil];
 #endif
-    } else if (!UIKit_EventPumpEnabled && self.isObservingNotifications) {
+    } else if (!UIKit_EventPumpEnabled && !SDL_HasMainCallbacks() && self.isObservingNotifications) {
         self.isObservingNotifications = NO;
         [notificationCenter removeObserver:self];
     }
@@ -108,6 +109,23 @@ static BOOL UIKit_EventPumpEnabled = YES;
 
 @end
 
+void SDL_UpdateLifecycleObserver(void)
+{
+    static SDL_LifecycleObserver *lifecycleObserver;
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+      lifecycleObserver = [SDL_LifecycleObserver new];
+    });
+    [lifecycleObserver update];
+}
+
+void SDL_SetiOSEventPump(SDL_bool enabled)
+{
+    UIKit_EventPumpEnabled = enabled;
+
+    SDL_UpdateLifecycleObserver();
+}
+
 Uint64 UIKit_GetEventTimestamp(NSTimeInterval nsTimestamp)
 {
     static Uint64 timestamp_offset;
@@ -126,18 +144,6 @@ Uint64 UIKit_GetEventTimestamp(NSTimeInterval nsTimestamp)
     return timestamp;
 }
 
-void SDL_SetiOSEventPump(SDL_bool enabled)
-{
-    UIKit_EventPumpEnabled = enabled;
-
-    static SDL_LifecycleObserver *lifecycleObserver;
-    static dispatch_once_t onceToken;
-    dispatch_once(&onceToken, ^{
-      lifecycleObserver = [SDL_LifecycleObserver new];
-    });
-    [lifecycleObserver eventPumpChanged];
-}
-
 void UIKit_PumpEvents(SDL_VideoDevice *_this)
 {
     if (!UIKit_EventPumpEnabled) {