home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / DDEX1 / DDEX1.CPP next >
C/C++ Source or Header  |  1996-08-28  |  9KB  |  296 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:       ddex1.cpp
  6.  *  Content:    Direct Draw example program 1.  Creates a Direct Draw 
  7.  *              object and then a primary surface with a back buffer.
  8.  *              Slowly flips between the primary surface and the back
  9.  *              buffer.  Press F12 to terminate the program.
  10.  *
  11.  ***************************************************************************/
  12.  
  13. #define NAME "DDExample1"
  14. #define TITLE "Direct Draw Example 1"
  15.  
  16. #define WIN32_LEAN_AND_MEAN
  17. #include <windows.h>
  18. #include <windowsx.h>
  19. #include <ddraw.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include "resource.h"
  23.  
  24. #define TIMER_ID        1
  25. #define TIMER_RATE      500
  26.  
  27. LPDIRECTDRAW            lpDD;           // DirectDraw object
  28. LPDIRECTDRAWSURFACE     lpDDSPrimary;   // DirectDraw primary surface
  29. LPDIRECTDRAWSURFACE     lpDDSBack;      // DirectDraw back surface
  30. BOOL                    bActive;        // is application active?
  31.  
  32. /*
  33.  * finiObjects
  34.  *
  35.  * finished with all objects we use; release them
  36.  */
  37. static void finiObjects( void )
  38. {
  39.     if( lpDD != NULL )
  40.     {
  41.         if( lpDDSPrimary != NULL )
  42.         {
  43.             lpDDSPrimary->Release();
  44.             lpDDSPrimary = NULL;
  45.         }
  46.         lpDD->Release();
  47.         lpDD = NULL;
  48.     }
  49. } /* finiObjects */
  50.  
  51. char szMsg[] = "Page Flipping Test: Press F12 to exit";
  52. char szFrontMsg[] = "Front buffer (F12 to quit)";
  53. char szBackMsg[] = "Back buffer (F12 to quit)";
  54.  
  55. long FAR PASCAL WindowProc( HWND hWnd, UINT message, 
  56.                             WPARAM wParam, LPARAM lParam )
  57. {
  58.     PAINTSTRUCT ps;
  59.     RECT        rc;
  60.     SIZE        size;
  61.     static BYTE phase = 0;
  62.     HDC         hdc;
  63.  
  64.     switch( message )
  65.     {
  66.     case WM_ACTIVATEAPP:
  67.         bActive = wParam;
  68.         break;
  69.  
  70.     case WM_CREATE:
  71.         break;
  72.  
  73.     case WM_SETCURSOR:
  74.         SetCursor(NULL);
  75.         return TRUE;
  76.  
  77.     case WM_TIMER:
  78.         // Flip surfaces
  79.         if( bActive )
  80.         {
  81.             if (lpDDSBack->GetDC(&hdc) == DD_OK)
  82.             {
  83.                 SetBkColor( hdc, RGB( 0, 0, 255 ) );
  84.                 SetTextColor( hdc, RGB( 255, 255, 0 ) );
  85.                 if( phase )
  86.                 {
  87.                     TextOut( hdc, 0, 0, szFrontMsg, lstrlen(szBackMsg) );
  88.                     phase = 0;
  89.                 }
  90.                 else
  91.                 {
  92.                     TextOut( hdc, 0, 0, szBackMsg, lstrlen(szBackMsg) );
  93.                     phase = 1;
  94.                 }
  95.                 lpDDSBack->ReleaseDC(hdc);
  96.             }
  97.  
  98.             while( 1 )
  99.             {
  100.                 HRESULT ddrval;
  101.                 ddrval = lpDDSPrimary->Flip( NULL, 0 );
  102.                 if( ddrval == DD_OK )
  103.                 {
  104.                     break;
  105.                 }
  106.                 if( ddrval == DDERR_SURFACELOST )
  107.                 {
  108.                     ddrval = lpDDSPrimary->Restore();
  109.                     if( ddrval != DD_OK )
  110.                     {
  111.                         break;
  112.                     }
  113.                 }
  114.                 if( ddrval != DDERR_WASSTILLDRAWING )
  115.                 {
  116.                     break;
  117.                 }
  118.             }
  119.         }
  120.         break;
  121.  
  122.     case WM_KEYDOWN:
  123.         switch( wParam )
  124.         {
  125.         case VK_ESCAPE:
  126.         case VK_F12:
  127.             PostMessage(hWnd, WM_CLOSE, 0, 0);
  128.             break;
  129.         }
  130.         break;
  131.  
  132.     case WM_PAINT:
  133.         BeginPaint( hWnd, &ps );
  134.         GetClientRect(hWnd, &rc);
  135.         GetTextExtentPoint( ps.hdc, szMsg, lstrlen(szMsg), &size );
  136.         SetBkColor( ps.hdc, RGB( 0, 0, 255 ) );
  137.         SetTextColor( ps.hdc, RGB( 255, 255, 0 ) );
  138.         TextOut( ps.hdc, (rc.right - size.cx)/2, (rc.bottom - size.cy)/2,
  139.             szMsg, sizeof( szMsg )-1 );
  140.         EndPaint( hWnd, &ps );
  141.         break;
  142.  
  143.     case WM_DESTROY:
  144.         finiObjects();
  145.         PostQuitMessage( 0 );
  146.         break;
  147.     }
  148.  
  149.     return DefWindowProc(hWnd, message, wParam, lParam);
  150.  
  151. } /* WindowProc */
  152.  
  153. /*
  154.  * doInit - do work required for every instance of the application:
  155.  *                create the window, initialize data
  156.  */
  157. static BOOL doInit( HINSTANCE hInstance, int nCmdShow )
  158. {
  159.     HWND                hwnd;
  160.     WNDCLASS            wc;
  161.     DDSURFACEDESC       ddsd;
  162.     DDSCAPS             ddscaps;
  163.     HRESULT             ddrval;
  164.     HDC                 hdc;
  165.     char                buf[256];
  166.  
  167.     /*
  168.      * set up and register window class
  169.      */
  170.     wc.style = CS_HREDRAW | CS_VREDRAW;
  171.     wc.lpfnWndProc = WindowProc;
  172.     wc.cbClsExtra = 0;
  173.     wc.cbWndExtra = 0;
  174.     wc.hInstance = hInstance;
  175.     wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
  176.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  177.     wc.hbrBackground = NULL;
  178.     wc.lpszMenuName = NAME;
  179.     wc.lpszClassName = NAME;
  180.     RegisterClass( &wc );
  181.     
  182.     /*
  183.      * create a window
  184.      */
  185.     hwnd = CreateWindowEx(
  186.         WS_EX_TOPMOST,
  187.         NAME,
  188.         TITLE,
  189.         WS_POPUP,
  190.         0, 0,
  191.         GetSystemMetrics( SM_CXSCREEN ),
  192.         GetSystemMetrics( SM_CYSCREEN ),
  193.         NULL,
  194.         NULL,
  195.         hInstance,
  196.         NULL );
  197.  
  198.     if( !hwnd )
  199.     {
  200.         return FALSE;
  201.     }
  202.  
  203.     ShowWindow( hwnd, nCmdShow );
  204.     UpdateWindow( hwnd );
  205.  
  206.     /*
  207.      * create the main DirectDraw object
  208.      */
  209.     ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
  210.     if( ddrval == DD_OK )
  211.     {
  212.         // Get exclusive mode
  213.         ddrval = lpDD->SetCooperativeLevel( hwnd,
  214.                                 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
  215.         if(ddrval == DD_OK )
  216.         {
  217.             ddrval = lpDD->SetDisplayMode( 640, 480, 8 );
  218.             if( ddrval == DD_OK )
  219.             {
  220.                 // Create the primary surface with 1 back buffer
  221.                 ddsd.dwSize = sizeof( ddsd );
  222.                 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  223.                 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
  224.                                       DDSCAPS_FLIP | 
  225.                                       DDSCAPS_COMPLEX;
  226.                 ddsd.dwBackBufferCount = 1;
  227.                 ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
  228.                 if( ddrval == DD_OK )
  229.                 {
  230.                     // Get a pointer to the back buffer
  231.                     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
  232.                     ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps, 
  233.                                                           &lpDDSBack);
  234.                     if( ddrval == DD_OK )
  235.                     {
  236.                         // draw some text.
  237.                         if (lpDDSPrimary->GetDC(&hdc) == DD_OK)
  238.                         {
  239.                             SetBkColor( hdc, RGB( 0, 0, 255 ) );
  240.                             SetTextColor( hdc, RGB( 255, 255, 0 ) );
  241.                             TextOut( hdc, 0, 0, szFrontMsg, lstrlen(szFrontMsg) );
  242.                             lpDDSPrimary->ReleaseDC(hdc);
  243.                         }
  244.  
  245.                         if (lpDDSBack->GetDC(&hdc) == DD_OK)
  246.                         {
  247.                             SetBkColor( hdc, RGB( 0, 0, 255 ) );
  248.                             SetTextColor( hdc, RGB( 255, 255, 0 ) );
  249.                             TextOut( hdc, 0, 0, szBackMsg, lstrlen(szBackMsg) );
  250.                             lpDDSBack->ReleaseDC(hdc);
  251.                         }
  252.  
  253.                         // Create a timer to flip the pages
  254.                         if( SetTimer( hwnd, TIMER_ID, TIMER_RATE, NULL ) )
  255.                         {
  256.                              return TRUE;
  257.                         }
  258.                     }
  259.                 }
  260.             }
  261.         }
  262.     }
  263.  
  264.     wsprintf(buf, "Direct Draw Init Failed (%08lx)\n", ddrval );
  265.     MessageBox( hwnd, buf, "ERROR", MB_OK );
  266.     finiObjects();
  267.     DestroyWindow( hwnd );
  268.     return FALSE;
  269. } /* doInit */
  270.  
  271. /*
  272.  * WinMain - initialization, message loop
  273.  */
  274. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  275.                         LPSTR lpCmdLine, int nCmdShow)
  276. {
  277.     MSG         msg;
  278.  
  279.     lpCmdLine = lpCmdLine;
  280.     hPrevInstance = hPrevInstance;
  281.  
  282.     if( !doInit( hInstance, nCmdShow ) )
  283.     {
  284.         return FALSE;
  285.     }
  286.  
  287.     while (GetMessage(&msg, NULL, 0, 0))
  288.     {
  289.         TranslateMessage(&msg);
  290.         DispatchMessage(&msg);
  291.     }
  292.  
  293.     return msg.wParam;
  294.  
  295. } /* WinMain */
  296.