From d5723f0f2c8f8834eaf482e3bbaca80240f2b1e7 Mon Sep 17 00:00:00 2001 From: xszAzy <3486170277@qq.com> Date: Tue, 30 Sep 2025 19:00:14 +0800 Subject: [PATCH] Window class update --- LICENSE | 201 ++++++++++++++++++ .../MetalLearning.xcodeproj/project.pbxproj | 57 +++-- MetalLearning/Window.h | 35 +++ MetalLearning/src/MEL/Application.h | 10 +- MetalLearning/src/MEL/Application.mm | 15 +- MetalLearning/src/MEL/Events/Event.h | 2 +- MetalLearning/src/MEL/Input/Input.cpp | 1 + MetalLearning/src/MEL/Input/Input.h | 1 + MetalLearning/src/MEL/Layer/Layer.cpp | 4 + MetalLearning/src/MEL/Layer/Layer.h | 19 ++ MetalLearning/src/MEL/Log/Log.h | 12 +- MetalLearning/src/MEL/Log/Log.mm | 3 + MetalLearning/src/MEL/Renderer/AppDelegate.mm | 7 +- .../src/MEL/Renderer/ViewController.mm | 6 + MetalLearning/src/MEL/Window/CocoaWindow.h | 19 ++ .../MEL/Window/{Window.mm => CocoaWindow.mm} | 41 +--- MetalLearning/src/MEL/Window/MacWindow.h | 42 ++++ MetalLearning/src/MEL/Window/MacWindow.mm | 81 +++++++ MetalLearning/src/MEL/Window/Window.h | 62 ------ MetalLearning/src/MEL/Window/WindowManager.h | 14 -- MetalLearning/src/MEL/Window/WindowManager.mm | 37 ---- README.md | 2 + 22 files changed, 487 insertions(+), 184 deletions(-) create mode 100644 LICENSE create mode 100644 MetalLearning/Window.h create mode 100644 MetalLearning/src/MEL/Input/Input.cpp create mode 100644 MetalLearning/src/MEL/Input/Input.h create mode 100644 MetalLearning/src/MEL/Layer/Layer.cpp create mode 100644 MetalLearning/src/MEL/Layer/Layer.h create mode 100644 MetalLearning/src/MEL/Window/CocoaWindow.h rename MetalLearning/src/MEL/Window/{Window.mm => CocoaWindow.mm} (76%) create mode 100644 MetalLearning/src/MEL/Window/MacWindow.h create mode 100644 MetalLearning/src/MEL/Window/MacWindow.mm delete mode 100644 MetalLearning/src/MEL/Window/Window.h delete mode 100644 MetalLearning/src/MEL/Window/WindowManager.h delete mode 100644 MetalLearning/src/MEL/Window/WindowManager.mm create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/MetalLearning/MetalLearning.xcodeproj/project.pbxproj b/MetalLearning/MetalLearning.xcodeproj/project.pbxproj index ba8f052..f32dfa5 100644 --- a/MetalLearning/MetalLearning.xcodeproj/project.pbxproj +++ b/MetalLearning/MetalLearning.xcodeproj/project.pbxproj @@ -11,8 +11,10 @@ 1817955824A247CA9F871398 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA5C1300C9792D32A5828940 /* CoreFoundation.framework */; }; 470B56372E897D33001040D0 /* Sandbox.mm in Sources */ = {isa = PBXBuildFile; fileRef = 470B56362E897D33001040D0 /* Sandbox.mm */; }; 470B563A2E8988BF001040D0 /* Entrypoint.mm in Sources */ = {isa = PBXBuildFile; fileRef = 470B56392E8988BF001040D0 /* Entrypoint.mm */; }; - 47FF21F62E8A056A004D6F5D /* Window.mm in Sources */ = {isa = PBXBuildFile; fileRef = 47FF21F52E8A056A004D6F5D /* Window.mm */; }; - 47FF21F92E8A07BE004D6F5D /* WindowManager.mm in Sources */ = {isa = PBXBuildFile; fileRef = 47FF21F82E8A07BE004D6F5D /* WindowManager.mm */; }; + 473EC1802E8BED99009A2988 /* Layer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 473EC17F2E8BED99009A2988 /* Layer.cpp */; }; + 4798F2BB2E8AE124000FC97A /* MacWindow.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4798F2BA2E8AE124000FC97A /* MacWindow.mm */; }; + 4798F2CA2E8AF18B000FC97A /* Input.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4798F2C92E8AF18B000FC97A /* Input.cpp */; }; + 47FF21F62E8A056A004D6F5D /* CocoaWindow.mm in Sources */ = {isa = PBXBuildFile; fileRef = 47FF21F52E8A056A004D6F5D /* CocoaWindow.mm */; }; 4B007C2E118E73A07F909A6E /* MetalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A0271576359DD4A8A43A2BB6 /* MetalKit.framework */; }; 755C09380F7FFE6ABFA5DF78 /* ViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = CC2A80804084C572BE684EC0 /* ViewController.mm */; }; 9130093853F069AAB1254778 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8B91C4E06FD8CD12F3C5FB20 /* QuartzCore.framework */; }; @@ -55,10 +57,15 @@ 470B56412E898C34001040D0 /* KeyEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KeyEvent.h; sourceTree = ""; }; 470B56422E898C34001040D0 /* MouseEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MouseEvent.h; sourceTree = ""; }; 470B56432E898D43001040D0 /* Core.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Core.h; sourceTree = ""; }; - 47FF21F42E8A056A004D6F5D /* Window.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Window.h; sourceTree = ""; }; - 47FF21F52E8A056A004D6F5D /* Window.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = Window.mm; sourceTree = ""; }; - 47FF21F72E8A07BE004D6F5D /* WindowManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WindowManager.h; sourceTree = ""; }; - 47FF21F82E8A07BE004D6F5D /* WindowManager.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WindowManager.mm; sourceTree = ""; }; + 473EC17E2E8BED99009A2988 /* Layer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Layer.h; sourceTree = ""; }; + 473EC17F2E8BED99009A2988 /* Layer.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Layer.cpp; sourceTree = ""; }; + 4798F2B92E8AE124000FC97A /* MacWindow.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MacWindow.h; sourceTree = ""; }; + 4798F2BA2E8AE124000FC97A /* MacWindow.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MacWindow.mm; sourceTree = ""; }; + 4798F2C12E8AE55E000FC97A /* Window.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Window.h; sourceTree = ""; }; + 4798F2C82E8AF18B000FC97A /* Input.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Input.h; sourceTree = ""; }; + 4798F2C92E8AF18B000FC97A /* Input.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Input.cpp; sourceTree = ""; }; + 47FF21F42E8A056A004D6F5D /* CocoaWindow.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CocoaWindow.h; sourceTree = ""; }; + 47FF21F52E8A056A004D6F5D /* CocoaWindow.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = CocoaWindow.mm; sourceTree = ""; }; 4ADA29160D9A89886ACF6756 /* MetalRenderer.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = MetalRenderer.mm; path = src/MEL/Renderer/MetalRenderer.mm; sourceTree = ""; }; 6038CC64F5AF8B96644BE2A4 /* Application.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = Application.mm; path = src/MEL/Application.mm; sourceTree = ""; }; 6188BCBA2816B42C9618DAFA /* AppDelegate.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = src/MEL/Renderer/AppDelegate.mm; sourceTree = ""; }; @@ -103,6 +110,8 @@ 2C8FAEDB7CA3438DFF21751B /* MEL */ = { isa = PBXGroup; children = ( + 473EC17D2E8BED88009A2988 /* Layer */, + 4798F2C72E8AF171000FC97A /* Input */, 47FF21F32E8A0556004D6F5D /* Window */, 470B56392E8988BF001040D0 /* Entrypoint.mm */, 7B436A80AD3CB532271BF0C0 /* Application.h */, @@ -111,6 +120,7 @@ B69F209AFF1004CC43AED6DA /* Renderer */, 470B563D2E898A81001040D0 /* Events */, 470B56432E898D43001040D0 /* Core.h */, + 4798F2C12E8AE55E000FC97A /* Window.h */, ); name = MEL; sourceTree = ""; @@ -136,13 +146,33 @@ path = src/MEL/Events; sourceTree = ""; }; + 473EC17D2E8BED88009A2988 /* Layer */ = { + isa = PBXGroup; + children = ( + 473EC17E2E8BED99009A2988 /* Layer.h */, + 473EC17F2E8BED99009A2988 /* Layer.cpp */, + ); + name = Layer; + path = src/MEL/Layer; + sourceTree = ""; + }; + 4798F2C72E8AF171000FC97A /* Input */ = { + isa = PBXGroup; + children = ( + 4798F2C82E8AF18B000FC97A /* Input.h */, + 4798F2C92E8AF18B000FC97A /* Input.cpp */, + ); + name = Input; + path = src/MEL/Input; + sourceTree = ""; + }; 47FF21F32E8A0556004D6F5D /* Window */ = { isa = PBXGroup; children = ( - 47FF21F42E8A056A004D6F5D /* Window.h */, - 47FF21F52E8A056A004D6F5D /* Window.mm */, - 47FF21F72E8A07BE004D6F5D /* WindowManager.h */, - 47FF21F82E8A07BE004D6F5D /* WindowManager.mm */, + 47FF21F42E8A056A004D6F5D /* CocoaWindow.h */, + 47FF21F52E8A056A004D6F5D /* CocoaWindow.mm */, + 4798F2B92E8AE124000FC97A /* MacWindow.h */, + 4798F2BA2E8AE124000FC97A /* MacWindow.mm */, ); name = Window; path = src/MEL/Window; @@ -277,11 +307,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 4798F2BB2E8AE124000FC97A /* MacWindow.mm in Sources */, CE38F4BC94C6EC2E02C912FC /* Application.mm in Sources */, - 47FF21F62E8A056A004D6F5D /* Window.mm in Sources */, - 47FF21F92E8A07BE004D6F5D /* WindowManager.mm in Sources */, + 47FF21F62E8A056A004D6F5D /* CocoaWindow.mm in Sources */, + 473EC1802E8BED99009A2988 /* Layer.cpp in Sources */, 470B563A2E8988BF001040D0 /* Entrypoint.mm in Sources */, 030AAB3B742D4C2D29F3F97B /* Log.mm in Sources */, + 4798F2CA2E8AF18B000FC97A /* Input.cpp in Sources */, D73895124B210CC46D6EBB52 /* AppDelegate.mm in Sources */, 470B56372E897D33001040D0 /* Sandbox.mm in Sources */, EC4D1F2E8A2E1FE061AC656E /* MetalRenderer.mm in Sources */, @@ -321,6 +353,7 @@ ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_TESTABILITY = YES; ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp; GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( diff --git a/MetalLearning/Window.h b/MetalLearning/Window.h new file mode 100644 index 0000000..a72624b --- /dev/null +++ b/MetalLearning/Window.h @@ -0,0 +1,35 @@ +#pragma once + +#include "melpch.h" +#include "MEL.h" +#include "Events/Event.h" +namespace MEL { + struct WindowProps{ + std::string Title; + unsigned int Width; + unsigned int Height; + WindowProps(const std::string &title="MEL Engine",unsigned int width=1280,unsigned int height=720) + :Title(title),Width(width),Height(height){} + }; + class Window{ + public: + using EventCallbackFn=std::function; + virtual ~Window(){} + + virtual void OnUpdate()=0; + virtual unsigned int GetWidth()const=0; + virtual unsigned int GetHeight()const=0; + + virtual void* GetNativeWindow()const=0; + + virtual void SetEventCallback(const EventCallbackFn& callback)=0; + + virtual void SetSync(bool enable)=0; + + virtual bool IsVSync()const=0; + + virtual void Show()=0; + + static Window* Create(const WindowProps& props=WindowProps()); + }; +} diff --git a/MetalLearning/src/MEL/Application.h b/MetalLearning/src/MEL/Application.h index 425dfc2..a6c0ecb 100644 --- a/MetalLearning/src/MEL/Application.h +++ b/MetalLearning/src/MEL/Application.h @@ -1,19 +1,25 @@ #pragma once +#include "Window.h" #include "Events/Event.h" #include "Events/KeyEvent.h" #include "Events/MouseEvent.h" #include "Events/ApplicationEvent.h" +namespace MEL{ + class Window; +} namespace MEL { class Application{ public: Application(); virtual ~Application(); + void OnEvent(Event& e); - void OnEvent(MEL::Event& e); void Run(); private: bool OnWindowClose(WindowCloseEvent& event); - bool m_Running; + private: + std::unique_ptr m_Window; + bool m_Running=true; }; Application* CreateApplication(); } diff --git a/MetalLearning/src/MEL/Application.mm b/MetalLearning/src/MEL/Application.mm index 11b59fd..ac421e0 100644 --- a/MetalLearning/src/MEL/Application.mm +++ b/MetalLearning/src/MEL/Application.mm @@ -6,27 +6,32 @@ namespace MEL{ Application::Application(){ - + m_Window=std::unique_ptr(Window::Create()); + m_Window->SetEventCallback([this](MEL::Event& e){ + this->OnEvent(e); + }); } Application::~Application(){ } - void Application::OnEvent(MEL::Event& e){ + void Application::OnEvent(Event &e){ + EventDispatcher dispathcher(e); + dispathcher.Dispatch(MEL_BIND_EVENT_FN(Application::OnWindowClose)); + MEL_CORE_INFO("{0}",e.ToString()); } void Application::Run() { - /* + m_Window->Show(); NSApplication* application=[NSApplication sharedApplication]; AppDelegate* appDelegate=[[AppDelegate alloc] init]; [application setDelegate:appDelegate]; - [application run]; - */ while (m_Running){ + m_Window->OnUpdate(); } } diff --git a/MetalLearning/src/MEL/Events/Event.h b/MetalLearning/src/MEL/Events/Event.h index ed71dd3..8d0daaf 100644 --- a/MetalLearning/src/MEL/Events/Event.h +++ b/MetalLearning/src/MEL/Events/Event.h @@ -53,7 +53,7 @@ namespace MEL { { } - template + template bool Dispatch(EventFn func) { if (m_Event.GetEventType() == T::GetStaticType()) diff --git a/MetalLearning/src/MEL/Input/Input.cpp b/MetalLearning/src/MEL/Input/Input.cpp new file mode 100644 index 0000000..0da0418 --- /dev/null +++ b/MetalLearning/src/MEL/Input/Input.cpp @@ -0,0 +1 @@ +#include "Input.h" diff --git a/MetalLearning/src/MEL/Input/Input.h b/MetalLearning/src/MEL/Input/Input.h new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/MetalLearning/src/MEL/Input/Input.h @@ -0,0 +1 @@ + diff --git a/MetalLearning/src/MEL/Layer/Layer.cpp b/MetalLearning/src/MEL/Layer/Layer.cpp new file mode 100644 index 0000000..a062ed6 --- /dev/null +++ b/MetalLearning/src/MEL/Layer/Layer.cpp @@ -0,0 +1,4 @@ +#include "melpch.h" +#include "Layer.h" + + diff --git a/MetalLearning/src/MEL/Layer/Layer.h b/MetalLearning/src/MEL/Layer/Layer.h new file mode 100644 index 0000000..55b1689 --- /dev/null +++ b/MetalLearning/src/MEL/Layer/Layer.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Core.h" +#include "Event.h" + +namespace MEL { + class Layer{ + public: + Layer(const std::string& name="Layer"); + virtual ~Layer(); + virtual void OnAttach(){} + virtual void OnDetach(){} + virtual void OnUpdate(){} + virtual void OnEvent(Event& event){} + inline const std::string& GetName()const{return m_DebugName;} + protected: + std::string m_DebugName; + }; +} diff --git a/MetalLearning/src/MEL/Log/Log.h b/MetalLearning/src/MEL/Log/Log.h index 1359b17..7a5d155 100644 --- a/MetalLearning/src/MEL/Log/Log.h +++ b/MetalLearning/src/MEL/Log/Log.h @@ -4,14 +4,14 @@ #include #include "Core.h" #include "spdlog/spdlog.h" -#include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/fmt/ostr.h" //if occurr "use quotes" error in spdlog source file,add "include" dir to "header search path" to solve it namespace MEL { class Log{ public: static void Init(); - inline static std::shared_ptr& GetCoreLogger(){return s_CoreLogger;} - inline static std::shared_ptr& GetClientLogger(){return s_ClientLogger;} + static std::shared_ptr& GetCoreLogger(); + static std::shared_ptr& GetClientLogger(); private: static std::shared_ptr s_CoreLogger; @@ -28,7 +28,7 @@ namespace MEL { //Client log macros #define MEL_TRACE(...) ::MEL::Log::GetClientLogger()->trace(__VA_ARGS__) -#define MEL_INFO(...) ::MEL::Log::GetClientLogger()->trace(__VA_ARGS__) -#define MEL_WARN(...) ::MEL::Log::GetClientLogger()->trace(__VA_ARGS__) -#define MEL_ERROR(...) ::MEL::Log::GetClientLogger()->trace(__VA_ARGS__) +#define MEL_INFO(...) ::MEL::Log::GetClientLogger()->info(__VA_ARGS__) +#define MEL_WARN(...) ::MEL::Log::GetClientLogger()->error(__VA_ARGS__) +#define MEL_ERROR(...) ::MEL::Log::GetClientLogger()->error(__VA_ARGS__) diff --git a/MetalLearning/src/MEL/Log/Log.mm b/MetalLearning/src/MEL/Log/Log.mm index c2f84e6..1668654 100644 --- a/MetalLearning/src/MEL/Log/Log.mm +++ b/MetalLearning/src/MEL/Log/Log.mm @@ -1,4 +1,5 @@ #include "Log.h" +#include "spdlog/sinks/stdout_color_sinks.h" namespace MEL { @@ -12,4 +13,6 @@ namespace MEL { s_ClientLogger=spdlog::stdout_color_mt("APP"); s_ClientLogger->set_level(spdlog::level::trace); } + std::shared_ptr& Log::GetCoreLogger() { return s_CoreLogger; } + std::shared_ptr& Log::GetClientLogger() { return s_ClientLogger; } } diff --git a/MetalLearning/src/MEL/Renderer/AppDelegate.mm b/MetalLearning/src/MEL/Renderer/AppDelegate.mm index 7648ec7..d0ba4fd 100644 --- a/MetalLearning/src/MEL/Renderer/AppDelegate.mm +++ b/MetalLearning/src/MEL/Renderer/AppDelegate.mm @@ -1,14 +1,9 @@ #import "AppDelegate.h" -#import "Window/WindowManager.h" +#include "Window.h" @implementation AppDelegate -(void)applicationDidFinishLaunching:(NSNotification *)notification{ - NSRect frame=NSMakeRect(0, 0, 1280, 720); - - WindowManager* windowManager=[[WindowManager alloc] initWithFrame:frame title:@"Metal Learning - first triangle"]; - - [windowManager show]; } -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{ diff --git a/MetalLearning/src/MEL/Renderer/ViewController.mm b/MetalLearning/src/MEL/Renderer/ViewController.mm index 2b8a5ca..5a8e29a 100644 --- a/MetalLearning/src/MEL/Renderer/ViewController.mm +++ b/MetalLearning/src/MEL/Renderer/ViewController.mm @@ -29,4 +29,10 @@ self.metalView.frame=self.view.bounds; } +-(void)loadView{ + self.view=[[NSView alloc] init]; + self.view.wantsLayer=YES; + //self.view.layer.backgroundColor=[[NSColor blueColor] CGColor]; +} + @end diff --git a/MetalLearning/src/MEL/Window/CocoaWindow.h b/MetalLearning/src/MEL/Window/CocoaWindow.h new file mode 100644 index 0000000..f6ab1ca --- /dev/null +++ b/MetalLearning/src/MEL/Window/CocoaWindow.h @@ -0,0 +1,19 @@ +#pragma once + +#import +namespace MEL { + class Event; +} +typedef void (^MELEventCallback)(MEL::Event& event); + +@interface CocoaWindow :NSWindow + +@property (nonatomic,copy)MELEventCallback eventCallback; + +-(instancetype)initWithFrame: +(NSRect)frame styleMask:(NSWindowStyleMask)styleMask title:(NSString*)title; + +-(void)dispatchEvent: +(MEL::Event&)event; + +@end diff --git a/MetalLearning/src/MEL/Window/Window.mm b/MetalLearning/src/MEL/Window/CocoaWindow.mm similarity index 76% rename from MetalLearning/src/MEL/Window/Window.mm rename to MetalLearning/src/MEL/Window/CocoaWindow.mm index 4bb27a1..7bb9e5d 100644 --- a/MetalLearning/src/MEL/Window/Window.mm +++ b/MetalLearning/src/MEL/Window/CocoaWindow.mm @@ -1,10 +1,10 @@ -#import "Window.h" +#import "CocoaWindow.h" #import "Events/KeyEvent.h" #import "Events/MouseEvent.h" #import "Events/ApplicationEvent.h" -@implementation Window +@implementation CocoaWindow - (instancetype)initWithFrame:(NSRect)frame styleMask:(NSWindowStyleMask)styleMask title:(NSString *)title { self=[super initWithContentRect:frame @@ -84,40 +84,3 @@ } } @end - - -namespace MEL { - Window* Window::Create(const WindowProps& props){ - return new Window(props); - } - Window::Window(const WindowProps& props){ - Init(props); - } - Window::~Window(){ - ShutDown(); - } - - void Window::Init(const WindowProps &props){ - m_Data.Title=props.Title; - m_Data.Width=props.Width; - m_Data.Height=props.Height; - MEL_CORE_INFO("Create Window: {0},{1},{2}",m_Data.Title,m_Data.Width,m_Data.Height); - - } - - void Window::ShutDown(){ - - } - - void Window::OnUpdate(){ - - } - - void Window::SetSync(bool enable){ - m_Data.VSync=enable; - } - - bool Window::IsVSync()const{ - return m_Data.VSync; - } -} diff --git a/MetalLearning/src/MEL/Window/MacWindow.h b/MetalLearning/src/MEL/Window/MacWindow.h new file mode 100644 index 0000000..e600fef --- /dev/null +++ b/MetalLearning/src/MEL/Window/MacWindow.h @@ -0,0 +1,42 @@ +#pragma once + +#include "melpch.h" +#include "MEL.h" +#include "Window.h" +namespace MEL { + class MacWindow:public Window{ + public: + MacWindow(const WindowProps& props); + virtual ~MacWindow(); + + void OnUpdate() override; + unsigned int GetWidth() const override {return m_Data.Width;} + unsigned int GetHeight()const override {return m_Data.Height;} + + void* GetNativeWindow() const override {return m_Window;} + + void SetEventCallback(const EventCallbackFn& callback) override {m_Data.EventCallback =callback;} + + void SetSync(bool enable) override; + + bool IsVSync()const override; + + static Window* Create(const WindowProps& props=WindowProps()); + + void Show()override; + + private: + virtual void Init(const WindowProps& props); + virtual void ShutDown(); + private: + NSWindow* m_Window; + struct WindowData{ + std::string Title; + unsigned int Width,Height; + bool VSync; + EventCallbackFn EventCallback; + }; + WindowData m_Data; + }; +} + diff --git a/MetalLearning/src/MEL/Window/MacWindow.mm b/MetalLearning/src/MEL/Window/MacWindow.mm new file mode 100644 index 0000000..d45ba41 --- /dev/null +++ b/MetalLearning/src/MEL/Window/MacWindow.mm @@ -0,0 +1,81 @@ +#include "MacWindow.h" +#import "CocoaWindow.h" +#import "ViewController.h" + +namespace MEL { + Window* Window::Create(const WindowProps& props){ + return new MacWindow(props); + } + MacWindow::MacWindow(const WindowProps& props){ + Init(props); + } + MacWindow::~MacWindow(){ + ShutDown(); + } + + void MacWindow::Init(const WindowProps &props){ + m_Data.Title=props.Title; + m_Data.Width=props.Width; + m_Data.Height=props.Height; + MEL_CORE_INFO("Create Window: {0},{1},{2}",props.Title,props.Width,props.Height); + + NSRect frame=NSMakeRect(0, 0, props.Width, props.Height); + + NSWindowStyleMask style= + NSWindowStyleMaskTitled | + NSWindowStyleMaskClosable | + NSWindowStyleMaskResizable | + NSWindowStyleMaskMiniaturizable; + + NSString* title =[NSString stringWithUTF8String:props.Title.c_str()]; + + m_Window=[[CocoaWindow alloc] initWithFrame:frame + styleMask:style + title:title]; + + ViewController* viewController=[[ViewController alloc] init]; + NSView* contentView=[[NSView alloc] initWithFrame:frame]; + + viewController.view.frame=contentView.bounds; + + viewController.view.autoresizingMask=NSViewWidthSizable|NSViewHeightSizable; + + m_Window.contentView=contentView; + m_Window.contentViewController=viewController; + + + if([m_Window isKindOfClass:[CocoaWindow class]]){ + CocoaWindow* cocoaWindow=(CocoaWindow*)m_Window; + + cocoaWindow.eventCallback=^(MEL::Event& event){ + if(m_Data.EventCallback) + m_Data.EventCallback(event); + }; + } + } + + void MacWindow::ShutDown(){ + + } + + void MacWindow::OnUpdate(){ + + } + + void MacWindow::SetSync(bool enable){ + m_Data.VSync=enable; + } + + bool MacWindow::IsVSync()const{ + return m_Data.VSync; + } + + void MacWindow::Show(){ + if(m_Window){ + if([NSThread isMainThread]){ + [m_Window makeKeyAndOrderFront:nil]; + } + } + } +} + diff --git a/MetalLearning/src/MEL/Window/Window.h b/MetalLearning/src/MEL/Window/Window.h deleted file mode 100644 index 4a97226..0000000 --- a/MetalLearning/src/MEL/Window/Window.h +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once - -#import -namespace MEL { - class Event; -} -typedef void (*EventCallback)(MEL::Event& event); - -@interface Window :NSWindow - -@property (nonatomic,assign)EventCallback eventCallback; - --(instancetype)initWithFrame: -(NSRect)frame styleMask:(NSWindowStyleMask)styleMask title:(NSString*)title; - --(void)dispatchEvent: -(MEL::Event&)event; - -@end - -#include "melpch.h" -#include "MEL.h" -namespace MEL { - struct WindowProps{ - std::string Title; - unsigned int Width; - unsigned int Height; - WindowProps(const std::string &title="MEL Engine",unsigned int width=1280,unsigned int height=720) - :Title(title),Width(width),Height(height){} - }; - class Window{ - public: - using EventCallbackFN=std::function; - Window(const WindowProps& props); - ~Window(); - - void OnUpdate(); - unsigned int GetWidth(); - unsigned int GetHeight(); - - void SetEventCallback(const EventCallback& callback); - - void SetSync(bool enable); - - bool IsVSync()const; - - static Window* Create(const WindowProps& props=WindowProps()); - - private: - virtual void Init(const WindowProps& props); - virtual void ShutDown(); - private: - NSWindow* m_Window; - struct WindowData{ - std::string Title; - unsigned int Width,Height; - bool VSync; - EventCallbackFN EventCallback; - }; - WindowData m_Data; - }; -} diff --git a/MetalLearning/src/MEL/Window/WindowManager.h b/MetalLearning/src/MEL/Window/WindowManager.h deleted file mode 100644 index 9c9525b..0000000 --- a/MetalLearning/src/MEL/Window/WindowManager.h +++ /dev/null @@ -1,14 +0,0 @@ -#import - -@class ViewController; - -@interface WindowManager:NSObject - -@property (nonatomic,strong,readonly)NSWindow* window; -@property (nonatomic,strong,readonly)ViewController* viewController; - --(instancetype)initWithFrame: -(NSRect)frame title:(NSString*)title; --(void)show; - -@end diff --git a/MetalLearning/src/MEL/Window/WindowManager.mm b/MetalLearning/src/MEL/Window/WindowManager.mm deleted file mode 100644 index 337ae0a..0000000 --- a/MetalLearning/src/MEL/Window/WindowManager.mm +++ /dev/null @@ -1,37 +0,0 @@ -#import "WindowManager.h" -#import "Window.h" -#import "ViewController.h" - -@implementation WindowManager - -- (instancetype)initWithFrame:(NSRect)frame title:(NSString *)title { - self=[super init]; - if(self){ - NSWindowStyleMask style= - NSWindowStyleMaskTitled | - NSWindowStyleMaskClosable | - NSWindowStyleMaskResizable | - NSWindowStyleMaskMiniaturizable; - - _window=[[Window alloc] initWithFrame:frame styleMask:style title:title]; - _viewController=[[ViewController alloc] init]; - - NSView* contentView=[[NSView alloc] initWithFrame:frame]; - [contentView addSubview:_viewController.view]; - _viewController.view.frame=contentView.bounds; - - _viewController.view.autoresizingMask=NSViewWidthSizable|NSViewHeightSizable; - - _window.contentView=contentView; - _window.contentViewController=_viewController; - } - return self; -} - - -- (void)show { - [_window makeKeyAndOrderFront:nil]; -} - - -@end diff --git a/README.md b/README.md new file mode 100644 index 0000000..65e2385 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# MEL_MetalEngineLearning +I'll create a engine using Metal for rendering,as Metal is not well-supported in Hazel.I hope I can do that.:P