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

📄 gfx.cpp

📁 liu7788414
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	
	// store vars back
	*px1 = xc1;
	*py1 = yc1;
	*px2 = xc2;
	*py2 = yc2;
	
	return(1);
	
} // end Clip_Line

/////////////////////////////////////////////////////////////

int CGfx::Draw_Line(int x0, int y0, int x1, int y1, unsigned short color, unsigned short *vb_start, int lpitch)
{
	// this function draws a line from xo,yo to x1,y1 using differential error
	// terms (based on Bresenahams work)
	
	int dx,             // difference in x's
		dy,             // difference in y's
		dx2,            // dx,dy * 2
		dy2, 
		x_inc,          // amount in pixel space to move during drawing
		y_inc,          // amount in pixel space to move during drawing
		error,          // the discriminant i.e. error i.e. decision variable
		index;          // used for looping
	
	// pre-compute first pixel address in video buffer
	vb_start = vb_start + x0 + y0*lpitch;
	
	// compute horizontal and vertical deltas
	dx = x1-x0;
	dy = y1-y0;
	
	// test which direction the line is going in i.e. slope angle
	if (dx>=0)
	{
		x_inc = 1;
		
	} // end if line is moving right
	else
	{
		x_inc = -1;
		dx    = -dx;  // need absolute value
		
	} // end else moving left
	
	// test y component of slope
	
	if (dy>=0)
	{
		y_inc = lpitch;
	} // end if line is moving down
	else
	{
		y_inc = -lpitch;
		dy    = -dy;  // need absolute value
		
	} // end else moving up
	
	// compute (dx,dy) * 2
	dx2 = dx << 1;
	dy2 = dy << 1;
	
	// now based on which delta is greater we can draw the line
	if (dx > dy)
	{
		// initialize error term
		error = dy2 - dx; 
		
		// draw the line
		for (index=0; index <= dx; index++)
		{
			// set the pixel
			*vb_start = color;
			
			// test if error has overflowed
			if (error >= 0) 
			{
				error-=dx2;
				
				// move to next line
				vb_start+=y_inc;
				
			} // end if error overflowed
			
			// adjust the error term
			error+=dy2;
			
			// move to the next pixel
			vb_start+=x_inc;
			
		} // end for
		
	} // end if |slope| <= 1
	else
	{
		// initialize error term
		error = dx2 - dy; 
		
		// draw the line
		for (index=0; index <= dy; index++)
		{
			// set the pixel
			*vb_start = color;
			
			// test if error overflowed
			if (error >= 0)
			{
				error-=dy2;
				
				// move to next line
				vb_start+=x_inc;
				
			} // end if error overflowed
			
			// adjust the error term
			error+=dx2;
			
			// move to the next pixel
			vb_start+=y_inc;
			
		} // end for
		
	} // end else |slope| > 1
	
	// return success
	return(1);
	
} // end Draw_Line

void CGfx::writePixel(GFXBMP *pDes, int dx, int dy, unsigned short color, bool bSmallCircle)
{
	if(dy < 0 || dy >= SCREEN_HEIGHT)
		return;
	if(dx < 0 || dx >= SCREEN_WIDTH)
		return;
	if(bSmallCircle)
		if(dx > 20 || dy < SCREEN_HEIGHT - 21)
			return;
		
		SYS_ASSERT(pDes != NULL);
		SYS_ASSERT(pDes->bpp == 16);
		
		unsigned short *pD;
		pD = (unsigned short*)(pDes->pData + (pDes->w * dy + dx)*2);
		*pD++ = color;
}

