Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple of minor cleanups and use std::unique_ptr where possible... #4

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3BC9198D2AF69BCD006A018C /* private.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3BC9198C2AF69BCD006A018C /* private.cpp */; };
7650266F2AD2657900E44B72 /* app_delegate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76A09A292AB46683003FD92C /* app_delegate.cpp */; };
765026702AD2657D00E44B72 /* view_delegate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76A09A2C2AB46683003FD92C /* view_delegate.cpp */; };
76A099A92AB452E0003FD92C /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76A099A82AB452E0003FD92C /* main.cpp */; };
Expand All @@ -29,6 +30,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
3BC9198C2AF69BCD006A018C /* private.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = private.cpp; sourceTree = "<group>"; };
3BC9198E2AF69F29006A018C /* release.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = release.h; sourceTree = "<group>"; };
76A099A52AB452E0003FD92C /* hello_metal_cpp */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = hello_metal_cpp; sourceTree = BUILT_PRODUCTS_DIR; };
76A099A82AB452E0003FD92C /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
76A099B02AB4542B003FD92C /* MetalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MetalKit.framework; path = System/Library/Frameworks/MetalKit.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -80,6 +83,8 @@
76A09A282AB46683003FD92C /* control */,
76A09A232AB465CE003FD92C /* view */,
76A099A82AB452E0003FD92C /* main.cpp */,
3BC9198E2AF69F29006A018C /* release.h */,
3BC9198C2AF69BCD006A018C /* private.cpp */,
76A09A222AB46548003FD92C /* config.h */,
);
path = hello_metal_cpp;
Expand Down Expand Up @@ -175,6 +180,7 @@
765026702AD2657D00E44B72 /* view_delegate.cpp in Sources */,
7650266F2AD2657900E44B72 /* app_delegate.cpp in Sources */,
76A09A262AB46630003FD92C /* renderer.cpp in Sources */,
3BC9198D2AF69BCD006A018C /* private.cpp in Sources */,
76A099A92AB452E0003FD92C /* main.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion cpp/01 hello metal/finished/hello_metal_cpp/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//
// Created by Andrew Mengede on 15/9/2023.
//

#pragma once

#include <Metal/Metal.hpp>
#include <AppKit/AppKit.hpp>
#include <MetalKit/MetalKit.hpp>
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@
//
// Created by Andrew Mengede on 13/9/2023.
//

#include "app_delegate.h"
AppDelegate::~AppDelegate()
{
mtkView->release();
window->release();
device->release();
delete viewDelegate;
}

void AppDelegate::applicationWillFinishLaunching(NS::Notification* notification)
{
Expand All @@ -24,22 +16,22 @@ void AppDelegate::applicationDidFinishLaunching(NS::Notification* notification)
{
CGRect frame = (CGRect){ {100.0, 100.0}, {640.0, 480.0} };

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

device = MTL::CreateSystemDefaultDevice();
device.reset(MTL::CreateSystemDefaultDevice());

mtkView = MTK::View::alloc()->init(frame, device);
mtkView.reset(MTK::View::alloc()->init(frame, device.get()));
mtkView->setColorPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm_sRGB);
mtkView->setClearColor(MTL::ClearColor::Make(1.0, 1.0, 0.6, 1.0));

viewDelegate = new ViewDelegate(device);
mtkView->setDelegate(viewDelegate);
viewDelegate = std::make_unique<ViewDelegate>(device.get());
mtkView->setDelegate(viewDelegate.get());

window->setContentView(mtkView);
window->setContentView(mtkView.get());
window->setTitle(NS::String::string("Window", NS::StringEncoding::UTF8StringEncoding));
window->makeKeyAndOrderFront(nullptr);

Expand Down
20 changes: 11 additions & 9 deletions cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
// Created by Andrew Mengede on 13/9/2023.
//
#pragma once

#include <memory>

#include "../config.h"
#include "../release.h"
#include "view_delegate.h"

class AppDelegate : public NS::ApplicationDelegate
{
public:
~AppDelegate();

virtual void applicationWillFinishLaunching(NS::Notification* notification) override;
virtual void applicationDidFinishLaunching(NS::Notification* notification) override;
virtual bool applicationShouldTerminateAfterLastWindowClosed(NS::Application* sender) override;
void applicationWillFinishLaunching(NS::Notification* notification) override;
void applicationDidFinishLaunching(NS::Notification* notification) override;
bool applicationShouldTerminateAfterLastWindowClosed(NS::Application* sender) override;

private:
NS::Window* window;
MTK::View* mtkView;
MTL::Device* device;
ViewDelegate* viewDelegate = nullptr;
release_ptr<NS::Window> window;
release_ptr<MTK::View> mtkView;
release_ptr<MTL::Device> device;
std::unique_ptr<ViewDelegate> viewDelegate;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@
//
// Created by Andrew Mengede on 13/9/2023.
//

#include "view_delegate.h"

ViewDelegate::ViewDelegate(MTL::Device* device)
: MTK::ViewDelegate()
, renderer(new Renderer(device))
{
}

ViewDelegate::~ViewDelegate()
{
delete renderer;
}
, renderer(std::make_unique<Renderer>(device))
{}

void ViewDelegate::drawInMTKView(MTK::View* view)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
//
// Created by Andrew Mengede on 13/9/2023.
//

#pragma once

#include <memory>

#include "config.h"
#include "../view/renderer.h"

class ViewDelegate : public MTK::ViewDelegate
{
public:
ViewDelegate(MTL::Device* device);
virtual ~ViewDelegate() override;
virtual void drawInMTKView(MTK::View* view) override;
explicit ViewDelegate(MTL::Device* device);

void drawInMTKView(MTK::View* view) override;

private:
Renderer* renderer;
const std::unique_ptr<Renderer> renderer;
};
11 changes: 2 additions & 9 deletions cpp/01 hello metal/finished/hello_metal_cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,19 @@
//
// Created by Andrew Mengede on 15/9/2023.
//

#define NS_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#define MTK_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION

#include "config.h"
#include "control/app_delegate.h"
#include "release.h"

int main( int argc, char* argv[] )
{
NS::AutoreleasePool* autoreleasePool = NS::AutoreleasePool::alloc()->init();
const release_ptr<NS::AutoreleasePool> autoreleasePool(NS::AutoreleasePool::alloc()->init());

AppDelegate controller;

NS::Application* app = NS::Application::sharedApplication();
app->setDelegate(&controller);
app->run();

autoreleasePool->release();

return 0;
}
12 changes: 12 additions & 0 deletions cpp/01 hello metal/finished/hello_metal_cpp/private.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// private.cpp
// hello_metal_cpp
//
// Created by Dr. Colin Hirsch on 04/11/23.
//
#define NS_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#define MTK_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION

#include "config.h"
22 changes: 22 additions & 0 deletions cpp/01 hello metal/finished/hello_metal_cpp/release.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// release.h
// hello_metal_cpp
//
// Created by Dr. Colin Hirsch on 04/11/23.
//
#pragma once

#include <memory>

struct release_delete
{
void operator()(auto* t) const
{
if(t != nullptr) {
t->release();
}
}
};

template<typename T>
using release_ptr = std::unique_ptr<T, release_delete >;
21 changes: 6 additions & 15 deletions cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,21 @@
//
// Created by Andrew Mengede on 15/9/2023.
//

#include "renderer.h"

Renderer::Renderer(MTL::Device* device):
device(device->retain())
{
commandQueue = device->newCommandQueue();
}
device(device->retain()),
commandQueue(device->newCommandQueue())
{}

Renderer::~Renderer() {
commandQueue->release();
device->release();
}
void Renderer::draw(MTK::View* view)
{
const release_ptr<NS::AutoreleasePool> pool(NS::AutoreleasePool::alloc()->init());

void Renderer::draw(MTK::View* view) {

NS::AutoreleasePool* pool = NS::AutoreleasePool::alloc()->init();

MTL::CommandBuffer* commandBuffer = commandQueue->commandBuffer();
MTL::RenderPassDescriptor* renderPass = view->currentRenderPassDescriptor();
MTL::RenderCommandEncoder* encoder = commandBuffer->renderCommandEncoder(renderPass);
encoder->endEncoding();
commandBuffer->presentDrawable(view->currentDrawable());
commandBuffer->commit();

pool->release();
}
11 changes: 6 additions & 5 deletions cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
//
// Created by Andrew Mengede on 15/9/2023.
//

#pragma once

#include "../config.h"
#include "../release.h"

class Renderer
{
public:
Renderer(MTL::Device* device);
~Renderer();
explicit Renderer(MTL::Device* device);

void draw(MTK::View* view);

private:
MTL::Device* device;
MTL::CommandQueue* commandQueue;
const release_ptr<MTL::Device> device;
const release_ptr<MTL::CommandQueue> commandQueue;
};