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

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