Resync'd ScreenGrab
This commit is contained in:
parent
c64466e096
commit
f732044b20
@ -43,6 +43,17 @@
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
// VS 2010/2012 do not support =default =delete
|
||||
#ifndef DIRECTX_CTOR_DEFAULT
|
||||
#if defined(_MSC_VER) && (_MSC_VER < 1800)
|
||||
#define DIRECTX_CTOR_DEFAULT {}
|
||||
#define DIRECTX_CTOR_DELETE ;
|
||||
#else
|
||||
#define DIRECTX_CTOR_DEFAULT =default;
|
||||
#define DIRECTX_CTOR_DELETE =delete;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "ScreenGrab.h"
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
@ -203,12 +214,64 @@ static const DDS_PIXELFORMAT DDSPF_DX10 =
|
||||
{ sizeof(DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC('D','X','1','0'), 0, 0, 0, 0, 0 };
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
struct handle_closer { void operator()(HANDLE h) { if (h) CloseHandle(h); } };
|
||||
namespace
|
||||
{
|
||||
struct handle_closer { void operator()(HANDLE h) { if (h) CloseHandle(h); } };
|
||||
|
||||
typedef public std::unique_ptr<void, handle_closer> ScopedHandle;
|
||||
typedef public std::unique_ptr<void, handle_closer> ScopedHandle;
|
||||
|
||||
inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; }
|
||||
inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; }
|
||||
|
||||
class auto_delete_file
|
||||
{
|
||||
public:
|
||||
auto_delete_file(HANDLE hFile) : m_handle(hFile) {}
|
||||
~auto_delete_file()
|
||||
{
|
||||
if (m_handle)
|
||||
{
|
||||
FILE_DISPOSITION_INFO info = {0};
|
||||
info.DeleteFile = TRUE;
|
||||
(void)SetFileInformationByHandle(m_handle, FileDispositionInfo, &info, sizeof(info));
|
||||
}
|
||||
}
|
||||
|
||||
void clear() { m_handle = 0; }
|
||||
|
||||
private:
|
||||
HANDLE m_handle;
|
||||
|
||||
auto_delete_file(const auto_delete_file&) DIRECTX_CTOR_DELETE;
|
||||
auto_delete_file& operator=(const auto_delete_file&) DIRECTX_CTOR_DELETE;
|
||||
};
|
||||
|
||||
#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (_WIN32_WINNT > _WIN32_WINNT_WIN8)
|
||||
|
||||
class auto_delete_file_wic
|
||||
{
|
||||
public:
|
||||
auto_delete_file_wic(ComPtr<IWICStream>& hFile, LPCWSTR szFile) : m_handle(hFile), m_filename(szFile) {}
|
||||
~auto_delete_file_wic()
|
||||
{
|
||||
if (m_filename)
|
||||
{
|
||||
m_handle.Reset();
|
||||
DeleteFileW(m_filename);
|
||||
}
|
||||
}
|
||||
|
||||
void clear() { m_filename = 0; }
|
||||
|
||||
private:
|
||||
LPCWSTR m_filename;
|
||||
ComPtr<IWICStream>& m_handle;
|
||||
|
||||
auto_delete_file_wic(const auto_delete_file_wic&) DIRECTX_CTOR_DELETE;
|
||||
auto_delete_file_wic& operator=(const auto_delete_file_wic&) DIRECTX_CTOR_DELETE;
|
||||
};
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Return the BPP for a particular format
|
||||
@ -735,13 +798,15 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext,
|
||||
|
||||
// Create file
|
||||
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
||||
ScopedHandle hFile( safe_handle( CreateFile2( fileName, GENERIC_WRITE, 0, CREATE_ALWAYS, 0 ) ) );
|
||||
ScopedHandle hFile( safe_handle( CreateFile2( fileName, GENERIC_WRITE | DELETE, 0, CREATE_ALWAYS, 0 ) ) );
|
||||
#else
|
||||
ScopedHandle hFile( safe_handle( CreateFileW( fileName, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0 ) ) );
|
||||
ScopedHandle hFile( safe_handle( CreateFileW( fileName, GENERIC_WRITE | DELETE, 0, 0, CREATE_ALWAYS, 0, 0 ) ) );
|
||||
#endif
|
||||
if ( !hFile )
|
||||
return HRESULT_FROM_WIN32( GetLastError() );
|
||||
|
||||
auto_delete_file delonfail(hFile.get());
|
||||
|
||||
// Setup header
|
||||
const size_t MAX_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10);
|
||||
uint8_t fileHeader[ MAX_HEADER_SIZE ];
|
||||
@ -872,6 +937,8 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext,
|
||||
if ( bytesWritten != slicePitch )
|
||||
return E_FAIL;
|
||||
|
||||
delonfail.clear();
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
@ -956,6 +1023,8 @@ HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext,
|
||||
if ( FAILED(hr) )
|
||||
return hr;
|
||||
|
||||
auto_delete_file_wic delonfail(stream, fileName);
|
||||
|
||||
ComPtr<IWICBitmapEncoder> encoder;
|
||||
hr = pWIC->CreateEncoder( guidContainerFormat, 0, encoder.GetAddressOf() );
|
||||
if ( FAILED(hr) )
|
||||
@ -1157,6 +1226,8 @@ HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext,
|
||||
if ( FAILED(hr) )
|
||||
return hr;
|
||||
|
||||
delonfail.clear();
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user