属于Win32程序的基本框架的我们用蓝色标出,而用红色表出的是我们要重点学习的。下面的所有程序都是 FullScreenMode.cpp 文件中的内容,其中“IDB_DIRECTX”和“IDB_WinXP”都是图片资源的ID号,我想如何向程序中添加资源应该不用我多说了吧:) 工程文件:FullScreenMode.cpp #define STRICT #include #include #include #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->S
 【责编:admin】
|