From 0893d0fe799903c93496929ed4797561efac74e8 Mon Sep 17 00:00:00 2001 From: "Dr. Colin Hirsch" Date: Sat, 4 Nov 2023 16:37:39 +0100 Subject: [PATCH 1/7] Move private implementations to separate file. --- .../hello_metal_cpp.xcodeproj/project.pbxproj | 4 ++++ .../finished/hello_metal_cpp/main.cpp | 5 ----- .../finished/hello_metal_cpp/private.cpp | 13 +++++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 cpp/01 hello metal/finished/hello_metal_cpp/private.cpp diff --git a/cpp/01 hello metal/finished/hello_metal_cpp.xcodeproj/project.pbxproj b/cpp/01 hello metal/finished/hello_metal_cpp.xcodeproj/project.pbxproj index 43398ee..835b3b1 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp.xcodeproj/project.pbxproj +++ b/cpp/01 hello metal/finished/hello_metal_cpp.xcodeproj/project.pbxproj @@ -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 */; }; @@ -29,6 +30,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 3BC9198C2AF69BCD006A018C /* private.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = private.cpp; sourceTree = ""; }; 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 = ""; }; 76A099B02AB4542B003FD92C /* MetalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MetalKit.framework; path = System/Library/Frameworks/MetalKit.framework; sourceTree = SDKROOT; }; @@ -80,6 +82,7 @@ 76A09A282AB46683003FD92C /* control */, 76A09A232AB465CE003FD92C /* view */, 76A099A82AB452E0003FD92C /* main.cpp */, + 3BC9198C2AF69BCD006A018C /* private.cpp */, 76A09A222AB46548003FD92C /* config.h */, ); path = hello_metal_cpp; @@ -175,6 +178,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; diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp index ce9c6da..d1337b8 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp @@ -5,11 +5,6 @@ // 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" diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/private.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/private.cpp new file mode 100644 index 0000000..6bf6611 --- /dev/null +++ b/cpp/01 hello metal/finished/hello_metal_cpp/private.cpp @@ -0,0 +1,13 @@ +// +// 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" From a797bafaa03a5205651528445b005789ad97a662 Mon Sep 17 00:00:00 2001 From: "Dr. Colin Hirsch" Date: Sat, 4 Nov 2023 16:44:00 +0100 Subject: [PATCH 2/7] Use std::unique_ptr. --- .../finished/hello_metal_cpp/control/app_delegate.cpp | 5 ++--- .../finished/hello_metal_cpp/control/app_delegate.h | 5 ++++- .../finished/hello_metal_cpp/control/view_delegate.cpp | 10 ++-------- .../finished/hello_metal_cpp/control/view_delegate.h | 8 ++++++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp index a767cd5..d467e1d 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp @@ -11,7 +11,6 @@ AppDelegate::~AppDelegate() mtkView->release(); window->release(); device->release(); - delete viewDelegate; } void AppDelegate::applicationWillFinishLaunching(NS::Notification* notification) @@ -36,8 +35,8 @@ void AppDelegate::applicationDidFinishLaunching(NS::Notification* notification) 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(device); + mtkView->setDelegate(viewDelegate.get()); window->setContentView(mtkView); window->setTitle(NS::String::string("Window", NS::StringEncoding::UTF8StringEncoding)); diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h index 6c4389b..2e17a4f 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h @@ -5,6 +5,9 @@ // Created by Andrew Mengede on 13/9/2023. // #pragma once + +#include + #include "../config.h" #include "view_delegate.h" @@ -21,5 +24,5 @@ class AppDelegate : public NS::ApplicationDelegate NS::Window* window; MTK::View* mtkView; MTL::Device* device; - ViewDelegate* viewDelegate = nullptr; + std::unique_ptr viewDelegate; }; diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.cpp index f1a6307..0fa892e 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.cpp @@ -9,14 +9,8 @@ ViewDelegate::ViewDelegate(MTL::Device* device) : MTK::ViewDelegate() -, renderer(new Renderer(device)) -{ -} - -ViewDelegate::~ViewDelegate() -{ - delete renderer; -} +, renderer(std::make_unique(device)) +{} void ViewDelegate::drawInMTKView(MTK::View* view) { diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h index 2efe6db..4bb3e0c 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h @@ -6,15 +6,19 @@ // #pragma once + +#include + #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; private: - Renderer* renderer; + const std::unique_ptr renderer; }; From 216a7e575d14ac8d8d242c8ccaef8e8bbe010e09 Mon Sep 17 00:00:00 2001 From: "Dr. Colin Hirsch" Date: Sat, 4 Nov 2023 16:47:06 +0100 Subject: [PATCH 3/7] Consistent line formatting. --- cpp/01 hello metal/finished/hello_metal_cpp/config.h | 2 +- .../finished/hello_metal_cpp/control/app_delegate.cpp | 2 +- .../finished/hello_metal_cpp/control/view_delegate.cpp | 1 - .../finished/hello_metal_cpp/control/view_delegate.h | 1 - cpp/01 hello metal/finished/hello_metal_cpp/main.cpp | 1 - cpp/01 hello metal/finished/hello_metal_cpp/private.cpp | 1 - .../finished/hello_metal_cpp/view/renderer.cpp | 8 +++++--- .../finished/hello_metal_cpp/view/renderer.h | 2 +- 8 files changed, 8 insertions(+), 10 deletions(-) diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/config.h b/cpp/01 hello metal/finished/hello_metal_cpp/config.h index 0adc5db..729757a 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/config.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/config.h @@ -4,8 +4,8 @@ // // Created by Andrew Mengede on 15/9/2023. // - #pragma once + #include #include #include diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp index d467e1d..29e8866 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp @@ -4,8 +4,8 @@ // // Created by Andrew Mengede on 13/9/2023. // - #include "app_delegate.h" + AppDelegate::~AppDelegate() { mtkView->release(); diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.cpp index 0fa892e..505130a 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.cpp @@ -4,7 +4,6 @@ // // Created by Andrew Mengede on 13/9/2023. // - #include "view_delegate.h" ViewDelegate::ViewDelegate(MTL::Device* device) diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h index 4bb3e0c..06c9476 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h @@ -4,7 +4,6 @@ // // Created by Andrew Mengede on 13/9/2023. // - #pragma once #include diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp index d1337b8..7624239 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp @@ -4,7 +4,6 @@ // // Created by Andrew Mengede on 15/9/2023. // - #include "config.h" #include "control/app_delegate.h" diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/private.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/private.cpp index 6bf6611..6cfa925 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/private.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/private.cpp @@ -4,7 +4,6 @@ // // Created by Dr. Colin Hirsch on 04/11/23. // - #define NS_PRIVATE_IMPLEMENTATION #define MTL_PRIVATE_IMPLEMENTATION #define MTK_PRIVATE_IMPLEMENTATION diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.cpp index 42c4c24..89b0a6b 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.cpp @@ -13,13 +13,15 @@ device(device->retain()) commandQueue = device->newCommandQueue(); } -Renderer::~Renderer() { +Renderer::~Renderer() +{ commandQueue->release(); device->release(); } -void Renderer::draw(MTK::View* view) { - +void Renderer::draw(MTK::View* view) +{ + NS::AutoreleasePool* pool = NS::AutoreleasePool::alloc()->init(); MTL::CommandBuffer* commandBuffer = commandQueue->commandBuffer(); diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h index 519b729..83ba76c 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h @@ -4,8 +4,8 @@ // // Created by Andrew Mengede on 15/9/2023. // - #pragma once + #include "../config.h" class Renderer From 3ab3d5029e3ff053f9e1762cb1c8ac7dcb70d83f Mon Sep 17 00:00:00 2001 From: "Dr. Colin Hirsch" Date: Sat, 4 Nov 2023 16:57:51 +0100 Subject: [PATCH 4/7] More std::unique_ptr. --- .../hello_metal_cpp/control/app_delegate.cpp | 19 +++++----------- .../hello_metal_cpp/control/app_delegate.h | 9 ++++---- .../finished/hello_metal_cpp/main.cpp | 5 ++--- .../finished/hello_metal_cpp/release.h | 22 +++++++++++++++++++ .../hello_metal_cpp/view/renderer.cpp | 19 ++++------------ .../finished/hello_metal_cpp/view/renderer.h | 7 +++--- 6 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 cpp/01 hello metal/finished/hello_metal_cpp/release.h diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp index 29e8866..566746f 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.cpp @@ -6,13 +6,6 @@ // #include "app_delegate.h" -AppDelegate::~AppDelegate() -{ - mtkView->release(); - window->release(); - device->release(); -} - void AppDelegate::applicationWillFinishLaunching(NS::Notification* notification) { NS::Application* app = reinterpret_cast(notification->object()); @@ -23,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 = std::make_unique(device); + viewDelegate = std::make_unique(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); diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h index 2e17a4f..33172f4 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h @@ -9,20 +9,19 @@ #include #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; private: - NS::Window* window; - MTK::View* mtkView; - MTL::Device* device; + release_ptr window; + release_ptr mtkView; + release_ptr device; std::unique_ptr viewDelegate; }; diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp index 7624239..f6492c9 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp @@ -6,10 +6,11 @@ // #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 autoreleasePool(NS::AutoreleasePool::alloc()->init()); AppDelegate controller; @@ -17,7 +18,5 @@ int main( int argc, char* argv[] ) app->setDelegate(&controller); app->run(); - autoreleasePool->release(); - return 0; } diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/release.h b/cpp/01 hello metal/finished/hello_metal_cpp/release.h new file mode 100644 index 0000000..3e08bde --- /dev/null +++ b/cpp/01 hello metal/finished/hello_metal_cpp/release.h @@ -0,0 +1,22 @@ +// +// release.h +// hello_metal_cpp +// +// Created by Dr. Colin Hirsch on 04/11/23. +// +#pragma once + +#include + +struct release_delete +{ + void operator()(auto* t) const + { + if(t != nullptr) { + t->release(); + } + } +}; + +template +using release_ptr = std::unique_ptr; diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.cpp index 89b0a6b..b53d906 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.cpp @@ -4,32 +4,21 @@ // // Created by Andrew Mengede on 15/9/2023. // - #include "renderer.h" Renderer::Renderer(MTL::Device* device): -device(device->retain()) -{ - commandQueue = device->newCommandQueue(); -} - -Renderer::~Renderer() -{ - commandQueue->release(); - device->release(); -} +device(device->retain()), +commandQueue(device->newCommandQueue()) +{} void Renderer::draw(MTK::View* view) { + const release_ptr pool(NS::AutoreleasePool::alloc()->init()); - 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(); } diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h index 83ba76c..afcd221 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h @@ -7,15 +7,16 @@ #pragma once #include "../config.h" +#include "../release.h" class Renderer { public: Renderer(MTL::Device* device); - ~Renderer(); + void draw(MTK::View* view); private: - MTL::Device* device; - MTL::CommandQueue* commandQueue; + release_ptr device; + release_ptr commandQueue; }; From a4c00cc0e5cb158a3bde30a6e0aa3d48665d7e48 Mon Sep 17 00:00:00 2001 From: "Dr. Colin Hirsch" Date: Sat, 4 Nov 2023 16:58:56 +0100 Subject: [PATCH 5/7] Remove superfluous virtual. --- .../finished/hello_metal_cpp/control/app_delegate.h | 6 +++--- .../finished/hello_metal_cpp/control/view_delegate.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h index 33172f4..2ea33bd 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/app_delegate.h @@ -15,9 +15,9 @@ class AppDelegate : public NS::ApplicationDelegate { public: - 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: release_ptr window; diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h index 06c9476..39fb28f 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h @@ -16,7 +16,7 @@ class ViewDelegate : public MTK::ViewDelegate public: ViewDelegate(MTL::Device* device); - virtual void drawInMTKView(MTK::View* view) override; + void drawInMTKView(MTK::View* view) override; private: const std::unique_ptr renderer; From fa9615781bbd038a96cbeb3a247239cc3b9e30b9 Mon Sep 17 00:00:00 2001 From: "Dr. Colin Hirsch" Date: Sat, 4 Nov 2023 17:01:07 +0100 Subject: [PATCH 6/7] More const and explicit. --- .../finished/hello_metal_cpp/control/view_delegate.h | 2 +- cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h index 39fb28f..f85a8e1 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/control/view_delegate.h @@ -14,7 +14,7 @@ class ViewDelegate : public MTK::ViewDelegate { public: - ViewDelegate(MTL::Device* device); + explicit ViewDelegate(MTL::Device* device); void drawInMTKView(MTK::View* view) override; diff --git a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h index afcd221..92b4505 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h +++ b/cpp/01 hello metal/finished/hello_metal_cpp/view/renderer.h @@ -12,11 +12,11 @@ class Renderer { public: - Renderer(MTL::Device* device); + explicit Renderer(MTL::Device* device); void draw(MTK::View* view); private: - release_ptr device; - release_ptr commandQueue; + const release_ptr device; + const release_ptr commandQueue; }; From bbecdb454ef8d1c7aaeec917db51d795fbc693a6 Mon Sep 17 00:00:00 2001 From: "Dr. Colin Hirsch" Date: Sat, 4 Nov 2023 17:03:22 +0100 Subject: [PATCH 7/7] Add release.h to project. --- .../finished/hello_metal_cpp.xcodeproj/project.pbxproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp/01 hello metal/finished/hello_metal_cpp.xcodeproj/project.pbxproj b/cpp/01 hello metal/finished/hello_metal_cpp.xcodeproj/project.pbxproj index 835b3b1..56d237e 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp.xcodeproj/project.pbxproj +++ b/cpp/01 hello metal/finished/hello_metal_cpp.xcodeproj/project.pbxproj @@ -31,6 +31,7 @@ /* Begin PBXFileReference section */ 3BC9198C2AF69BCD006A018C /* private.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = private.cpp; sourceTree = ""; }; + 3BC9198E2AF69F29006A018C /* release.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = release.h; sourceTree = ""; }; 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 = ""; }; 76A099B02AB4542B003FD92C /* MetalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MetalKit.framework; path = System/Library/Frameworks/MetalKit.framework; sourceTree = SDKROOT; }; @@ -82,6 +83,7 @@ 76A09A282AB46683003FD92C /* control */, 76A09A232AB465CE003FD92C /* view */, 76A099A82AB452E0003FD92C /* main.cpp */, + 3BC9198E2AF69F29006A018C /* release.h */, 3BC9198C2AF69BCD006A018C /* private.cpp */, 76A09A222AB46548003FD92C /* config.h */, );