缩放很简单,就是把各个坐标乘以缩放因子即可。
这次是一个多边形
int Scale_Polygon2D(POLYGON2D_PTR poly, float sx, float sy)
{
// this function scalesthe local coordinates of the polygon
// test for valid pointer
if (!poly)
return(0);
// loop and scale each point
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)
{
// scale and store result back
poly->vlist[curr_vert].x *= sx;
poly->vlist[curr_vert].y *= sy;
} // end for curr_vert
// return success
return(1);
} // end Scale_Polygon2D
POLYGON2D asteroid;
在game_init()中,
// define points of asteroid
VERTEX2DF asteroid_vertices[8] = {33,-3, 9,-18, -12,-9, -21,-12, -9,6, -15,15, -3,27, 21,21};
// initialize asteroid
asteroid.state = 1; // turn it on
asteroid.num_verts = 8;
asteroid.x0 = SCREEN_WIDTH/2; // position it
asteroid.y0 = SCREEN_HEIGHT/2;
asteroid.xv = 0;
asteroid.yv = 0;
asteroid.color = 255; // white
asteroid.vlist = new VERTEX2DF [asteroid.num_verts];
for (int index = 0; index < asteroid.num_verts; index++)
asteroid.vlist[index] = asteroid_vertices[index];
在game_main()中,按 s和a键,可以放缩
// do the graphics
Draw_Polygon2D(&asteroid, (UCHAR *)ddsd.lpSurface, ddsd.lPitch);
// test for scale
if (KEYDOWN('A')) // scale up
Scale_Polygon2D(&asteroid, 1.1, 1.1);
else
if (KEYDOWN('S')) // scale down
Scale_Polygon2D(&asteroid, 0.9, 0.9);
// rotate the polygon by 5 degrees
Rotate_Polygon2D(&asteroid, 5);
截图如下:
![](https://img-blog.csdn.net/20130920111739671?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGlyZWN0eDNkX2JlZ2lubmVy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
现在进行封装
只需要加个成员函数即可。
int DDRAW_Interface::Scale_Polygon2D(POLYGON2D_PTR poly, float sx, float sy)
{
// this function scalesthe local coordinates of the polygon
// test for valid pointer
if (!poly)
return(0);
// loop and scale each point
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)
{
// scale and store result back
poly->vlist[curr_vert].x *= sx;
poly->vlist[curr_vert].y *= sy;
} // end for curr_vert
// return success
return(1);
}
但是由于cos_look和sin_look在成员变量中,所以,进行了设置
void DDRAW_Interface::setCosLook( int i , float cos_value )
{
cos_look[i] = cos_value;
}
void DDRAW_Interface::setSinLook( int i , float sin_value )
{
sin_look[i] = sin_value;
}
在game_init()中
// define points of asteroid
VERTEX2DF asteroid_vertices[8] = {33,-3, 9,-18, -12,-9, -21,-12, -9,6, -15,15, -3,27, 21,21};
// initialize asteroid
asteroid.state = 1; // turn it on
asteroid.num_verts = 8;
asteroid.x0 = SCREEN_WIDTH/2; // position it
asteroid.y0 = SCREEN_HEIGHT/2;
asteroid.xv = 0;
asteroid.yv = 0;
asteroid.color = 255; // white
asteroid.vlist = new VERTEX2DF [asteroid.num_verts];
for (int index = 0; index < asteroid.num_verts; index++)
asteroid.vlist[index] = asteroid_vertices[index];
// generate the tables
for (int ang = 0; ang < 360; ang++)
{
// convert ang to radians
float theta = (float)ang*PI/(float)180;
ddraw->setCosLook( ang, cos(theta) );
ddraw->setSinLook(ang, sin( theta));
} // end for ang
在game_main()中,ddraw->Draw_Polygon2D(&asteroid, ddraw->getbackbuffer(), ddraw->getbacklpitch());
// test for scale
if (KEYDOWN('A')) // scale up
ddraw->Scale_Polygon2D(&asteroid, 1.1, 1.1);
else
if (KEYDOWN('S')) // scale down
ddraw->Scale_Polygon2D(&asteroid, 0.9, 0.9);
// rotate the polygon by 5 degrees
ddraw->Rotate_Polygon2D(&asteroid, 5);
ddraw->DDraw_Unlock_Back_Surface();
ddraw->DDraw_Flip();
//while( FAILED( ddraw->DDraw_Flip() ) );
Sleep( 33 );