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

📄 t3dlib1.cpp

📁 一个完整的基于DirectX的射击游戏 没有使用OO
💻 CPP
📖 第 1 页 / 共 5 页
字号:
int Load_Frame_BOB(BOB_PTR bob, // bob to load with data
                   BITMAP_FILE_PTR bitmap, // bitmap to scan image data from
                   int frame,       // frame to load
                   int cx,int cy,   // cell or absolute pos. to scan image from
                   int mode)        // if 0 then cx,cy is cell position, else 
                                    // cx,cy are absolute coords
{
// this function extracts a bitmap out of a bitmap file

DDSURFACEDESC2 ddsd;  //  direct draw surface description 

// is this a valid bob
if (!bob)
   return(0);

UCHAR *source_ptr,   // working pointers
      *dest_ptr;

// test the mode of extraction, cell based or absolute
if (mode==BITMAP_EXTRACT_MODE_CELL)
   {
   // re-compute x,y
   cx = cx*(bob->width+1) + 1;
   cy = cy*(bob->height+1) + 1;
   } // end if

// extract bitmap data
source_ptr = bitmap->buffer + cy*bitmap->bitmapinfoheader.biWidth+cx;

// get the addr to destination surface memory

// set size of the structure
ddsd.dwSize = sizeof(ddsd);

// lock the display surface
(bob->images[frame])->Lock(NULL,
                           &ddsd,
                           DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
                           NULL);

// assign a pointer to the memory surface for manipulation
dest_ptr = (UCHAR *)ddsd.lpSurface;

// iterate thru each scanline and copy bitmap
for (int index_y=0; index_y<bob->height; index_y++)
    {
    // copy next line of data to destination
    memcpy(dest_ptr, source_ptr,bob->width);

    // advance pointers
    dest_ptr   += (ddsd.lPitch); // (bob->width+bob->width_fill);
    source_ptr += bitmap->bitmapinfoheader.biWidth;
    } // end for index_y

// unlock the surface 
(bob->images[frame])->Unlock(NULL);

// set state to loaded
bob->attr |= BOB_ATTR_LOADED;

// return success
return(1);

} // end Load_Frame_BOB

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

int Load_Frame_BOB16(BOB_PTR bob, // bob to load with data
                     BITMAP_FILE_PTR bitmap, // bitmap to scan image data from
                     int frame,       // frame to load
                     int cx,int cy,   // cell or absolute pos. to scan image from
                     int mode)        // if 0 then cx,cy is cell position, else 
                                    // cx,cy are absolute coords
{
// this function extracts a 16-bit bitmap out of a 16-bit bitmap file

DDSURFACEDESC2 ddsd;  //  direct draw surface description 

// is this a valid bob
if (!bob)
   return(0);

USHORT *source_ptr,   // working pointers
       *dest_ptr;

// test the mode of extraction, cell based or absolute
if (mode==BITMAP_EXTRACT_MODE_CELL)
   {
   // re-compute x,y
   cx = cx*(bob->width+1) + 1;
   cy = cy*(bob->height+1) + 1;
   } // end if

// extract bitmap data
source_ptr = (USHORT *)bitmap->buffer + cy*bitmap->bitmapinfoheader.biWidth+cx;

// get the addr to destination surface memory

// set size of the structure
ddsd.dwSize = sizeof(ddsd);

// lock the display surface
(bob->images[frame])->Lock(NULL,
                           &ddsd,
                           DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
                           NULL);

// assign a pointer to the memory surface for manipulation
dest_ptr = (USHORT *)ddsd.lpSurface;

// iterate thru each scanline and copy bitmap
for (int index_y=0; index_y<bob->height; index_y++)
    {
    // copy next line of data to destination
    memcpy(dest_ptr, source_ptr,(bob->width*2));

    // advance pointers
    dest_ptr   += (ddsd.lPitch >> 1); 
    source_ptr += bitmap->bitmapinfoheader.biWidth;
    } // end for index_y

// unlock the surface 
(bob->images[frame])->Unlock(NULL);

// set state to loaded
bob->attr |= BOB_ATTR_LOADED;

// return success
return(1);

} // end Load_Frame_BOB16

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

