此例子很简单,就是把四边形分为两个三角形绘制即可。
inline void Draw_QuadFP_2D(int x0,int y0,
int x1,int y1,
int x2,int y2,
int x3, int y3,
int color,
UCHAR *dest_buffer, int mempitch)
{
// this function draws a 2D quadrilateral
// simply call the triangle function 2x, let it do all the work
Draw_TriangleFP_2D(x0,y0,x1,y1,x3,y3,color,dest_buffer,mempitch);
Draw_TriangleFP_2D(x1,y1,x2,y2,x3,y3,color,dest_buffer,mempitch);
} // end Draw_QuadFP_2D
在Game_Main()中,
int x0 = rand()%SCREEN_WIDTH;
int y0 = rand()%SCREEN_HEIGHT;
int width = rand()%SCREEN_WIDTH;
int height = rand()%SCREEN_HEIGHT;
// draw the triangle
Draw_QuadFP_2D(x0,y0,
x0+width,y0,
x0+width, y0+height,
x0, y0+height,
rand()%256,(UCHAR *)ddsd.lpSurface, ddsd.lPitch);
Ok,下一步进行封装,这个也简单,
成员函数
void DDRAW_Interface::Draw_QuadFP_2D( int x0,int y0,
int x1,int y1,
int x2,int y2,
int x3, int y3,
int color,
UCHAR *dest_buffer, int mempitch)
{
// this function draws a 2D quadrilateral
// simply call the triangle function 2x, let it do all the work
Draw_TriangleFP_2D(x0,y0,x1,y1,x3,y3,color,dest_buffer,mempitch);
Draw_TriangleFP_2D(x1,y1,x2,y2,x3,y3,color,dest_buffer,mempitch);
} // end Draw_Q
在game_main()中,
int x0 = rand()%SCREEN_WIDTH;
int y0 = rand()%SCREEN_HEIGHT;
int width = rand()%SCREEN_WIDTH;
int height = rand()%SCREEN_HEIGHT;
// draw the triangle
ddraw->Draw_QuadFP_2D(x0,y0,
x0+width,y0,
x0+width, y0+height,
x0, y0+height,
rand()%256, ddraw->getbackbuffer(), ddraw->getbacklpitch() );
OK
本文详细介绍了如何使用基本的三角形绘制技术来实现2D四边形的绘制,并通过封装成员函数进一步优化代码。在游戏主循环中,随机生成坐标参数并调用封装后的函数绘制四边形,展示了将复杂操作简化为简洁调用的过程。

被折叠的 条评论
为什么被折叠?



