From 07ecf2a3d64cb471856e15ddf131d876f5d577f5 Mon Sep 17 00:00:00 2001 From: OldSerpskiStalker Date: Tue, 3 Sep 2024 19:07:09 +0500 Subject: [PATCH] Added CustomWindow and NativeWindow --- src/RenderLibrary/Exports.hpp | 17 +++- src/RenderLibrary/FabricCustomWindow.cpp | 98 +++++++++++++++++++ src/RenderLibrary/FabricCustomWindow.hpp | 1 - src/RenderLibrary/FabricNativeWindow.cpp | 13 +-- src/RenderLibrary/Memory.cpp | 24 ++--- src/RenderLibrary/RenderLibrary.vcxproj | 2 + .../RenderLibrary.vcxproj.filters | 2 + src/RenderTestApp/StartUp.cpp | 15 +++ 8 files changed, 144 insertions(+), 28 deletions(-) diff --git a/src/RenderLibrary/Exports.hpp b/src/RenderLibrary/Exports.hpp index fe835fb..d818dbf 100644 --- a/src/RenderLibrary/Exports.hpp +++ b/src/RenderLibrary/Exports.hpp @@ -6,11 +6,22 @@ #define API __declspec(dllimport) #endif +/* Нативное окно для Авалонии */ extern "C" { - API bool Frame(); - API void MessageLoop(); + API bool FrameNative(); + API void MessageNativeLoop(); API HWND CreateNativeWindow(); API void DestroyNativeWindow(); - API void CreateScene(HWND ptr); + API void CreateNativeScene(HWND ptr); +} + +/*Кастомное окно для отдельных тестов */ +extern "C" +{ + API bool FrameCustom(); + API void MessageCustomLoop(); + API HWND CreateCustomWindow(); + API void DestroyCustomWindow(); + API void CreateCustomScene(HWND pointer); } diff --git a/src/RenderLibrary/FabricCustomWindow.cpp b/src/RenderLibrary/FabricCustomWindow.cpp index 02061cf..c213a8d 100644 --- a/src/RenderLibrary/FabricCustomWindow.cpp +++ b/src/RenderLibrary/FabricCustomWindow.cpp @@ -21,3 +21,101 @@ using namespace DirectX; #include "FabricCustomWindow.hpp" using namespace RenderLibary; + +API HWND CreateCustomWindow() +{ + _renderCustomObject._wndClassExW.cbSize = sizeof(WNDCLASSEX); + _renderCustomObject._wndClassExW.style = CS_HREDRAW | CS_VREDRAW; + + _renderCustomObject._wndClassExW.lpfnWndProc = [](HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -> LRESULT + { + switch (message) + { + case WM_CREATE: + case WM_CLOSE: + case WM_ACTIVATE: + case WM_MOVE: + case WM_SIZE: + case WM_MOUSEMOVE: + case WM_LBUTTONUP: + case WM_LBUTTONDOWN: + case WM_MBUTTONUP: + case WM_MBUTTONDOWN: + case WM_RBUTTONUP: + case WM_RBUTTONDOWN: + case WM_MOUSEWHEEL: + case WM_KEYDOWN: + case WM_KEYUP: + break; + default: + return DefWindowProcW(hWnd, message, wParam, lParam); + } + + return 0; + }; + + _renderCustomObject._wndClassExW.cbClsExtra = 0; + _renderCustomObject._wndClassExW.cbWndExtra = 0; + _renderCustomObject._wndClassExW.hInstance = GetModuleHandle(nullptr); + _renderCustomObject._wndClassExW.hbrBackground = CreateSolidBrush(RGB(0, 156, 65)); + _renderCustomObject._wndClassExW.lpszClassName = _renderCustomObject._wndClassName; + _renderCustomObject._wndClassExW.hCursor = LoadCursor(NULL, IDC_ARROW); + _renderCustomObject._wndClassExW.hIcon = LoadIcon(NULL, IDI_APPLICATION); + + RegisterClassExW(&_renderCustomObject._wndClassExW); + +#ifdef _DEBUG + constexpr auto _debug = true; +#else + constexpr auto _debug = false; +#endif + + auto ACreateWindowW = []() -> HWND + { + return CreateWindowW + ( + _renderCustomObject._wndClassName, + _debug ? L"Main Render (Debug)" : L"Main Render (Release)", + WS_OVERLAPPEDWINDOW | WS_VISIBLE, + CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, + nullptr, + nullptr, + GetModuleHandle(nullptr), + nullptr + ); + }; + + _renderCustomObject._window = ACreateWindowW(); + + ShowWindow(_renderCustomObject._window, SW_SHOWNORMAL); + UpdateWindow(_renderCustomObject._window); + + return _renderCustomObject._window; +} + +API void CreateCustomScene(HWND pointer) +{ + +} + +API void MessageCustomLoop() +{ + while (PeekMessage(&_renderCustomObject._msg, nullptr, 0, 0, PM_REMOVE)) + { + TranslateMessage(&_renderCustomObject._msg); + DispatchMessage(&_renderCustomObject._msg); + } +} + +API void DestroyCustomWindow() +{ + DestroyWindow(_renderCustomObject._window); + _renderCustomObject._window = nullptr; +} + +API bool FrameCustom() +{ + MessageCustomLoop(); + + return true; +} diff --git a/src/RenderLibrary/FabricCustomWindow.hpp b/src/RenderLibrary/FabricCustomWindow.hpp index 1f41fed..a88cdf2 100644 --- a/src/RenderLibrary/FabricCustomWindow.hpp +++ b/src/RenderLibrary/FabricCustomWindow.hpp @@ -8,6 +8,5 @@ namespace RenderLibary HWND _window = {}; WNDCLASSEXW _wndClassExW = {}; const wchar_t* _wndClassName = L"ShaderPlaygroundRender"; - } _renderCustomObject; } diff --git a/src/RenderLibrary/FabricNativeWindow.cpp b/src/RenderLibrary/FabricNativeWindow.cpp index 6aa6b68..2506f87 100644 --- a/src/RenderLibrary/FabricNativeWindow.cpp +++ b/src/RenderLibrary/FabricNativeWindow.cpp @@ -91,12 +91,12 @@ API HWND CreateNativeWindow() return _renderNativeObject._window; } -API void CreateScene(HWND pointer) +API void CreateNativeScene(HWND pointer) { - + } -API void MessageLoop() +API void MessageNativeLoop() { while (PeekMessage(&_renderNativeObject._msg, 0, 0, 0, PM_REMOVE)) { @@ -107,12 +107,13 @@ API void MessageLoop() API void DestroyNativeWindow() { - delete _renderNativeObject._window; + DestroyWindow(_renderNativeObject._window); + _renderNativeObject._window = nullptr; } -API bool Frame() +API bool FrameNative() { - MessageLoop(); + MessageNativeLoop(); return true; } diff --git a/src/RenderLibrary/Memory.cpp b/src/RenderLibrary/Memory.cpp index cf4c51d..07ae366 100644 --- a/src/RenderLibrary/Memory.cpp +++ b/src/RenderLibrary/Memory.cpp @@ -7,14 +7,10 @@ #include "Memory.hpp" -#pragma section(".Hook", read) - using namespace RenderLibary; -Allocator* Allocator::instance = nullptr; - -std::unordered_set Allocator::objectCollection; -std::atomic Allocator::uniqueIDCounter{ 0 }; +std::atomic Allocator::uniqueIDCounter = { 0 }; +std::unordered_set Allocator::objectCollection = { 0 }; void* Allocator::operator new(std::size_t size) { @@ -61,30 +57,22 @@ void Allocator::PrintCollection() { for (const auto& obj : objectCollection) { - //Log::Get()->Debug("~ Object at address: %p", obj); + // Log::Get()->Debug("~ Object at address: %p", obj); } } std::size_t Allocator::GenerateUniqueID() { - return ++uniqueIDCounter; // Увеличиваем и возвращаем уникальный идентификатор + return ++uniqueIDCounter; } Allocator& Allocator::GetInstance() { - if (!instance) - { - instance = new Allocator(); - } - - return *instance; + static Allocator instance; + return instance; } Allocator::Allocator() { std::cout << "Allocator created" << std::endl; } - -#pragma init_seg(lib) -__declspec(allocate(".Hook")) -Allocator& Memory = Allocator::GetInstance(); diff --git a/src/RenderLibrary/RenderLibrary.vcxproj b/src/RenderLibrary/RenderLibrary.vcxproj index b14c6e4..02bfe9f 100644 --- a/src/RenderLibrary/RenderLibrary.vcxproj +++ b/src/RenderLibrary/RenderLibrary.vcxproj @@ -166,6 +166,7 @@ + @@ -178,6 +179,7 @@ + diff --git a/src/RenderLibrary/RenderLibrary.vcxproj.filters b/src/RenderLibrary/RenderLibrary.vcxproj.filters index d0a6f3e..f4d146f 100644 --- a/src/RenderLibrary/RenderLibrary.vcxproj.filters +++ b/src/RenderLibrary/RenderLibrary.vcxproj.filters @@ -14,6 +14,7 @@ + @@ -21,5 +22,6 @@ + \ No newline at end of file diff --git a/src/RenderTestApp/StartUp.cpp b/src/RenderTestApp/StartUp.cpp index e440916..0496051 100644 --- a/src/RenderTestApp/StartUp.cpp +++ b/src/RenderTestApp/StartUp.cpp @@ -16,5 +16,20 @@ using namespace DirectX; void Start::Launch() { + HWND windowHandle = CreateCustomWindow(); + if (!windowHandle) + { + MessageBox(nullptr, L"Не удалось создать окно.", L"Ошибка", MB_ICONERROR); + return; + } + + CreateCustomScene(windowHandle); + + while (FrameCustom()) + { + + } + + DestroyCustomWindow(); }