diff --git a/platform/macos/display_server_macos.h b/platform/macos/display_server_macos.h index abce3bac06..a5e5d5d310 100644 --- a/platform/macos/display_server_macos.h +++ b/platform/macos/display_server_macos.h @@ -64,7 +64,7 @@ #undef CursorShape class DisplayServerMacOS : public DisplayServer { - // No need to register with GDCLASS, it's platform-specific and nothing is added. + GDCLASS(DisplayServerMacOS, DisplayServer); // Note: required for Object::cast_to. _THREAD_SAFE_CLASS_ diff --git a/platform/macos/godot_application.mm b/platform/macos/godot_application.mm index f6063dd0d1..900d2ca99b 100644 --- a/platform/macos/godot_application.mm +++ b/platform/macos/godot_application.mm @@ -81,7 +81,7 @@ } break; } - DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); + DisplayServerMacOS *ds = Object::cast_to(DisplayServer::get_singleton()); if (ds && keycode != Key::NONE) { DisplayServerMacOS::KeyEvent ke; @@ -109,7 +109,7 @@ [self mediaKeyEvent:keyCode state:keyState repeat:keyRepeat]; } - DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); + DisplayServerMacOS *ds = Object::cast_to(DisplayServer::get_singleton()); if (ds) { if ([event type] == NSEventTypeLeftMouseDown || [event type] == NSEventTypeRightMouseDown || [event type] == NSEventTypeOtherMouseDown) { if (ds->mouse_process_popups()) { diff --git a/platform/macos/godot_application_delegate.mm b/platform/macos/godot_application_delegate.mm index 0421ea92e9..90b30c1a44 100644 --- a/platform/macos/godot_application_delegate.mm +++ b/platform/macos/godot_application_delegate.mm @@ -48,7 +48,7 @@ - (void)searchForItemsWithSearchString:(NSString *)searchString resultLimit:(NSInteger)resultLimit matchedItemHandler:(void (^)(NSArray *items))handleMatchedItems { NSMutableArray *found_items = [[NSMutableArray alloc] init]; - DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); + DisplayServerMacOS *ds = Object::cast_to(DisplayServer::get_singleton()); if (ds && ds->_help_get_search_callback().is_valid()) { Callable cb = ds->_help_get_search_callback(); @@ -77,7 +77,7 @@ } - (void)performActionForItem:(id)item { - DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); + DisplayServerMacOS *ds = Object::cast_to(DisplayServer::get_singleton()); if (ds && ds->_help_get_action_callback().is_valid()) { Callable cb = ds->_help_get_action_callback(); @@ -118,7 +118,7 @@ } - (void)system_theme_changed:(NSNotification *)notification { - DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); + DisplayServerMacOS *ds = Object::cast_to(DisplayServer::get_singleton()); if (ds) { ds->emit_system_theme_changed(); } @@ -199,7 +199,7 @@ } - (void)applicationDidResignActive:(NSNotification *)notification { - DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); + DisplayServerMacOS *ds = Object::cast_to(DisplayServer::get_singleton()); if (ds) { ds->mouse_process_popups(true); } @@ -215,7 +215,7 @@ } - (void)globalMenuCallback:(id)sender { - DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); + DisplayServerMacOS *ds = Object::cast_to(DisplayServer::get_singleton()); if (ds) { return ds->menu_callback(sender); } @@ -239,7 +239,7 @@ } - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { - DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); + DisplayServerMacOS *ds = Object::cast_to(DisplayServer::get_singleton()); if (ds && ds->has_window(DisplayServerMacOS::MAIN_WINDOW_ID)) { ds->send_window_event(ds->get_window(DisplayServerMacOS::MAIN_WINDOW_ID), DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST); } diff --git a/platform/macos/os_macos.h b/platform/macos/os_macos.h index b9cb61ae9c..0256147edc 100644 --- a/platform/macos/os_macos.h +++ b/platform/macos/os_macos.h @@ -46,6 +46,7 @@ class OS_MacOS : public OS_Unix { id delegate = nullptr; bool should_terminate = false; + bool main_stared = false; JoypadApple *joypad_apple = nullptr; diff --git a/platform/macos/os_macos.mm b/platform/macos/os_macos.mm index 439f43da31..f40f5d0c30 100644 --- a/platform/macos/os_macos.mm +++ b/platform/macos/os_macos.mm @@ -51,8 +51,13 @@ void OS_MacOS::pre_wait_observer_cb(CFRunLoopObserverRef p_observer, CFRunLoopAc @autoreleasepool { @try { - if (DisplayServer::get_singleton()) { - static_cast(DisplayServer::get_singleton())->_process_events(false); // Get rid of pending events. + // Get rid of pending events. + DisplayServer *ds = DisplayServer::get_singleton(); + DisplayServerMacOS *ds_mac = Object::cast_to(ds); + if (ds_mac) { + ds_mac->_process_events(false); + } else if (ds) { + ds->process_events(); } os->joypad_apple->process_joypads(); @@ -839,6 +844,8 @@ void OS_MacOS::start_main() { } if (err == OK) { + main_stared = true; + int ret; @autoreleasepool { ret = Main::start(); @@ -883,8 +890,10 @@ void OS_MacOS::cleanup() { if (main_loop) { main_loop->finalize(); } - @autoreleasepool { - Main::cleanup(); + if (main_stared) { + @autoreleasepool { + Main::cleanup(); + } } }