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

📄 demo13_9_16b.cpp

📁 《Windows游戏编程大师技巧(第二版)》源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
DSound_Init();


// define points of arm1
VERTEX2DF arm1_vertices[4] =  { -10,-10, 100,-10, 100,10, -10,10, };

// initialize arm1
arm1[0].state       = 1;   // turn it on
arm1[0].num_verts   = 4;  
arm1[0].x0          = 0; // position it
arm1[0].y0          = 0;
arm1[0].xv          = 0;
arm1[0].yv          = 0;
arm1[0].color       = RGB16Bit(255,0,0); // red
arm1[0].vlist       = new VERTEX2DF [arm1[0].num_verts];
 
for (index = 0; index < arm1[0].num_verts; index++)
    arm1[0].vlist[index] = arm1_vertices[index];

// define points of arm2
VERTEX2DF arm2_vertices[4] =  { -10,-10, 100,-10, 100,10, -10,10, };

// initialize arm2
arm2[0].state       = 1;   // turn it on
arm2[0].num_verts   = 4;  
arm2[0].x0          = 0; // position it
arm2[0].y0          = 0;
arm2[0].xv          = 0;
arm2[0].yv          = 0;
arm2[0].color       = RGB16Bit(0,0,255); // blue
arm2[0].vlist       = new VERTEX2DF [arm2[0].num_verts];
 
for (index = 0; index < arm2[0].num_verts; index++)
    arm2[0].vlist[index] = arm2_vertices[index];

// build the 360 degree look ups
Build_Sin_Cos_Tables();

// set clipping region
min_clip_x = 0;
max_clip_x = screen_width - 1;
min_clip_y = 0;
max_clip_y = screen_height - 1;

// return success
return(1);

} // end Game_Init

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

int Game_Shutdown(void *parms)
{
// this function is where you shutdown your game and
// release all resources that you allocated

// shut everything down

// shutdown directdraw last
DDraw_Shutdown();

// now directsound
DSound_Stop_All_Sounds();
DSound_Shutdown();

// shut down directinput
DInput_Shutdown();

// return success
return(1);

} // end Game_Shutdown

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

void Copy_Polygon2D(POLYGON2D_PTR dest, POLYGON2D_PTR source)
{
// copies one poly to another
dest->state     = source->state;     
dest->num_verts = source->num_verts  ;
dest->x0    = source->x0;        
dest->y0    = source->y0;       
dest->xv    = source->xv;         
dest->yv    = source->yv;         
dest->color = source->color;

// now copy vertex list
if (dest->vlist)
   free (dest->vlist);

// allocate memory for new list
dest->vlist   = new VERTEX2DF [dest->num_verts];

// now copy the vertex list
memcpy(dest->vlist, source->vlist, sizeof(VERTEX2DF)*dest->num_verts);

} // end Copy_Polygon2D

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

inline int Mat_Init_3X2(MATRIX3X2_PTR ma, 
                        float m00, float m01,
                        float m10, float m11,
                        float m20, float m21)
{
// this function fills a 3x2 matrix with the sent data in row major form
ma->M[0][0] = m00; ma->M[0][1] = m01; 
ma->M[1][0] = m10; ma->M[1][1] = m11; 
ma->M[2][0] = m20; ma->M[2][1] = m21; 

// return success
return(1);

} // end Mat_Init_3X2

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

int Translate_Polygon2D_Verts_Mat(POLYGON2D_PTR poly, float dx, float dy)
{
// this function translates the local coordinates of the polygon at the vertex
// level rather than the center point

// test for valid pointer
if (!poly)
   return(0);


MATRIX3X2 mt; // used to hold translation transform matrix

// initialize the matrix with translation values dx dy
Mat_Init_3X2(&mt,1,0, 0,1, dx, dy); 

// loop and translate each point
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)
    {
    // scale and store result back
    
    // create a 1x2 matrix to do the transform
    MATRIX1X2 p0 = {poly->vlist[curr_vert].x, poly->vlist[curr_vert].y};
    MATRIX1X2 p1 = {0,0}; // this will hold result

    // now translate via a matrix multiply
    Mat_Mul_1X2_3X2(&p0, &mt, &p1);

    // now copy the result back into vertex
    poly->vlist[curr_vert].x = p1.M[0];
    poly->vlist[curr_vert].y = p1.M[1];

    } // end for curr_vert

