⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utility.cpp

📁 这是一个用BREW和VC++开发的暴力摩托游戏,与大家一同分享.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	ellipse.wy = realH;

	IGRAPHICS_SetFillMode( m_pIGraphics, FALSE );
	IGRAPHICS_DrawEllipse( m_pIGraphics, &ellipse );
}

void Graphics::FillRoundRect(int posX, int posY, int width, int height, int arcWidth, int arcHeight)
{
#ifdef BREW20
	AEERect rect;
	rect.x = posX;
	rect.y = posY;
	rect.dx = width;
	rect.dy = height;
	IGRAPHICS_SetFillMode( m_pIGraphics, TRUE );
	IGRAPHICS_DrawRoundRectangle( m_pIGraphics, &rect, arcWidth, arcHeight );
#else
	FillArc(posX, posY, width, height);
#endif
}

void Graphics::DrawRoundRect(int posX, int posY, int width, int height, int arcWidth, int arcHeight)
{
#ifdef BREW20
	AEERect rect;

	rect.x = posX;
	rect.y = posY;
	rect.dx = width;
	rect.dy = height;

	IGRAPHICS_SetFillMode( m_pIGraphics, FALSE );
	IGRAPHICS_DrawRoundRectangle( m_pIGraphics, &rect, arcWidth, arcHeight );
#else
	DrawArc(posX, posY, width, height);
#endif
}

//========================================================================
//Draw text
//========================================================================

void Graphics::getFontMetrics(IDisplay* pIDisplay, AEEFont font, int *pascent, int *pdescent) {
#ifdef TINY
	*pdescent=0;
	if (tinyFont)
		*pascent=tinyFont->charHeight((AECHAR)('A')); 
	else 
		*pascent=0;
#else
	IDISPLAY_GetFontMetrics( pIDisplay, font, pascent, pdescent );
#endif
}

int Graphics::measureText(IDisplay* pIDisplay, AEEFont font, AECHAR *szBuf) {
#ifdef TINY
	if (tinyFont)
		return tinyFont->stringWidth(szBuf);
	else 
		return 0;
#else
	return IDISPLAY_MeasureText( pIDisplay, font, szBuf );
#endif
}

void Graphics::drawText(IDisplay* pIDisplay, AEEFont font, AECHAR *szBuf, int length, int x, int y, AEERect *pRect, uint32 dwFlags) { 
#ifdef TINY
	if (tinyFont) {
		short xx=x; short yy=y;
		AEERect *pClipRect=pRect;
#ifdef BREW20
		AEERect displayClipRect;
		if (pClipRect==NULL) {
			IDISPLAY_GetClipRect( pIDisplay, &displayClipRect);
			pClipRect=&displayClipRect;
		}
#endif
		tinyFont->drawString(szBuf, pClipRect, &xx, &yy);
	}
#else
	IDISPLAY_DrawText( pIDisplay, font, szBuf, length, x, y, pRect, dwFlags );
#endif
}

bool Graphics::SetMaxFontHeight(int height) {
	int ascent;
	int descent;

	getFontMetrics( m_pIDisplay, AEE_FONT_NORMAL, &ascent, &descent );
	if (ascent+descent > height) {
	  FONT_STEP = height-ascent-descent;
		FONT_SHIFT = (FONT_STEP+1)/2;
		if (FONT_STEP < -2) return false; else return true;
	}
	else {
		FONT_STEP = 0;
		FONT_SHIFT = 0;
		return true;
	}
}

int Graphics::GetFontHeight(AEEFont font)
{
	int ascent;
	int descent;

	getFontMetrics( m_pIDisplay, font, &ascent, &descent );

	return(ascent + descent + FONT_STEP);
}

int Graphics::GetFontHeight()
{
	int ascent;
	int descent;

	getFontMetrics( m_pIDisplay, m_currentFont, &ascent, &descent );

	return(ascent + descent + FONT_STEP);
}

AEEFont Graphics::GetFont()
{
	return(m_currentFont);
}

void Graphics::SetFont(AEEFont font)
{
	m_currentFont = font;
}

int Graphics::GetTextWidth( JString text, AEEFont font )
{
	AECHAR* szBuf;
	int length = text.length();

	szBuf = ( AECHAR* ) MALLOC( length * sizeof( AECHAR ) + 1 );
	Str2wstr( text.cstr(), length, szBuf );
	*( szBuf + length ) = 0; // null out last part of the szBuf  szBuf[length] = NULL;
	int width = measureText( m_pIDisplay, font, szBuf );

	FREE( szBuf );
	return width;
}

int Graphics::GetTextWidth( JString text )
{
	AECHAR* szBuf;
	int length = text.length();

	szBuf = ( AECHAR* ) MALLOC( length * sizeof( AECHAR ) + 1 );
	Str2wstr( text.cstr(), length, szBuf );
	*( szBuf + length ) = 0; // null out last part of the szBuf  szBuf[length] = NULL;
	int width = measureText( m_pIDisplay, m_currentFont, szBuf );

	FREE( szBuf );
	return width;
}

