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

📄 raiders3d_2b.cpp

📁 3D游戏编程大师技巧第九章的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:

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

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!

static MATRIX4X4 mrot;   // general rotation matrix

// these are used to create a circling camera
static float view_angle = 0; 
static float camera_distance = 6000;
static VECTOR4D pos = {0,0,0,0};
static float tank_speed;
static float turning = 0;
// state variables for different rendering modes and help
static int wireframe_mode = 0;
static int backface_mode  = 1;
static int lighting_mode  = 1;
static int zsort_mode     = 1;

char work_string[256]; // temp string
int index; // looping var

// what state is game in?
switch(game_state)
      {
      case GAME_STATE_INIT:
      {
      // load the font
      Load_Bitmap_Font("tech_char_set_01.bmp", &tech_font);
     
      // transition to un state
      game_state = GAME_STATE_RESTART;
      } break;

      case GAME_STATE_RESTART:
      {
      // start music
      DMusic_Play(main_track_id);
      DSound_Play(ambient_systems_id, DSBPLAY_LOOPING);

      // initialize the stars
      Init_Starfield();      

      // initialize all the aliens
      Init_Aliens();

      // reset all vars
      player_z_vel = 4; // virtual speed of viewpoint/ship

      cross_x = CROSS_START_X; // cross hairs
      cross_y = CROSS_START_Y;

      cross_x_screen  = CROSS_START_X;   // used for cross hair
      cross_y_screen  = CROSS_START_Y; 
      target_x_screen = CROSS_START_X;   // used for targeter
      target_y_screen = CROSS_START_Y;

      escaped       = 0;   // tracks number of missed ships
      hits          = 0;   // tracks number of hits
      score         = 0;   // take a guess :)
      weapon_energy = 100; // weapon energy
      weapon_active = 0;   // weapons are off
      
      game_state_count1    = 0;    // general counters
      game_state_count2    = 0;
      restart_state        = 0;    // state when game is restarting
      restart_state_count1 = 0;    // general counter

      // difficulty
      diff_level           = 1;

      // transition to run state
      game_state = GAME_STATE_RUN;

      } break;

      case GAME_STATE_RUN:
      case GAME_STATE_OVER: // keep running sim, but kill diff_level, and player input
      {
      // start the clock
      Start_Clock();

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

      // blt to destination surface
      lpddsback->Blt(NULL, starscape.images[starscape.curr_frame], NULL, DDBLT_WAIT, NULL); 

      // reset the render list
      Reset_RENDERLIST4DV2(&rend_list);

      // read keyboard and other devices here
      DInput_Read_Keyboard();
      DInput_Read_Mouse();

      // game logic begins here...


#ifdef DEBUG_ON      
      if (keyboard_state[DIK_UP])
         {
         py+=10;        

         } // end if
 
      if (keyboard_state[DIK_DOWN])
         {
         py-=10;     
         } // end if

      if (keyboard_state[DIK_RIGHT])
         {
         px+=10;
         } // end if

      if (keyboard_state[DIK_LEFT])
         {
         px-=10;
         } // end if

      if (keyboard_state[DIK_1])
         {
         pz+=20;

         } // end if

      if (keyboard_state[DIK_2])
         {
         pz-=20;

         } // end if
#endif

      // modes and lights

      // wireframe mode
      if (keyboard_state[DIK_W])
        {
        // toggle wireframe mode
        if (++wireframe_mode > 1)
           wireframe_mode = 0;

           Wait_Clock(100); // wait, so keyboard doesn't bounce
        } // end if

     // backface removal
     if (keyboard_state[DIK_B])
        {
        // toggle backface removal
        backface_mode = -backface_mode;
        Wait_Clock(100); // wait, so keyboard doesn't bounce
        } // end if

     // lighting
     if (keyboard_state[DIK_L])
        {
        // toggle lighting engine completely
        lighting_mode = -lighting_mode;
        Wait_Clock(100); // wait, so keyboard doesn't bounce
        } // end if

     // toggle ambient light
     if (keyboard_state[DIK_A])
        {
        // toggle ambient light
        if (lights[AMBIENT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
           lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
        else
           lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;

        Wait_Clock(100); // wait, so keyboard doesn't bounce
        } // end if

      // toggle infinite light
      if (keyboard_state[DIK_I])
         {
         // toggle ambient light
         if (lights[INFINITE_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
            lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
         else
            lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_ON;

         Wait_Clock(100); // wait, so keyboard doesn't bounce
          } // end if

      // toggle point light
      if (keyboard_state[DIK_P])
         {
         // toggle point light
         if (lights[POINT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
            lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
        else
            lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;

         Wait_Clock(100); // wait, so keyboard doesn't bounce
         } // end if

       // toggle spot light
       if (keyboard_state[DIK_S])
          {
          // toggle spot light
          if (lights[SPOT_LIGHT2_INDEX].state == LIGHTV1_STATE_ON)
             lights[SPOT_LIGHT2_INDEX].state = LIGHTV1_STATE_OFF;
          else
             lights[SPOT_LIGHT2_INDEX].state = LIGHTV1_STATE_ON;

          Wait_Clock(100); // wait, so keyboard doesn't bounce
          } // end if

       // z-sorting
       if (keyboard_state[DIK_Z])
          {
          // toggle z sorting
          zsort_mode = -zsort_mode;
          Wait_Clock(100); // wait, so keyboard doesn't bounce
          } // end if

       // track cross hair
       cross_x+=(mouse_state.lX);
       cross_y-=(mouse_state.lY); 

       // check bounds (x,y) are in 2D space coords, not screen coords
       if (cross_x >= (WINDOW_WIDTH/2-CROSS_WIDTH/2))
           cross_x = (WINDOW_WIDTH/2-CROSS_WIDTH/2) - 1;
       else
       if (cross_x <= -(WINDOW_WIDTH/2-CROSS_WIDTH/2))
           cross_x = -(WINDOW_WIDTH/2-CROSS_WIDTH/2) + 1;

       if (cross_y >= (WINDOW_HEIGHT/2-CROSS_HEIGHT/2))
           cross_y = (WINDOW_HEIGHT/2-CROSS_HEIGHT/2) - 1;
       else
       if (cross_y <= -(WINDOW_HEIGHT/2-CROSS_HEIGHT/2))
           cross_y = -(WINDOW_HEIGHT/2-CROSS_HEIGHT/2) + 1;

       // player is done moving create camera matrix //////////////////////////
       Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);

       // perform all non-player game AI and motion here /////////////////////

       // move starfield
       Move_Starfield();

       // move aliens
       Process_Aliens(); 

       // update difficulty of game
       if ((diff_level+=DIFF_RATE) > DIFF_PMAX)
           diff_level = DIFF_PMAX;

       // start a random alien as a function of game difficulty
       if ( (rand()%(DIFF_PMAX - (int)diff_level+2)) == 1)
          Start_Alien();
       
       // perform animation and transforms on lights //////////////////////////

       // lock the back buffer
       DDraw_Lock_Back_Surface();

       // draw all 3D entities
       Draw_Aliens();

       // draw mesh explosions
       Draw_Mesh_Explosions();
    
       // entire into final 3D pipeline /////////////////////////////////////// 
   
       // remove backfaces
       if (backface_mode==1)
          Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);

       // light scene all at once 
       if (lighting_mode==1)
          Light_RENDERLIST4DV2_World16(&rend_list, &cam, lights, 4);

       // apply world to camera transform
       World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);

       // sort the polygon list (hurry up!)
       if (zsort_mode == 1)
          Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);

       // apply camera to perspective transformation
       Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);

       // apply screen transform
       Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);

       // draw the starfield now
       Draw_Starfield(back_buffer, back_lpitch);

       // reset number of polys rendered
       debug_polys_rendered_per_frame = 0;

       // render the list
       if (wireframe_mode  == 0)
          {
          Draw_RENDERLIST4DV2_Solid16(&rend_list, back_buffer, back_lpitch);
          }
       else
       if (wireframe_mode  == 1)
          {
          Draw_RENDERLIST4DV2_Wire16(&rend_list, back_buffer, back_lpitch);
          } // end if

       // draw energy bindings
       Render_Energy_Bindings(energy_bindings, 1, 8, 16, RGB16Bit(0,255,0), back_buffer, back_lpitch);

       // fire the weapon
       if (game_state == GAME_STATE_RUN && mouse_state.rgbButtons[0] && weapon_energy > 20)
          {
          // set endpoints of energy bolts
          weapon_bursts[1].x = cross_x_screen + RAND_RANGE(-4,4);
          weapon_bursts[1].y = cross_y_screen + RAND_RANGE(-4,4);

          weapon_bursts[3].x = cross_x_screen + RAND_RANGE(-4,4);
          weapon_bursts[3].y = cross_y_screen + RAND_RANGE(-4,4);

          // draw the weapons firing
          Render_Weapon_Bursts(weapon_bursts, 2, 8, 16, RGB16Bit(0,255,0), back_buffer, back_lpitch);

          // decrease weapon energy
          if ((weapon_energy-=5) < 0)
             weapon_energy = 0;

          // make sound
          DSound_Play(laser_id);

          // energize weapons
          weapon_active  = 1; 

          // turn the lights on!
          lights[POINT_LIGHT2_INDEX].state = LIGHTV1_STATE_ON;
          cockpit.curr_frame = 1;

          } // end if
        else
           {
           weapon_active  = 0; 

           // turn the lights off!
           lights[POINT_LIGHT2_INDEX].state = LIGHTV1_STATE_OFF;
           cockpit.curr_frame = 0;
           } // end else

          if ((weapon_energy+=1) > 100)
             weapon_energy = 100;

       // draw any overlays ///////////////////////////////////////////////////

       // unlock the back buffer
       DDraw_Unlock_Back_Surface();

       // draw cockpit
       Draw_BOB16(&cockpit, lpddsback);

       // draw information
       sprintf(work_string, "SCORE:%d", score);  
       Draw_Bitmap_Font_String(&tech_font, TPOS_SCORE_X,TPOS_SCORE_Y, work_string, FONT_HPITCH, FONT_VPITCH, lpddsback);

       sprintf(work_string, "HITS:%d", hits);  
       Draw_Bitmap_Font_String(&tech_font, TPOS_HITS_X,TPOS_HITS_Y, work_string, FONT_HPITCH, FONT_VPITCH, lpddsback);

       sprintf(work_string, "ESCAPED:%d", escaped);  
       Draw_Bitmap_Font_String(&tech_font, TPOS_ESCAPED_X,TPOS_ESCAPED_Y, work_string, FONT_HPITCH, FONT_VPITCH, lpddsback);

⌨️ 快捷键说明

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