int Animate_BOB(BOB_PTR bob)
{
// this function animates a bob, basically it takes a look at
// the attributes of the bob and determines if the bob is 
// a single frame, multiframe, or multi animation, updates
// the counters and frames appropriately

// is this a valid bob
if (!bob)
   return(0);

// test the level of animation
if (bob->attr & BOB_ATTR_SINGLE_FRAME)
    {
    // current frame always = 0
    bob->curr_frame = 0;
    return(1);
    } // end if
else
if (bob->attr & BOB_ATTR_MULTI_FRAME)
   {
   // update the counter and test if its time to increment frame
   if (++bob->anim_counter >= bob->anim_count_max)
      {
      // reset counter
      bob->anim_counter = 0;

      // move to next frame
      if (++bob->curr_frame >= bob->num_frames)
         bob->curr_frame = 0;

      } // end if
  
   } // end elseif
else
if (bob->attr & BOB_ATTR_MULTI_ANIM)
   {
   // this is the most complex of the animations it must look up the
   // next frame in the animation sequence

   // first test if its time to animate
   if (++bob->anim_counter >= bob->anim_count_max)
      {
      // reset counter
      bob->anim_counter = 0;

      // increment the animation frame index
      bob->anim_index++;
      
      // extract the next frame from animation list 
      bob->curr_frame = bob->animations[bob->curr_animation][bob->anim_index];
     
      // is this and end sequence flag -1
      if (bob->curr_frame == -1)
         {
         // test if this is a single shot animation
         if (bob->attr & BOB_ATTR_ANIM_ONE_SHOT)
            {
            // set animation state message to done
            bob->anim_state = BOB_STATE_ANIM_DONE;
            
            // reset frame back one
            bob->anim_index--;

            // extract animation frame
            bob->curr_frame = bob->animations[bob->curr_animation][bob->anim_index];    

            } // end if
        else
           {
           // reset animation index
           bob->anim_index = 0;

           // extract first animation frame
           bob->curr_frame = bob->animations[bob->curr_animation][bob->anim_index];
           } // end else

         }  // end if
      
      } // end if

   } // end elseif

// return success
return(1);

} // end Amimate_BOB

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

int Scroll_BOB(void)
{
// this function scrolls a bob 
// not implemented

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

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

int Move_BOB(BOB_PTR bob)
{
// this function moves the bob based on its current velocity
// also, the function test for various motion attributes of the'
// bob and takes the appropriate actions
   

// is this a valid bob
if (!bob)
   return(0);

// translate the bob
bob->x+=bob->xv;
bob->y+=bob->yv;

// test for wrap around
if (bob->attr & BOB_ATTR_WRAPAROUND)
   {
   // test x extents first
   if (bob->x > max_clip_x)
       bob->x = min_clip_x - bob->width;
   else
   if (bob->x < min_clip_x-bob->width)
       bob->x = max_clip_x;
   
   // now y extents
   if (bob->x > max_clip_x)
       bob->x = min_clip_x - bob->width;
   else
   if (bob->x < min_clip_x-bob->width)
       bob->x = max_clip_x;

   } // end if
else
// test for bounce
if (bob->attr & BOB_ATTR_BOUNCE)
   {
   // test x extents first
   if ((bob->x > max_clip_x - bob->width) || (bob->x < min_clip_x) )
       bob->xv = -bob->xv;
    
   // now y extents 
   if ((bob->y > max_clip_y - bob->height) || (bob->y < min_clip_y) )
       bob->yv = -bob->yv;

   } // end if

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

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

int Load_Animation_BOB(BOB_PTR bob, 
                       int anim_index, 
                       int num_frames, 
                       int *sequence)
{
// this function load an animation sequence for a bob
// the sequence consists of frame indices, the function
// will append a -1 to the end of the list so the display
// software knows when to restart the animation sequence

// is this bob valid
if (!bob)
   return(0);

// allocate memory for bob animation
if (!(bob->animations[anim_index] = (int *)malloc((num_frames+1)*sizeof(int))))
   return(0);

// load data into 
for (int index=0; index<num_frames; index++)
    bob->animations[anim_index][index] = sequence[index];

// set the end of the list to a -1
bob->animations[anim_index][index] = -1;

// return success
return(1);

} // end Load_Animation_BOB

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

int Set_Pos_BOB(BOB_PTR bob, int x, int y)
{
// this functions sets the postion of a bob

// is this a valid bob
if (!bob)
   return(0);

// set positin
bob->x = x;
bob->y = y;

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

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

int Set_Anim_Speed_BOB(BOB_PTR bob,int speed)
{
// this function simply sets the animation speed of a bob
    
// is this a valid bob
if (!bob)
   return(0);

// set speed
bob->anim_count_max = speed;

// return success
return(1);

} // end Set_Anim_Speed

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

int Set_Animation_BOB(BOB_PTR bob, int anim_index)
{
// this function sets the animation to play

// is this a valid bob
if (!bob)
   return(0);

// set the animation index
bob->curr_animation = anim_index;

// reset animation 
bob->anim_index = 0;

// return success
return(1);

} // end Set_Animation_BOB

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

int Set_Vel_BOB(BOB_PTR bob,int xv, int yv)
{
// this function sets the velocity of a bob

// is this a valid bob
if (!bob)
   return(0);

// set velocity
bob->xv = xv;
bob->yv = yv;

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

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

int Hide_BOB(BOB_PTR bob)
{
// this functions hides bob 

// is this a valid bob
if (!bob)
   return(0);

// reset the visibility bit
RESET_BIT(bob->attr, BOB_ATTR_VISIBLE);

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

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

int Show_BOB(BOB_PTR bob)
{
// this function shows a bob

// is this a valid bob
if (!bob)
   return(0);

// set the visibility bit
SET_BIT(bob->attr, BOB_ATTR_VISIBLE);

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

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

int Collision_BOBS(BOB_PTR bob1, BOB_PTR bob2)
{
// are these a valid bobs
if (!bob1 || !bob2)
   return(0);

// get the radi of each rect
int width1  = (bob1->width>>1) - (bob1->width>>3);
int height1 = (bob1->height>>1) - (bob1->height>>3);

int width2  = (bob2->width>>1) - (bob2->width>>3);
int height2 = (bob2->height>>1) - (bob2->height>>3);

// compute center of each rect
int cx1 = bob1->x + width1;
int cy1 = bob1->y + height1;

int cx2 = bob2->x + width2;
int cy2 = bob2->y + height2;

// compute deltas
int dx = abs(cx2 - cx1);
int dy = abs(cy2 - cy1);

// test if rects overlap
if (dx < (width1+width2) && dy < (height1+height2))
   return(1);
else
// else no collision
return(0);

} // end Collision_BOBS

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

int DDraw_Init(int width, int height, int bpp, int windowed)
{
// this function initializes directdraw
int index; // looping variable

// create IDirectDraw interface 7.0 object and test for error
if (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)))
   return(0);

// based on windowed or fullscreen set coorperation level
if (windowed)
   {
   // set cooperation level to windowed mode 
   if (FAILED(lpdd->SetCooperativeLevel(main_window_handle,DDSCL_NORMAL)))
       return(0);

   } // end if
else
   {
   // set cooperation level to fullscreen mode 
   if (FAILED(lpdd->SetCooperativeLevel(main_window_handle,
              DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | 
              DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT | DDSCL_MULTITHREADED )))
       return(0);

   // set the display mode
   if (FAILED(lpdd->SetDisplayMode(width,height,bpp,0,0)))
      return(0);

⌨️ 快捷键说明

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