Search code examples
c++xcodemetalmetalkit

Content of metal-cpp window is not shown on startup


I'm trying to follow the metal-cpp tutorials I've found at https://developer.apple.com/metal/sample-code/?q=learn

The program seems to be launching correctly (I can see the menu bar and interact with it), but nothing is rendered inside the window.

I start the application from my main function like this

    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 overrides NS::ApplicationDelegate. I suppose the culprit is somewhere in the following function (I see it binds the device, the view and the window with the object in charge of drawing stuff in the view):

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);
    
}

As you can infer from the fact that I'm failing at running the very first tutorial I'm quite lost and I really need a hint about where to look for weird stuff going on.

I tried to debug the program and I saw it never enters in this function:

void ViewDelegate::drawInMTKView(MTK::View *pView)
{
    m_Renderer->Draw(pView);
}

Which is the one that is supposed to call the renderer's draw function. That feels weird, because class ViewDelegate overrides MTK::ViewDelegate (I know, I'm bad at naming stuff) and that function in particular is called (should be called) by the function that assigns the ViewDelegate to the View.

Thank you in advance for your help


Solution

  • I solved it before this question could get an answer, here is the solution in case someone incurs in the same (silly) mistake:

    In the snipped I posted I initialised the window as

    m_Window->init(frame, NS::WindowStyleMaskClosable | NS::WindowStyleMaskTitled, NS::BackingStoreBuffered, false);
    

    That is incorrect, because it doesn't allocate the Window before trying to initialise it. The right way to create a Window is to call

    m_Window = NS::Window::alloc()->init(frame, NS::WindowStyleMaskClosable | NS::WindowStyleMaskTitled, NS::BackingStoreBuffered, false);
    

    This small modification was enough to solve the issue.