📄 cedraw.cpp
字号:
/* ---------------------------------------------------------------------------------------------------
*
* Windows CE Graphics Libary v1.00.0000
*
*
* Written by James.
* Bug report : jiet@msn.com
* Copyright 2001
*/
// File: CEDraw.cpp
// Graphics library
//-----------------------------------------------------------------------------------------------------
// Update Information.
//-----------------------------------------------------------------------------------------------------
/*
* Created by James D. 2001.
* Date: 01/11/2001
*/
#include "StdAfx.h"
#include "CEDraw.h"
#include "Polygon.h"
#include "CEGDefine.h"
//
// Function : CEDraw()
// Purpose : Construction function.
//
CCEDraw::CCEDraw()
{
m_pFont = NULL;
m_pBrush = NULL;
m_pPen = NULL;
// Read the font to the memory...
// Unsuitable code
FILE* fp;
UINT nFileSize;
fp=fopen("HZK16","rb++");
if( fp )
{
fseek( fp, 0, SEEK_END );
nFileSize = ftell( fp );
fseek( fp, 0, SEEK_SET );
m_pFontFile = (LPBYTE)malloc( nFileSize );
if( m_pFontFile )
fread( m_pFontFile, nFileSize, 1, fp );
fclose( fp );
}
}
//
// Function : CEDraw()
// Purpose : Destruction function.
//
CCEDraw::~CCEDraw()
{
// Release the font file alloc memory...
if( NULL != m_pFontFile ) free( m_pFontFile );
}
//
// Function : DrawPixel()
// Putpose : Draw a Point
//
inline VOID CCEDraw::DrawPixel( DWORD dwX, DWORD dwY, unsigned short Color )
{
if( IsPointOutside( dwX, dwY ) ) return;
if( m_gxdp.cBPP == 8 ) *( m_pVB + dwY*m_cbyPitch + m_cbxPitch*dwX ) = (BYTE)Color;
*(unsigned short*)( m_pVB + dwY*m_cbyPitch + m_cbxPitch*dwX ) = Color;
}
//
// Function : IsAllPointInside()
// Check the point in side
//
inline BOOL CCEDraw::IsAllPointInside( POINT* ptPoint, int nNumber )
{
if( NULL == ptPoint ) return FALSE;
for( int nIndex = 0 ; nIndex < nNumber ; nIndex ++ )
{
if( ptPoint[nIndex].x < 0 && ptPoint[nIndex].y < 0 ) return FALSE;
if( ptPoint[nIndex].x >= (LONG)m_gxdp.cxWidth && ptPoint[nIndex].y >= (LONG)m_gxdp.cyHeight ) return FALSE;
}
return TRUE;
}
//
// Function : IsAllIsPointOutside()
// Check the point out side
//
inline BOOL CCEDraw::IsAllPointOutside( POINT* ptPoint, int nNumber )
{
if( NULL == ptPoint ) return FALSE;
for( int nIndex = 0 ; nIndex < nNumber ; nIndex ++ )
{
if( ptPoint[nIndex].x < (LONG)m_gxdp.cxWidth && ptPoint[nIndex].y < (LONG)m_gxdp.cyHeight
&& ptPoint[nIndex].x >= 0 && ptPoint[nIndex].y >= 0 ) return FALSE;
}
return TRUE;
}
//
// Function : GetBitmapPointColor()
// Get the point color from the bitmap
//
inline unsigned short CCEDraw::GetBitmapPointColor( unsigned char * pBuffer )
{
unsigned short Color;
int nColorRed = (int)((BYTE)*(pBuffer+2));
int nColorGreen = (int)((BYTE)*(pBuffer+1));
int nColorBlue = (int)((BYTE)*(pBuffer));
switch( m_gxdp.cBPP )
{
case 16:
if ( m_gxdp.ffFormat & kfDirect555 )
{
nColorRed >>= 3;
nColorGreen >>= 3;
nColorBlue >>= 3;
Color = (unsigned short)( ( nColorRed & 0xff ) << 10 |
( nColorGreen & 0xff ) << 5 |
( nColorBlue & 0xff ) );
}
else if( m_gxdp.ffFormat & kfDirect565)
{
nColorRed >>= 3;
nColorGreen >>= 2;
nColorBlue >>= 3;
Color = (unsigned short)( ( nColorRed & 0xff ) << 11 |
( nColorGreen & 0xff ) << 5 |
( nColorBlue & 0xff ) );
}
else if( m_gxdp.ffFormat & kfDirect444 )
{
nColorRed >>= 4;
nColorGreen >>= 4;
nColorBlue >>= 4;
Color = (unsigned short)( ( nColorRed & 0xf0 ) << 8 |
( nColorGreen & 0xf0 ) << 4 |
( nColorBlue & 0xf0 ) );
}
break;
case 8:
Color = (unsigned short)( ( nColorRed & 0xf ) << 5 |
( nColorGreen & 0x3 ) <<3 |
( nColorBlue & 0xF ) );
break;
}
return Color;
}
//
// Function : DrawLine()
// Purpose : This method draws lines
//
BOOL CCEDraw::DrawLine( POINT &ptOrig, POINT &ptDes )
{
DrawLine( ptOrig.x, ptOrig.y, ptDes.x, ptDes.y );
return TRUE;
}
BOOL CCEDraw::DrawHLine( LONG lOrigX, LONG lOrigY, LONG lDestX, LONG lDestY, short nColor )
{
if( lOrigY != lDestY ) return TRUE; // Because Draw H Line...
if( lOrigY < 0 || lOrigY >= (LONG)m_gxdp.cyHeight ) return TRUE;
if( ( lOrigX < 0 && lDestX < 0 ) || ( lOrigX >= (LONG)m_gxdp.cxWidth && lDestX >= (LONG)m_gxdp.cxWidth ) ) return TRUE;
LONG lTemp;
if( lOrigX > lDestX )
{
lTemp = lOrigX;
lOrigX = lDestX;
lDestX = lTemp;
}
if( lOrigX < 0 ) lOrigX = 0;
if( lDestX >= (LONG)m_gxdp.cxWidth ) lDestX = m_gxdp.cxWidth - 1;
if( nColor == -1 && m_pPen != NULL ) nColor = m_pPen->m_Color;
unsigned char* pBuffer = ( m_pVB + lOrigY*m_cbyPitch + m_cbxPitch*lOrigX );
if( m_gxdp.cBPP == 8 )
{
for( int nX = lOrigX ; nX < lDestX ; nX ++ )
{
*pBuffer = (BYTE)nColor;
pBuffer += m_cbxPitch;
}
}
else if( m_gxdp.cBPP == 16 )
{
for( int nX = lOrigX ; nX < lDestX ; nX ++ )
{
*(unsigned short*)pBuffer = (unsigned short)nColor;
pBuffer += m_cbxPitch;
}
}
return TRUE;
}
BOOL CCEDraw::DrawVLine( LONG lOrigX, LONG lOrigY, LONG lDestX, LONG lDestY, short nColor )
{
if( lOrigX != lDestX ) return TRUE; // Because Draw V Line...
if( lOrigX < 0 || lOrigX >= (LONG)m_gxdp.cxWidth ) return TRUE;
if( ( lOrigY < 0 && lDestY < 0 ) || ( lOrigY >= (LONG)m_gxdp.cyHeight && lDestY >= (LONG)m_gxdp.cyHeight ) ) return TRUE;
LONG lTemp;
if( lOrigY > lDestY )
{
lTemp = lOrigY;
lOrigY = lDestY;
lDestY = lTemp;
}
if( lOrigY < 0 ) lOrigY = 0;
if( lDestY >= (LONG)m_gxdp.cyHeight ) lDestY = m_gxdp.cyHeight - 1;
if( nColor == -1 && m_pPen != NULL ) nColor = m_pPen->m_Color;
unsigned char* pBuffer = ( m_pVB + lOrigY*m_cbyPitch + m_cbxPitch*lOrigX );
if( m_gxdp.cBPP == 8 )
{
for( int nY = lOrigY ; nY < lDestY ; nY ++ )
{
*pBuffer = (BYTE)nColor;
pBuffer += m_cbyPitch;
}
}
else if( m_gxdp.cBPP == 16 )
{
for( int nY = lOrigY ; nY < lDestY ; nY ++ )
{
*(unsigned short*)pBuffer = (unsigned short)nColor;
pBuffer += m_cbyPitch;
}
}
return TRUE;
}
BOOL CCEDraw::DrawLine( LONG lOrigX, LONG lOrigY, LONG lDestX, LONG lDestY )
{
//int dX = abs( lDestX-lOrigX ); // store the change in X and Y of the line endpoints
//int dY = abs( lDestY-lOrigY );
int dX, dY;
int CurrentX = lOrigX; // store the starting point (just point A)
int CurrentY = lOrigY;
// Draw none if all point outside the screen...
if( IsPointOutside( lOrigX, lOrigY ) && IsPointOutside( lDestX, lDestY ) ) return TRUE;
int Xincr, Yincr;
if( lOrigX > lDestX ) { Xincr=-1; dX = lOrigX - lDestX; } else { Xincr=1; dX = lDestX - lOrigX; } // which direction in X?
if( lOrigY > lDestY ) { Yincr=-1; dY = lOrigY - lDestY; } else { Yincr=1; dY = lDestY - lOrigY; } // which direction in Y?
if( dX >= dY ) // if X is the independent variable
{
int dPr = dY << 1;
int dPru = dPr - ( dX << 1 );
int P = dPr - dX;
for ( ; dX >= 0; dX -- )
{
DrawPixel( CurrentX, CurrentY, m_pPen->m_Color );
if (P > 0)
{
CurrentX += Xincr;
CurrentY += Yincr;
P += dPru;
}
else
{
CurrentX += Xincr;
P += dPr;
}
}
}
else
{
int dPr = dX << 1;
int dPru = dPr - ( dY << 1 );
int P = dPr - dY;
for( ; dY >= 0; dY -- )
{
DrawPixel( CurrentX, CurrentY, m_pPen->m_Color );
if ( P > 0 )
{
CurrentX += Xincr;
CurrentY += Yincr;
P += dPru;
}
else
{
CurrentY += Yincr;
P += dPr;
}
}
}
return TRUE;
}
//
// Function : ConvertColor()
// Convert the COLORREF to CE support color
//
unsigned short CCEDraw::ConvertColor( COLORREF crColor )
{
unsigned short Color = 0;
int nColorRed = ((int)crColor>>16)&0xff;
int nColorGreen = ((int)crColor>>8)&0xff;
int nColorBlue = ((int)crColor)&0xff;
switch( m_gxdp.cBPP )
{
case 16:
if ( m_gxdp.ffFormat & kfDirect555 )
{
nColorRed >>= 3;
nColorGreen >>= 3;
nColorBlue >>= 3;
Color = (unsigned short)( ( nColorRed & 0xff ) << 10 |
( nColorGreen & 0xff ) << 5 |
( nColorBlue & 0xff ) );
}
else if( m_gxdp.ffFormat & kfDirect565)
{
nColorRed >>= 3;
nColorGreen >>= 2;
nColorBlue >>= 3;
Color = (unsigned short)( ( nColorRed & 0xff ) << 11 |
( nColorGreen & 0xff ) << 5 |
( nColorBlue & 0xff ) );
}
else if( m_gxdp.ffFormat & kfDirect444 )
{
nColorRed >>= 4;
nColorGreen >>= 4;
nColorBlue >>= 4;
Color = (unsigned short)( ( nColorRed & 0xf0 ) << 8 |
( nColorGreen & 0xf0 ) << 4 |
( nColorBlue & 0xf0 ) );
}
break;
case 8:
Color = (unsigned short)( ( nColorRed & 0xf ) << 5 |
( nColorGreen & 0x3 ) <<3 |
( nColorBlue & 0xF ) );
break;
}
return Color;
}
//
// Function : ConvertColor()
// Convert the COLORREF to CE support color
//
unsigned short CCEDraw::ConvertColor( int nColorRed, int nColorGreen, int nColorBlue )
{
unsigned short Color = 0;
switch( m_gxdp.cBPP )
{
case 16:
if ( m_gxdp.ffFormat & kfDirect555 )
{
nColorRed >>= 3;
nColorGreen >>= 3;
nColorBlue >>= 3;
Color = (unsigned short)( ( nColorRed & 0xff ) << 10 |
( nColorGreen & 0xff ) << 5 |
( nColorBlue & 0xff ) );
}
else if( m_gxdp.ffFormat & kfDirect565)
{
nColorRed >>= 3;
nColorGreen >>= 2;
nColorBlue >>= 3;
Color = (unsigned short)( ( nColorRed & 0xff ) << 11 |
( nColorGreen & 0xff ) << 5 |
( nColorBlue & 0xff ) );
}
else if( m_gxdp.ffFormat & kfDirect444 )
{
nColorRed >>= 4;
nColorGreen >>= 4;
nColorBlue >>= 4;
Color = (unsigned short)( ( nColorRed & 0xf0 ) << 8 |
( nColorGreen & 0xf0 ) << 4 |
( nColorBlue & 0xf0 ) << 0 );
}
break;
case 8:
Color = (unsigned short)( ( nColorRed & 0xf ) << 5 |
( nColorGreen & 0x3 ) <<3 |
( nColorBlue & 0xF ) );
break;
}
return Color;
}
//
// Function : Clear()
// Purpose : Clear the background with the specify pen
//
BOOL CCEDraw::Clear( CCEPen* pPen )
{
if ( NULL == pPen ) pPen = m_pPen;
if ( NULL == pPen )
{
m_dwLastError = CEG_DRAW_PEN_NULL;
return FALSE; // Not Select Pen Object;
}
unsigned char * pusLine = (unsigned char*)m_pVB;
if( m_gxdp.cBPP == 8 )
{
for( unsigned int y = 0; y < m_gxdp.cyHeight - 1; y ++ )
{
unsigned char * pusDest = pusLine;
for( unsigned int x = 0; x < m_gxdp.cxWidth - 1; x ++ )
{
*pusDest = (BYTE)pPen->m_Color;
pusDest += m_cbxPitch;
}
pusLine += m_cbyPitch;
}
}
else if( m_gxdp.cBPP == 16 )
{
for( unsigned int y = 0; y < m_gxdp.cyHeight - 1; y ++ )
{
unsigned char * pusDest = pusLine;
for( unsigned int x = 0; x < m_gxdp.cxWidth - 1; x ++ )
{
//*(unsigned short*)pusDest = pPen->m_Color;
DrawPixel( x, y, pPen->m_Color);
pusDest += m_cbxPitch;
}
pusLine += m_cbyPitch;
}
}
return TRUE;
}
//
// Function : Clear()
// Purpose : Clear the background with the specify Color
//
BOOL CCEDraw::Clear( DWORD dwColor )
{
// The dwColor must be 0~256
memset( m_pDoubleBuffer, dwColor, m_gxdp.cxWidth*m_gxdp.cyHeight*m_gxdp.cBPP/8 );
return TRUE;
}
//
// Function : FadeOut()
// Purpose : Fade Out the Screen
//
VOID CCEDraw::FadeOut( DWORD dwStep )
{
int nCount;
unsigned char * pusLine = m_pVB;
unsigned short ColorR, ColorG, ColorB, Color;
for( nCount = 0 ; nCount < 255 ; nCount ++ )
{
BeginDraw();
pusLine = m_pVB;
for( unsigned int y = 0; y < m_gxdp.cyHeight - 1; y ++ )
{
unsigned char * pusDest = pusLine;
for( unsigned int x = 0; x < m_gxdp.cxWidth - 1; x ++ )
{
if( m_gxdp.cBPP == 8 )
{
*pusDest -= (unsigned short)dwStep;;
}
else
{
if( m_gxdp.ffFormat & kfDirect565 )
{
/* n% ALPHA */
Color = *(unsigned short*)pusDest;
ColorR = (unsigned short)((Color >> 11) & 0x1f) - dwStep;
ColorG = (unsigned short)((Color >> 5) & 0x3f) - dwStep;
ColorB = (unsigned short)(Color & 0x1f) - dwStep;
if( ColorR < 0 ) ColorR = 0;
if( ColorG < 0 ) ColorG = 0;
if( ColorB < 0 ) ColorB = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -