From 60f30036f1338b6fba8a0c23d7a8b70eab697644 Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Sat, 6 Feb 2021 01:51:38 +0100 Subject: [PATCH] Codechange: [OSX] Drain autoreleased objects in each game loop cycle. --- src/video/cocoa/cocoa_wnd.mm | 19 ++++++---- src/video/cocoa/event.mm | 73 +++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/video/cocoa/cocoa_wnd.mm b/src/video/cocoa/cocoa_wnd.mm index 80c2e2e215..e7c916443b 100644 --- a/src/video/cocoa/cocoa_wnd.mm +++ b/src/video/cocoa/cocoa_wnd.mm @@ -203,6 +203,7 @@ bool CocoaSetupApplication() } /* Become the front process, important when start from the command line. */ + [ [ NSApplication sharedApplication ] setActivationPolicy:NSApplicationActivationPolicyRegular ]; [ [ NSApplication sharedApplication ] activateIgnoringOtherApps:YES ]; /* Set up the menubar */ @@ -247,15 +248,17 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel return; } - NSAlert *alert = [ [ NSAlert alloc ] init ]; - [ alert setAlertStyle: NSCriticalAlertStyle ]; - [ alert setMessageText:[ NSString stringWithUTF8String:title ] ]; - [ alert setInformativeText:[ NSString stringWithUTF8String:message ] ]; - [ alert addButtonWithTitle: [ NSString stringWithUTF8String:buttonLabel ] ]; - [ alert runModal ]; - [ alert release ]; + @autoreleasepool { + NSAlert *alert = [ [ NSAlert alloc ] init ]; + [ alert setAlertStyle: NSCriticalAlertStyle ]; + [ alert setMessageText:[ NSString stringWithUTF8String:title ] ]; + [ alert setInformativeText:[ NSString stringWithUTF8String:message ] ]; + [ alert addButtonWithTitle: [ NSString stringWithUTF8String:buttonLabel ] ]; + [ alert runModal ]; + [ alert release ]; + } - if (!wasstarted && VideoDriver::GetInstance() != NULL) VideoDriver::GetInstance()->Stop(); + if (!wasstarted && VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop(); _cocoa_video_dialog = false; } diff --git a/src/video/cocoa/event.mm b/src/video/cocoa/event.mm index 858cd51ad5..00559e6bce 100644 --- a/src/video/cocoa/event.mm +++ b/src/video/cocoa/event.mm @@ -609,57 +609,60 @@ void VideoDriver_Cocoa::GameLoop() this->Draw(true); for (;;) { - uint32 prev_cur_ticks = cur_ticks; // to check for wrapping - InteractiveRandom(); // randomness + @autoreleasepool { - while (this->PollEvent()) {} + uint32 prev_cur_ticks = cur_ticks; // to check for wrapping + InteractiveRandom(); // randomness - if (_exit_game) { - /* Restore saved resolution if in fullscreen mode. */ - if (this->IsFullscreen()) _cur_resolution = this->orig_res; - break; - } + while (this->PollEvent()) {} + + if (_exit_game) { + /* Restore saved resolution if in fullscreen mode. */ + if (this->IsFullscreen()) _cur_resolution = this->orig_res; + break; + } #if defined(_DEBUG) - if (_current_mods & NSShiftKeyMask) + if (_current_mods & NSShiftKeyMask) #else - if (_tab_is_down) + if (_tab_is_down) #endif - { - if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2; - } else if (_fast_forward & 2) { - _fast_forward = 0; - } + { + if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2; + } else if (_fast_forward & 2) { + _fast_forward = 0; + } - cur_ticks = GetTick(); - if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) { - _realtime_tick += cur_ticks - last_cur_ticks; - last_cur_ticks = cur_ticks; - next_tick = cur_ticks + MILLISECONDS_PER_TICK; + cur_ticks = GetTick(); + if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) { + _realtime_tick += cur_ticks - last_cur_ticks; + last_cur_ticks = cur_ticks; + next_tick = cur_ticks + MILLISECONDS_PER_TICK; - bool old_ctrl_pressed = _ctrl_pressed; + bool old_ctrl_pressed = _ctrl_pressed; - _ctrl_pressed = !!(_current_mods & ( _settings_client.gui.right_mouse_btn_emulation != RMBE_CONTROL ? NSControlKeyMask : NSCommandKeyMask)); - _shift_pressed = !!(_current_mods & NSShiftKeyMask); + _ctrl_pressed = !!(_current_mods & ( _settings_client.gui.right_mouse_btn_emulation != RMBE_CONTROL ? NSControlKeyMask : NSCommandKeyMask)); + _shift_pressed = !!(_current_mods & NSShiftKeyMask); - if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged(); + if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged(); - ::GameLoop(); + ::GameLoop(); - UpdateWindows(); - this->CheckPaletteAnim(); - this->Draw(); - } else { + UpdateWindows(); + this->CheckPaletteAnim(); + this->Draw(); + } else { #ifdef _DEBUG - uint32 st0 = GetTick(); + uint32 st0 = GetTick(); #endif - CSleep(1); + CSleep(1); #ifdef _DEBUG - st += GetTick() - st0; + st += GetTick() - st0; #endif - NetworkDrawChatMessage(); - DrawMouseCursor(); - this->Draw(); + NetworkDrawChatMessage(); + DrawMouseCursor(); + this->Draw(); + } } }