void Graphics::DrawString(const JString& string, int x, int y, AEERect* rect, uint32 dwFlags )
{
	AECHAR* szBuf;
	int length = string.length();

	szBuf = ( AECHAR * ) MALLOC( length * sizeof( AECHAR ) );
	//Str2wstr( string.cstr(), length, szBuf );
	//szBuf[length] = 0;

	Util_KSC5601ToAECHAR(string.Buffer, szBuf);

	drawText( m_pIDisplay, m_currentFont, szBuf, length, x, y+FONT_SHIFT, rect, dwFlags );
	FREE( szBuf );
}

/*
void Graphics::DrawString(JString string, int x, int y, uint32 dwFlags )
{
	AECHAR* szBuf;
	int length = string.length();

	szBuf = ( AECHAR * ) MALLOC( (length + 1) * sizeof( AECHAR ) );
	Str2wstr( string.cstr(), length, szBuf );
	szBuf[length] = 0;

#ifdef BREW20
	drawText( m_pIDisplay, m_currentFont, szBuf, length, x, y+FONT_SHIFT, NULL, dwFlags  );
#else
	drawText( m_pIDisplay, m_currentFont, szBuf, length, x, y+FONT_SHIFT, &DisplayClip, dwFlags );
#endif
	FREE( szBuf );
}
*/

void Graphics::DrawString(JString string, int x, int y, int anchor)
{
	AECHAR* szBuf;
	AEEPoint pt;
	int length = string.length();

	// Allocate the memory for the wide string.  Make sure we add one character after
	// for the NULL.
	szBuf = ( AECHAR * ) MALLOC( (length + 1) * sizeof( AECHAR ) );
	//Str2wstr( string.cstr(), length, szBuf );
	//szBuf[length] = 0; // null out that last part of the szBuf

	Util_KSC5601ToAECHAR(string.Buffer, szBuf);

	// Get the font height so we can adjust the anchor point correctly.
	int height = GetFontHeight();

	// Get the text width so we can adjust the anchor point correctly
	int width = measureText( m_pIDisplay, m_currentFont, szBuf );

	pt.x = x;
	pt.y = y;
	AdjustCoordsByAnchor( &pt, width, height, anchor );

	// Use NULL for the clipping rect as one should have been specified elsewhere.  Also
	// specify -1 for the number of characters so the function will calc it itself.
	//YingZ: Set the dwFlag to 0 - may need to enable this?
#ifdef BREW20
	drawText( m_pIDisplay, m_currentFont, szBuf, -1, pt.x, pt.y + FONT_SHIFT, NULL, IDF_TEXT_TRANSPARENT );
#else
	drawText( m_pIDisplay, m_currentFont, szBuf, -1, pt.x, pt.y + FONT_SHIFT, &DisplayClip, IDF_TEXT_TRANSPARENT );
	//drawText( m_pIDisplay, m_currentFont, szBuf, -1, pt.x, pt.y + FONT_SHIFT, NULL, IDF_TEXT_TRANSPARENT );
#endif
	FREE( szBuf );
}

//////////////////////////////////////////////////////////////////////
//JAVA compatible Random Class
//////////////////////////////////////////////////////////////////////
Random::Random()
{
	SetSeed(GETTIMEMS());
}

Random::Random(__int64 seed)
{
	SetSeed(seed);
} 

Random::~Random()
{

}

void Random::SetSeed(__int64 seed)
{
	/*
	m_seed = (__int64)((seed ^ MULTIPLIER) & MASK);
	*/
}

int Random::Next(int bits)
{
	/*
	__int64 nextseed = (__int64)(m_seed * MULTIPLIER + ADDEND & MASK);
	m_seed = nextseed;
	return (int)((unsigned __int64)nextseed >> (48 - bits));
	*/
	int rand;
	GETRAND((unsigned char*)&rand,sizeof(rand));
	return rand;
}

int Random::NextInt()
{
	return Next(32); 
}

long Random::NextLong()
{
	//YingZ: Seems long is 32 bit in Brew! So this really is the same as an int
	return /*((long)Next(32) << 32) +*/ (long)Next(32);
}


#ifdef TINY

Font::Font()
{
}

Font::~Font()
{
	//delete fontImg;
}

boolean Font::init(const int aName, IImage *aFontImg, AECHAR aCharAlpha, AECHAR aCharOmega, const int16 *aCharWidths, int16 aCharHeight, Graphics *g_)
{
	name = aName;
	fontImg = aFontImg;
	charAlpha = aCharAlpha;
	charOmega = aCharOmega;
	charWidths = aCharWidths;
	charHeights = aCharHeight;
	g = g_;
	return(true);
}

const int Font::getName() const
{
	return(name);
}

