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

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