/***************************************************************************/
/*                                                                         */
/*  File: screenshot.cpp                                                   */
/*  URL: www.xbdev.net                                                     */
/*  Email: webmaster@xbdev.net                                             */
/*  version: 1.0.0                                                         */
/*                                                                         */
/***************************************************************************/
/*

So how does this screensaver functions work?

It can't be simpler...you include the header file in your code...e.g:
#include "screenshot.h"

Then part two, you call it...and it saves a file for you on the hard drive
a .bmp file...e.g.

screenshot( g_pD3DDevice, "D:\\Demo.bmp");

Thats it ;)

*/
/***************************************************************************/

#include <xgraphics.h>
#pragma comment(lib, "xgraphics.lib")

// Call this before directX initilisation!...or else it wont work
//***** XInitDevices(0, 0);**********

// JUST MOVE UP!! to take a screenshot and save it to file
bool mygamepad()
{
    //static bool bFirstTime = true;
    //if(bFirstTime)
    //  XInitDevices(0, 0);

    XINPUT_GAMEPAD myGamePad;
    DWORD dwInsertions, dwRemovals;
    XGetDeviceChanges( XDEVICE_TYPE_GAMEPAD, &dwInsertions, &dwRemovals );
    static HANDLE pGamePd;

    if( dwInsertions & 1 )
    {
        pGamePd = XInputOpen( XDEVICE_TYPE_GAMEPAD, 0, 
            XDEVICE_NO_SLOT, NULL );
    }
    if( pGamePd )
    {
        XINPUT_STATE myInputStates;
        XInputGetState( pGamePd, &myInputStates );
        memcpy( &myGamePad, &myInputStates.Gamepad, sizeof(XINPUT_GAMEPAD) );
    }
    if( myGamePad.wButtons & XINPUT_GAMEPAD_DPAD_UP )
    {
        return true;
    }
    return false;
}// End mygamepad(..)

void screenshot( IDirect3DDevice8* pD3DDevice, char* szFileName )
{
    if( szFileName == NULL )
        szFileName = "D:\\Demo.bmp";

    static float delay = 0.0f;

    float nowTime = (float)GetTickCount();

    bool bGrab = mygamepad();


    // This nowTime variable is so there is a 5 second delay between 
    // possible image grabs...
    if( (nowTime > (delay + 5000)) && bGrab)
    {
        delay = nowTime;


        IDirect3DSurface8* pSurface;

        pD3DDevice->GetBackBuffer(-1,D3DBACKBUFFER_TYPE_MONO,&pSurface);

        /********************method -1-*********************************/

        XGWriteSurfaceToFile( pSurface, szFileName );

        pSurface->Release();

        /***************************************************************/


    }
}// End screenshot(...)


