home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / directx2 / sdk / samples / palette / palette.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-28  |  11.3 KB  |  461 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:       palette.cpp
  6.  *  Content:    Does some basic palette manipulation
  7.  *
  8.  ***************************************************************************/
  9. #define WIN32_LEAN_AND_MEAN
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <ddraw.h>
  13. #include "resource.h"
  14. #include "lbprintf.h"
  15.  
  16. static char     szClass[32]="DDTest1Class";
  17. static char     szCaption[32]="DirectDraw Palette Test";
  18.  
  19. /*
  20.  * DirectDraw objects we will use
  21.  */
  22. #define MAX_PALETTES    4
  23. LPDIRECTDRAW            lpDD;
  24. LPDIRECTDRAWSURFACE     lpDDPSurf;
  25. LPDIRECTDRAWPALETTE     lpDDPal[MAX_PALETTES];
  26. LPDIRECTDRAWPALETTE     lpDDPalCurr;
  27.  
  28. HWND                    hWndMain;
  29.  
  30. BOOL                    bIsExclusive = TRUE;
  31. BOOL                    bActive;
  32.  
  33.  
  34. /*
  35.  * doAttach
  36.  *
  37.  * attach palette to the primary surface
  38.  */
  39. static void doAttach( int index )
  40. {
  41.     HRESULT     ddrval;
  42.  
  43.     ddrval = lpDDPSurf->SetPalette( lpDDPal[index] );
  44.     if( ddrval == DDERR_SURFACELOST )
  45.     {
  46.         lpDDPSurf->Restore();
  47.         ddrval = lpDDPSurf->SetPalette( lpDDPal[index] );
  48.     }
  49.     LBPrintfDDRC( ddrval, "doAttach1: SetPalette" );
  50.     lpDDPalCurr = lpDDPal[index];
  51.  
  52. } /* doAttach */
  53.  
  54. /*
  55.  * doDetach
  56.  *
  57.  * detach the current palette from the primary surface
  58.  */
  59. static void doDetach( void )
  60. {
  61.     HRESULT     ddrval;
  62.  
  63.     ddrval = lpDDPSurf->SetPalette( NULL );
  64.     if( ddrval == DDERR_SURFACELOST )
  65.     {
  66.         lpDDPSurf->Restore();
  67.         ddrval = lpDDPSurf->SetPalette( NULL );
  68.     }
  69.     LBPrintfDDRC( ddrval, "doDetach: SetPalette" );
  70.     lpDDPalCurr = NULL;
  71.  
  72. } /* doDetach */
  73.  
  74. /*
  75.  * doCycle
  76.  *
  77.  * cycle the current palette's red entries by 10
  78.  */
  79. static void doCycle( void )
  80. {
  81.     if( lpDDPalCurr != NULL )
  82.     {
  83.         #define ENTRIES 10
  84.         #define BASE 0
  85.         PALETTEENTRY    pe[ENTRIES];
  86.         HRESULT         ddrval;
  87.         int             i;
  88.  
  89.         /*
  90.          * display the original entries
  91.          */
  92.         ddrval = lpDDPalCurr->GetEntries( 0, BASE, ENTRIES, pe );
  93.         if( ddrval == DDERR_SURFACELOST )
  94.         {
  95.             lpDDPSurf->Restore();
  96.             ddrval = lpDDPalCurr->GetEntries( 0, BASE, ENTRIES, pe );
  97.         }
  98.         LBPrintfDDRC( ddrval, "GetEntries" );
  99.         LBPrintf( "Old Entries" );
  100.         for( i=0;i<ENTRIES;i++ )
  101.         {
  102.             LBPrintf( "    pe[%d] (r,g,b) = (%d,%d,%d)", i, pe[i].peRed,
  103.                         pe[i].peGreen, pe[i].peBlue );
  104.         }
  105.  
  106.         /*
  107.          * cycle the red values
  108.          */
  109.         for( i=0;i<ENTRIES;i++ )
  110.         {
  111.             pe[i].peRed = (BYTE) (((int)pe[i].peRed + 50) % 256);
  112.         }
  113.         ddrval = lpDDPalCurr->SetEntries( 0, BASE, ENTRIES, pe );
  114.         if( ddrval == DDERR_SURFACELOST )
  115.         {
  116.             lpDDPSurf->Restore();
  117.             ddrval = lpDDPalCurr->SetEntries( 0, BASE, ENTRIES, pe );
  118.         }
  119.         LBPrintfDDRC( ddrval, "SetEntries" );
  120.  
  121.         /*
  122.          * display the new values
  123.          */
  124.         ddrval = lpDDPalCurr->GetEntries( 0, BASE, ENTRIES, pe );
  125.         if( ddrval == DDERR_SURFACELOST )
  126.         {
  127.             lpDDPSurf->Restore();
  128.             ddrval = lpDDPalCurr->GetEntries( 0, BASE, ENTRIES, pe );
  129.         }
  130.         LBPrintfDDRC( ddrval, "GetEntries" );
  131.         LBPrintf( "New Entries" );
  132.         for( i=0;i<ENTRIES;i++ )
  133.         {
  134.             LBPrintf( "    pe[%d] (r,g,b) = (%d,%d,%d)", i, pe[i].peRed,
  135.                         pe[i].peGreen, pe[i].peBlue );
  136.         }
  137.     }
  138.  
  139. } /* doCycle */
  140.  
  141. /*
  142.  * resetExclusiveAndPalettes
  143.  *
  144.  * Sets exclusive mode to the desired value, and the rebuilds
  145.  * the palettes.
  146.  *
  147.  * Palettes in exclusive mode bypass GDI and go directly to the display
  148.  * driver, so the performance is much higher.
  149.  */
  150. static BOOL resetExclusiveAndPalettes( BOOL excl )
  151. {
  152.     LPPALETTEENTRY      ppe;
  153.     int                 i;
  154.     HRESULT             ddrval;
  155.  
  156.     /*
  157.      * create palette entries
  158.      */
  159.     ppe = (LPPALETTEENTRY) LocalAlloc( LPTR, sizeof( PALETTEENTRY ) * 256 );
  160.     if( ppe == NULL )
  161.     {
  162.         return FALSE;
  163.     }
  164.  
  165.     /*
  166.      * release existing palettes
  167.      */
  168.     for( i=0;i<MAX_PALETTES;i++ )
  169.     {
  170.         if( lpDDPal[i] != NULL )
  171.         {
  172.             ddrval = lpDDPal[i]->Release();
  173.             LBPrintfDDRC( ddrval, "Release" );
  174.             lpDDPal[i] = NULL;
  175.         }
  176.     }
  177.  
  178.     if( excl )
  179.     {
  180.         ddrval = lpDD->SetCooperativeLevel( hWndMain, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
  181.     }
  182.     else
  183.     {
  184.         ddrval = lpDD->SetCooperativeLevel( hWndMain, DDSCL_NORMAL );
  185.     }
  186.         
  187.     LBPrintfDDRC( ddrval, "SetCooperativeLevel" );
  188.  
  189.     /*
  190.      * set up 4 palettes
  191.      */
  192.     ppe[0].peRed = 0;
  193.     ppe[0].peGreen = 0;
  194.     ppe[0].peBlue = 0;
  195.     ppe[255].peRed = 255;
  196.     ppe[255].peGreen = 255;
  197.     ppe[255].peBlue = 255;
  198.  
  199.     for( i=1;i<255;i++ )
  200.     {
  201.         ppe[i].peRed = 0;
  202.         ppe[i].peGreen = 0;
  203.         ppe[i].peBlue = 0;
  204.     }
  205.     ddrval = lpDD->CreatePalette( DDPCAPS_8BIT, ppe, &lpDDPal[0], NULL );
  206.     LBPrintfDDRC( ddrval, "CreatePalette" );
  207.  
  208.     for( i=1;i<255;i++ )
  209.     {
  210.         ppe[i].peRed = 0;
  211.         ppe[i].peGreen = 255;
  212.         ppe[i].peBlue = 255;
  213.     }
  214.     ddrval = lpDD->CreatePalette( DDPCAPS_8BIT, ppe, &lpDDPal[1], NULL );
  215.     LBPrintfDDRC( ddrval, "CreatePalette" );
  216.  
  217.     for( i=1;i<255;i++ )
  218.     {
  219.         ppe[i].peRed = 255;
  220.         ppe[i].peGreen = 255;
  221.         ppe[i].peBlue = 0;
  222.     }
  223.     ddrval = lpDD->CreatePalette( DDPCAPS_8BIT, ppe, &lpDDPal[2], NULL );
  224.     LBPrintfDDRC( ddrval, "CreatePalette" );
  225.  
  226.     for( i=1;i<255;i++ )
  227.     {
  228.         ppe[i].peRed = 255;
  229.         ppe[i].peGreen = 0;
  230.         ppe[i].peBlue = 255;
  231.     }
  232.     ddrval = lpDD->CreatePalette( DDPCAPS_8BIT, ppe, &lpDDPal[3], NULL );
  233.     LBPrintfDDRC( ddrval, "CreatePalette" );
  234.     LocalFree( ppe );
  235.  
  236.     for( i=0;i<MAX_PALETTES;i++ )
  237.     {
  238.         LBPrintf( "lpDDPal %d = %08lx", i+1, lpDDPal[i] );
  239.     }
  240.     return TRUE;
  241.  
  242. } /* resetExclusiveAndPalettes */
  243.  
  244. /*
  245.  * WindowProc
  246.  *
  247.  * Messages for our window are handled here
  248.  */
  249. LRESULT CALLBACK WindowProc( HWND hWnd, unsigned uMsg, WPARAM wParam, LPARAM lParam )
  250. {
  251.  
  252.     switch( uMsg )
  253.     {
  254.     case WM_COMMAND:
  255.         switch( LOWORD( wParam ) )
  256.         {
  257.         case IDM_EXCLUSIVE:
  258.             bIsExclusive = !bIsExclusive;
  259.             resetExclusiveAndPalettes( bIsExclusive );
  260.             break;
  261.         case IDM_ATTACH1:
  262.         case IDM_ATTACH2:
  263.         case IDM_ATTACH3:
  264.         case IDM_ATTACH4:
  265.             doAttach( LOWORD( wParam ) - IDM_ATTACH1 );
  266.             break;
  267.         case IDM_DETACH:
  268.             doDetach();
  269.             break;
  270.         case IDM_CYCLE:
  271.             doCycle();
  272.             break;
  273.         case IDM_CLEAR:
  274.             LBClear();
  275.             break;
  276.         case IDM_EXIT:
  277.             DestroyWindow( hWnd );
  278.             break;
  279.         }
  280.         break;
  281.  
  282.     case WM_PAINT:
  283.     {
  284.         PAINTSTRUCT     ps;
  285.  
  286.         BeginPaint( hWnd, &ps );
  287.         EndPaint( hWnd, &ps );
  288.         break;
  289.     }
  290.  
  291.     case WM_INITMENU:
  292.         CheckMenuItem( (HMENU) wParam, IDM_EXCLUSIVE, MF_BYCOMMAND |
  293.                         (bIsExclusive ? MF_CHECKED:MF_UNCHECKED) );
  294.         break;
  295.  
  296.     case WM_SIZE:
  297.         /*
  298.          * resize our message listbox
  299.          */
  300.         LBSize( LOWORD( lParam ), HIWORD( lParam ) );
  301.         break;
  302.  
  303.     case WM_DESTROY:
  304.         /*
  305.          * free all DirectDraw objects
  306.          */
  307.         if( lpDD != NULL )
  308.         {
  309.             int i;
  310.  
  311.             for( i=0;i<MAX_PALETTES;i++ )
  312.             {
  313.                 if( lpDDPal[i] != NULL )
  314.                 {
  315.                     lpDDPal[i]->Release();
  316.                     lpDDPal[i] = NULL;
  317.                 }
  318.             }
  319.  
  320.             if( lpDDPSurf != NULL )
  321.             {
  322.                 lpDDPSurf->Release();
  323.                 lpDDPSurf = NULL;
  324.             }
  325.             lpDD->Release();
  326.             lpDD = NULL;
  327.         }
  328.         PostQuitMessage( 0 );
  329.         break;
  330.  
  331.     default:
  332.         return DefWindowProc( hWnd, uMsg, wParam, lParam );
  333.     }
  334.     return 0L;
  335.  
  336. } /* WindowProc */
  337.  
  338. /*
  339.  * doInit - do work required for every instance of the application:
  340.  *                create the window, initialize data
  341.  */
  342. static BOOL doInit( HINSTANCE hInstance, int nCmdShow )
  343. {
  344.     WNDCLASS            wc;
  345.     HRESULT             ddrval;
  346.     DDSURFACEDESC       ddsd;
  347.  
  348.     /*
  349.      * set up and register window class
  350.      */
  351.     wc.style = CS_HREDRAW | CS_VREDRAW;
  352.     wc.lpfnWndProc = (WNDPROC) WindowProc;
  353.     wc.cbClsExtra = 0;
  354.     wc.cbWndExtra = sizeof( DWORD );
  355.     wc.hInstance = hInstance;
  356.     wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
  357.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  358.     wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
  359.     wc.lpszMenuName = MAKEINTRESOURCE( IDR_MENU );
  360.     wc.lpszClassName = szClass;
  361.     if( !RegisterClass( &wc ) )
  362.     {
  363.         return FALSE;
  364.     }
  365.     
  366.     /*
  367.      * create a window
  368.      */
  369.     hWndMain = CreateWindow(
  370.         szClass,                // class
  371.         szCaption,              // caption
  372.         WS_OVERLAPPEDWINDOW,    // style 
  373.         CW_USEDEFAULT,          // x pos
  374.         CW_USEDEFAULT,          // y pos
  375.         CW_USEDEFAULT,          // width
  376.         CW_USEDEFAULT,          // height
  377.         NULL,                   // parent window
  378.         NULL,                   // menu 
  379.         hInstance,              // instance
  380.         NULL                    // parms
  381.         );
  382.                     
  383.     if( !hWndMain )
  384.     {
  385.         return FALSE;
  386.     }
  387.  
  388.     LBCreate( hWndMain, 100 );
  389.  
  390.     ShowWindow( hWndMain, nCmdShow );
  391.     UpdateWindow( hWndMain );
  392.  
  393.     /*
  394.      * create the main DirectDraw object
  395.      */
  396.     ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
  397.     LBPrintfDDRC( ddrval, "DirectDrawCreate" );
  398.     if( ddrval != DD_OK )
  399.     {
  400.         DestroyWindow( hWndMain );
  401.         return FALSE;
  402.     }
  403.  
  404.     ddrval = lpDD->SetCooperativeLevel( hWndMain, DDSCL_NORMAL );
  405.  
  406.     /*
  407.      * create the primary surface
  408.      */
  409.     ddsd.dwSize = sizeof( ddsd );
  410.     ddsd.dwFlags = DDSD_CAPS;
  411.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  412.     ddrval = lpDD->CreateSurface( &ddsd, &lpDDPSurf, NULL );
  413.     LBPrintfDDRC( ddrval, "CreateSurface" );
  414.     if( ddrval != DD_OK )
  415.     {
  416.         lpDD->Release();
  417.         DestroyWindow( hWndMain );
  418.         return FALSE;
  419.     }
  420.  
  421.     /*
  422.      * set up palettes
  423.      */
  424.     if( !resetExclusiveAndPalettes( bIsExclusive ) )
  425.     {
  426.         lpDDPSurf->Release();
  427.         lpDD->Release();
  428.         DestroyWindow( hWndMain );
  429.         return FALSE;
  430.     }
  431.     
  432.     return TRUE;
  433.                         
  434. } /* doInit */
  435.  
  436. /*
  437.  * WinMain - initialization, message loop
  438.  */
  439. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  440.                         LPSTR lpCmdLine, int nCmdShow)
  441. {
  442.     MSG         msg;
  443.  
  444.     hPrevInstance = hPrevInstance;
  445.     lpCmdLine = lpCmdLine;
  446.  
  447.     if( !doInit( hInstance, nCmdShow ) )
  448.     {
  449.         return FALSE;
  450.     }
  451.  
  452.     while( GetMessage( &msg, NULL, NULL, NULL ) )
  453.     {
  454.         TranslateMessage( &msg );
  455.         DispatchMessage( &msg );
  456.     }
  457.  
  458.     return( msg.wParam );
  459.  
  460. } /* WinMain */
  461.