Click here to Skip to main content
15,895,142 members
Articles / Mobile Apps

High Speed Graphics Library for WinCE

Rate me:
Please Sign up or sign in to vote.
4.81/5 (15 votes)
24 Jun 2002CPOL2 min read 266.1K   2.3K   91  
CEDraw is a high speed graphics library for WinCE.
/* ---------------------------------------------------------------------------------------------------
 *  
 *                          Windows CE Graphics Libary v1.00.0000
 *  
 *        
 *    Written by James.
 *    Bug report : jiet@msn.com
 *                                                             Copyright 2001
 */
//-----------------------------------------------------------------------------------------------------

/*
 * Created by James D. 2001.
 * Date: 01/11/2001
 */

#include "StdAfx.h"
#include "CEGraphics.h"
#include "CEGDefine.h"

//
// Function : CEGraphics()
// Purpose  : Construction function.
//
CCEGraphics::CCEGraphics()
{
	m_pVB           = NULL;
	m_pDoubleBuffer = NULL;
	m_bSuspend      = FALSE;
	m_bDirectDraw   = FALSE;
	m_dwDrawState   = 0;
	m_dwLastError   = 0;
}

//
// Function : CEGraphics()
// Purpose  : Destruction function.
//
CCEGraphics::~CCEGraphics()
{
	// Release the memory buffer.
	if( NULL != m_pDoubleBuffer ) 
	{
		free( m_pDoubleBuffer );
		m_pDoubleBuffer = NULL;
	}
}

//
// Function : Create( HWND hWnd )
// Purpose  : Create the CEGraphics Object, And Open the Display for use.
//
HRESULT CCEGraphics::Create( HWND hWnd )
{
	if( GXOpenDisplay( hWnd, GX_FULLSCREEN ) == 0 )
	{
		// Error in open display.
		m_dwLastError = CEG_GRAPH_OPENDISP_FAILED;
		return ::GetLastError();
	}

	if( GXOpenInput() == 0 )
	{
		// Error in Open the direct input key.
		m_dwLastError = CEG_GRAPH_OPENINPUT_FAILED;
		return ::GetLastError();
	}

	// Get the display properties.
	m_gxdp = GXGetDisplayProperties();
	// Get the default key properties.
	m_gxkl = GXGetDefaultKeys(GX_NORMALKEYS);

	//if( m_gxdp.ffFormat & kfDirect555 ) MessageBox( NULL, L"kfDirect555", L"Test", MB_OK );
	//if( m_gxdp.ffFormat & kfDirect565 ) MessageBox( NULL, L"kfDirect565", L"Test", MB_OK );
	//if( m_gxdp.ffFormat & kfDirect444 ) MessageBox( NULL, L"kfDirect444", L"Test", MB_OK );
	//if( m_gxdp.ffFormat & kfDirectInverted ) MessageBox( NULL, L"kfInverted", L"Test", MB_OK );
	// Calc the X and Y Pitch's
	m_cbxPitch = m_gxdp.cbxPitch;
	m_cbyPitch = m_gxdp.cbyPitch;

	// Create the back buffer...
	m_lMemorySize = m_gxdp.cxWidth * m_gxdp.cyHeight * m_gxdp.cBPP / 8;

	m_pDoubleBuffer = (unsigned char*)malloc( m_lMemorySize );
	// Failed to alloc memory.
	if( NULL == m_pDoubleBuffer ) 
	{
		m_dwLastError = CEG_GRAPH_MEMALLOC_FAILED;
		return FALSE;
	}
	m_pVB = m_pDoubleBuffer; // copy the backbuffer address

	/* --- Set the back buffer to the right position --- */
	if( m_cbxPitch < 0 ) m_pVB += m_gxdp.cxWidth * ( - m_cbxPitch );
	if( m_cbyPitch < 0 ) m_pVB += m_gxdp.cyHeight * ( - m_cbyPitch );
    /* --- End ---*/

	// Save the windows handel.
	m_hWnd = hWnd;

	return TRUE;
}

//
// Function : Release()
// Purpose  : Release the CEGraphics Object.
//
HRESULT CCEGraphics::Release()
{
	if( GXCloseInput() == 0 )
	{
		// Error...
		m_dwLastError = CEG_GRAPH_CLOSEINPUT_FAILED;
		return ::GetLastError();
	}
	if( GXCloseDisplay() == 0 )
	{
		// Error...
		m_dwLastError = CEG_GRAPH_CLOSEDISP_FAILED;
		return ::GetLastError();
	}
	if( NULL != m_pDoubleBuffer ) 
	{
		free( m_pDoubleBuffer );
		m_pDoubleBuffer = NULL;
	}
	return TRUE;
}