int16 Font::charWidth(AECHAR aChar) const
{
	int16 charWidthIdx = aChar - charAlpha;
	if(charWidthIdx < 0 || charWidthIdx > (charOmega - charAlpha))
		return(0);

	return(charWidths[charWidthIdx]);
}

int16 Font::charHeight(AECHAR aChar) const
{
	return(charHeights);
}

int16 Font::stringWidth(const AECHAR *aStr) const
{
	int16 width = 0;
	int32 slen = WSTRLEN(aStr);
	for(int32 t = 0; t < slen; t++)
		width += charWidth(aStr[t])+1;

	return(width);
}

int16 Font::stringHeight(const AECHAR *aStr) const
{
	return(charHeight(aStr[0]));
}

int16 Font::drawChar(const AECHAR aChar, AEERect *clip, int16 *aXPos, int16 *aYPos) const
{
	int16 charImgXOffset = 0;
	int16 charWidthIdx = aChar - charAlpha;
	
	if(charWidthIdx < 0 || charWidthIdx > (charOmega - charAlpha))
	{
//		(*aXPos) += 0;
		return charHeights;
	}

	for(int16 t = 0; t < charWidthIdx; t++)
		charImgXOffset += charWidths[t];

	IIMAGE_SetParm(fontImg,IPARM_ROP,AEE_RO_TRANSPARENT,0);

#ifndef BREW20
	if (clip == NULL) clip = &(g->DisplayClip);
#endif
	int px = *aXPos;
	(*aXPos) += charWidths[charWidthIdx]+1;
	int py = *aYPos;
	int offx = 0;
	int offy = 0;
	int sx = charWidths[charWidthIdx];
	int sy = charHeights;
	if (px < clip->x) { offx=clip->x-px; px=clip->x; sx-=offx; }
	if (py < clip->y) { offy=clip->y-py; py=clip->y; sy-=offy; }
	if (sx<=0 || sy<=0) return charHeights;
	if (px+sx-offx > clip->x + clip->dx) sx=clip->x+clip->dx-px;
	if (py+sy-offy > clip->y + clip->dy) sy=clip->y+clip->dy-py;
	if (sx<=0 || sy<=0) return charHeights;

	IIMAGE_SetDrawSize(fontImg,sx,sy);
	IIMAGE_SetOffset(fontImg,charImgXOffset+offx,offy);
	IIMAGE_Draw(fontImg,px,py);

	/*
	IIMAGE_SetOffset(fontImg,charImgXOffset,0);
	IIMAGE_SetDrawSize(fontImg,charWidths[charWidthIdx],charHeights);
	IIMAGE_Draw(fontImg,*aXPos,*aYPos);
  */
	return charHeights;
}

int16 Font::drawString(const AECHAR *aStr, AEERect *clip, int16 *aXPos, int16 *aYPos) const
{
	int32 slen = WSTRLEN(aStr);
	for(int32 t = 0; t < slen; t++)
		drawChar(aStr[t],clip,aXPos,aYPos);

	return(charHeights);
}

#endif

void SOUND_STREAM::Init(IShell *Shell, IFileMgr *FileMgr, const char *FileName) {
  FileInfo fInfo;
	if (IFILEMGR_GetInfo(FileMgr,FileName,&fInfo)==SUCCESS) {
		size=fInfo.dwSize;
		if (size>0) {
			pBuffer=(unsigned char*)(MALLOC(size));
			if (pBuffer) {
			  IFile *File=IFILEMGR_OpenFile(FileMgr, FileName, _OFM_READ);
				IFILE_Read(File, pBuffer, size);
				IFILE_Release(File);
				ISHELL_CreateInstance(Shell, AEECLSID_MEMASTREAM, (void **)&Stream);
				IMEMASTREAM_Set(Stream, pBuffer, size, 0, false);
			}
		}
	}
}

void SOUND_STREAM::Release() {
  if (pBuffer) {
    IMEMASTREAM_Release(Stream); // also releases pBuffer
		pBuffer=NULL;
	}
}

// 2005 02 25 眠啊内靛
// KSC5601内靛甫 AECHAR肺 函券
void Util_KSC5601ToAECHAR(const char *src, AECHAR *dest)
{	
	int			i, j;

	for( i = 0, j = 0 ; src[i] != NULL ; i++ )
	{
        // if( src[i] > 128 || src[i] < 0 )
		if (((uint8)src[i]) & 0x80)
		{
			MEMCPY( &dest[j], &src[i], 2 );
			i++;
		}
		else
			dest[j] = (AECHAR)src[i];
		j++;
	}
	dest[j] = 0;
}

// AECHAR甫 KSC5601内靛肺 函券
void Util_AECHARToKSC5601(const AECHAR *src, char *dest)
{
	int			i, j;

	for( i = 0, j = 0 ; src[i] != NULL ; i++ )
	{
		if( src[i] > 128 )
		{
			MEMCPY( &dest[j], &src[i], 2 );
			j++;
		}
		else
			dest[j] = (char)src[i];
		j++;
	}
	dest[j] = 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -