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
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;
}
... {
HRESULT hRet;
static int iX = 0, iY = 0;
static iLastBlit;
if(GetTickCount() - iLastBlit < 50)
...{
return;
}
g_surfCar.Draw(g_pDDSBack, 245, 170, iX, iY, 150, 140);
while( 1 )
...{
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();
}