home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectDraw / FullScreenDialog / fullscreendialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  18.0 KB  |  556 lines

  1. //-----------------------------------------------------------------------------
  2. // File: FullScreenDialog.cpp
  3. //
  4. // Desc: This sample demonstrates how to bring up a dialog box, or any other 
  5. //       type of window while running in DirectDraw full-screen exclusive mode.
  6. //
  7. // Copyright (c) 1999-2001 Microsoft Corporation. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define STRICT
  10. #include <windows.h>
  11. #include <basetsd.h>
  12. #include <ddraw.h>
  13. #include <mmsystem.h>
  14. #include "resource.h"
  15. #include "ddutil.h"
  16.  
  17.  
  18.  
  19.  
  20. //-----------------------------------------------------------------------------
  21. // Defines, constants, and global variables
  22. //-----------------------------------------------------------------------------
  23. #define SAFE_DELETE(p)  { if(p) { delete (p);     (p)=NULL; } }
  24. #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
  25.  
  26. #define SCREEN_WIDTH    640
  27. #define SCREEN_HEIGHT   480
  28. #define SCREEN_BPP      16
  29.  
  30. #define SPRITE_DIAMETER 48
  31. #define NUM_SPRITES     25
  32.  
  33. #define HELPTEXT TEXT("Press Escape to quit.  Press F1 to bring up the dialog.")
  34.  
  35. struct SPRITE_STRUCT
  36. {
  37.     FLOAT                fPosX; 
  38.     FLOAT                fPosY;
  39.     FLOAT                fVelX; 
  40.     FLOAT                fVelY;
  41. };
  42.  
  43. CDisplay*            g_pDisplay        = NULL;
  44. CSurface*            g_pLogoSurface    = NULL;  
  45. CSurface*            g_pTextSurface    = NULL;  
  46. BOOL                 g_bActive     = FALSE; 
  47. DWORD                g_dwLastTick;
  48. SPRITE_STRUCT        g_Sprite[NUM_SPRITES]; 
  49. HWND                 g_hWndDlg     = NULL;
  50.  
  51.  
  52.  
  53.  
  54. //-----------------------------------------------------------------------------
  55. // Function-prototypes
  56. //-----------------------------------------------------------------------------
  57. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
  58. INT_PTR CALLBACK SampleDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  59.  
  60. HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd, HACCEL* phAccel );
  61. HRESULT InitDirectDraw( HWND hWnd );
  62. VOID    FreeDirectDraw();
  63. HRESULT ProcessNextFrame();
  64. VOID    UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta );
  65. HRESULT DisplayFrame();
  66. HRESULT RestoreSurfaces();
  67.  
  68.  
  69.  
  70.  
  71. //-----------------------------------------------------------------------------
  72. // Name: WinMain()
  73. // Desc: Entry point to the program. Initializes everything and calls
  74. //       UpdateFrame() when idle from the message pump.
  75. //-----------------------------------------------------------------------------
  76. int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow )
  77. {
  78.     MSG       msg;
  79.     HWND   hWnd;
  80.     HACCEL hAccel;
  81.  
  82.     ZeroMemory( &g_Sprite, sizeof(SPRITE_STRUCT) * NUM_SPRITES );
  83.     srand( GetTickCount() );
  84.  
  85.     if( FAILED( WinInit( hInst, nCmdShow, &hWnd, &hAccel ) ) )
  86.         return FALSE;
  87.  
  88.     if( FAILED( InitDirectDraw( hWnd ) ) )
  89.     {
  90.         MessageBox( hWnd, TEXT("DirectDraw init failed. ")
  91.                     TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"), 
  92.                     MB_ICONERROR | MB_OK );
  93.         return FALSE;
  94.     }
  95.  
  96.     // Show the dialog immediately
  97.     g_hWndDlg = CreateDialog( hInst, MAKEINTRESOURCE(IDD_DIALOG_SAMPLE),
  98.                               hWnd, (DLGPROC) SampleDlgProc );
  99.     ShowWindow( g_hWndDlg, SW_SHOWNORMAL );
  100.  
  101.     g_dwLastTick = timeGetTime();
  102.  
  103.     while( TRUE )
  104.     {
  105.         // Look for messages, if none are found then 
  106.         // update the state and display it
  107.         if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
  108.         {
  109.             if( 0 == GetMessage(&msg, NULL, 0, 0 ) )
  110.             {
  111.                 // WM_QUIT was posted, so exit
  112.                 return (int)msg.wParam;
  113.             }
  114.  
  115.             // Translate and dispatch the message. If the dialog is showing, 
  116.             // translate messages for it since it's a modeless dialog.
  117.             if( g_hWndDlg == NULL || !IsDialogMessage( g_hWndDlg, &msg ) )
  118.             {
  119.                 if( 0 == TranslateAccelerator( hWnd, hAccel, &msg ) )
  120.                 {
  121.                     TranslateMessage( &msg ); 
  122.                     DispatchMessage( &msg );
  123.                 }
  124.             }
  125.         }
  126.         else
  127.         {
  128.             if( g_bActive )
  129.             {
  130.                 // Move the sprites, blt them to the back buffer, then 
  131.                 // flip or blt the back buffer to the primary buffer
  132.                 if( FAILED( ProcessNextFrame() ) )
  133.                 {
  134.                     SAFE_DELETE( g_pDisplay );
  135.  
  136.                     MessageBox( hWnd, TEXT("Displaying the next frame failed. ")
  137.                                 TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"), 
  138.                                 MB_ICONERROR | MB_OK );
  139.                     return FALSE;
  140.                 }
  141.             }
  142.             else
  143.             {
  144.                 // Make sure we go to sleep if we have nothing else to do
  145.                 WaitMessage();
  146.  
  147.                 // Ignore time spent inactive 
  148.                 g_dwLastTick = timeGetTime();
  149.             }
  150.         }
  151.     }
  152. }
  153.  
  154.  
  155.  
  156.  
  157. //-----------------------------------------------------------------------------
  158. // Name: WinInit()
  159. // Desc: Init the window
  160. //-----------------------------------------------------------------------------
  161. HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd, HACCEL* phAccel )
  162. {
  163.     WNDCLASS wc;
  164.     HWND     hWnd;
  165.     HACCEL   hAccel;
  166.  
  167.     // Register the Window Class
  168.     wc.lpszClassName = TEXT("FullScreenDialog");
  169.     wc.lpfnWndProc   = MainWndProc;
  170.     wc.style         = CS_VREDRAW | CS_HREDRAW;
  171.     wc.hInstance     = hInst;
  172.     wc.hIcon         = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN) );
  173.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  174.     wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  175.     wc.lpszMenuName  = NULL;
  176.     wc.cbClsExtra    = 0;
  177.     wc.cbWndExtra    = 0;
  178.  
  179.     if( RegisterClass( &wc ) == 0 )
  180.         return E_FAIL;
  181.  
  182.     // Load keyboard accelerators
  183.     hAccel = LoadAccelerators( hInst, MAKEINTRESOURCE(IDR_MAIN_ACCEL) );
  184.  
  185.     // Create and show the main window
  186.     hWnd = CreateWindowEx( 0, TEXT("FullScreenDialog"), TEXT("DirectDraw FullScreenDialog Sample"),
  187.                            WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
  188.                              CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL );
  189.     if( hWnd == NULL )
  190.         return E_FAIL;
  191.  
  192.     ShowWindow( hWnd, nCmdShow );
  193.     UpdateWindow( hWnd );
  194.  
  195.     *phWnd   = hWnd;
  196.     *phAccel = hAccel;
  197.  
  198.     return S_OK;
  199. }
  200.  
  201.  
  202.  
  203.  
  204. //-----------------------------------------------------------------------------
  205. // Name: InitDirectDraw()
  206. // Desc: Create the DirectDraw object, and init the surfaces
  207. //-----------------------------------------------------------------------------
  208. HRESULT InitDirectDraw( HWND hWnd )
  209. {
  210.     HRESULT             hr;
  211.     int                 iSprite;
  212.  
  213.     g_pDisplay = new CDisplay();
  214.     if( FAILED( hr = g_pDisplay->CreateFullScreenDisplay( hWnd, SCREEN_WIDTH, 
  215.                                                           SCREEN_HEIGHT, SCREEN_BPP ) ) )
  216.     {
  217.         MessageBox( hWnd, TEXT("This display card does not support 640x480x8. "),
  218.                     TEXT("DirectDraw Sample"), MB_ICONERROR | MB_OK );
  219.         return hr;
  220.     }
  221.  
  222.     // Check if the device supports DDCAPS2_CANRENDERWINDOWED.  
  223.     // If it does, then it supports GDI writing directly to the primary surface
  224.     // Otherwise, to do GDI displaying on these cards, you you must create a 
  225.     // bitmap of the GDI window and BitBlt the bitmap to the backbuffer 
  226.     // then flip as normal. However, this sample does not show this. 
  227.     DDCAPS ddcaps;
  228.     ZeroMemory( &ddcaps, sizeof(ddcaps) );
  229.     ddcaps.dwSize = sizeof(ddcaps);
  230.     g_pDisplay->GetDirectDraw()->GetCaps( &ddcaps, NULL );
  231.     if( (ddcaps.dwCaps2 & DDCAPS2_CANRENDERWINDOWED) == 0 )
  232.     {
  233.         MessageBox( hWnd, TEXT("This display card can not render GDI."),
  234.                     TEXT("DirectDraw Sample"), MB_ICONERROR | MB_OK );
  235.         return E_FAIL;
  236.     }
  237.  
  238.     // Create a clipper so DirectDraw will not blt over the GDI dialog
  239.     if( FAILED( hr = g_pDisplay->InitClipper() ) )
  240.         return hr;
  241.  
  242.     // Create a surface, and draw text to it.  
  243.     if( FAILED( hr = g_pDisplay->CreateSurfaceFromText( &g_pTextSurface, NULL, HELPTEXT, 
  244.                                                         RGB(0,0,0), RGB(255, 255, 0) ) ) )
  245.         return hr;
  246.  
  247.     // Create a surface, and draw a bitmap resource on it.  
  248.     if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pLogoSurface, MAKEINTRESOURCE( IDB_DIRECTX ), 
  249.                                                           SPRITE_DIAMETER, SPRITE_DIAMETER ) ) )
  250.         return hr;
  251.  
  252.     // Set the color key for the logo sprite to black
  253.     if( FAILED( hr = g_pLogoSurface->SetColorKey( 0 ) ) )
  254.         return hr;
  255.  
  256.     // Init all the sprites.  All of these sprites look the same, 
  257.     // using the g_pDDSLogo surface. 
  258.     for( iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
  259.     {
  260.         // Set the sprite's position and velocity
  261.         g_Sprite[iSprite].fPosX = (float) (rand() % SCREEN_WIDTH);
  262.         g_Sprite[iSprite].fPosY = (float) (rand() % SCREEN_HEIGHT); 
  263.  
  264.         g_Sprite[iSprite].fVelX = 500.0f * rand() / RAND_MAX - 250.0f;
  265.         g_Sprite[iSprite].fVelY = 500.0f * rand() / RAND_MAX - 250.0f;
  266.     }
  267.  
  268.     return S_OK;
  269. }
  270.  
  271.  
  272.  
  273.  
  274. //-----------------------------------------------------------------------------
  275. // Name: FreeDirectDraw()
  276. // Desc: Release all the DirectDraw objects
  277. //-----------------------------------------------------------------------------
  278. VOID FreeDirectDraw()
  279. {
  280.     SAFE_DELETE( g_pLogoSurface );
  281.     SAFE_DELETE( g_pTextSurface );
  282.     SAFE_DELETE( g_pDisplay );
  283. }
  284.  
  285.  
  286.  
  287.  
  288.  
  289. //-----------------------------------------------------------------------------
  290. // Name: MainWndProc()
  291. // Desc: The main window procedure
  292. //-----------------------------------------------------------------------------
  293. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  294. {
  295.     switch (msg)
  296.     {
  297.         case WM_COMMAND:
  298.             switch( LOWORD(wParam) )
  299.             {
  300.                 case IDM_EXIT:
  301.                     // Received key/menu command to exit app
  302.                     PostMessage( hWnd, WM_CLOSE, 0, 0 );
  303.                     return 0L;
  304.  
  305.                 case IDM_SHOW_DIALOG:
  306.                     {
  307.                         HINSTANCE hInst;
  308. #ifdef _WIN64
  309.                         hInst = (HINSTANCE) GetWindowLongPtr( hWnd, GWLP_HINSTANCE );
  310. #else
  311.                         hInst = (HINSTANCE) GetWindowLong( hWnd, GWL_HINSTANCE );
  312. #endif
  313.                         g_hWndDlg = CreateDialog( hInst, MAKEINTRESOURCE(IDD_DIALOG_SAMPLE),
  314.                                                  hWnd, (DLGPROC) SampleDlgProc );
  315.                         ShowWindow( g_hWndDlg, SW_SHOWNORMAL );
  316.                     }
  317.                     break;
  318.             }
  319.             break; // Continue with default processing
  320.  
  321.         case WM_SIZE:
  322.             // Check to see if we are losing our window...
  323.             if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam )
  324.                 g_bActive = FALSE;
  325.             else
  326.                 g_bActive = TRUE;
  327.             break;
  328.  
  329.         case WM_EXITMENULOOP:
  330.             // Ignore time spent in menu
  331.             g_dwLastTick = timeGetTime();
  332.             break;
  333.  
  334.         case WM_EXITSIZEMOVE:
  335.             // Ignore time spent resizing
  336.             g_dwLastTick = timeGetTime();
  337.             break;
  338.             
  339.         case WM_SYSCOMMAND:
  340.             // Prevent moving/sizing and power loss in fullscreen mode
  341.             switch( wParam )
  342.             {
  343.                 case SC_MOVE:
  344.                 case SC_SIZE:
  345.                 case SC_MAXIMIZE:
  346.                 case SC_MONITORPOWER:
  347.                     return TRUE;
  348.             }
  349.             break;
  350.             
  351.         case WM_DESTROY:
  352.             // Cleanup and close the app
  353.             FreeDirectDraw();
  354.             PostQuitMessage( 0 );
  355.             return 0L;
  356.     }
  357.  
  358.     return DefWindowProc(hWnd, msg, wParam, lParam);
  359. }
  360.  
  361.  
  362.  
  363.  
  364. //-----------------------------------------------------------------------------
  365. // Name: SampleDlgProc()
  366. // Desc: A simple dialog that has some standard controls, so you can see that
  367. //       they update properly.
  368. //-----------------------------------------------------------------------------
  369. INT_PTR CALLBACK SampleDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  370. {
  371.     static TCHAR* pszCombo[] = {"One","Two","Three","Four","Five","Six"};
  372.  
  373.     switch (msg)
  374.     {
  375.         case WM_INITDIALOG:
  376.             {
  377.                 for( int i = 0; i < 6; i++ )
  378.                     SendDlgItemMessage( hDlg, IDC_COMBO1, CB_ADDSTRING, 
  379.                                         0, (LPARAM) pszCombo[i] );
  380.  
  381.                 SendDlgItemMessage( hDlg, IDC_COMBO1, CB_SETCURSEL, 0, 0 );
  382.                 CheckDlgButton( hDlg, IDC_RADIO1, BST_CHECKED );
  383.             }
  384.             return TRUE;
  385.  
  386.         case WM_COMMAND:
  387.             switch( LOWORD(wParam) )
  388.             {
  389.                 case IDCANCEL:
  390.                 case IDOK:
  391.                     EndDialog( hDlg, TRUE );
  392.                     return TRUE;
  393.             }
  394.             break;
  395.  
  396.         case WM_MOVE:
  397.             // The window is moving around, so re-draw the backbuffer
  398.             DisplayFrame();
  399.             break;
  400.  
  401.         case WM_DESTROY:
  402.             g_hWndDlg = NULL;
  403.             break;
  404.     }
  405.  
  406.     return FALSE;
  407. }
  408.  
  409.  
  410.  
  411.  
  412. //-----------------------------------------------------------------------------
  413. // Name: ProcessNextFrame()
  414. // Desc: Move the sprites, blt them to the back buffer, then 
  415. //       flips the back buffer to the primary buffer
  416. //-----------------------------------------------------------------------------
  417. HRESULT ProcessNextFrame()
  418. {
  419.     HRESULT hr;
  420.  
  421.     // Figure how much time has passed since the last time
  422.     DWORD dwCurrTick = timeGetTime();
  423.     DWORD dwTickDiff = dwCurrTick - g_dwLastTick;
  424.  
  425.     // Don't update if no time has passed 
  426.     if( dwTickDiff == 0 )
  427.         return S_OK; 
  428.  
  429.     g_dwLastTick = dwCurrTick;
  430.  
  431.     // Move the sprites according to how much time has passed
  432.     for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
  433.         UpdateSprite( &g_Sprite[ iSprite ], dwTickDiff / 1000.0f );
  434.  
  435.     // Display the sprites on the screen
  436.     if( FAILED( hr = DisplayFrame() ) )
  437.     {
  438.         if( hr != DDERR_SURFACELOST )
  439.             return hr;
  440.  
  441.         // The surfaces were lost so restore them 
  442.         RestoreSurfaces();
  443.     }
  444.  
  445.     return S_OK;
  446. }
  447.  
  448.  
  449.  
  450.  
  451. //-----------------------------------------------------------------------------
  452. // Name: UpdateSprite()
  453. // Desc: Move the sprite around and make it bounce based on how much time 
  454. //       has passed
  455. //-----------------------------------------------------------------------------
  456. VOID UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta )
  457. {    
  458.     // Update the sprite position
  459.     pSprite->fPosX += pSprite->fVelX * fTimeDelta;
  460.     pSprite->fPosY += pSprite->fVelY * fTimeDelta;
  461.  
  462.     // Clip the position, and bounce if it hits the edge
  463.     if( pSprite->fPosX < 0.0f )
  464.     {
  465.         pSprite->fPosX  = 0;
  466.         pSprite->fVelX = -pSprite->fVelX;
  467.     }
  468.  
  469.     if( pSprite->fPosX >= SCREEN_WIDTH - SPRITE_DIAMETER )
  470.     {
  471.         pSprite->fPosX = SCREEN_WIDTH - 1 - SPRITE_DIAMETER;
  472.         pSprite->fVelX = -pSprite->fVelX;
  473.     }
  474.  
  475.     if( pSprite->fPosY < 0 )
  476.     {
  477.         pSprite->fPosY = 0;
  478.         pSprite->fVelY = -pSprite->fVelY;
  479.     }
  480.  
  481.     if( pSprite->fPosY > SCREEN_HEIGHT - SPRITE_DIAMETER )
  482.     {
  483.         pSprite->fPosY = SCREEN_HEIGHT - 1 - SPRITE_DIAMETER;
  484.         pSprite->fVelY = -pSprite->fVelY;
  485.     }   
  486. }
  487.  
  488.  
  489.  
  490.  
  491. //-----------------------------------------------------------------------------
  492. // Name: DisplayFrame()
  493. // Desc: Blts a the sprites to the back buffer, then flips the 
  494. //       back buffer onto the primary buffer.
  495. //-----------------------------------------------------------------------------
  496. HRESULT DisplayFrame()
  497. {
  498.     HRESULT hr;
  499.  
  500.     // Fill the back buffer with black, ignoring errors until the flip
  501.     g_pDisplay->Clear( 0 );
  502.  
  503.     // Blt the help text on the backbuffer, ignoring errors until the flip
  504.     g_pDisplay->Blt( 10, 10, g_pTextSurface, NULL );
  505.  
  506.     // Blt all the sprites onto the back buffer using color keying,
  507.     // ignoring errors until the last call. Note that all of these sprites 
  508.     // use the same DirectDraw surface.
  509.     for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
  510.     {
  511.         g_pDisplay->Blt( (DWORD)g_Sprite[iSprite].fPosX, 
  512.                          (DWORD)g_Sprite[iSprite].fPosY, 
  513.                          g_pLogoSurface, NULL );
  514.     }
  515.  
  516.     // Updating the primary buffer with a blt
  517.     LPDIRECTDRAWSURFACE7 pddsFrontBuffer = g_pDisplay->GetFrontBuffer();
  518.     LPDIRECTDRAWSURFACE7 pddsBackBuffer = g_pDisplay->GetBackBuffer();
  519.  
  520.     if( FAILED( hr = pddsFrontBuffer->Blt( NULL, pddsBackBuffer, NULL, 
  521.                                            DDBLT_WAIT, NULL ) ) )
  522.         return hr;
  523.  
  524.     return S_OK;
  525. }
  526.  
  527.  
  528.  
  529.  
  530. //-----------------------------------------------------------------------------
  531. // Name: RestoreSurfaces()
  532. // Desc: Restore all the surfaces, and redraw the sprite surfaces.
  533. //-----------------------------------------------------------------------------
  534. HRESULT RestoreSurfaces()
  535. {
  536.     HRESULT hr;
  537.  
  538.     if( FAILED( hr = g_pDisplay->GetDirectDraw()->RestoreAllSurfaces() ) )
  539.         return hr;
  540.  
  541.     // No need to re-create the surface, just re-draw it.
  542.     if( FAILED( hr = g_pTextSurface->DrawText( NULL, HELPTEXT, 
  543.                                                0, 0, RGB(0,0,0), RGB(255, 255, 0) ) ) )
  544.         return hr;
  545.  
  546.     // No need to re-create the surface, just re-draw it.
  547.     if( FAILED( hr = g_pLogoSurface->DrawBitmap( MAKEINTRESOURCE( IDB_DIRECTX ),
  548.                                                  SPRITE_DIAMETER, SPRITE_DIAMETER ) ) )
  549.         return hr;
  550.  
  551.     return S_OK;
  552. }
  553.  
  554.  
  555.  
  556.