下面我们开始分析源程序:
下载源程序 (编辑者:链接丢失)
属于Win32程序的基本框架的我们用蓝色标出,而用红色表出的是我们要重点学习的。下面的所有程序都是 FullScreenMode.cpp 文件中的内容,其中“IDB_DIRECTX”和“IDB_WinXP”都是图片资源的ID号,我想如何向程序中添加资源应该不用我多说了吧:)
工程文件:FullScreenMode.cpp
#define STRICT #include <windows.h> #include <ddraw.h> #include <mmsystem.h> #include "resource.h" #include "ddutil.h"
//定义删除指针和释放对象的宏
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
#define SCREEN_WIDTH 1024 //定义屏幕宽度
#define SCREEN_HEIGHT 768 //定义屏幕高度
#define SCREEN_BPP 8 //定义调色板位数
#define SPRITE_DIAMETER 32 //定义飘动的子画面 的直径(宽度和高度一样)
#define NUM_SPRITES 10 //定义飘动的子画面 的个数
#define HELPTEXT TEXT("Press Escape to quit.") //定义文本住表面
|
上面出现的这个 TEXT( ) 是一个系统头文件里定义的宏,起作用是检查程序中是否定义了 Unicode ,如果有,就将括号中的文本转化成宽自负,如果没有,则转化成ASCLL 码。以下定义的是子画面飘动时的属性:
struct SPRITE_STRUCT
{ FLOAT fPosX; //sprite当前坐标的x值,如果在初始化时,即为初始坐标x值 FLOAT fPosY; //sprite当前坐标的y值,如果在初始化时,即为初始坐标y值 FLOAT fVelX; //sprite在x轴上的速度 FLOAT fVelY; //sprite在y轴上的速度 };
CDisplay* g_pDisplay = NULL; /*CDisplay就是ddutil.h(我们又新加入的目录中的)中定义的类,用于处理表面之间的拷贝翻页等操作的类,再次定义一个全局变量,用于以后对指向的表面之间进行操作*/
CSurface* g_pBackSurface = NULL; /* CSurface也是ddutil.h头文件中定义的类,用于对表面本身进行操作,如设置色彩键码,在此定义的是背景图画指针*/
CSurface* g_pLogoSurface = NULL; /*子画面图画指针*/
CSurface* g_pTextSurface = NULL; /*背景文本指针*/
BOOL g_bActive = FALSE; /*定义一个bool形变量,起到一个开关的作用,对程序流程进行控制*/
DWORD g_dwLastTick; /*用于记录最后一次的系统时间*/
SPRITE_STRUCT g_Sprite[NUM_SPRITES]; /*定义多个子画面,其中包括多个 运动时的属性*/
|
Function-prototypes
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );//主窗口消息处理函数
HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd, HACCEL* phAccel );//窗口类设置,注册,并创建窗口
HRESULT InitDirectDraw( HWND hWnd );
VOID FreeDirectDraw();
HRESULT ProcessNextFrame();
VOID UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta );
HRESULT DisplayFrame();
HRESULT RestoreSurfaces();
|
WinMain()
Desc: Entry point to the program. Initializes everything and calls
UpdateFrame() when idle from the message pump.
int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow ) { MSG msg; HWND hWnd; HACCEL hAccel;
ZeroMemory( &g_Sprite, sizeof(SPRITE_STRUCT) * NUM_SPRITES );//清空内存 srand( GetTickCount() /*用于获取自windows启动以来经历的时间长度(毫秒)*/ ); //设置随机数随时间变化而变化
if( FAILED( WinInit( hInst, nCmdShow, &hWnd, &hAccel ) ) ) return FALSE;
if( FAILED( InitDirectDraw( hWnd ) ) ) { MessageBox( hWnd, TEXT("DirectDraw init failed. ") TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"), MB_ICONERROR | MB_OK );
return FALSE; }
g_dwLastTick = timeGetTime();//获得当前系统时间
while( TRUE ) {
// Look for messages, if none are found then // update the state and display it
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) ) { if( 0 == GetMessage(&msg, NULL, 0, 0 ) ) {
// WM_QUIT was posted, so exit
return (int)msg.wParam; }
// Translate and dispatch the message
if( 0 == TranslateAccelerator( hWnd, hAccel, &msg ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } else { if( g_bActive ) {
// Move the sprites, blt them to the back buffer, then // flip or blt the back buffer to the primary buffer
if( FAILED( ProcessNextFrame() ) /*设置下张页面的子画面的位置*/) { SAFE_DELETE( g_pDisplay ); MessageBox( hWnd, TEXT("Displaying the next frame failed. ") TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"),
MB_ICONERROR | MB_OK ); return FALSE; } } else {
// Make sure we go to sleep if we have nothing else to do
WaitMessage();
// Ignore time spent inactive
g_dwLastTick = timeGetTime();//获得当前系统时间 } } } }
|
WinInit()
Desc: Init the window
HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd, HACCEL* phAccel ) { WNDCLASS wc; HWND hWnd; HACCEL hAccel;
// Register the Window Class 设置窗口类
wc.lpszClassName = TEXT("FullScreenMode");
wc.lpfnWndProc = MainWndProc;
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.hInstance = hInst;
wc.hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN) );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
if( RegisterClass( &wc ) == 0 /*注册窗口类*/ ) return E_FAIL;
// Load keyboard accelerators
hAccel = LoadAccelerators( hInst, MAKEINTRESOURCE(IDR_MAIN_ACCEL) );
// Create and show the main window
hWnd = CreateWindowEx( 0, TEXT("FullScreenMode"), TEXT("DirectDraw FullScreenMode Sample"),
WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL );
if( hWnd == NULL ) return E_FAIL;
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
*phWnd = hWnd;
*phAccel = hAccel;
return S_OK; }
|
InitDirectDraw()
Desc: Create the DirectDraw object, and init the surfaces
HRESULT InitDirectDraw( HWND hWnd ) { HRESULT hr; //接受返回值,其实是long型变量
LPDIRECTDRAWPALETTE pDDPal = NULL; //定义程序中的调色板
int iSprite; //定义与sprite个数有关的计数器
g_pDisplay = new CDisplay();//动态开辟一个CDisplay类
if( FAILED( hr = g_pDisplay->CreateFullScreenDisplay( hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP ) ) ) //设置程序为全屏,并且g_pDisplay就为后备缓冲区表面的句柄,即指向后备缓冲区表面的指针。 { MessageBox( hWnd, TEXT("This display card does not support 1024x768x8. "), TEXT("DirectDraw Sample"), MB_ICONERROR | MB_OK ); return hr; }
// Create and set the palette when in palettized color
if( FAILED( hr = g_pDisplay->CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( IDB_DIRECTX ) ) ) ) //顾名思义,就是从bmp图片中获得调色板值,并赋值在pDDPal结构指针中。 return hr; if( FAILED( hr = g_pDisplay->SetPalette( pDDPal ) ) ) //用刚才从IDB_DIRECTX中获得的调色板制来设置程序调色板 return hr;
SAFE_RELEASE( pDDPal );//释放指针
// Create a surface, and draw a bitmap resource on it. // 用IDB_DIRECTX图片创建一个表面,并用g_pLogoSurface指向这个表面 if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pLogoSurface, MAKEINTRESOURCE( IDB_DIRECTX ), SPRITE_DIAMETER, SPRITE_DIAMETER ) ) ) return hr;
if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pBackSurface, MAKEINTRESOURCE( IDB_WINXP ), SCREEN_WIDTH, SCREEN_HEIGHT ) ) ) return hr;
// Create a surface, and draw text to it. //创建文本表面
if( FAILED( hr = g_pDisplay->CreateSurfaceFromText( &g_pTextSurface, NULL, HELPTEXT, RGB(0,0,0), RGB(255, 255, 0) ) ) ) return hr;
// Set the color key for the logo sprite to black //设置色彩键码为黑色,0代表黑色,这样在表面的拷贝过程中黑色像素的点将不会被拷贝
if( FAILED( hr = g_pLogoSurface->SetColorKey( 0 ) ) ) return hr;
if( FAILED( hr = g_pTextSurface->SetColorKey( 0 ) ) ) return hr;
// Init all the sprites. All of these sprites look the same, // using the g_pDDSLogo surface.
for( iSprite = 0; iSprite < NUM_SPRITES; iSprite++ ) {
// Set the sprite's position and velocity // 设置这些 sprite(小怪物)的初始坐标随机产生 g_Sprite[iSprite].fPosX = (float) (rand() % SCREEN_WIDTH); g_Sprite[iSprite].fPosY = (float) (rand() % SCREEN_HEIGHT);
// 速度也随机产生
g_Sprite[iSprite].fVelX = 500.0f * rand() / RAND_MAX - 250.0f; g_Sprite[iSprite].fVelY = 500.0f * rand() / RAND_MAX - 250.0f; } return S_OK; }
|
FreeDirectDraw()
Release all the DirectDraw objects 释放所有指针。
VOID FreeDirectDraw() { SAFE_DELETE( g_pBackSurface ); SAFE_DELETE( g_pLogoSurface ); SAFE_DELETE( g_pTextSurface ); SAFE_DELETE( g_pDisplay ); }
|
MainWndProc()
Desc: The main window procedure
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch (msg) { case WM_COMMAND: switch( LOWORD(wParam) ) { case IDM_EXIT:
// Received key/menu command to exit app PostMessage( hWnd, WM_CLOSE, 0, 0 ); return 0L; } break; // Continue with default processing
case WM_SETCURSOR:
// Hide the cursor in fullscreen SetCursor( NULL );
return TRUE;
case WM_SIZE:
// Check to see if we are losing our window...
if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam ) g_bActive = FALSE; else g_bActive = TRUE; if( g_pDisplay ) g_pDisplay->UpdateBounds();
break;
case WM_EXITMENULOOP:
// Ignore time spent in menu
g_dwLastTick = timeGetTime();
break;
case WM_EXITSIZEMOVE:
// Ignore time spent resizing
g_dwLastTick = timeGetTime();
break;
case WM_MOVE:
if( g_pDisplay ) g_pDisplay->UpdateBounds();
break;
case WM_SYSCOMMAND:
// Prevent moving/sizing and power loss in fullscreen mode
switch( wParam ) { case SC_MOVE:
case SC_SIZE:
case SC_MAXIMIZE:
case SC_MONITORPOWER:
return TRUE; }
break;
case WM_DESTROY:
// Cleanup and close the app
FreeDirectDraw();
PostQuitMessage( 0 );
return 0L; } return DefWindowProc(hWnd, msg, wParam, lParam); }
|
ProcessNextFrame()
Desc: Move the sprites, blt them to the back buffer, then
flips the back buffer to the primary buffer
HRESULT ProcessNextFrame() { HRESULT hr;
// Figure how much time has passed since the last time
DWORD dwCurrTick = timeGetTime(); //get current time 获得当前时间 DWORD dwTickDiff = dwCurrTick - g_dwLastTick; //the difference between current-time (dwCurrTick) and last time (g_dwLastTick)
//计算当前时间 (dwCurrTick) 与最后一次时间 (g_dwLastTick) 的差值 // Don't update if no time has passed
if( dwTickDiff == 0 ) //如果时间差值为0,即时间没有变化,则不更新屏幕,而返回 return S_OK;
//如果程序运行到这里,而没有在前面返回,则说明时间有变化,则下面将要更新屏幕
g_dwLastTick = dwCurrTick; //使最后时间=当前时间
// Move the sprites according to how much time has passed //根据时间的变化来移动子画面,即移动sprites
for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ ) UpdateSprite( &g_Sprite[ iSprite ], dwTickDiff / 1000.0f );
// Display the sprites on the screen
if( FAILED( hr = DisplayFrame() ) ) { if( hr != DDERR_SURFACELOST ) return hr;
// The surfaces were lost so restore them
RestoreSurfaces(); }
return S_OK; }
|
UpdateSprite()
Desc: Move the sprite around and make it bounce based on how much time has passed ,此函数属于动画的关键算法,就是控制spirte如何移动
VOID UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta ) {
// Update the sprite position
pSprite->fPosX += pSprite->fVelX * fTimeDelta; pSprite->fPosY += pSprite->fVelY * fTimeDelta;
// Clip the position, and bounce if it hits the edge
if( pSprite->fPosX < 0.0f ) { pSprite->fPosX = 0; pSprite->fVelX = -pSprite->fVelX; } if( pSprite->fPosX >= SCREEN_WIDTH - SPRITE_DIAMETER ) { pSprite->fPosX = SCREEN_WIDTH - 1 - SPRITE_DIAMETER; pSprite->fVelX = -pSprite->fVelX; } if( pSprite->fPosY < 0 ) { pSprite->fPosY = 0; pSprite->fVelY = -pSprite->fVelY; }
if( pSprite->fPosY > SCREEN_HEIGHT - SPRITE_DIAMETER ) { pSprite->fPosY = SCREEN_HEIGHT - 1 - SPRITE_DIAMETER; pSprite->fVelY = -pSprite->fVelY; } }
|
DisplayFrame()
Desc: Blts a the sprites to the back buffer, then flips the back buffer onto the primary buffer.
HRESULT DisplayFrame() { HRESULT hr;
// Fill the back buffer with black, ignoring errors until the flip
g_pDisplay->Clear( 0 ); //清空后备缓冲区表面
// Blt the help text on the backbuffer, ignoring errors until the flip //将g_pBackSurface所指向的图片拷贝到后备缓冲区表面
g_pDisplay->Blt( 0, 0, g_pBackSurface, NULL );
//将g_pTextSurface所指向的文本拷贝到后备缓冲区表面
g_pDisplay->Blt( 10, 10, g_pTextSurface, NULL );
// Blt all the sprites onto the back buffer using color keying, // ignoring errors until the flip. Note that all of these sprites // use the same DirectDraw surface. //将所有的spirits拷贝到后备缓冲区表面,这里要注意拷贝顺序,后拷贝上的画面会压盖在先拷贝的画面上
for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ ) { g_pDisplay->Blt( (DWORD)g_Sprite[iSprite].fPosX,(DWORD)g_Sprite[iSprite].fPosY,g_pLogoSurface, NULL ); }
// We are in fullscreen mode, so perform a flip and return // any errors like DDERR_SURFACELOST //最关键的地方在这里,请看下面的语句,只要我们一执行翻页操作,就可以将改动了的图像了显示在屏幕上了
if( FAILED( hr = g_pDisplay->Present() /*翻页操作*/) ) return hr;
return S_OK; }
|
RestoreSurfaces()
Desc: Restore all the surfaces, and redraw the sprite surfaces.
HRESULT RestoreSurfaces() { HRESULT hr;
LPDIRECTDRAWPALETTE pDDPal = NULL;
if( FAILED( hr = g_pDisplay->GetDirectDraw()->RestoreAllSurfaces() ) ) return hr;
// No need to re-create the surface, just re-draw it.
if( FAILED( hr = g_pTextSurface->DrawText( NULL, HELPTEXT, 0, 0, RGB(0,0,0), RGB(255, 255, 0) ) ) ) return hr;
// We need to release and re-load, and set the palette again to // redraw the bitmap on the surface. Otherwise, GDI will not // draw the bitmap on the surface with the right palette
if( FAILED( hr = g_pDisplay->CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( IDB_DIRECTX ) ) ) ) return hr;
if( FAILED( hr = g_pDisplay->SetPalette( pDDPal ) ) ) return hr;
SAFE_RELEASE( pDDPal );
// No need to re-create the surface, just re-draw it.
if( FAILED( hr = g_pLogoSurface->DrawBitmap( MAKEINTRESOURCE( IDB_DIRECTX ),SPRITE_DIAMETER, SPRITE_DIAMETER ) ) ) return hr; return S_OK; }
|
好了,我们的程序分析就到这了,我想您现在应该大概了解了DirectDraw程序的大概流程了吧,就是先创建DDraw对象,然后进行相关的设置,然后绘制后备缓冲区页,然后执行翻页操作,这样循环,就会产生很好的动画效果了,其实用DirectDraw编程很简单,说白了其实就是:“几个表面之间拷来拷去”。只不过这其间可是大有文章可作的哟!
|