网摘-Introduction to DirectDraw and Surface Blitting

http://www.codeproject.com/directx/basicdd.asp

WinMain and Message Loop - The Starting point 

用MFC开发Direct程序是不明智的,应为MFC需要很大的系统开销,而这些开销对开发Direct来说是不必要的。所以作者选择了以SDK做开发。让我们又回到了WinMain、WinProc时代。

Initializing DirectX and DirectDraw

1. surfaces

Surfaces are memory regions that contains graphics that can be used in your application. Everything we need to drawn on the screen needs to be created on a surface first.

In fact, for DirectDraw applications, the area that displays what we are seeing on the screen is considered a surface too, and it's called the FrontBuffer. Attached to this FrontBuffer surface, we have another surface called the BackBuffer. This surface stores the information of what will be showed to the user in the next frame of our application.

2.page flipping

What really happens in the background is that DirectDraw changes the pointer of backbuffer with the pointer of frontbuffer, so that next time the video card send the video data to the monitor it uses the backbuffered content and not the old frontbuffer. When we do a page flip, the content of the backbuffer becomes the content of the previously showed frontbuffer, and not the same content of the drawn backbuffer as you might think.

工程

#include <ddraw.h>

kernel32.lib user32.lib ddraw.lib dxguid.lib gdi32.lib

Blitting GraphicsDrawing in the BackBuffer using the cSurface Class

BOOL cSurface::Draw(LPDIRECTDRAWSURFACE7 lpDest,  int  iDestX,  int  iDestY, 
                    
int  iSrcX,  int  iSrcY,  int  nWidth,  int  nHeight)
{
    RECT    rcRect;
    HRESULT    hRet;

    
if(nWidth == 0)
        nWidth 
= m_Width;

    
if(nHeight == 0)
        nHeight 
= m_Height;

    rcRect.left   
= iSrcX;
    rcRect.top    
= iSrcY;
    rcRect.right  
= nWidth  + iSrcX;
    rcRect.bottom 
= nHeight + iSrcY;

    
while(1)
    
{
        
if((int)m_ColorKey < 0)
        
{
            hRet 
= lpDest->BltFast(iDestX, iDestY, m_pSurface, 
                                   
&rcRect,  DDBLTFAST_NOCOLORKEY);
        }

        
else
        
{
            hRet 
= lpDest->BltFast(iDestX, iDestY, m_pSurface, 
                                   
&rcRect,  DDBLTFAST_SRCCOLORKEY);
        }


        
if(hRet == DD_OK)
            
break;

        
if(hRet == DDERR_SURFACELOST)
        
{
            Restore();
        }

        
else
        
{
            
if(hRet != DDERR_WASSTILLDRAWING)
                
return FALSE;
        }

    }


    
return TRUE;

}

 

void  ProcessIdle()
{
    HRESULT hRet;
    
static int iX = 0, iY = 0;
    
static iLastBlit;

    
if(GetTickCount() - iLastBlit < 50)
    
{
        
return;
    }


    g_surfCar.Draw(g_pDDSBack, 
245170, iX, iY, 150140);

    
while1 )
    
{
        hRet 
= g_pDDSFront->Flip(NULL, 0 );
        
if( hRet == DD_OK )
        
{
            
break;
        }

        
if( hRet == DDERR_SURFACELOST )
        
{
            g_pDDSFront
->Restore();
        }

        
if( hRet != DDERR_WASSTILLDRAWING )
        
{
            
break;
        }

    }


    iX 
+= 150;
    
if(iX >= 1500)
    
{
        iX 
= 0;
        iY 
+= 140;
        
if(iY >= 280)
        
{
            iY 
= 0;
        }

    }


    iLastBlit 
= GetTickCount();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值