/*********************************************************************************/
/*                                                                               */
/*  XBOX OpenXDK ScreenCatpure                                                   */
/*                                                                               */
/*  File:   screencapture.c                                                      */
/*                                                                               */
/*  Date:   23-02-04                                                             */
/*                                                                               */
/*********************************************************************************/
/*                                                                               */
/*  Platform: XBOX - OpenXDK                                                     */
/*                                                                               */
/*********************************************************************************/

// tga header structure.
#pragma pack(1)
typedef struct _tgaheader
{
	uint08 idlength;			// Misc header
	uint08 colourmap;
	uint08 imagetype;
	uint16 colourmap_origin;	// Colour map spec
	uint16 colourmap_length;
	uint08 colourmap_type;
	uint16 xorigin;				// Image spec
	uint16 yorigin;
	uint16 width;
	uint16 height;
	uint08 pixelsize;
	uint08 descriptor;
} TGAHeader, *PTGAHeader;
#pragma pack()



void ScreenCapture(char *path)
{
	TGAHeader Header = {0,0,2, 0,0,32, 0,480,640,480,32,0x00000008};

	HANDLE TGAoutput = CreateFile(path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
	WriteFile(TGAoutput, &Header, sizeof(Header), 0, 0);

	ScreenInfo ScrStruct = vga_get_screen_info();
	uint32* pScr = (uint32*)ScrStruct.ScreenAddress;
	uint32 *FileBuffer = (uint32*)malloc(640*480*4);

	uint32 i, j;
	for (i=0; i<480; i++)
	{
		for (j=0; j<640; j++) 
		{
			FileBuffer[j+((479-i)*640)] = *pScr;
			pScr++;
		}// End inner loop
	}// End outer loop

	WriteFile(TGAoutput, FileBuffer, 640*480*4, 0, 0);
	CloseHandle(TGAoutput);

	free(FileBuffer);
}// End ScreenCapture(..)


/*********************************************************************************/
/*                                                                               */
/*                          Program Entry Point                                  */
/*                                                                               */
/*********************************************************************************/
/*                                                                               */
/*  Few lines of code to test our code, launch different apps etc.               */
/*                                                                               */
/*********************************************************************************/

void main()
{
	Bitmap         *screen;
	unsigned int   *p;
	int		       x, y, xx=0;

	//let's init the screen
	vga_init_mode(MODE_320x240x32);

	vga_vsync();					// Wait for Vertical Blank
    vga_flip();						// Flip
	vga_clear();                    // Clear Screen

	// So where's the screen? who knows if we're page flipping?
	screen = get_screen_bitmap();

	// Nice pattern on the screen
	p = screen->data;
	for (y=0; y<screen->h; y++)
	{
		for (x=0; x<screen->w; x++)
		{
				*p++ = ((x+xx)^y)&0xFF; //yay a XOR pattern!!! :D
		}
	}
	xx++;

	// Save our data that we've rendered to the screen, before
	// we try and save it to file
	vga_vsync();					// Wait for Vertical Blank
    vga_flip();						// Flip


	ScreenCapture("D:\\screengrab.tga");

}// End main