void CGfx::drawCircle(GFXBMP *pDes, int x, int y, int r, unsigned short color, bool bSmallCircle)
{
	SYS_ASSERT(pDes != NULL);
	if (r <= 0)
		return;
	int xx,yy,x1,x2,x3,x4,x5,x6,x7,x8,y1,y2,y3,y4,y5,y6,y7,y8,pk;
	xx = 0;
	yy = r;
	x1 = x , y1 = y + r;
	x2 = x , y2 = y + r;
	x3 = x , y3 = y - r;
	x4 = x , y4 = y - r;
	x5 = x + r , y5 = y;
	x6 = x - r , y6 = y;
	x7 = x + r , y7 = y;
	x8 = x - r , y8 = y;
	pk = 1 - r;
	writePixel(pDes,x1,y1,color, bSmallCircle);
	writePixel(pDes,x2,y2,color, bSmallCircle);
	writePixel(pDes,x3,y3,color, bSmallCircle);
	writePixel(pDes,x4,y4,color, bSmallCircle);
	writePixel(pDes,x5,y5,color, bSmallCircle);
	writePixel(pDes,x6,y6,color, bSmallCircle);
	writePixel(pDes,x7,y7,color, bSmallCircle);
	writePixel(pDes,x8,y8,color, bSmallCircle);
	while(xx < yy)
	{	xx++;
	x1++,x2--,x3++,x4--;
	y5++,y6++,y7--,y8--;
	if(pk < 0)
		pk+= 2*xx + 1;
	else
	{	yy--;
	y1--,y2--,y3++,y4++;
	x5--,x6++,x7--,x8++;
	pk+= 2*(xx - yy) + 1;
	}
	writePixel(pDes,x1,y1,color, bSmallCircle);
	writePixel(pDes,x2,y2,color, bSmallCircle);
	writePixel(pDes,x3,y3,color, bSmallCircle);
	writePixel(pDes,x4,y4,color, bSmallCircle);
	writePixel(pDes,x5,y5,color, bSmallCircle);
	writePixel(pDes,x6,y6,color, bSmallCircle);
	writePixel(pDes,x7,y7,color, bSmallCircle);
	writePixel(pDes,x8,y8,color, bSmallCircle);
	}
}

#ifdef BREW11//BREW11
#ifdef AEE_SIMULATOR

BITMAPFILEHEADER1* CGfx::CreateEmuBitmapFile(int w, int h)
{
	BITMAPFILEHEADER1 *pfilehdr;
	BITMAPINFOHEADER *pinfohdr;
	RGBQUAD *pPal;
	int sizeHeader;
	int xpitch = w ;
	int sizePixelData = xpitch * h;
	int r, g, b;
	SYS_ASSERT(w % 4 == 0);
	
	sizeHeader = sizeof(BITMAPFILEHEADER1) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
	sizeHeader = (sizeHeader + 3) & ~3;
	//#ifdef AEE_SIMULATOR
	pfilehdr = (BITMAPFILEHEADER1*)SMemory->GlobalMalloc(sizeHeader+sizePixelData);//malloc(sizeHeader + sizePixelData);
	//#else
	//	pfilehdr = (BITMAPFILEHEADER*)MALLOC(sizeHeader + sizePixelData);
	//#endif
	SYS_ASSERT(pfilehdr != NULL);
	pinfohdr = (BITMAPINFOHEADER*)((char*)pfilehdr + sizeof(BITMAPFILEHEADER1));
	
	pfilehdr->bfType = 'MB';
	pfilehdr->bfSize = sizeHeader + sizePixelData;
	pfilehdr->bfOffBits = sizeHeader;
	pfilehdr->bfReserved1 = pfilehdr->bfReserved2 = 0;
	
	pinfohdr->biSize = sizeof(BITMAPINFOHEADER);
	pinfohdr->biWidth = w;
	pinfohdr->biHeight = h;
	pinfohdr->biPlanes = 1;
	pinfohdr->biBitCount = 8;
	pinfohdr->biCompression = 0;//BI_BITFIELDS;
	pinfohdr->biSizeImage = w * h;
	pinfohdr->biXPelsPerMeter = 0;
	pinfohdr->biYPelsPerMeter = 0;
	pinfohdr->biClrUsed = 256;
	pinfohdr->biClrImportant = 256;
	
	pPal = (RGBQUAD*)((char*)pinfohdr + sizeof(BITMAPINFOHEADER));
	for (r = 0; r < 8; r++)
		for (g = 0; g < 8; g++)
			for (b = 0; b < 4; b++)
			{
				pPal->rgbRed = (r*255)/7;
				pPal->rgbGreen = (g*255)/7;
				pPal->rgbBlue = (b*255)/3;
				pPal->rgbReserved = 0;
				pPal++;
			}
			return pfilehdr;
}
void CGfx::InitAsEmuBuff(GFXBMP *d)
{
	BITMAPFILEHEADER1* pbf = CreateEmuBitmapFile(SCREEN_WIDTH, SCREEN_HEIGHT);
	//Disp_Init(d, DISPW, DISPH, 1, DISPW, CM_RGB332, GetBmpBits((char*)pbf), pIDisplay, (NativeImage*)pbf, global);
	d->w = SCREEN_WIDTH;
	d->h = SCREEN_HEIGHT;
	d->pNativeBmp = (NativeImage*)pbf;
	d->pData = (unsigned char*)GetBmpBits((char*)pbf);
	//	d->bpp = 16;
	//	d->npal = 256;
	
}
char* CGfx::GetBmpBits(char *bmp)
{
	BITMAPFILEHEADER1* fileHead = (BITMAPFILEHEADER1*)bmp;
	return (char*)fileHead + fileHead->bfOffBits;
}
void CGfx::Conv565To332FlipV(unsigned short *pSrc, unsigned char *pDes, int w, int h)
{
	register int c;
	unsigned short *pS;
	unsigned char *pD;
	int i;
	
	pS = pSrc;
	while(h--)
	{
		pD = pDes + w * h;
		i = w;
		while(i--)
		{
			c = *pS++;
			*pD++ = ((c >> 8) & 0xe0) | ((c >> 6) & 0x1c) | ((c >> 3) & 0x3);
		}
	}
}
void CGfx::Conv444To332FlipV(unsigned short *pSrc, unsigned char *pDes, int w, int h)
{
	register int c;
	unsigned short *pS;
	unsigned char *pD;
	int i;
	
	pS = pSrc;
	while(h--)
	{
		pD = pDes + w * h;
		i = w;
		while(i--)
		{
			c = *pS++;
			*pD++ = ((c >> 4) & 0xe0) | ((c >> 3) & 0x1c) | ((c >> 2) & 0x3);
		}
	}
}
#endif//AEE_SIMUALTOR

