From 51f91b120afa6740a47743e307f5c721e3c666b8 Mon Sep 17 00:00:00 2001 From: REDDRAGON Date: Wed, 1 Nov 2017 12:16:04 +0100 Subject: [PATCH] Added screenshot functionality --- MAGE/MAGE/src/rendering/swap_chain.cpp | 21 +++++ MAGE/MAGE/src/rendering/swap_chain.hpp | 9 ++ MAGE/MAGE/src/system/system_time.cpp | 106 +++++++++++++++++++++++ MAGE/MAGE/src/system/system_time.hpp | 21 +++++ MAGE/MAGE/src/texture/texture_loader.cpp | 2 +- MAGE/MAGE/src/ui/main_window.cpp | 33 +++++++ 6 files changed, 191 insertions(+), 1 deletion(-) diff --git a/MAGE/MAGE/src/rendering/swap_chain.cpp b/MAGE/MAGE/src/rendering/swap_chain.cpp index 1f0931a75..94a544e51 100644 --- a/MAGE/MAGE/src/rendering/swap_chain.cpp +++ b/MAGE/MAGE/src/rendering/swap_chain.cpp @@ -4,6 +4,8 @@ #pragma region #include "rendering\rendering_manager.hpp" +#include "texture\texture_loader.hpp" +#include "system\system_time.hpp" #include "logging\error.hpp" #include "logging\exception.hpp" @@ -159,6 +161,25 @@ namespace mage { m_swap_chain->Present(sync_interval, 0u); } + void SwapChain::TakeScreenShot() const { + ComPtr< ID3D11Texture2D > back_buffer; + { + // Access the only back buffer of the swap-chain. + const HRESULT result = m_swap_chain->GetBuffer( + 0u, __uuidof(ID3D11Texture2D), + (void **)back_buffer.GetAddressOf()); + ThrowIfFailed(result, + "Back buffer texture creation failed: %08X.", result); + } + + const wstring fname = L"screenshot-" + + GetCurrentLocalSystemDateAndTimeAsString() + + L".png"; + + ExportTextureToFile(fname, + Pipeline::GetImmediateDeviceContext(), back_buffer.Get()); + } + //------------------------------------------------------------------------- // Member Methods: Display Configuration //------------------------------------------------------------------------- diff --git a/MAGE/MAGE/src/rendering/swap_chain.hpp b/MAGE/MAGE/src/rendering/swap_chain.hpp index 33c8c9bc8..4d8bb4161 100644 --- a/MAGE/MAGE/src/rendering/swap_chain.hpp +++ b/MAGE/MAGE/src/rendering/swap_chain.hpp @@ -205,6 +205,15 @@ namespace mage { */ void Present() const noexcept; + /** + Takes a screenshot of the current back buffer of this swap chain. + + @throws FormattedException + Failed to take a screenshot of the current back buffer + of this swap chain. + */ + void TakeScreenShot() const; + private: //--------------------------------------------------------------------- diff --git a/MAGE/MAGE/src/system/system_time.cpp b/MAGE/MAGE/src/system/system_time.cpp index a59531fdc..9ddc51f73 100644 --- a/MAGE/MAGE/src/system/system_time.cpp +++ b/MAGE/MAGE/src/system/system_time.cpp @@ -36,6 +36,112 @@ namespace mage { return ConvertTimestamp(ftime); } + const wstring GetCurrentLocalSystemDateAsString() noexcept { + + FILETIME ftime; + // Retrieves the current system date and time. + // The information is in Coordinated Universal Time (UTC) format. + GetSystemTimeAsFileTime(&ftime); + + FILETIME local_ftime; + { + const BOOL result = FileTimeToLocalFileTime(&ftime, &local_ftime); + if (!result) { + return wstring(); + } + } + + SYSTEMTIME local_stime; + { + const BOOL result = FileTimeToSystemTime(&local_ftime, &local_stime); + if (!result) { + return wstring(); + } + } + + wchar_t str_date[255]; + + const int result = GetDateFormat(LOCALE_USER_DEFAULT, 0, + &local_stime, L"yyyy-MM-dd", str_date, _countof(str_date)); + return (result) ? wstring(str_date) : wstring(); + } + + const wstring GetCurrentLocalSystemTimeAsString() noexcept { + + FILETIME ftime; + // Retrieves the current system date and time. + // The information is in Coordinated Universal Time (UTC) format. + GetSystemTimeAsFileTime(&ftime); + + FILETIME local_ftime; + { + const BOOL result = FileTimeToLocalFileTime(&ftime, &local_ftime); + if (!result) { + return wstring(); + } + } + + SYSTEMTIME local_stime; + { + const BOOL result = FileTimeToSystemTime(&local_ftime, &local_stime); + if (!result) { + return wstring(); + } + } + + wchar_t str_time[255]; + + const int result = GetTimeFormat(LOCALE_USER_DEFAULT, 0, + &local_stime, L"HH-mm-ss", str_time, _countof(str_time)); + return (result) ? wstring(str_time) : wstring(); + } + + const wstring GetCurrentLocalSystemDateAndTimeAsString() noexcept { + + FILETIME ftime; + // Retrieves the current system date and time. + // The information is in Coordinated Universal Time (UTC) format. + GetSystemTimeAsFileTime(&ftime); + + FILETIME local_ftime; + { + const BOOL result = FileTimeToLocalFileTime(&ftime, &local_ftime); + if (!result) { + return wstring(); + } + } + + SYSTEMTIME local_stime; + { + const BOOL result = FileTimeToSystemTime(&local_ftime, &local_stime); + if (!result) { + return wstring(); + } + } + + wchar_t str_date[255]; + wchar_t str_time[255]; + + { + const int result + = GetDateFormat(LOCALE_USER_DEFAULT, 0, + &local_stime, L"yyyy-MM-dd", str_date, _countof(str_date)); + if (!result) { + return wstring(); + } + } + { + const int result + = GetTimeFormat(LOCALE_USER_DEFAULT, 0, + &local_stime, L"HH-mm-ss", str_time, _countof(str_time)); + if (!result) { + return wstring(); + } + } + + return wstring(str_date) + L'-' + wstring(str_time); + } + void GetCurrentCoreTimestamp(HANDLE handle_process, U64 *kernel_mode_timestamp, U64 *user_mode_timestamp) noexcept { diff --git a/MAGE/MAGE/src/system/system_time.hpp b/MAGE/MAGE/src/system/system_time.hpp index 8849d5f1b..f55419a56 100644 --- a/MAGE/MAGE/src/system/system_time.hpp +++ b/MAGE/MAGE/src/system/system_time.hpp @@ -21,6 +21,27 @@ namespace mage { */ U64 GetCurrentSystemTimestamp() noexcept; + /** + Returns the current local system date as a string. + + @return The current local system date as a string. + */ + const wstring GetCurrentLocalSystemDateAsString() noexcept; + + /** + Returns the current local system time as a string. + + @return The current local system time as a string. + */ + const wstring GetCurrentLocalSystemTimeAsString() noexcept; + + /** + Returns the current local system date and time as a string. + + @return The current local system date and time as a string. + */ + const wstring GetCurrentLocalSystemDateAndTimeAsString() noexcept; + /** Returns the current core timestamp (in 100 ns). diff --git a/MAGE/MAGE/src/texture/texture_loader.cpp b/MAGE/MAGE/src/texture/texture_loader.cpp index 02d9c4637..2f75a2ea1 100644 --- a/MAGE/MAGE/src/texture/texture_loader.cpp +++ b/MAGE/MAGE/src/texture/texture_loader.cpp @@ -61,7 +61,7 @@ namespace mage { */ inline const GUID GetGUIDContainerFormat(const wstring &extension) noexcept { if (extension == L"png" || extension == L"PNG") { - return GUID_ContainerFormatGif; + return GUID_ContainerFormatPng; } else if (extension == L"jpe" || extension == L"JPE" || extension == L"jpeg" || extension == L"JPEG" diff --git a/MAGE/MAGE/src/ui/main_window.cpp b/MAGE/MAGE/src/ui/main_window.cpp index 761f04ec7..4e6677bfe 100644 --- a/MAGE/MAGE/src/ui/main_window.cpp +++ b/MAGE/MAGE/src/ui/main_window.cpp @@ -60,6 +60,21 @@ namespace mage { break; } + case WM_KEYUP: { + // Sent to the window with the keyboard focus when a nonsystem key + // is released. + + // Check whether the user wants to take a screenshot. + if (wParam == VK_SNAPSHOT) { + SwapChain::Get()->TakeScreenShot(); + } + + // Calls the default window procedure to provide default processing + // for any window messages that an application does not process. + // This function ensures that every message is processed. + return DefWindowProc(hWnd, msg, wParam, lParam); + } + case WM_MENUCHAR: { // Sent when a menu is active and the user presses a key // that does not correspond to any mnemonic or accelerator key. @@ -85,6 +100,24 @@ namespace mage { break; } + case WM_SYSKEYUP: { + // Sent to the window with the keyboard focus when the user + // releases a key that was pressed while the ALT key was held down. + // It also occurs when no window currently has the keyboard focus; + // in this case, the WM_SYSKEYUP message is sent to the active + // window. + + // Check whether the user wants to take a screenshot. + if (wParam == VK_SNAPSHOT) { + SwapChain::Get()->TakeScreenShot(); + } + + // Calls the default window procedure to provide default processing + // for any window messages that an application does not process. + // This function ensures that every message is processed. + return DefWindowProc(hWnd, msg, wParam, lParam); + } + case WM_SYSKEYDOWN: { // Sent to the window with the keyboard focus when the user presses // the F10 key (which activates the menu bar) or holds down the ALT