2013年9月5日星期四(8-5,缩放)

缩放很简单,就是把各个坐标乘以缩放因子即可。

 

这次是一个多边形

 

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);

截图如下:

现在进行封装

只需要加个成员函数即可。

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 );

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值