我正在尝试遵循我在 https://developer.apple.com/metal/sample-code/?q=learn
找到的 metal-cpp 教程程序似乎启动正确(我可以看到菜单栏并与其交互),但窗口内没有呈现任何内容。
我像这样从我的主函数启动应用程序
NS::AutoreleasePool* poolAllocator = NS::AutoreleasePool::alloc()->init();
core::Application appDel;
NS::Application* psharedApp = NS::Application::sharedApplication();
psharedApp->setDelegate(&appDel);
psharedApp->run();
poolAllocator->release();
core::Application
覆盖 NS::ApplicationDelegate
。我想罪魁祸首是在以下函数中的某个地方(我看到它将设备、视图和窗口与负责在视图中绘制内容的对象绑定在一起):
void core::Application::applicationDidFinishLaunching(NS::Notification *pNotification)
{
CGRect frame = (CGRect){{100.0, 100.0}, {512.0, 512.0}};
m_Window->init(frame, NS::WindowStyleMaskClosable | NS::WindowStyleMaskTitled, NS::BackingStoreBuffered, false);
m_Device = MTL::CreateSystemDefaultDevice();
m_View = MTK::View::alloc()->init(frame, m_Device);
m_View->setColorPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm);
m_View->setClearColor(MTL::ClearColor::Make(1.0, 0.0, 0.0, 1.0));
m_ViewDelegate = new graphics::ViewDelegate(m_Device);
m_View->setDelegate(m_ViewDelegate);
m_Window->setContentView(m_View);
m_Window->setTitle(NS::String::string("Template 1", NS::StringEncoding::UTF8StringEncoding));
m_Window->makeKeyAndOrderFront(nullptr);
NS::Application* nsApp = reinterpret_cast<NS::Application*>(pNotification->object());
nsApp->activateIgnoringOtherApps(true);
}
正如你可以从我运行第一个教程失败的事实中推断出的那样,我很迷失,我真的需要一个关于在哪里寻找正在发生的奇怪事情的提示。
我尝试调试程序,但发现它从未进入此函数:
void ViewDelegate::drawInMTKView(MTK::View *pView)
{
m_Renderer->Draw(pView);
}
这是应该调用渲染器的绘制函数的函数。这感觉很奇怪,因为类 ViewDelegate 覆盖了 MTK::ViewDelegate (我知道,我不擅长命名东西),并且该函数特别是由将 ViewDelegate 分配给 View 的函数调用(应该调用)。
提前感谢您的帮助
在这个问题得到答案之前我就解决了它,这是解决方案,以防有人犯同样的(愚蠢的)错误:
在我发布的片段中,我将窗口初始化为
m_Window->init(frame, NS::WindowStyleMaskClosable | NS::WindowStyleMaskTitled, NS::BackingStoreBuffered, false);
这是不正确的,因为它在尝试初始化窗口之前不会分配窗口。创建窗口的正确方法是调用
m_Window = NS::Window::alloc()->init(frame, NS::WindowStyleMaskClosable | NS::WindowStyleMaskTitled, NS::BackingStoreBuffered, false);
这个小修改足以解决问题。