Skip to content

Commit

Permalink
Added screenshot functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
matt77hias committed Nov 1, 2017
1 parent 5516c82 commit 51f91b1
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 1 deletion.
21 changes: 21 additions & 0 deletions MAGE/MAGE/src/rendering/swap_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
//-------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions MAGE/MAGE/src/rendering/swap_chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:

//---------------------------------------------------------------------
Expand Down
106 changes: 106 additions & 0 deletions MAGE/MAGE/src/system/system_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
21 changes: 21 additions & 0 deletions MAGE/MAGE/src/system/system_time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion MAGE/MAGE/src/texture/texture_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 33 additions & 0 deletions MAGE/MAGE/src/ui/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 51f91b1

Please sign in to comment.