设定了端点和多边形两个结构体
// a 2D vertex
typedef struct VERTEX2DI_TYP
{
int x,y; // the vertex
} VERTEX2DI, *VERTEX2DI_PTR;
// a 2D polygon
typedef struct POLYGON2D_TYP
{
int state; // state of polygon
int num_verts; // number of vertices
int x0,y0; // position of center of polygon
int xv,yv; // initial velocity
DWORD color; // could be index or PALETTENTRY
VERTEX2DI *vlist; // pointer to vertex list
} POLYGON2D, *POLYGON2D_PTR;
其实就是绘制封闭线段。在上节的基础上,
初始化代码如下述:
// define points of asteroid
VERTEX2DI asteroid_vertices[8] = {33,-3, 9,-18, -12,-9, -21,-12, -9,6, -15,15, -3,27, 21,21};
// loop and initialize all asteroids
for (int curr_index = 0; curr_index < NUM_ASTEROIDS; curr_index++)
{
// initialize the asteroid
asteroids[curr_index].state = 1; // turn it on
asteroids[curr_index].num_verts = 8;
asteroids[curr_index].x0 = rand()%SCREEN_WIDTH; // position it
asteroids[curr_index].y0 = rand()%SCREEN_HEIGHT;
asteroids[curr_index].xv = -8 + rand()%17;
asteroids[curr_index].yv = -8 + rand()%17;
asteroids[curr_index].color = rand()%256;
asteroids[curr_index].vlist = new VERTEX2DI [asteroids[curr_index].num_verts];
for (int index = 0; index < asteroids[curr_index].num_verts; index++)
asteroids[curr_index].vlist[index] = asteroid_vertices[index];
} // end for curr_index
在game_main()中,确定边界
DDraw_Fill_Surface( lpddsback, 0 );
DDRAW_INIT_STRUCT( ddsd );
lpddsback->Lock( NULL, & ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL );
for( int curr_index = 0; curr_index < NUM_ASTEROIDS; curr_index ++ )
{
asteroids[curr_index].color = rand() % 256;
Draw_Polygon2D( & asteroids[curr_index], ( UCHAR * )ddsd.lpSurface, ddsd.lPitch );
asteroids[curr_index].x0 += asteroids[curr_index].xv;
asteroids[curr_index].y0 += asteroids[curr_index].yv;
if( asteroids[curr_index].x0 > SCREEN_WIDTH + 100 )
asteroids[curr_index].x0 = -100;
if( asteroids[curr_index].y0 > SCREEN_HEIGHT + 100 )
asteroids[curr_index].y0 = -100;
if( asteroids[curr_index].x0 < - 100 )
asteroids[curr_index].x0 = SCREEN_WIDTH + 100;
if( asteroids[curr_index].y0 < - 100 )
asteroids[curr_index].y0 = SCREEN_HEIGHT + 100;
}
lpddsback->Unlock( NULL );
while( FAILED( lpddsprimary->Flip( NULL, DDFLIP_WAIT ) ) );
Sleep( 33 );
结果如图所示
封装,
加个成员变量
int DDRAW_Interface::Draw_Polygon2D( POLYGON2D_PTR poly, UCHAR * vbuffer, int lpitch )
{
int index = 0;
if( poly->state )
{
for(index = 0; index < poly->num_verts - 1; index ++ )
{
Draw_Clip_Line( poly->vlist[index].x + poly->x0,
poly->vlist[index].y + poly->y0,
poly->vlist[index+1].x + poly->x0,
poly->vlist[index+1].y + poly->y0,
poly->color,
vbuffer, lpitch );
}
Draw_Clip_Line( poly->vlist[0].x + poly->x0,
poly->vlist[0].y + poly->y0,
poly->vlist[index].x + poly->x0,
poly->vlist[index].y + poly->y0,
poly->color,
vbuffer, lpitch );
return ( 1 );
}
else
return ( 0 );
}
在main()中
POLYGON2D asteroids[64];
VERTEX2DI asteroid_vertices[8] = { 30, -3, 9, -18, -12, -9, -21, -12, -9, 6, -15, 15, -3, 27, 21, 21 };
for( int curr_index = 0; curr_index < NUM_ASTEROIDS; curr_index ++ )
{
asteroids[curr_index].state= 1;
asteroids[curr_index].num_verts = 8;
asteroids[curr_index].x0 = rand() % SCREEN_WIDTH;
asteroids[curr_index].y0 = rand() % SCREEN_HEIGHT;
asteroids[curr_index].xv = -8 + rand() % 17;
asteroids[curr_index].yv = -8 + rand() % 17;
asteroids[curr_index].color = rand() % 256;
asteroids[curr_index].vlist = new VERTEX2DI[asteroids[curr_index].num_verts];
for( int index = 0; index < asteroids[curr_index].num_verts; index ++ )
asteroids[curr_index].vlist[index] = asteroid_vertices[index];
}
在GAME_MAIN()中
ddraw->DDraw_Fill_Surface( ddraw->getbackSurface(), 0 );
ddraw->DDraw_Lock_Back_Surface();
for( int curr_index = 0; curr_index < 64; curr_index ++ )
{
asteroids[curr_index].color = rand() % 256;
ddraw->Draw_Polygon2D( & asteroids[curr_index], ddraw->getbackbuffer(), ddraw->getbacklpitch() );
asteroids[curr_index].x0 += asteroids[curr_index].xv;
asteroids[curr_index].y0 += asteroids[curr_index].yv;
if( asteroids[curr_index].x0 > SCREEN_WIDTH + 100 )
asteroids[curr_index].x0 = -100;
if( asteroids[curr_index].y0 > SCREEN_HEIGHT + 100 )
asteroids[curr_index].y0 = -100;
if( asteroids[curr_index].x0 < - 100 )
asteroids[curr_index].x0 = SCREEN_WIDTH + 100;
if( asteroids[curr_index].y0 < - 100 )
asteroids[curr_index].y0 = SCREEN_HEIGHT + 100;
}
ddraw->DDraw_Unlock_Back_Surface();
while( FAILED( ddraw->DDraw_Flip() ) );
Sleep( 33 );
OK
下一步根据t3dlib进行封装。今天下午做个大清理吧,从头看到尾,
先看下DDRAW_Shutdown();,析构函数重新整理
DDRAW_Interface::~DDRAW_Interface()
{
if( m_lpddclipper )
m_lpddclipper->Release();
if( m_lpddclipperwin )
m_lpddclipperwin->Release();
if( m_lpddsback )
{
m_lpddsback->Release();
}
// now the primary surface
if (m_lpddsprimary)
{
m_lpddsprimary->Release();
} // end if
if (m_lpddpal)
{
m_lpddpal->Release();
} // end if
if( m_lpdd )
{
m_lpdd->Release();
}
}
USHORT DDRAW_Interface::RGB16Bit565( int r, int g, int b )
{
r >>= 3;
g >>= 2;
b >>= 3;
return ( _RGB16BIT565( ( r ), ( g ), ( b ) ) );
}
int DDRAW_Interface::Draw_Pixel16( int x, int y, int color, USHORT * video_buffer, int lpitch16 )
{
( ( USHORT * ) video_buffer )[x + y * ( lpitch16 >> 1 )] = color;
return ( 1 );
}
int DDRAW_Interface::Draw_Text_GDI( char * text, int x, int y, COLORREF color, LPDIRECTDRAWSURFACE7 lpdds )
{
HDC xdc;
if( FAILED( lpdds->GetDC( & xdc ) ) )
return ( 0 );
SetTextColor( xdc, color );
SetBkMode( xdc, TRANSPARENT );
TextOut( xdc, x, y, text, strlen( text ) );
lpdds->ReleaseDC( xdc );
return 1;
}
int DDRAW_Interface::Draw_Text_GDI( char * text, int x, int y, int color, LPDIRECTDRAWSURFACE7 lpdds )
{
HDC xdc;
if( FAILED( lpdds->GetDC( & xdc ) ) )
return ( 0 );
SetTextColor( xdc, RGB( palette[color].peRed,palette[color].peGreen,palette[color].peBlue ) );
SetBkMode( xdc, TRANSPARENT );
TextOut( xdc, x, y, text, strlen( text ) );
lpdds->ReleaseDC( xdc );
return 1;
}