Device->BeginScene(); // assume _surface is a pointer to an idirect3dsurface9 interface. // assumes a 32-bit pixel format for each pixel. // get the surface description.描述表面 D3DSURFACE_DESC surfacedesc; Device->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&_surface); _surface->GetDesc(&surfacedesc);//注意已经假定了_surface是指向idirect3dsurface9借//口的指针 // get a pointer to the surface pixel data.取得指向该内存区的指针 D3DLOCKED_RECT lockedrect; _surface->LockRect( &lockedrect,// pointer to receive locked data指向申请到的内存区域 0, // lock entire surface 0); // no lock flags specified // iterate through each pixel in the surface and set it to red.设置指定的表面为红色 DWORD * imagedata = (DWORD *)lockedrect.pBits; for(int i = 0; i < surfacedesc.Height; i++) { for(int j = 0; j < surfacedesc.Width; j++) { // index into texture, note we use the pitch and divide by // four since the pitch is given in bytes and there are // 4 bytes per dword. int index = i * lockedrect.Pitch / 4 + j; imagedata[index] = 0xffff0000; // red } } _surface->UnlockRect(); Device->EndScene();
d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; // d3dpp是D3DPRESENT_PARAMETERS 结构体变量 这一条非常关键哦
以上是主要代码。采用directx 9.0实现的。当时探索了很久,费了好久的功夫啊。。
在此感谢这两位朋友的博客
IDirect3DSurface9::LockRect失败的问题
Directx IDirect3DSurface9:LockRect调用返回一个绝对值很大负数
欢迎交流。