开发者

Grabbing the backbuffer with DirectX 7

开发者 https://www.devze.com 2023-01-03 23:48 出处:网络
I\'m trying to write a small chunk of code to grab the backbuffer into an array of pixels. I\'ve barely used directX before as I\'m more of a OpenGL fan.

I'm trying to write a small chunk of code to grab the backbuffer into an array of pixels. I've barely used directX before as I'm more of a OpenGL fan. My wish is to actually replace some code in a project that grabs the backbuffer using BitBlt and DC which is very slow.

This is supposed to work on all computers and that's why I chose directx7.

My question is.. h开发者_Go百科ow would I do that?

Thank you.


What I do is to use a helper class to do the lock /unlock as below. Then you use it like so :

mBackBuffer->Flip( DDFLIP_WAIT );

{
   DDSURFACEDESC2 ddsd;
   ZeroMemory( &ddsd, sizeof( ddsd ) );
   ddsd.dwSize = sizeof( ddsd );

   ReadLock r( mBackBuffer, ddsd, NULL /* for whole surface */ );

   if ( r )
   {
      // ddsd.lpSurface contains the void* pointer to the bytes
      // ddsd.lPitch contains the byte count of each horizontal line
   }

} // ReadLock unlocks when it goes out of scope

class ReadLock
{
    public:
        ReadLock(IDirectDrawSurface7* surface, DDSURFACEDESC2& ddsd, LPRECT pRect = 0 ) : surface_(surface), mpRect( pRect ), hr( S_OK )
        {
            hr = surface_->Lock( mpRect, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_NOSYSLOCK | DDLOCK_WAIT | DDLOCK_READONLY, 0 );
        }

        HRESULT getResult() const { return hr; }
        bool operator!() const { return FAILED( hr ); }
        operator bool() const { return SUCCEEDED( hr ); }

        ~ReadLock()
        {
            if ( surface_ && SUCCEEDED( hr ) )
                surface_->Unlock(mpRect);
        }

    private:
        HRESULT hr;
        RECT* mpRect;
        IDirectDrawSurface7* surface_;
};


TBH DirectX 9 will work even with ancient cards. You don't have all the features available but you have a a SHED load more usable information out there. Although I think you might be a bit knackered on Win 95/9/me support and win 2K. Bear in mind NT4 never had a decent version of DirectX.

Alas I don't have the DX7 docs anywhere handy but I'm pretty sure you could just get the back buffer surface and then lock it to get at the data. Though you need to bear in mind just how slow grabbing the back buffer can be, especially on old cards. Copying the back buffer from local video memory to system memory across the PCI or AGP bus is incredibly slow.

What exactly are you trying to achieve? There must be better ways to achieve what you are after doing ...

0

精彩评论

暂无评论...
验证码 换一张
取 消