后备缓冲

来源:互联网 发布:淘宝内衣营销方案 编辑:程序博客网 时间:2024/04/19 19:47


int Game_Init(void *parms = NULL, int num_parms = 0)
{
 //创建IID_IDirectDraw7接口
 if (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)))
    return(0);

 //设置协作级别
 if (FAILED(lpdd->SetCooperativeLevel(main_window_handle,
            DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
            DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
    return(0);

 //设置显示模块
 if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,0,0)))
    return(0);


 // clear ddsd and set size
 DDRAW_INIT_STRUCT(ddsd);

 // enable valid fields
 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;

 //设置后备缓冲数目
 ddsd.dwBackBufferCount = 1;

 // request a complex, flippable
 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;

 // create the primary surface
 if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)))
    return(0);

 // now query for attached surface from the primary surface

 // this line is needed by the call
 ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;

 //获取后备缓冲
 if (FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsback)))
   return(0);

 // build up the palette data array
 for (int color=1; color < 255; color++)
 {
  // fill with random RGB values
  palette[color].peRed   = rand()%256;
  palette[color].peGreen = rand()%256;
  palette[color].peBlue  = rand()%256;

  // set flags field to PC_NOCOLLAPSE
  palette[color].peFlags = PC_NOCOLLAPSE;
 } // end for color

 // now fill in entry 0 and 255 with black and white
 palette[0].peRed     = 0;
 palette[0].peGreen   = 0;
 palette[0].peBlue    = 0;
 palette[0].peFlags   = PC_NOCOLLAPSE;

 palette[255].peRed   = 255;
 palette[255].peGreen = 255;
 palette[255].peBlue  = 255;
 palette[255].peFlags = PC_NOCOLLAPSE;

 // create the palette object
 if (FAILED(lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 |
         DDPCAPS_INITIALIZE,
         palette,&lpddpal, NULL)))
 return(0);

 // finally attach the palette to the primary surface
 if (FAILED(lpddsprimary->SetPalette(lpddpal)))
    return(0);

 // return success or failure or your own return code here
 return(1);

} // end Game_Init

int Game_Main(void *parms = NULL, int num_parms = 0)
{
 if (window_closed)
    return(0);

 if (KEYDOWN(VK_ESCAPE))
   {
    PostMessage(main_window_handle,WM_CLOSE,0,0);
    window_closed = 1;
   } // end if

 // lock the back buffer
 DDRAW_INIT_STRUCT(ddsd);
 lpddsback->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);

 UCHAR *back_buffer = (UCHAR *)ddsd.lpSurface;

 if (ddsd.lPitch == SCREEN_WIDTH)
  memset(back_buffer,0,SCREEN_WIDTH*SCREEN_HEIGHT);
 else
   {
    // non-linear memory
  
    // make copy of video pointer
    UCHAR *dest_ptr = back_buffer;

    // clear out memory one line at a time
    for (int y=0; y<SCREEN_HEIGHT; y++)
    {
     // clear next line
     memset(dest_ptr,0,SCREEN_WIDTH);
      
     // advance pointer to next line
     dest_ptr+=ddsd.lPitch;
    } // end for y

   } // end else


 // you would perform game logic...
      
 // draw the next frame into the back buffer, notice that we
 // must use the lpitch since it's a surface and may not be linear

 // plot 5000 random pixels
 for (int index=0; index < 5000; index++)
 {
  int   x   = rand()%SCREEN_WIDTH;
  int   y   = rand()%SCREEN_HEIGHT;
  UCHAR col = rand()%256;
  back_buffer[x+y*ddsd.lPitch] = col;
 } // end for index

 // unlock the back buffer
 if (FAILED(lpddsback->Unlock(NULL)))
    return(0);

 // perform the flip
 while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));

 // wait a sec
 Sleep(500);

 // return success or failure or your own return code here
 return(1);

} // end Game_Main

原创粉丝点击