📄 utility.cpp
字号:
//////////////////////////////////////////////////////////////////////
//FILE: Utility.cpp
//Helper functions
//////////////////////////////////////////////////////////////////////
#include "Utility.h"
//////////////////////////////////////////////////////////////////////
//Misc Global Helper functions
//////////////////////////////////////////////////////////////////////
int GetRandom( void )
{
uint32 t;
GETRAND( ( unsigned char * ) &t, sizeof( t ) );
return t;
}
int GetRandom( int low, int high )
{
uint32 t;
GETRAND( ( unsigned char * ) &t, sizeof( t ) );
int r = high - low;
t = ( t % r ) + low;
return t;
}
AEEImageInfo GetImageInfo(IImage* pImage)
{
AEEImageInfo ImageInfo;
IIMAGE_GetInfo(pImage, &ImageInfo);
return(ImageInfo);
}
int GetImageWidth(IImage* pImage)
{
AEEImageInfo ImageInfo;
IIMAGE_GetInfo(pImage, &ImageInfo);
return(ImageInfo.cx);
}
int GetImageHeight(IImage* pImage)
{
AEEImageInfo ImageInfo;
IIMAGE_GetInfo(pImage, &ImageInfo);
return(ImageInfo.cy);
}
// The Java flags basically tell where the bounding rect of the text or image should be placed
// relative to the passed in point. The Brew flags simply refer to the screen position,
// and thus the passed in coordinate is frequently ignored. This function adjusts the
// passed in point based on the anchor flags and the height and width.
void AdjustCoordsByAnchor( AEEPoint* pPt, int width, int height, int anchor )
{
// In the case of LEFT and TOP no adjustment is necessary.
// I'm assuming that this tells us that the x value passed in is where the
// middle of the image should go.
if ( anchor & HCENTER )
{
pPt->x = pPt->x - ( width / 2 );
}
if ( anchor & VCENTER )
{
pPt->y = pPt->y - ( height / 2 );
}
if ( anchor & RIGHT )
{
pPt->x = pPt->x - width;
}
if ( anchor & BOTTOM )
{
pPt->y = pPt->y - height;
}
}
/*
#ifndef BREW20
#ifdef MOTOROLAT720
AEERect DisplayClip = { 0, 0, 120, 130 };
#else
#ifdef LG4400
AEERect DisplayClip = { 0, 0, 120, 115 };
#else
#ifdef SMALL
AEERect DisplayClip = { 0, 0, 128, 116 };
#else
#ifdef KYOCERAKX2
AEERect DisplayClip = { 0, 0, 132, 164 };
#else
AEERect DisplayClip = { 0, 0, 128, 146 };
#endif
#endif
#endif
#endif
#endif
*/
void Graphics::DrawImage(IImage* pImage, int posX, int posY)
{
#ifdef BREW20
IIMAGE_SetParm(pImage,IPARM_ROP,AEE_RO_TRANSPARENT,0);
IIMAGE_Draw(pImage, posX, posY);
#else
AEEImageInfo ii;
IIMAGE_GetInfo(pImage,&ii);
int px = posX;
int py = posY;
int offx = 0;
int offy = 0;
int sx = ii.cxFrame;
int sy = ii.cy;
if (posX < DisplayClip.x) { offx=DisplayClip.x-posX; px=DisplayClip.x; }
if (posY < DisplayClip.y) { offy=DisplayClip.y-posY; py=DisplayClip.y; }
if (px+sx-offx > DisplayClip.x + DisplayClip.dx) sx=DisplayClip.x+DisplayClip.dx-px;
if (py+sy-offy > DisplayClip.y + DisplayClip.dy) sy=DisplayClip.y+DisplayClip.dy-py;
if (sx<=0 || sy<=0) return;
IIMAGE_SetParm(pImage,IPARM_ROP,AEE_RO_TRANSPARENT,0);
IIMAGE_SetDrawSize(pImage,sx,sy);
IIMAGE_SetOffset(pImage,offx,offy);
IIMAGE_Draw(pImage, px, py);
#endif
}
void Graphics::DrawImage(IImage* pImage, int posX, int posY, int anchor)
{
if (pImage==NULL)
return;
// As I understand the Java documentation, the x and y define where the
// anchor point is. So in order to put the image in the right place we need
// to adjust our coordinates based on the anchor specification.
AEEPoint pt;
AEEImageInfo info;
IIMAGE_GetInfo( pImage, &info );
pt.x = posX;
pt.y = posY;
AdjustCoordsByAnchor( &pt, info.cx, info.cy, anchor );
#ifdef BREW20
IIMAGE_SetParm(pImage,IPARM_ROP,AEE_RO_TRANSPARENT,0);
IIMAGE_Draw(pImage, pt.x, pt.y);
#else
IIMAGE_SetParm(pImage,IPARM_ROP,AEE_RO_TRANSPARENT,0);
DrawImage(pImage, pt.x, pt.y);
#endif
}
void Str2wstr( const char* pSrc, int nlen, AECHAR* pDest )
{
MEMMOVE( ( ( char * ) pDest ) + nlen, pSrc, nlen );
/* might be a noop, move to end */
pSrc = ( ( char * ) pDest ) + nlen;
while ( nlen > 0 )
{
*pDest++ = ( AECHAR ) ( unsigned char ) * pSrc++;
nlen--;
}
}
//////////////////////////////////////////////////////////////////////
//Graphics wrapper class
//////////////////////////////////////////////////////////////////////
Graphics::Graphics(IGraphics* pIGraphics, IDisplay* pIDisplay)
{
m_pIGraphics = pIGraphics;
m_pIDisplay = pIDisplay;
#ifdef TINY
tinyFont = NULL;
#endif
FONT_STEP=FONT_SHIFT=0;
#ifndef BREW20
DisplayClip.x=0; DisplayClip.y=0;
#ifdef MOTOROLAT720
DisplayClip.dx=120; DisplayClip.dy=130;
#else
#ifdef LG4400
DisplayClip.dx=120; DisplayClip.dy=115;
#else
#ifdef SMALL
DisplayClip.dx=128; DisplayClip.dy=116;
#else
#ifdef KYOCERAKX2
DisplayClip.dx=132; DisplayClip.dy=164;
#else
#ifdef TINY
DisplayClip.dx=101; DisplayClip.dy=68;
#else
DisplayClip.dx=120; DisplayClip.dy=146;
//DisplayClip.dx=120; DisplayClip.dy=117;
#endif
#endif
#endif
#endif
#endif
#endif
}
Graphics::~Graphics()
{
m_pIGraphics = NULL;
m_pIDisplay = NULL;
}
#ifdef TINY
void Graphics::initTinyFont(IImage *pImage) {
tinyFont = new Font();
tinyFont->init(0, pImage, tinyFontCharAlpha, tinyFontCharOmega, tinyFontCharWidths, tinyFontCharHeight, this);
}
void Graphics::deleteTinyFont() {
if (tinyFont) {
delete tinyFont;
tinyFont = NULL;
}
}
#endif
//========================================================================
//Clipping
//========================================================================
void Graphics::SetScreenClip(int posX, int posY, int width, int height)
{
AEEClip clip;
uint8 nFlag = 0; // the nFlag are not required to emulate Java setClip function.
clip.type = CLIPPING_RECT;
clip.shape.rect.x = posX;
clip.shape.rect.y = posY;
clip.shape.rect.dx = width;
clip.shape.rect.dy = height;
IGRAPHICS_SetClip( m_pIGraphics, &clip, nFlag );
#ifndef BREW20
StoreClip=clip;
#endif
}
void Graphics::SetBitmapClip(int posX, int posY, int width, int height)
{
#ifdef BREW20
AEERect rect;
rect.x = posX;
rect.y = posY;
rect.dx = width;
rect.dy = height;
IDISPLAY_SetClipRect( m_pIDisplay, &rect );
#else
AEEClip clip;
uint8 nFlag = 0; // the nFlag are not required to emulate Java setClip function.
clip.type = CLIPPING_RECT;
clip.shape.rect.x = posX;
clip.shape.rect.y = posY;
clip.shape.rect.dx = width;
clip.shape.rect.dy = height;
//IGRAPHICS_SetClip( m_pIGraphics, &clip, nFlag );
DisplayClip=clip.shape.rect;
#endif
}
void Graphics::ResetBitmapClip()
{
#ifdef BREW20
IDISPLAY_SetClipRect( m_pIDisplay, NULL );
#else
//IGRAPHICS_SetClip(m_pIGraphics, &StoreClip, 0);
DisplayClip=StoreClip.shape.rect;
#endif
}
//========================================================================
//Colour
//========================================================================
uint32 Graphics::GetColor()
{
uint8 red,
green,
blue,
alpha;
IGRAPHICS_GetColor(m_pIGraphics, &red, &green, &blue, &alpha);
return(MAKERGB(red, green, blue));
}
void Graphics::SetColor(int red, int green, int blue)
{
IGRAPHICS_SetColor(m_pIGraphics, red, green, blue, 0xFF);
}
void Graphics::SetColor(uint32 rgb)
{
uint8 red,
green,
blue,
mask = 0xFF;
red = (int8)((rgb >> 16) & mask);
green = (int8)((rgb >> 8) & mask);
blue = (int8)(rgb & mask);
SetColor((int)red, (int)green, (int)blue);
}
void Graphics::SetTextColor(int red, int green, int blue)
{
IDISPLAY_SetColor(m_pIDisplay,CLR_USER_TEXT, MAKE_RGB(red, green, blue) );
}
void Graphics::SetTextColor(uint32 rgb)
{
uint8 red,
green,
blue,
mask = 0xFF;
red = (int8)((rgb >> 16) & mask);
green = (int8)((rgb >> 8) & mask);
blue = (int8)(rgb & mask);
SetTextColor((int)red, (int)green, (int)blue);
}
void Graphics::SetFillColor(int red, int green, int blue)
{
IGRAPHICS_SetFillColor(m_pIGraphics, red, green, blue, 0xFF);
}
void Graphics::SetFillColor(uint32 rgb)
{
uint8 red,
green,
blue,
mask = 0xFF;
red = (int8)((rgb >> 16) & mask);
green = (int8)((rgb >> 8) & mask);
blue = (int8)(rgb & mask);
SetFillColor((int)red, (int)green, (int)blue);
}
//========================================================================
//Vector graphics
//========================================================================
void Graphics::DrawLine(int x0, int y0, int x1, int y1)
{
AEELine line;
line.sx = x0;
line.sy = y0;
line.ex = x1;
line.ey = y1;
IGRAPHICS_DrawLine(m_pIGraphics, &line);
}
void Graphics::FillRect(int posX, int posY, int width, int height)
{
AEERect rect;
rect.x = posX;
rect.y = posY;
rect.dx = width;
rect.dy = height;
IGRAPHICS_SetFillMode( m_pIGraphics, TRUE );
IGRAPHICS_DrawRect( m_pIGraphics, &rect );
}
void Graphics::DrawRect(int posX, int posY, int width, int height)
{
AEERect rect;
rect.x = posX;
rect.y = posY;
rect.dx = width;
rect.dy = height;
IGRAPHICS_SetFillMode( m_pIGraphics, FALSE );
IGRAPHICS_DrawRect( m_pIGraphics, &rect );
}
void Graphics::FillTriangle(int x0, int y0, int x1, int y1, int x2, int y2)
{
AEETriangle tri;
tri.x0 = x0;
tri.y0 = y0;
tri.x1 = x1;
tri.y1 = y1;
tri.x2 = x2;
tri.y2 = y2;
IGRAPHICS_SetFillMode( m_pIGraphics, TRUE );
IGRAPHICS_DrawTriangle( m_pIGraphics, &tri );
}
void Graphics::DrawTriangle(int x0, int y0, int x1, int y1, int x2, int y2)
{
AEETriangle tri;
tri.x0 = x0;
tri.y0 = y0;
tri.x1 = x1;
tri.y1 = y1;
tri.x2 = x2;
tri.y2 = y2;
IGRAPHICS_SetFillMode( m_pIGraphics, FALSE );
IGRAPHICS_DrawTriangle( m_pIGraphics, &tri );
}
//YingZ: The concept of an "ARC" in JAVA is equivalent to a "Ellipse" in Brew
//startAngle and arcAngle are redundant data not used.
void Graphics::FillArc(int posX, int posY, int width, int height)
{
AEEEllipse ellipse;
int16 realW = width / 2;
int16 realH = height / 2;
ellipse.cx = posX + realW;
ellipse.cy = posY + realH;
ellipse.wx = realW;
ellipse.wy = realH;
IGRAPHICS_SetFillMode( m_pIGraphics, TRUE );
IGRAPHICS_DrawEllipse( m_pIGraphics, &ellipse );
}
void Graphics::DrawArc(int posX, int posY, int width, int height)
{
AEEEllipse ellipse;
int16 realW = width / 2;
int16 realH = height / 2;
ellipse.cx = posX + realW;
ellipse.cy = posY + realH;
ellipse.wx = realW;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -