Override Cocoa app lifecycle defined by SDL

This commit is contained in:
Phil Marell
2021-07-24 16:32:58 +10:00
parent f16ffa0c4c
commit 00488c6fea
8 changed files with 126 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
#import <Cocoa/Cocoa.h>
NS_ASSUME_NONNULL_BEGIN
@interface AerofoilAppDelegate : NSObject <NSApplicationDelegate>
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,44 @@
#import "AerofoilAppDelegate.h"
#import "About.h"
#include "WindowManager.h"
#include "GliderDefines.h" // kPlayMode
extern short theMode;
void DoSettingsMain(void);
@interface AerofoilAppDelegate ()
@property (weak) IBOutlet NSMenuItem *aboutAerofoilMenuItem;
@property (weak) IBOutlet NSMenuItem *aboutGliderPROMenuItem;
@property (weak) IBOutlet NSMenuItem *preferencesMenuItem;
@end
@implementation AerofoilAppDelegate
- (IBAction)showAboutAerofoil:(id)sender {
DoAboutFramework();
}
- (IBAction)showAboutGliderPRO:(id)sender {
DoAbout();
}
- (IBAction)showPreferences:(id)sender {
DoSettingsMain();
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if (menuItem == _aboutAerofoilMenuItem || menuItem == _aboutGliderPROMenuItem) {
return ![self menuItemsDisabled];
} else if (menuItem == _preferencesMenuItem) {
return ![self menuItemsDisabled];
} else {
return NO;
}
}
- (BOOL)menuItemsDisabled {
PortabilityLayer::WindowManager* wm = PortabilityLayer::WindowManager::GetInstance();
return theMode == kPlayMode || wm->IsExclusiveWindowVisible();
}
@end

View File

@@ -0,0 +1,9 @@
#import <Cocoa/Cocoa.h>
NS_ASSUME_NONNULL_BEGIN
@interface AerofoilApplication : NSApplication
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,13 @@
#import "AerofoilApplication.h"
#include "SDL.h"
extern int SDL_SendQuit(void);
@implementation AerofoilApplication
- (void)terminate:(id)sender {
// TODO: Use Aerofoil method instead of private SDL method
SDL_SendQuit();
}
@end

View File

@@ -0,0 +1,6 @@
#ifndef MacInit_h
#define MacInit_h
int MacInit(void);
#endif /* MacInit_h */

View File

@@ -0,0 +1,22 @@
#import <Cocoa/Cocoa.h>
#import "AerofoilApplication.h"
#import "AerofoilAppDelegate.h"
#import "MacInit.h"
#include "SDL.h"
int MacInit(void) {
// Instantiate NSApp and its delegate first, so SDL chooses those over its own implementation.
[AerofoilApplication sharedApplication];
[[NSBundle mainBundle] loadNibNamed:@"MainMenu" owner:NSApp topLevelObjects:nil];
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0)
return -1;
// Gracefully activate app.
//
// (SDL forcefully does this via [NSApp activateIgnoringOtherApps:YES],
// which isn't consistent with normal Mac apps).
[NSApp finishLaunching];
return 0;
}