Roj*_*nex 4 macos xcode objective-c metal programmatically
我正在为这个绞尽脑汁。我一直在尝试复制 Apple 提供的初始金属项目,但根本没有使用界面构建器。我能够创建一个窗口,但它是空白的,没有任何渲染,我不知道为什么。
#import <Cocoa/Cocoa.h>
#import "AppDelegate.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSApplication* app = [NSApplication sharedApplication];
AppDelegate* appDelegate = [[AppDelegate alloc] init];
[app setDelegate:appDelegate];
[app run];
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
}
@end
Run Code Online (Sandbox Code Playgroud)
#import <Metal/Metal.h>
#import "AppDelegate.h"
#import "GameViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (id)init {
if (self = [super init]) {
NSScreen* mainScreen = [NSScreen mainScreen];
NSRect frame = NSMakeRect(0, 0, mainScreen.frame.size.width / 2, mainScreen.frame.size.height / 2);
NSUInteger styleMask =
NSWindowStyleMaskTitled |
NSWindowStyleMaskResizable |
NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable;
NSBackingStoreType backing = NSBackingStoreBuffered;
window = [[NSWindow alloc] initWithContentRect:frame styleMask:styleMask backing:backing defer:YES];
[window center];
GameViewController* gvc = [[GameViewController alloc] init];
MTKView* metalView = [[MTKView alloc] initWithFrame:frame device:MTLCreateSystemDefaultDevice()];
[metalView setClearColor:MTLClearColorMake(0, 0, 0, 1)];
[metalView setColorPixelFormat:MTLPixelFormatBGRA8Unorm];
[metalView setDepthStencilPixelFormat:MTLPixelFormatDepth32Float];
[metalView setDelegate:gvc];
[gvc setView:metalView];
[window setContentView:metalView];
//[window setContentViewController: gvc];
}
return self;
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
[window makeKeyAndOrderFront:self];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
@end
Run Code Online (Sandbox Code Playgroud)
其他文件;GameViewController.h, GameViewController.m, Shaders.metal, SharedStructures.h; 与创建使用 Metal 和 Objective-c 的游戏项目时 XCode 8.2.1 (8C1002) 自动生成的内容相同。
您会注意到该行[window setContentViewController: gvc];已被注释掉。当取消注释时,我会EXEC_BAD_ACCESS在GameViewController.m's render 函数的第一行得到一个dispatch_semaphore_wait(_inflight_semaphore, DISPATCH_TIME_FOREVER);
显然我遗漏了一些东西,但我整天都在谷歌上搜索,我似乎无法弄清楚。任何帮助深表感谢。
问题是,当NSViewController诸如 的GameViewController不是从 NIB 加载时,它不会调用该-viewDidLoad方法。该GameViewController方法在该方法中做了重要的工作。
您可以将视图的创建移动到-loadViewin的覆盖中GameViewController。创建视图并将其分配给self.view. 你不需要设置它的委托。现有的GameViewController代码已经做到了。设置视图的属性应该移到-_setupView现有代码已经完成这类事情的地方。
不要在-[AppDelegate init]. 显然,您也无法在那里设置视图控制器的视图。只需将视图控制器设置为窗口的内容视图控制器(取消注释该行),其余的将自动处理。当窗口请求内容视图控制器的view属性时,它将调用您的覆盖-loadView以及现有的-viewDidLoad等。
| 归档时间: |
|
| 查看次数: |
1058 次 |
| 最近记录: |