// return success
return(1);

} // end Translate_Polygon2D_Verts_Mat

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

int Game_Main(void *parms)
{
// this is the workhorse of your game it will be called
// continuously in real-time this is like main() in C
// all the calls for you game go here!

int index; // looping var

static int   curr_angle1 = -90, 
             curr_angle2 = 45 ; // current link angles

// start the timing clock
Start_Clock();

// clear the drawing surface
//DDraw_Fill_Surface(lpddsback, 0);

// lock back buffer and copy background into it
DDraw_Lock_Back_Surface();

// draw background
Draw_Bitmap16(&background_bmp, back_buffer, back_lpitch,0);

// unlock back surface
DDraw_Unlock_Back_Surface();

// read keyboard
DInput_Read_Keyboard();

// test for arm1 rotation
if (keyboard_state[DIK_A])  
   { 
   curr_angle1-=ARM_ANG;
   } // end if
else
if (keyboard_state[DIK_S]) 
   { 
   curr_angle1+=ARM_ANG;
   } // end if


// test for arm2 rotation
if (keyboard_state[DIK_D])  
   { 
   curr_angle2-=ARM_ANG;
   } // end if
else
if (keyboard_state[DIK_F]) 
   { 
   curr_angle2+=ARM_ANG;
   } // end if


// test for overflow
if (curr_angle1 >= 360)
   curr_angle1-=360;
else
if (curr_angle1 < 0)
   curr_angle1+=360;

if (curr_angle2 >= 360)
   curr_angle2-=360;
else
if (curr_angle2 < 0)
   curr_angle2+=360;


// first arm1

// first copy arms to transform polygon
Copy_Polygon2D(&arm1[1], &arm1[0]);

// rotate
Rotate_Polygon2D_Mat(&arm1[1], curr_angle1);

// now translate
Translate_Polygon2D_Verts_Mat(&arm1[1], 324, 400);

// now arm2

// first copy arms to transform polygon
Copy_Polygon2D(&arm2[1], &arm2[0]);

// rotate
Rotate_Polygon2D_Mat(&arm2[1], curr_angle2);

// and rotate around link 1
Rotate_Polygon2D_Mat(&arm2[1], curr_angle1);

// now translate to link 1
Translate_Polygon2D_Verts_Mat(&arm2[1], (arm1[1].vlist[1].x+arm1[1].vlist[2].x)/2, 
                                        (arm1[1].vlist[1].y+arm1[1].vlist[2].y)/2 );

// lock back buffer and copy background into it
DDraw_Lock_Back_Surface();

// draw arms
Draw_Polygon2D16(&arm1[1], back_buffer, back_lpitch);
Draw_Polygon2D16(&arm2[1], back_buffer, back_lpitch);

// unlock back surface
DDraw_Unlock_Back_Surface();

// draw the title
Draw_Text_GDI("(16-Bit Version) Forward Kinematic DEMO, Press <ESC> to Exit.",10, 10,RGB(255,255,255), lpddsback);
Draw_Text_GDI("<A>, <S> to adjust Arm 1, <D>, <F> to adjust Arm 2",10, 25, RGB(255,255,255), lpddsback);

sprintf(buffer, "Arm 1 Angle=%d, Arm 2 Angle=%d", 360-curr_angle1, 360-curr_angle2);
Draw_Text_GDI(buffer,10, 60, RGB(255,255,255), lpddsback);

// flip the surfaces
DDraw_Flip();

// sync to 30 fps = 1/30sec = 33 ms
Wait_Clock(33);

// check of user is trying to exit
if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
    {
    PostMessage(main_window_handle, WM_DESTROY,0,0);

    // stop all sounds
    DSound_Stop_All_Sounds();
    } // end if

// return success
return(1);

} // end Game_Main

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

⌨️ 快捷键说明

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