/*******************************************************************
*
*	Title:	GDI.h
*	Desc:	GDI Hedaer file
*
*	Note:	1) This is a borring header file isnt it?
*
********************************************************************/
#ifndef GDI_H
#define GDI_H TRUE

/*******************************************************************/
// Global Functions
/*******************************************************************/

long 		GDI_Open( void );
void 		GDI_Close( void );
void 		GDI_Flip( void );
dword GDI_GetAddress( void );

// our virtual page
dword m_VPage = NULL;

//char m_bibuf[sizeof(BITMAPINFOHEADER) + 12 ];
struct BITMAPINFO m_bi;
byte DopRGB[12];

/*******************************************************************/
// Functions
/*******************************************************************/

/*******************************************************************
*
*	Function:	GDI_Open()
*
*	Desc:		Sets up our DIB and Virtaul page
*
*	Notes:
*
*********************************************************************/
long GDI_Open()
{
	// get the windows Device Contex
	GetDC(g_hWnd);

	IF(EAX==NULL)return 1;

	// close resource
	ReleaseDC( g_hWnd, EAX );

	// Set most of the header info fields
	m_bi=0;
	m_bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	m_bi.bmiHeader.biWidth = 320;
	m_bi.bmiHeader.biHeight = -240;			// NOTE: -240. this is because DIB's are upside down
	m_bi.bmiHeader.biPlanes = 1;
	m_bi.bmiHeader.biBitCount = 16;
	m_bi.bmiHeader.biCompression = BI_BITFIELDS;

	// create a dib the same bit depth as the desktop, as GDI color space conversion are very slow
	// set format 555
	DSDWORD[#m_bi.bmiColors.rgbBlue]=0x00007c00;
	DSDWORD[#m_bi.bmiColors.rgbBlue+4]=0x000003e0;
	DSDWORD[#m_bi.bmiColors.rgbBlue+8]=0x0000001f;

	// allocate vpage
	m_VPage = GlobalAlloc(GMEM_FIXED, 320 * 240 * 2 );
	IF (EAX == NULL)return 1;
	return 0;
}

/******************************************************************
*
*	Function:	GDI_Close()
*
*	Desc:		Releases the Virtual pages memory
*
*	Notes:
*
********************************************************************/
void GDI_Close( void )
{
	// check we allocated it first
	IF (m_VPage)GlobalFree( m_VPage );
}

/******************************************************************
*
*	Function:	GDI_Flip()
*
*	Desc:		Copyies the virtual page to the window
*
*	Notes:
*
********************************************************************/
void GDI_Flip( void )
struct RECT client;
HDC hDC;
{
	// get the windows Device Contex
	hDC = GetDC( g_hWnd );

	// if its avaliable
	IF (EAX != NULL){

		// Get the the window dimentions
		GetClientRect(g_hWnd, #client);

		// Streach it
		StretchDIBits(	hDC,
						0, 				// Destination top left hand corner X Position
						0, 				// Destination top left hand corner Y Position
						client.right, 	// Destinations Width
						client.bottom, 	// Desitnations height
						0, 				// Source top left hand corner's X Position
						0, 				// Source top left hand corner's Y Position
						320, 			// Sources width
						240,			// Sources height
						m_VPage,	// Source's data
						#m_bi, 			// Bitmap Info
						DIB_RGB_COLORS, // operations
						SRCCOPY);

		// now relese the Device Contex.
		ReleaseDC( g_hWnd, hDC );
	}
}

/*************************************************************************
*
*	Function:	GDI_GetAddress()
*
*	Desc:		Gets the virtual page's top left hand corners memory address
*
*	Notes:
*
**************************************************************************/
dword GDI_GetAddress( void )
{
	return m_VPage;
}

#endif //GDI_H