GFXBMP* CGfx::CreateNativeBmp(int w, int h, bool bGlobal)
{
	CSimpleMemory* pMem = m_pEngine->GetMem();
	GFXBMP *p = (GFXBMP*)(bGlobal?pMem->GlobalMalloc(sizeof(GFXBMP)):
	pMem->HeapMalloc(sizeof(GFXBMP)));
	SYS_ASSERT(NULL != p);
	if(NULL != p)
		CSimpleMemory::Fill(p, 0, sizeof(GFXBMP));
	if(NULL != p)
	{
		p->pNativeBmp = (NativeImage*)(bGlobal?pMem->GlobalMalloc(sizeof(NativeImage) -4 + w * h * 2):
	pMem->HeapMalloc(sizeof(NativeImage) -4 + w * h * 2));
	SYS_ASSERT(NULL != p->pNativeBmp);
	if(NULL != p->pNativeBmp)
		CSimpleMemory::Fill(p->pNativeBmp->_data, 0, w * h * 2);
	((dword*)p->pNativeBmp->_head)[0] = h;
	((dword*)p->pNativeBmp->_head)[1] = w;
	((dword*)p->pNativeBmp->_head)[2] = (dword) (p->pNativeBmp->_data);
	p->bpp = 16;
	p->npal = 0;
	p->flag = 0;
	p->w = (short)w;
	p->h = (short)h;
	p->pData = (unsigned char*)p->pNativeBmp->_data;
	}
	else
		SYS_ASSERT(0);
	return p;
}
#else//BREW2

