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

📄 raiders3d_2b.cpp

📁 3D游戏编程大师技巧第九章的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
           mv.y = RAND_RANGE(-10,10);
           mv.z = RAND_RANGE(-10,10);
           mv.w = 1;

           VECTOR4D_Add(&mv, &trajectory[pindex*2+0], &trajectory[pindex*2+0]);

           // now rotation rate, this is difficult to do since we don't have the center of the polygon
           // maybe later...
           // trajectory[pindex*2+1] = 

           } // end for pindex


       // now transform final mesh into position destructively
       Transform_OBJECT4DV2(&explobj[eindex], mrot, TRANSFORM_LOCAL_ONLY,1);

       return(1);
       
       } // end if found a free explosion

    } // end for eindex

// return failure
return(0);

} // end Start_Mesh_Explosion

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

void Process_Aliens(void)
{
// this function moves the aliens and performs AI

for (int index = 0; index < NUM_ALIENS; index++)
    {
    // which state is alien in
    switch(aliens[index].state)
          {
          // is the alien dead? if so move on
          case ALIEN_STATE_DEAD: break;

          // is the alien alive?
          case ALIEN_STATE_ALIVE:
          case ALIEN_STATE_DYING:
          {
          // move the alien
          aliens[index].pos.x+=aliens[index].vel.y;
          aliens[index].pos.y+=aliens[index].vel.y;
          aliens[index].pos.z+=aliens[index].vel.z;
        
          // rotate the alien
          if ((aliens[index].ang.x+=aliens[index].rot.x) > 360)
              aliens[index].ang.x = 0;

          if ((aliens[index].ang.y+=aliens[index].rot.y) > 360)
              aliens[index].ang.y = 0;
            
          if ((aliens[index].ang.z+=aliens[index].rot.z) > 360)
              aliens[index].ang.z = 0;

          // test if alien has past players near clipping plane
          // if so remove from simulation
          if (aliens[index].pos.z < cam.near_clip_z)
             {
             // remove from simulation
             aliens[index].state = ALIEN_STATE_DEAD;

             // another one gets away!
             escaped++;
             } // end if 

          } break;
   
      
          default: break;

          } // end switch

    } // end for index

} // end Process_Aliens

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

void Init_Starfield(void)
{
// create the starfield
for (int 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

} // end Init_Starfield

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

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+3*diff_level);

    // 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(UCHAR *video_buffer, int lpitch)
{
// draw the stars in 3D using perspective transform

int index; // looping var

// convert pitch to words
lpitch>>=1;

// draw each star
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

       int i = ((float)(10000*NEAR_Z) / stars[index].z);
       if (i > 255) i = 255;

       ((USHORT *)video_buffer)[x_screen + y_screen*lpitch] = RGB16Bit(i,i,i);
       } // end else

    } // end for index

}  // Draw_Starfield

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

// 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; // looping var

// 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 and mouse
DInput_Init_Keyboard();
DInput_Init_Mouse();

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

// initialize directsound and directmusic
DSound_Init();
DMusic_Init();

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

game_over_id       = DSound_Load_WAV("gameover.wav");
ambient_systems_id = DSound_Load_WAV("stationthrob2.wav");

// initialize directmusic
DMusic_Init();

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

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

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

Open_Error_File("ERROR.TXT");

// initialize math engine
Build_Sin_Cos_Tables();

// initialize the camera with 90 FOV, normalized coordinates
Init_CAM4DV1(&cam,      // the camera object
             CAM_MODEL_EULER, // the euler model
             &cam_pos,  // initial camera position
             &cam_dir,  // initial camera angles
             &cam_target,    // no target
             150.0,          // near and far clipping planes
             20000.0,
             120.0,      // field of view in degrees
             WINDOW_WIDTH,   // size of final screen viewport
             WINDOW_HEIGHT);


// load flat shaded cube
VECTOR4D_INITXYZ(&vscale,18.00,18.00,18.00);
Load_OBJECT4DV2_COB(&obj_alien,"tie04.cob",  
                        &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ | VERTEX_FLAGS_SWAP_XZ |
                                               VERTEX_FLAGS_INVERT_WINDING_ORDER |
                                               VERTEX_FLAGS_TRANSFORM_LOCAL | 
                                               VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD );
