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

📄 raiders3d.cpp

📁 书的配套源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// shutdown game and release all resources
Game_Shutdown();

// enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
// if it causes your system to crash
SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);

// return to Windows like this
return(msg.wParam);

} // end WinMain

// T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////

int Game_Init(void *parms)
{
// this function is where you do all the initialization 
// for your game

int index; // used for looping

Open_Error_File("error.txt");

// start up DirectDraw (replace the parms as you desire)
DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);

// initialize directinput
DInput_Init();

// acquire the keyboard 
DInput_Init_Keyboard();

// initialize directsound
DSound_Init();

// load in sound fx
explosion_id = DSound_Load_WAV("exp1.wav");
laser_id     = DSound_Load_WAV("shocker.wav");

// initialize directmusic
DMusic_Init();

// load and start main music track
main_track_id = DMusic_Load_MIDI("midifile2.mid");
DMusic_Play(main_track_id);

// add calls to acquire other directinput devices here...

// hide the mouse
if (!WINDOWED_APP)
   ShowCursor(FALSE);

// seed random number generator
srand(Start_Clock());

// all your initialization code goes here...

// create system colors
rgb_green = RGB16Bit(0,255,0);
rgb_white = RGB16Bit(255,255,255);
rgb_blue  = RGB16Bit(0,0,255);
rgb_red   = RGB16Bit(255,0,0);

// create the starfield
for (index=0; index < NUM_STARS; index++)
    {
    // randomly position stars in an elongated cylinder stretching from
    // the viewpoint 0,0,-d to the yon clipping plane 0,0,far_z
    stars[index].x = -WINDOW_WIDTH/2  + rand()%WINDOW_WIDTH;
    stars[index].y = -WINDOW_HEIGHT/2 + rand()%WINDOW_HEIGHT;
    stars[index].z = NEAR_Z + rand()%(FAR_Z - NEAR_Z);

    // set color of stars
    stars[index].color = rgb_white;
    } // end for index

// create the tie fighter model

// the vertex list for the tie fighter
POINT3D temp_tie_vlist[NUM_TIE_VERTS] = 
// color, x,y,z
{ {rgb_white,-40,40,0},    // p0
  {rgb_white,-40,0,0},    // p1
  {rgb_white,-40,-40,0},   // p2
  {rgb_white,-10,0,0},    // p3
  {rgb_white,0,20,0},     // p4
  {rgb_white,10,0,0},     // p5
  {rgb_white,0,-20,0},    // p6
  {rgb_white,40,40,0},     // p7
  {rgb_white,40,0,0},     // p8
  {rgb_white,40,-40,0}};   // p9

// copy the model into the real global arrays
for (index=0; index<NUM_TIE_VERTS; index++)
     tie_vlist[index] = temp_tie_vlist[index];
  
// the edge list for the tie fighter
LINE3D temp_tie_shape[NUM_TIE_EDGES] = 
// color, vertex 1, vertex 2
{ {rgb_green,0,2      },    // l0
  {rgb_green,1,3      },    // l1
  {rgb_green,3,4      },    // l2
  {rgb_green,4,5      },    // l3
  {rgb_green,5,6      },    // l4
  {rgb_green,6,3      },    // l5
  {rgb_green,5,8      },    // l6
  {rgb_green,7,9      } };  // l7

// copy the model into the real global arrays
for (index=0; index<NUM_TIE_EDGES; index++)
     tie_shape[index] = temp_tie_shape[index];

// initialize the position of each tie fighter and it's velocity
for (index=0; index<NUM_TIES; index++)
    {
    // initialize this tie fighter
    Init_Tie(index);

    } // end for index

// 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

// release all your resources created for the game here....

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

// directmusic
DMusic_Delete_All_MIDI();
DMusic_Shutdown();

// shut down directinput
DInput_Shutdown();

// shutdown directdraw last
DDraw_Shutdown();

// return success
return(1);
} // end Game_Shutdown

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

void Start_Explosion(int tie)
{
// this starts an explosion based on the sent tie fighter

// first hunt and see if an explosion is free
for (int index=0; index < NUM_EXPLOSIONS; index++)
    {
    if (explosions[index].state==0)
       {
       // start this explosion up using the properties
       // if the tie figther index sent

       explosions[index].state   = 1; // enable state of explosion
       explosions[index].counter = 0; // reset counter for explosion 

       // set color of explosion
       explosions[index].color = rgb_green;

       // make copy of of edge list, so we can blow it up
       for (int edge=0; edge < NUM_TIE_EDGES; edge++)
           {
           // start point of edge
           explosions[index].p1[edge].x = ties[tie].x+tie_vlist[tie_shape[edge].v1].x;
           explosions[index].p1[edge].y = ties[tie].y+tie_vlist[tie_shape[edge].v1].y;
           explosions[index].p1[edge].z = ties[tie].z+tie_vlist[tie_shape[edge].v1].z;
 
           // end point of edge
           explosions[index].p2[edge].x = ties[tie].x+tie_vlist[tie_shape[edge].v2].x;
           explosions[index].p2[edge].y = ties[tie].y+tie_vlist[tie_shape[edge].v2].y;
           explosions[index].p2[edge].z = ties[tie].z+tie_vlist[tie_shape[edge].v2].z;

           // compute trajectory vector for edges
           explosions[index].vel[edge].x = ties[tie].xv - 8+rand()%16;
           explosions[index].vel[edge].y = ties[tie].yv - 8+rand()%16;
           explosions[index].vel[edge].z = -3+rand()%4;

           } // end for edge
     
       // done, so return
       return;
       } // end if found

    } // end for index

} // end Start_Explosion

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