GFXBMP* CGfx::CreateNativeBmp(void *pBuf, int w, int h, bool bGlobal)
{
	GFXBMP *p = (GFXBMP*)(bGlobal?SMemory->GlobalMalloc(sizeof(GFXBMP)):
SMemory->HeapMalloc(sizeof(GFXBMP)));
SYS_ASSERT(NULL != p);
if(NULL != p)
CSimpleMemory::Fill(p, 0, sizeof(GFXBMP));
if(NULL != p)
{
	p->bpp = 16;
	p->npal = 0;
	p->flag = 0;
	p->w = (short)w;
	p->h = (short)h;
	p->pData = (unsigned char*)pBuf;
}
else
SYS_ASSERT(0);
return p;
}

#endif//if BREW11


void CGfx::UpdateToForeground()
{	
	GFXBMP *d = m_pVideo;
#if 0
#ifdef BREW11//BREW11
#if defined AEE_SIMULATOR
	GFXBMP* pEmuDisp = &emuDisp;
#if (HW_COLORMODEL == CM_RGB565)
	Conv565To332FlipV((unsigned short*)d->pData, (unsigned char*)pEmuDisp->pData, d->w, d->h);
#elif (HW_COLORMODEL == CM_RGB444)
	Conv444To332FlipV((unsigned short*)d->pData, (unsigned char*)pEmuDisp->pData, d->w, d->h);
#endif
	{
		void *p;
		AEEImageInfo info;
		boolean b;
		p = CONVERTBMP(pEmuDisp->pNativeBmp, &info, &b);
		IDISPLAY_BitBlt (idisp, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, p, 0, 0,  AEE_RO_COPY);
		if(b)
			SYSFREE(p);
	}
#else
	IDISPLAY_BitBlt (idisp, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, m_pVideo->pNativeBmp, 0, 0, AEE_RO_COPY);
#endif
#else//BREW2
	//for lg6000:
	IDISPLAY_DrawHLine(idisp, 0, 0, 1);
	IDISPLAY_DrawHLine(idisp, SCREEN_WIDTH-1, SCREEN_HEIGHT-1, 1);
#endif
	IDISPLAY_UpdateEx(idisp, false);// draw immediately

#endif	//0	Qiu Li
}

void CGfx::DrawColorMask(short transcoeff, short addcolor, int x, int y, int w, int h) //color: r8g8b8 color, alph: 0~7
{
	SYS_ASSERT(w>=1&&h>=1);
	if (x<0)	{
		w+=x;
		x=0;
	}
	if (y<0)	{
		h+=y;
		y=0;
	}
	if (x+w>SCREEN_WIDTH)
		w = SCREEN_WIDTH-x;
	if (y+h>SCREEN_HEIGHT)
		h = SCREEN_HEIGHT-y;
	if (x>SCREEN_WIDTH||y>SCREEN_HEIGHT||w<1||h<1)
		return;
	unsigned short *pData = ((unsigned short*)m_pVideo->pData)+y*SCREEN_WIDTH+x;
	int i, j;
	int r, g, b;
	const unsigned char r2 = RCOLOR(transcoeff);
	const unsigned char g2 = GCOLOR(transcoeff);
	const unsigned char b2 = BCOLOR(transcoeff);
	if (addcolor)	{
		const unsigned char r3 = RCOLOR(addcolor);
		const unsigned char g3 = GCOLOR(addcolor);
		const unsigned char b3 = BCOLOR(addcolor);
		for (i=0; i<h; i++)	{
			for (j=0; j<w; j++)	{
				r = (r2*RCOLOR(pData[j])>>8)+r3;
				g = (g2*GCOLOR(pData[j])>>8)+g3;
				b = (b2*BCOLOR(pData[j])>>8)+b3;
				if (r>255)
					r=255;
				if (g>255)
					g=255;
				if (b>255)
					b=255;
				pData[j] = MAKERGB(r, g, b);
			}
			pData += SCREEN_WIDTH;
		}
	}	else
		for (i=0; i<h; i++)	{
			for (j=0; j<w; j++)	{
				r = r2*RCOLOR(pData[j])>>8;
				g = g2*GCOLOR(pData[j])>>8;
				b = b2*BCOLOR(pData[j])>>8;
				pData[j] = MAKERGB(r, g, b);
			}
			pData += SCREEN_WIDTH;
		}
}

⌨️ 快捷键说明

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