作者:不詳
#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.") //定義文本住表面 |
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]; /*定義多個子畫面,其中包括多個 運動時的屬性*/ |
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(); |
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();//獲得當前系統時間
} } } } |
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; } |
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; } |
VOID FreeDirectDraw()
{ SAFE_DELETE( g_pBackSurface ); SAFE_DELETE( g_pLogoSurface ); SAFE_DELETE( g_pTextSurface ); SAFE_DELETE( g_pDisplay ); } |
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); } |
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; } |
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; } } |
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; } |
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; } |