void Process_Explosions(void)
{
// this processes all the explosions

// loop thro all the explosions and render them
for (int index=0; index<NUM_EXPLOSIONS; index++)
    {
    // test if this explosion is active?
    if (explosions[index].state==0)
       continue;

    for (int edge=0; edge<NUM_TIE_EDGES; edge++)
        {
        // must be exploding, update edges (shrapel)
        explosions[index].p1[edge].x+=explosions[index].vel[edge].x;
        explosions[index].p1[edge].y+=explosions[index].vel[edge].y;
        explosions[index].p1[edge].z+=explosions[index].vel[edge].z;

        explosions[index].p2[edge].x+=explosions[index].vel[edge].x;
        explosions[index].p2[edge].y+=explosions[index].vel[edge].y;
        explosions[index].p2[edge].z+=explosions[index].vel[edge].z;
        } // end for edge

    // test for terminatation of explosion?
    if (++explosions[index].counter > 100)
       explosions[index].state = explosions[index].counter = 0;            

    } // end for index

} // end Process_Explosions

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

void Draw_Explosions(void)
{
// this draws all the explosions

// loop thro all the explosions and render them
for (int index=0; index<NUM_EXPLOSIONS; index++)
    {
    // test if this explosion is active?
    if (explosions[index].state==0)
       continue;

    // render this explosion
    // each explosion is made of a number of edges
    for (int edge=0; edge < NUM_TIE_EDGES; edge++)
    {
    POINT3D p1_per, p2_per; // used to hold perspective endpoints
    
    // test if edge if beyond near clipping plane
    if (explosions[index].p1[edge].z < NEAR_Z && 
        explosions[index].p2[edge].z < NEAR_Z)
       continue;
   
    // step 1: perspective transform each end point
    p1_per.x = VIEW_DISTANCE*explosions[index].p1[edge].x/explosions[index].p1[edge].z;
    p1_per.y = VIEW_DISTANCE*explosions[index].p1[edge].y/explosions[index].p1[edge].z;
    p2_per.x = VIEW_DISTANCE*explosions[index].p2[edge].x/explosions[index].p2[edge].z;
    p2_per.y = VIEW_DISTANCE*explosions[index].p2[edge].y/explosions[index].p2[edge].z;

    // step 2: compute screen coords
    int p1_screen_x = WINDOW_WIDTH/2  + p1_per.x;
    int p1_screen_y = WINDOW_HEIGHT/2 - p1_per.y;
    int p2_screen_x = WINDOW_WIDTH/2  + p2_per.x;
    int p2_screen_y = WINDOW_HEIGHT/2 - p2_per.y;

    // step 3: draw the edge
    Draw_Clip_Line16(p1_screen_x, p1_screen_y, p2_screen_x, p2_screen_y,
                     explosions[index].color,back_buffer, back_lpitch);    

    } // end for edge

    } // end for index

} // end Draw_Explosions

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

void Move_Starfield(void)
{
// move the stars

int index; // looping var

// the stars are technically stationary,but we are going
// to move them to simulate motion of the viewpoint
for (index=0; index<NUM_STARS; index++)
    {
    // move the next star
    stars[index].z-=player_z_vel;

    // test for past near clipping plane
    if (stars[index].z <= NEAR_Z)
        stars[index].z = FAR_Z;

    } // end for index

} // end Move_Starfield

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

void Draw_Starfield(void)
{
// draw the stars in 3D using perspective transform

int index; // looping var

for (index=0; index<NUM_STARS; index++)
    {
    // draw the next star
    // step 1: perspective transform
    float x_per = VIEW_DISTANCE*stars[index].x/stars[index].z;
    float y_per = VIEW_DISTANCE*stars[index].y/stars[index].z;
    
    // step 2: compute screen coords
    int x_screen = WINDOW_WIDTH/2  + x_per;
    int y_screen = WINDOW_HEIGHT/2 - y_per;

    // clip to screen coords
    if (x_screen>=WINDOW_WIDTH || x_screen < 0 || 
        y_screen >= WINDOW_HEIGHT || y_screen < 0)
       {
       // continue to next star
       continue; 
       } // end if
    else
       {
       // else render to buffer
       ((USHORT *)back_buffer)[x_screen + y_screen*(back_lpitch >> 1)] 
                              = stars[index].color;
       } // end else

    } // end for index

}  // Draw_Starfield

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

void Init_Tie(int index)
{
// this function starts a tie fighter up at the far end
// of the universe and sends it our way!

// position each tie in the viewing volume
ties[index].x = -WINDOW_WIDTH  + rand()%(2*WINDOW_WIDTH);
ties[index].y = -WINDOW_HEIGHT + rand()%(2*WINDOW_HEIGHT);
ties[index].z =  4*FAR_Z;

// initialize velocity of tie fighter

⌨️ 快捷键说明

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