Hello,
I recently started developing a game C++ and wanted to add a screenshot option for the game.
The feature works when the game is windowed but gives me a DEVICE_LOST error when the game is in fullscreen mode.
I managed to isolate my code to a single .cpp file and am testing it on regular fullscreen directX games I have installed :
#include "stdafx.h"
#include <d3d9.h>
#include <d3dx9.h>
#include "DirectXTools.h"
LPDIRECT3D9 directX;
LPDIRECT3DDEVICE9 directXDevice;
void initDevice(HWND myWindow);
void ConvertCharToWC(char* src, wchar_t* target);
void DirectXTools::CaptureWindow(const char* windowName)
{
Sleep(5000);
HWND windowHandle = GetForegroundWindow();
initDevice(windowHandle);
}
void initDevice(HWND myWindow) {
D3DDISPLAYMODE ddm;
D3DPRESENT_PARAMETERS d3dpp;
IDirect3DSurface9* g_pSurface;
if((directX=Direct3DCreate9(D3D_SDK_VERSION))==NULL)
{
throw "";
}
if(FAILED(directX->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&ddm)))
{
throw "";
}
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed=FALSE;
d3dpp.Flags=D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.BackBufferFormat=ddm.Format;
d3dpp.BackBufferHeight=ddm.Height;
d3dpp.BackBufferWidth=ddm.Width;
d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE;
// d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.SwapEffect=D3DSWAPEFFECT_COPY;
d3dpp.hDeviceWindow=myWindow;
d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
if(FAILED(directX->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,myWindow,D3DCREATE_SOFTWARE_VERTEXPROCESSING ,&d3dpp,&directXDevice)))
{
throw "";
}
if(FAILED(directXDevice->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &g_pSurface, NULL)))
{
throw "";
}
directXDevice->GetFrontBufferData(0, g_pSurface);
D3DXSaveSurfaceToFile("temp.bmp",D3DXIFF_BMP,g_pSurface,NULL,NULL);
}
The error occurs at the directX->CreateDevice function. it simply returns the error code D3DERR_DEVICELOST.
Notes :
- I researched a little and found out that when a program takes fullscreen control, all other windows become lost.
The thing is, I am passing the foreground window as my device handle so it shouldn't be lost.
- Maybe GetForegroundWindow doesn't know how to handle a fullscreen program, but only windows in windowed mode.
- I tried calling the SetForegroundWindow when the program was minimized in fullscreen mode (meaning it was fullscreen and I alt + tab it) and nothing happened
I tried it again when the program was minimized in windowed mode and it worked.
Anyone has experience with capturing fullscreen games and can solve this issue?
Thanks In Advance.