如何锁住内存表面(获取对表面的控制权)

访问任何显示表面--------主显示表面、从显示表面 、etc,------必须对内存进行加锁和解锁,

原因是:首先,告诉DirectDraw你已经控制内存了(就是说该内存不能被其他进程访问到)

              其次,要指示显示设备当你在操作锁住的内存时,它不能够移动任何cache和虚拟内存缓冲区。

 

加锁函数:

HRESULT Lock( LPRECT lpDestRect,
 LPDDSURFACEDESC lpDDSurfaceDesc,
 DWORD dwFlags,
 HANDLE hEvent
 );

Parameters

lpDestRect

Points to a RECT structure identifying the region of surface that is being locked.

lpDDSurfaceDesc

Points to a DDSURFACEDESC structure to be filled in with the relevant (相关的、有关的)details about the surface.

dwFlags

One or more of the following flags.

hEvent

Handle to a system event that should be triggered(触发、引发) when the surface is ready to be locked

 

解锁函数:

HRESULT Unlock( LPVOID lpSurfaceData
 );

Parameters

lpSurfaceData

This is the pointer returned by Lock. Since it is possible to call Lock multiple times for the same Surface with different destination rectangles, this pointer is used to tie the Lock and Unlock calls together.

 

一个应用例子:

// plot 1000 random pixels to the primary surface and return
// clear ddsd and set size, never assume it's clean
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);

if (FAILED(lpddsprimary->Lock(NULL, &ddsd,
                   DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,
                   NULL)))
   {
   // error
   return(0);
   } // end if

// now ddsd.lPitch is valid and so is ddsd.lpSurface

// make a couple aliases to make code cleaner, so we don't
// have to cast
int mempitch        = (int)ddsd.lPitch;
UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;

// plot 1000 random pixels with random colors on the
// primary surface, they will be instantly visible
for (int index=0; index < 1000; index++)            ///////绘制像素代码
    {
    // select random position and color for 640x480x8
    UCHAR color = rand()%256;
    int x = rand()%640;
    int y = rand()%480;

    // plot the pixel
    video_buffer[x+y*mempitch] = color;  //////说明color是系统硬件那个调色板的索引       (不明白的是如何关联上那个调色板了......思考......)

    } // end for index

// now unlock the primary surface
if (FAILED(lpddsprimary->Unlock(NULL)))
   return(0);

 

 

 

 

 

 

你可能感兴趣的:(null,Random,buffer,colors,plot,structure)