// 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);

// set up lights
Reset_Lights_LIGHTV1();

// create some working colors
white.rgba      = _RGBA32BIT(255,255,255,0);
light_gray.rgba = _RGBA32BIT(100,100,100,0);
gray.rgba       = _RGBA32BIT(50,50,50,0);
black.rgba      = _RGBA32BIT(0,0,0,0);
red.rgba        = _RGBA32BIT(255,0,0,0);
green.rgba      = _RGBA32BIT(0,255,0,0);
blue.rgba       = _RGBA32BIT(0,0,255,0);

// ambient light
Init_Light_LIGHTV1(AMBIENT_LIGHT_INDEX,   
                   LIGHTV1_STATE_ON,      // turn the light on
                   LIGHTV1_ATTR_AMBIENT,  // ambient light type
                   gray, black, black,    // color for ambient term only
                   NULL, NULL,            // no need for pos or dir
                   0,0,0,                 // no need for attenuation
                   0,0,0);                // spotlight info NA

VECTOR4D dlight_dir = {-1,-1,1,0};

// directional sun light
Init_Light_LIGHTV1(INFINITE_LIGHT_INDEX,  
                   LIGHTV1_STATE_ON,      // turn the light on
                   LIGHTV1_ATTR_INFINITE, // infinite light type
                   black, light_gray, black,    // color for diffuse term only
                   NULL, &dlight_dir,     // need direction only
                   0,0,0,                 // no need for attenuation
                   0,0,0);                // spotlight info NA

// the red giant
VECTOR4D plight_pos = {-2700,-1600,8000,0};

// point light
Init_Light_LIGHTV1(POINT_LIGHT_INDEX,
                   LIGHTV1_STATE_ON,      // turn the light on
                   LIGHTV1_ATTR_POINT,    // pointlight type
                   black, red, black,     // color for diffuse term only
                   &plight_pos, NULL,     // need pos only
                   0,.001,0,              // linear attenuation only
                   0,0,1);                // spotlight info NA

// the weapons blast green glow point light from front of ship
VECTOR4D plight2_pos = {0,0,200,0};

// point light2
Init_Light_LIGHTV1(POINT_LIGHT2_INDEX,
                   LIGHTV1_STATE_OFF,       // turn the light on
                   LIGHTV1_ATTR_POINT,     //  pointlight
                   black, green, black,      // color for diffuse term only
                   &plight2_pos, NULL, // need pos only
                   0,.0005,0,                 // linear attenuation only
                   0,0,1);    


// create lookup for lighting engine
RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565,  // format we want to build table for
                                  palette,             // source palette
                                  rgblookup);          // lookup table



// load in the cockpit image
Load_Bitmap_File(&bitmap16bit, "cockpit03.BMP");
Create_BOB(&cockpit, 0,0,800,600,2, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); 
Load_Frame_BOB16(&cockpit, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS);
Unload_Bitmap_File(&bitmap16bit);

Load_Bitmap_File(&bitmap16bit, "cockpit03b.BMP");
Load_Frame_BOB16(&cockpit, &bitmap16bit,1,0,0,BITMAP_EXTRACT_MODE_ABS);
Unload_Bitmap_File(&bitmap16bit);


// load in the background starscape
Load_Bitmap_File(&bitmap16bit, "nebgreen03.bmp");
Create_BOB(&starscape, 0,0,800,600,1, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); 
Load_Frame_BOB16(&starscape, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS);
Unload_Bitmap_File(&bitmap16bit); 

// load the crosshair image
Load_Bitmap_File(&bitmap16bit, "crosshair01.bmp");
Create_BOB(&crosshair, 0,0,CROSS_WIDTH, CROSS_HEIGHT,1, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); 
Load_Frame_BOB16(&crosshair, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS);
Unload_Bitmap_File(&bitmap16bit); 

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_Delete_All_Sounds();
DSound_Shutdown();

// directmusic
DMusic_Delete_All_MIDI();
DMusic_Shutdown();

// shut down directinput
DInput_Release_Keyboard();

// shutdown directinput
DInput_Shutdown();

// shutdown directdraw last
DDraw_Shutdown();

Close_Error_File();

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

⌨️ 快捷键说明

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