From 1855ff133ec25ad68f8f482ae0eef6b19b0d7b7e Mon Sep 17 00:00:00 2001 From: Menotdan Date: Wed, 10 May 2023 12:46:54 -0400 Subject: [PATCH] Tweak a few classes, add the Base classes. --- .../Engine/Platform/Base/ScreenUtilsBase.cpp | 0 Source/Engine/Platform/Base/ScreenUtilsBase.h | 0 Source/Engine/Platform/ScreenUtils.cpp | 0 Source/Engine/Platform/ScreenUtils.h | 17 ++++ .../Platform/Windows/WindowsScreenUtils.cpp | 22 ++++++ .../Platform/Windows/WindowsScreenUtils.h | 8 +- Source/Engine/Utilities/Screenshot.cpp | 79 ------------------- Source/Engine/Utilities/Screenshot.h | 10 --- 8 files changed, 45 insertions(+), 91 deletions(-) create mode 100644 Source/Engine/Platform/Base/ScreenUtilsBase.cpp create mode 100644 Source/Engine/Platform/Base/ScreenUtilsBase.h create mode 100644 Source/Engine/Platform/ScreenUtils.cpp create mode 100644 Source/Engine/Platform/ScreenUtils.h diff --git a/Source/Engine/Platform/Base/ScreenUtilsBase.cpp b/Source/Engine/Platform/Base/ScreenUtilsBase.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Source/Engine/Platform/Base/ScreenUtilsBase.h b/Source/Engine/Platform/Base/ScreenUtilsBase.h new file mode 100644 index 000000000..e69de29bb diff --git a/Source/Engine/Platform/ScreenUtils.cpp b/Source/Engine/Platform/ScreenUtils.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Source/Engine/Platform/ScreenUtils.h b/Source/Engine/Platform/ScreenUtils.h new file mode 100644 index 000000000..91192dd41 --- /dev/null +++ b/Source/Engine/Platform/ScreenUtils.h @@ -0,0 +1,17 @@ +#if PLATFORM_WINDOWS +#include "Windows/WindowsScreenUtils.h" +#elif PLATFORM_LINUX +#include "Linux/LinuxScreenUtils.h" +#elif PLATFORM_MAC +#include "Mac/MacScreenUtils.h" +#else +#error No Screen Utils For This Platform! Please ensure you are not targetting an Editor build. +#endif + +#include "Types.h" +#include "Engine/Scripting/ScriptingType.h" + +API_CLASS(Static) class FLAXENGINE_API ScreenUtils +{ + DECLARE_SCRIPTING_TYPE_NO_SPAWN(ScreenUtils); +}; diff --git a/Source/Engine/Platform/Windows/WindowsScreenUtils.cpp b/Source/Engine/Platform/Windows/WindowsScreenUtils.cpp index e69de29bb..8f9798184 100644 --- a/Source/Engine/Platform/Windows/WindowsScreenUtils.cpp +++ b/Source/Engine/Platform/Windows/WindowsScreenUtils.cpp @@ -0,0 +1,22 @@ +#include "WindowsScreenUtils.h" +#include "Engine/Core/Math/Color32.h" +#include "Engine/Core/Math/Vector2.h" + +#include + +Color32 PlatformScreenUtils::GetPixelAt(int32 x, int32 y) { + HDC deviceContext = GetDC(NULL); + COLORREF color = GetPixel(deviceContext, x, y); + ReleaseDC(NULL, deviceContext); + + Color32 returnColor = { GetRValue(color), GetGValue(color), GetBValue(color), 255 }; + return returnColor; +} + +Int2 PlatformScreenUtils::GetScreenCursorPosition() { + POINT cursorPos; + GetCursorPos(&cursorPos); + + Int2 returnCursorPos = { cursorPos.x, cursorPos.y }; + return returnCursorPos; +} diff --git a/Source/Engine/Platform/Windows/WindowsScreenUtils.h b/Source/Engine/Platform/Windows/WindowsScreenUtils.h index 4e8841525..690be0b88 100644 --- a/Source/Engine/Platform/Windows/WindowsScreenUtils.h +++ b/Source/Engine/Platform/Windows/WindowsScreenUtils.h @@ -1,5 +1,9 @@ #pragma once -class WindowsScreenUtils -{ +#include "Engine/Core/Math/Color32.h" + +class PlatformScreenUtils { +public: + static Color32 GetPixelAt(int32 x, int32 y); + static Int2 GetScreenCursorPosition(); }; diff --git a/Source/Engine/Utilities/Screenshot.cpp b/Source/Engine/Utilities/Screenshot.cpp index 0635180a8..fbde8043b 100644 --- a/Source/Engine/Utilities/Screenshot.cpp +++ b/Source/Engine/Utilities/Screenshot.cpp @@ -130,70 +130,6 @@ void CaptureScreenshot::OnFail() ThreadPoolTask::OnFail(); } -Delegate Screenshot::PixelReadDelegate; - -/// -/// Capture screenshot helper -/// -/// -class GetPixelData : public ThreadPoolTask -{ - friend Screenshot; -private: - TextureData _data; - Color32 _color; - -public: - int32 x; - int32 y; - -public: - /// - /// Gets the texture data container. - /// - /// Texture data - FORCE_INLINE TextureData& GetData() - { - return _data; - } - - FORCE_INLINE Color32 GetColor() - { - return _color; - } - -protected: - // [ThreadPoolTask] - bool Run() override; - void OnFail() override; -}; - -bool GetPixelData::Run() -{ - LOG(Warning, "REAL"); - TextureMipData *mipData = _data.GetData(0, 0); - Array pixels; - mipData->GetPixels(pixels, _data.Width, _data.Height, _data.Format); - - LOG(Warning, "{0}, {1} ({2} at {3})", x, y, pixels.Count(), (y * _data.Width) + x); - _color = pixels[(y * _data.Width) + x]; - LOG(Warning, "really real"); - LOG(Warning, "Color: R: {0}, G: {1}, B: {2}", _color.R, _color.G, _color.B); - - LOG(Warning, "Bound functions: {0}", Screenshot::PixelReadDelegate.Count()); - Screenshot::PixelReadDelegate(_color); - return false; -} - -void GetPixelData::OnFail() -{ - LOG(Warning, "Cannot get pixel data."); - - // Base - ThreadPoolTask::OnFail(); -} - - void Screenshot::Capture(GPUTexture* target, const StringView& path) { // Validate @@ -307,18 +243,3 @@ void Screenshot::Capture(const StringView& path) Capture(mainTask, path); } - -Color32 Screenshot::GetPixelAt(int32 x, int32 y) { - GPUSwapChain* swapChain = Engine::MainWindow->GetSwapChain(); - - auto getPixelTask = New(); - getPixelTask->x = x; - getPixelTask->y = y; - - Task* downloadTask = swapChain->DownloadDataAsync(getPixelTask->GetData()); - downloadTask->ContinueWith(getPixelTask); - LOG(Warning, "Started download task. real"); - downloadTask->Start(); - - return getPixelTask->GetColor(); -} diff --git a/Source/Engine/Utilities/Screenshot.h b/Source/Engine/Utilities/Screenshot.h index 4fe497355..c51c34199 100644 --- a/Source/Engine/Utilities/Screenshot.h +++ b/Source/Engine/Utilities/Screenshot.h @@ -39,14 +39,4 @@ API_CLASS(Static) class FLAXENGINE_API Screenshot /// /// The custom file location. Use null or empty to use default one. API_FUNCTION() static void Capture(const StringView& path = StringView::Empty); - - /// - /// Get the pixel at specified coordinates. - /// - /// The x coordinate to read. - /// The y coordinate to read. - /// The color - API_FUNCTION() static Color32 GetPixelAt(int32 x, int32 y); - - API_EVENT() static Delegate PixelReadDelegate; };