Prt11 - Screenshots! Using Code
Author: Ben Kenwright
As you do more and more coding on the xbox, you find that you build up a
number of your own xbox functions, that do lots of little things that you need
from time to time. As I developed this code way way back before using the
xdk, as I used to develop simple demos and nest a little function in the main
loop so I could capture screenshots when I wanted to the harddrive when pressing
certain gamepad keys. Ahhh those where the days.
The code is very simple and I think simple code is the best code....it just
checks for when the gamepad up is pressed then grabs the front buffer and saves
it to a file. Of course you could modify the code and use it for
reflections or capturing the screen for pause menus etc...sure you can think of
other things for it ...but its an interesting piece of code.
Here thats exciting code:
Source Code : screenshot.cpp |
/***************************************************************************/
/*
*/
/* 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(...) |
Of course if you have the xdk running on your xbox, with the debug bios, you
can use one of the xdk librarys that takes a screenshot and copies it to your
pc...but this is our own code :)
Happy coding.
|