home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / ddraw / src / ddex2 / ddex2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-14  |  11.2 KB  |  342 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DDEx2.CPP
  3. //
  4. // Desc: Direct Draw example program 2.  Adds functionality to 
  5. //       example program 1.  Changes the video mode to 640x480x8.
  6. //       Reads a bitmap file from disk and copies it into the 
  7. //       back buffer and then slowly flips between the primary
  8. //       surface and the back buffer.  Press F12 to exit the program.
  9. //
  10. // Copyright (c) 1995-1999 Microsoft Corporation. All rights reserved.
  11. //-----------------------------------------------------------------------------
  12.  
  13. #ifndef WIN32_LEAN_AND_MEAN
  14. #define WIN32_LEAN_AND_MEAN
  15. #endif
  16. //-----------------------------------------------------------------------------
  17. // Include files
  18. //-----------------------------------------------------------------------------
  19. #include <windows.h>
  20. #include <ddraw.h>
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include "resource.h"
  24. #include "ddutil.h"
  25.  
  26. //-----------------------------------------------------------------------------
  27. // Local definitions
  28. //-----------------------------------------------------------------------------
  29. #define NAME                "DDExample2"
  30. #define TITLE               "Direct Draw Example 2"
  31.  
  32. //-----------------------------------------------------------------------------
  33. // Default settings
  34. //-----------------------------------------------------------------------------
  35. #define TIMER_ID            1
  36. #define TIMER_RATE          500
  37.  
  38. //-----------------------------------------------------------------------------
  39. // Global data
  40. //-----------------------------------------------------------------------------
  41. LPDIRECTDRAW7               g_pDD = NULL;        // DirectDraw object
  42. LPDIRECTDRAWSURFACE7        g_pDDSPrimary = NULL;// DirectDraw primary surface
  43. LPDIRECTDRAWSURFACE7        g_pDDSBack = NULL;   // DirectDraw back surface
  44. LPDIRECTDRAWPALETTE         g_pDDPal = NULL;     // The primary surface palette
  45. BOOL                        g_bActive = FALSE;   // Is application active?
  46.  
  47. //-----------------------------------------------------------------------------
  48. // Local data
  49. //-----------------------------------------------------------------------------
  50. static char                 szBackground[] = "BACK";
  51. static char                 szMsg[] = "Page Flipping Test: Press F12 to exit";
  52. static char                 szFrontMsg[] = "Front buffer (F12 to quit)";
  53. static char                 szBackMsg[] = "Back buffer (F12 to quit)";
  54.  
  55.  
  56.  
  57.  
  58. //-----------------------------------------------------------------------------
  59. // Name: ReleaseAllObjects()
  60. // Desc: Finished with all objects we use; release them
  61. //-----------------------------------------------------------------------------
  62. static void
  63. ReleaseAllObjects(void)
  64. {
  65.     if (g_pDD != NULL)
  66.     {
  67.         if (g_pDDSPrimary != NULL)
  68.         {
  69.             g_pDDSPrimary->Release();
  70.             g_pDDSPrimary = NULL;
  71.         }
  72.         if (g_pDDPal != NULL)
  73.         {
  74.             g_pDDPal->Release();
  75.             g_pDDPal = NULL;
  76.         }
  77.         g_pDD->Release();
  78.         g_pDD = NULL;
  79.     }
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86. //-----------------------------------------------------------------------------
  87. // Name: InitFail()
  88. // Desc: This function is called if an initialization function fails
  89. //-----------------------------------------------------------------------------
  90. HRESULT
  91. InitFail(HWND hWnd, HRESULT hRet, LPCTSTR szError,...)
  92. {
  93.     char                        szBuff[128];
  94.     va_list                     vl;
  95.  
  96.     va_start(vl, szError);
  97.     vsprintf(szBuff, szError, vl);
  98.     ReleaseAllObjects();
  99.     MessageBox(hWnd, szBuff, TITLE, MB_OK);
  100.     DestroyWindow(hWnd);
  101.     va_end(vl);
  102.     return hRet;
  103. }
  104.  
  105.  
  106.  
  107.  
  108. //-----------------------------------------------------------------------------
  109. // Name: UpdateFrame()
  110. // Desc: Displays the proper text for the page
  111. //-----------------------------------------------------------------------------
  112. static void
  113. UpdateFrame(HWND hWnd)
  114. {
  115.     static BYTE                 phase = 0;
  116.     HDC                         hdc;
  117.     RECT                        rc;
  118.     SIZE                        size;
  119.  
  120.     // The back buffer already has a loaded bitmap, so don't clear it
  121.     if (g_pDDSBack->GetDC(&hdc) == DD_OK)
  122.     {
  123.         SetBkColor(hdc, RGB(0, 0, 255));
  124.         SetTextColor(hdc, RGB(255, 255, 0));
  125.         if (phase)
  126.         {
  127.             GetClientRect(hWnd, &rc);
  128.             GetTextExtentPoint(hdc, szMsg, lstrlen(szMsg), &size);
  129.             TextOut(hdc, (rc.right - size.cx) / 2, (rc.bottom - size.cy) / 2,
  130.                     szMsg, sizeof(szMsg) - 1);
  131.             TextOut(hdc, 0, 0, szFrontMsg, lstrlen(szFrontMsg));
  132.             phase = 0;
  133.         }
  134.         else
  135.         {
  136.             TextOut(hdc, 0, 0, szBackMsg, lstrlen(szBackMsg));
  137.             phase = 1;
  138.         }
  139.         g_pDDSBack->ReleaseDC(hdc);
  140.     }
  141. }
  142.  
  143.  
  144.  
  145.  
  146. //-----------------------------------------------------------------------------
  147. // Name: WindowProc()
  148. // Desc: The Main Window Procedure
  149. //-----------------------------------------------------------------------------
  150. long FAR PASCAL
  151. WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  152. {
  153.     HRESULT                     hRet;
  154.  
  155.     switch (message)
  156.     {
  157.         case WM_ACTIVATE:
  158.             // Pause if minimized
  159.             g_bActive = !((BOOL)HIWORD(wParam));
  160.             return 0L;
  161.  
  162.         case WM_DESTROY:
  163.             // Clean up and close the app
  164.             ReleaseAllObjects();
  165.             PostQuitMessage(0);
  166.             return 0L;
  167.  
  168.         case WM_KEYDOWN:
  169.             // Handle any non-accelerated key commands
  170.             switch (wParam)
  171.             {
  172.                 case VK_ESCAPE:
  173.                 case VK_F12:
  174.                     PostMessage(hWnd, WM_CLOSE, 0, 0);
  175.                     return 0L;
  176.             }
  177.             break;
  178.  
  179.         case WM_SETCURSOR:
  180.             // Turn off the cursor since this is a full-screen app
  181.             SetCursor(NULL);
  182.             return TRUE;
  183.  
  184.         case WM_TIMER:
  185.             // Update and flip surfaces
  186.             if (g_bActive && TIMER_ID == wParam)
  187.             {
  188.                 UpdateFrame(hWnd);
  189.                 while (TRUE)
  190.                 {
  191.                     hRet = g_pDDSPrimary->Flip(NULL, 0);
  192.                     if (hRet == DD_OK)
  193.                         break;
  194.                     if (hRet == DDERR_SURFACELOST)
  195.                     {
  196.                         hRet = g_pDDSPrimary->Restore();
  197.                         if (hRet != DD_OK)
  198.                             break;
  199.                         hRet = DDReLoadBitmap(g_pDDSBack, szBackground);
  200.                         if (hRet != DD_OK)
  201.                             break;
  202.                     }
  203.                     if (hRet != DDERR_WASSTILLDRAWING)
  204.                         break;
  205.                 }
  206.             }
  207.             break;
  208.     }
  209.     return DefWindowProc(hWnd, message, wParam, lParam);
  210. }
  211.  
  212.  
  213.  
  214.  
  215. //-----------------------------------------------------------------------------
  216. // Name: InitApp()
  217. // Desc: Do work required for every instance of the application:
  218. //          Create the window, initialize data
  219. //-----------------------------------------------------------------------------
  220. static HRESULT
  221. InitApp(HINSTANCE hInstance, int nCmdShow)
  222. {
  223.     HWND                        hWnd;
  224.     WNDCLASS                    wc;
  225.     DDSURFACEDESC2              ddsd;
  226.     DDSCAPS2                    ddscaps;
  227.     HRESULT                     hRet;
  228.  
  229.     // Set up and register window class
  230.     wc.style = CS_HREDRAW | CS_VREDRAW;
  231.     wc.lpfnWndProc = WindowProc;
  232.     wc.cbClsExtra = 0;
  233.     wc.cbWndExtra = 0;
  234.     wc.hInstance = hInstance;
  235.     wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MAIN_ICON));
  236.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  237.     wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
  238.     wc.lpszMenuName = NAME;
  239.     wc.lpszClassName = NAME;
  240.     RegisterClass(&wc);
  241.  
  242.     // Create a window
  243.     hWnd = CreateWindowEx(WS_EX_TOPMOST,
  244.                           NAME,
  245.                           TITLE,
  246.                           WS_POPUP,
  247.                           0,
  248.                           0,
  249.                           GetSystemMetrics(SM_CXSCREEN),
  250.                           GetSystemMetrics(SM_CYSCREEN),
  251.                           NULL,
  252.                           NULL,
  253.                           hInstance,
  254.                           NULL);
  255.     if (!hWnd)
  256.         return FALSE;
  257.     ShowWindow(hWnd, nCmdShow);
  258.     UpdateWindow(hWnd);
  259.     SetFocus(hWnd);
  260.  
  261.     ///////////////////////////////////////////////////////////////////////////
  262.     // Create the main DirectDraw object
  263.     ///////////////////////////////////////////////////////////////////////////
  264.     hRet = DirectDrawCreateEx(NULL, (VOID**)&g_pDD, IID_IDirectDraw7, NULL);
  265.     if (hRet != DD_OK)
  266.         return InitFail(hWnd, hRet, "DirectDrawCreateEx FAILED");
  267.  
  268.     // Get exclusive mode
  269.     hRet = g_pDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
  270.     if (hRet != DD_OK)
  271.         return InitFail(hWnd, hRet, "SetCooperativeLevel FAILED");
  272.  
  273.     // Set the video mode to 640x480x8
  274.     hRet = g_pDD->SetDisplayMode(640, 480, 8, 0, 0);
  275.     if (hRet != DD_OK)
  276.         return InitFail(hWnd, hRet, "SetDisplayMode FAILED");
  277.  
  278.     // Create the primary surface with 1 back buffer
  279.     ZeroMemory(&ddsd, sizeof(ddsd));
  280.     ddsd.dwSize = sizeof(ddsd);
  281.     ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  282.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
  283.         DDSCAPS_FLIP |
  284.         DDSCAPS_COMPLEX;
  285.     ddsd.dwBackBufferCount = 1;
  286.     hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL);
  287.     if (hRet != DD_OK)
  288.         return InitFail(hWnd, hRet, "CreateSurface FAILED");
  289.  
  290.     // Get a pointer to the back buffer
  291.     ZeroMemory(&ddscaps, sizeof(ddscaps));
  292.     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
  293.     hRet = g_pDDSPrimary->GetAttachedSurface(&ddscaps, &g_pDDSBack);
  294.     if (hRet != DD_OK)
  295.         return InitFail(hWnd, hRet, "GetAttachedSurface FAILED");
  296.  
  297.     // Create and set the palette
  298.     g_pDDPal = DDLoadPalette(g_pDD, szBackground);
  299.     if (g_pDDPal == NULL)
  300.         return InitFail(hWnd, hRet, "DDLoadPalette FAILED");
  301.     hRet = g_pDDSPrimary->SetPalette(g_pDDPal);
  302.     if (hRet != DD_OK)
  303.         return InitFail(hWnd, hRet, "SetPalette FAILED");
  304.  
  305.     // Load a bitmap into the back buffer.
  306.     hRet = DDReLoadBitmap(g_pDDSBack, szBackground);
  307.     if (hRet != DD_OK)
  308.         return InitFail(hWnd, hRet, "DDReLoadBitmap FAILED");
  309.  
  310.     // Create a timer to flip the pages
  311.     if (TIMER_ID != SetTimer(hWnd, TIMER_ID, TIMER_RATE, NULL))
  312.         return InitFail(hWnd, hRet, "SetTimer FAILED");
  313.  
  314.     return DD_OK;
  315. }
  316.  
  317.  
  318.  
  319.  
  320. //-----------------------------------------------------------------------------
  321. // Name: WinMain()
  322. // Desc: Initialization, message loop
  323. //-----------------------------------------------------------------------------
  324. int PASCAL
  325. WinMain(HINSTANCE hInstance,
  326.         HINSTANCE hPrevInstance,
  327.         LPSTR lpCmdLine,
  328.         int nCmdShow)
  329. {
  330.     MSG                         msg;
  331.  
  332.     if (InitApp(hInstance, nCmdShow) != DD_OK)
  333.         return FALSE;
  334.  
  335.     while (GetMessage(&msg, NULL, 0, 0))
  336.     {
  337.         TranslateMessage(&msg);
  338.         DispatchMessage(&msg);
  339.     }
  340.     return msg.wParam;
  341. }
  342.