//
// Function : BeginDraw( void )
// Purpose  : Prepares the display for writing
//	          Lock the video memory and return the buffer pointer to the variary m_pVB.
//
HRESULT CCEGraphics::BeginDraw( void )
{
	// if the system suspend, do not call the gxbegindraw()
	if( m_bSuspend ) 
	{
		m_dwLastError = CEG_GRAPH_SUSPENDED;
		return FALSE;
	}

	// Check the background buffer...
	if( NULL == m_pDoubleBuffer )
	{
		// Return false
		m_dwLastError = CEG_GRAPH_BEGINDRAW_FAILED;
		return FALSE;
	}

	// Drawing....
	m_dwDrawState = 1;

	return TRUE;
}

//
// Function : EndDraw( void )
// Purpose  : Finished drawing.
//
HRESULT CCEGraphics::EndDraw( void )
{
	// if the system suspend, do not call the gxenddraw()
	if( m_bSuspend ) 
	{
		m_dwLastError = CEG_GRAPH_SUSPENDED;
		return FALSE;
	}
	if( NULL == m_pDoubleBuffer ) 
	{
		m_dwLastError = CEG_GRAPH_VB_NULL;
		return FALSE;
	}

	// Set the drawstate NULL
	m_dwDrawState    = 0;
	return TRUE;
}

//
// Function : Flip ()
// Purpose  : Take the page flip.
//
HRESULT	CCEGraphics::Flip( void )
{
	if( m_bSuspend ) 
	{
		m_dwLastError = CEG_GRAPH_SUSPENDED;
		return FALSE;
	}
	if( NULL == m_pDoubleBuffer )
	{
		m_dwLastError = CEG_GRAPH_VB_NULL;
		return FALSE;
	}
	// Get the video system buffer...
	m_pDisplayMemory = (unsigned char*)GXBeginDraw();
	if( NULL == m_pDisplayMemory )
	{
		// Return false
		m_dwLastError = CEG_GRAPH_BEGINDRAW_FAILED;
		return FALSE;
	}

	// Persent the memory flip....
	// Copy the memory buffer to the display memory.
	memcpy( (m_pDisplayMemory + ( m_pDoubleBuffer - m_pVB ) ) , m_pDoubleBuffer, m_lMemorySize );
	// End the gx draw.
	if( GXEndDraw() == 0 )
	{
		// Error...
		m_dwLastError = CEG_GRAPH_ENDDRAW_FAILED;
		return GetLastError();
	}
	m_pDisplayMemory = NULL;
	return TRUE;
}

//
// Function : Suspend() & Resume()
// Purpose  : Suspend and Resume the operations.
//
HRESULT CCEGraphics::Suspend( void )
{
	if( GXSuspend() == 0 )
	{
		// Error....
		m_dwLastError = CEG_GRAPH_SUSPEND_FAILED;
		return ::GetLastError();
	}
	// if the system suspend, do not call the gxbegindraw()
	m_bSuspend = TRUE;
	return TRUE;
}

HRESULT CCEGraphics::Resume( void )
{
	if( GXResume() == 0 )
	{
		// Error....
		m_dwLastError = CEG_GRAPH_RESUME_FAILED;
		return ::GetLastError();
	}
	m_bSuspend = FALSE;
	return TRUE;
}

//
// Function : SetDrawMode()
// Purpose  : Do Not Use Page flip, direct draw screen only!!!
// NOTE     : Must careful use this function!!!!!!!!!!!
//
HRESULT CCEGraphics::SetDrawMode( DWORD dwMode )
{
	BOOL bDirectDraw;
	// Set the draw mode
	if( dwMode == CEG_DRAWMODE_DIRECTDRAW ) bDirectDraw = TRUE;
	else bDirectDraw = FALSE;

	// Return False if the Video buffer null
	if( m_pVB == NULL ) return FALSE;
	// SetDirectDraw can not been used in begindraw/enddraw
	if( m_dwDrawState == 1 ) return FALSE;

	if( bDirectDraw != m_bDirectDraw )
	{
		if( bDirectDraw )
		{
			// Get the video buffer...
			m_pDisplayMemory = (unsigned char*)GXBeginDraw();
			if( NULL == m_pDisplayMemory )
			{
				// Return false
				return FALSE;
			}
			// Save the buffer pointer...
			unsigned char* pTemp = m_pVB;
			m_pVB                = m_pDisplayMemory;
			m_pDisplayMemory     = pTemp;
			m_bDirectDraw        = bDirectDraw;
		}
		else if ( bDirectDraw == FALSE && m_pVB != NULL )
		{
			if( GXEndDraw() == 0 )
			{
				// Error...
				return ::GetLastError();
			}
			m_pVB            = m_pDisplayMemory;
			m_pDisplayMemory = NULL;
			m_bDirectDraw    = bDirectDraw;
		}
		
	}

	// Return successfully.
	return TRUE;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
China China
8 Years Development experience from MS-DOS v3.0 to WindowsXP in C/C++. Profession in Visual C++, Native Win32 and MFC programming, Embedded development.Computer Graphics(Computer Games),Network Program.

Living in China now.

Comments and Discussions