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..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 @@ -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,8 @@ /* End PBXCopyFilesBuildPhase section */ /* 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; }; @@ -80,6 +83,8 @@ 76A09A282AB46683003FD92C /* control */, 76A09A232AB465CE003FD92C /* view */, 76A099A82AB452E0003FD92C /* main.cpp */, + 3BC9198E2AF69F29006A018C /* release.h */, + 3BC9198C2AF69BCD006A018C /* private.cpp */, 76A09A222AB46548003FD92C /* config.h */, ); path = hello_metal_cpp; @@ -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; 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 a767cd5..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 @@ -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) { @@ -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(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 6c4389b..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 @@ -5,21 +5,23 @@ // Created by Andrew Mengede on 13/9/2023. // #pragma once + +#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; + 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 window; + release_ptr mtkView; + release_ptr device; + 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..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,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(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..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 @@ -4,17 +4,20 @@ // // Created by Andrew Mengede on 13/9/2023. // - #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; + explicit ViewDelegate(MTL::Device* device); + + void drawInMTKView(MTK::View* view) override; private: - Renderer* renderer; + const std::unique_ptr renderer; }; 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..f6492c9 100644 --- a/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp +++ b/cpp/01 hello metal/finished/hello_metal_cpp/main.cpp @@ -4,18 +4,13 @@ // // 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 autoreleasePool(NS::AutoreleasePool::alloc()->init()); AppDelegate controller; @@ -23,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/private.cpp b/cpp/01 hello metal/finished/hello_metal_cpp/private.cpp new file mode 100644 index 0000000..6cfa925 --- /dev/null +++ b/cpp/01 hello metal/finished/hello_metal_cpp/private.cpp @@ -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" 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 42c4c24..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,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 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(); } 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..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 @@ -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 device; + const release_ptr commandQueue; };