home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Extras / Sensaura / SDK1.0 / data1.cab / Example_Files / DS3DDemo / Source / dxwindow.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-13  |  9.7 KB  |  379 lines

  1. /*
  2.     Company:            Sensaura
  3.     Copyright:          (C) 1999
  4.  
  5.     Software Project:    Sensaura DirectSound3D Demo
  6.     File Name:            dxwindow.cpp
  7.     File Description:    Source file for implementation of DirectXWindow class
  8.     Author:                Adam Philp
  9.     Version:            1.02.000
  10.     Last Update:        10-FEB-99
  11.  
  12.     Target Compiler:    Microsoft Visual C++ Version 5.0
  13. */
  14.  
  15. ///////////////////////    Included files ////////////////////////////////////////////////////////////
  16.  
  17. #include <windows.h>
  18.  
  19. #include "dxwindow.h"                    // DirectXWindow class declaration
  20. #include "dxwldobj.h"                    // Classes for 3D objects
  21. #include "applicat.h"                    // Application object
  22. #include "sensaura.h"                    // Sensaura-specific DirectSound functions
  23. #include "directx.h"                    // DirectX error handling functions and macros
  24. #include "resource.h"                    // Menu item identifiers
  25.  
  26. #include "debug.h"                        // Debugging support
  27.  
  28. ///////////////////////    Local functions ///////////////////////////////////////////////////////////
  29.  
  30. /*
  31.     Function:        AboutBoxProc
  32.     Description:    Minimum dialog box procedure for modeless About Box
  33.     Parameters:        hwnd - Handle of the dialog box
  34.                     uMsg - Message ID
  35.                     wParam Message param
  36.     Return value:    FALSE if we processed the message, TRUE otherwise
  37. */
  38.  
  39. BOOL FAR PASCAL AboutBoxProc(HWND hwnd,    UINT uMsg, WPARAM wParam, LPARAM lParam)
  40. {
  41.     HWND hwndParent;
  42.  
  43.     switch(uMsg)
  44.     {
  45.     case WM_INITDIALOG:
  46.         hwndParent = GetParent(hwnd);
  47.         EnableWindow(hwndParent, FALSE);
  48.         return TRUE;
  49.  
  50.     case WM_COMMAND:
  51.         if(LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  52.         {
  53.             hwndParent = GetParent(hwnd);
  54.             EnableWindow(hwndParent, TRUE);
  55.             DestroyWindow(hwnd);
  56.         }
  57.         return FALSE;
  58.  
  59.     default:
  60.         return FALSE;
  61.     }
  62. }
  63.  
  64. /////////////////////// DirectXWindow class implementation ////////////////////////////////////////
  65.  
  66. DirectXWindow::DirectXWindow(Window* parent, LPCSTR name, Application* pApp) 
  67.     : Window(parent, name, pApp), DirectXWorld(pApp->GetInstance())
  68. {
  69.     m_Attr.W = 400;
  70.     m_Attr.H = 400;
  71. }
  72.  
  73. DirectXWindow::~DirectXWindow()
  74. {
  75. }
  76.  
  77. /*
  78.     Function:        Create
  79.     Description:    Create the main window object. Create the DirectX world and attach it to the 
  80.                     window.  Set the menu items to the current settings
  81.     Return value:    true if successful, false otherwise
  82. */
  83.  
  84. bool DirectXWindow::Create()
  85. {
  86.     if(!Window::Create())
  87.         return false;
  88.  
  89.     if(!DirectXWorld::Create(m_hWnd))    // Failed to create our DirectX world, so close down the
  90.     {                                    // program
  91.         PostMessage(WM_CLOSE);
  92.         return false;
  93.     }
  94.     return true;
  95. }
  96.  
  97. /*
  98.     Function:        GetClassName
  99.     Description:    Return the name of our derived window class
  100.     Return value:    Pointer to string containing the class name
  101. */
  102.  
  103. LPSTR DirectXWindow::GetClassName() const
  104. {
  105.     return "DirectXWindow";                // Name of our derived class
  106. }
  107.  
  108. /*
  109.     Function:        GetWindowClass
  110.     Description:    Fill window class structure for our window class
  111.     Parameters:        wc - Window class structure
  112. */
  113.  
  114. void DirectXWindow::GetWindowClass(WNDCLASS& wc) const
  115. {
  116.     Window::GetWindowClass(wc);
  117.  
  118.     wc.style |= CS_HREDRAW|CS_VREDRAW;
  119.     wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  120.     wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINMENU);
  121. }
  122.  
  123. /*
  124.     Function:        EvCommand
  125.     Description:    Handler for WM_COMMAND messages sent to our window
  126.     Parameters:        id - id of the menu item or control which sent the message
  127.                     hwndCtl - handle of the control or NULL for a menu item or accelerator
  128.                     notifyCode - specifies the notification code if the message is from a control
  129. */
  130.  
  131. LRESULT DirectXWindow::EvCommand(UINT id, HWND hwndCtl, UINT notifyCode)
  132. {
  133.     HWND hWndTemp;
  134.  
  135.     switch(id) 
  136.     {
  137.     case CM_SENSAURA:
  138.         SetDirectSound(DIRECTSOUND3D_SENSAURA);
  139.         if(m_Objects.First(ID_GLOBE))
  140.         {
  141.             DestroyObject(ID_GLOBE);
  142.             CreateObject(ID_GLOBE);
  143.         }
  144.         break;
  145.  
  146.     case CM_HARDWARE3D:
  147.         SetDirectSound(DIRECTSOUND3D_HARDWARE);
  148.         if(m_Objects.First(ID_GLOBE))
  149.         {
  150.             DestroyObject(ID_GLOBE);
  151.             CreateObject(ID_GLOBE);
  152.         }
  153.         break;
  154.  
  155.     case CM_SOFTWARE3D:
  156.         SetDirectSound(DIRECTSOUND3D_SOFTWARE);
  157.         if(m_Objects.First(ID_GLOBE))
  158.         {
  159.             DestroyObject(ID_GLOBE);
  160.             CreateObject(ID_GLOBE);
  161.         }
  162.         break;
  163.  
  164.     case CM_STEREO:
  165.         SetDirectSound(DIRECTSOUND3D_NONE);
  166.         if(m_Objects.First(ID_GLOBE))
  167.         {
  168.             DestroyObject(ID_GLOBE);
  169.             CreateObject(ID_GLOBE);
  170.         }
  171.         break;
  172.  
  173.     case CM_SPEAKERS:
  174.         SetSpeakerConfig(DSSPEAKER_STEREO);
  175.         break;
  176.  
  177.     case CM_HEADPHONES:
  178.         SetSpeakerConfig(DSSPEAKER_HEADPHONE);
  179.         break;
  180.  
  181.     case CM_ABOUT:
  182.         hWndTemp = CreateDialog(GetApplication()->GetInstance(), MAKEINTRESOURCE(IDD_ABOUT), m_hWnd, 
  183.             (DLGPROC)AboutBoxProc);
  184.         ::ShowWindow(hWndTemp, SW_SHOW);
  185.         break;
  186.         
  187.     case CM_CONTROLS:
  188.         hWndTemp = CreateDialog(GetApplication()->GetInstance(), MAKEINTRESOURCE(IDD_CONTROLS), m_hWnd, 
  189.             (DLGPROC)AboutBoxProc);
  190.         ::ShowWindow(hWndTemp, SW_SHOW);
  191.         break;
  192.         
  193.     default:
  194.         return Window::EvCommand(id, hwndCtl, notifyCode);
  195.     }
  196.     return 0;
  197. }
  198.  
  199. /*
  200.     Function:        IdleAction
  201.     Description:    Function called when no Windows messages are being processed.  This is where
  202.                     our DirectX world is updated so that objects can be moved and user input 
  203.                     responded to
  204.     Return value:    True if updated successfully, false otherwise
  205. */
  206.  
  207. bool DirectXWindow::IdleAction(long)
  208. {
  209.     return Update();
  210. }
  211.  
  212. /*
  213.     Function:        WindowProc
  214.     Description:    Window procedure for our window
  215.     Return value:    0 if we process the message, non-zero otherwise
  216. */
  217.  
  218. LRESULT DirectXWindow::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  219. {
  220.     switch (message) 
  221.     {
  222.     case WM_ACTIVATE:
  223.         EvActivate(LOWORD(wParam), HIWORD(wParam) ? true : false, (HWND)lParam);
  224.         return 0;
  225.  
  226.     case WM_PAINT:
  227.         EvPaint();
  228.         return 0;
  229.  
  230.     case WM_SIZE:
  231.         EvSize(wParam, LOWORD(lParam), HIWORD(lParam));
  232.         break;
  233.  
  234.     case WM_INITMENU:
  235.         UpdateMainMenu();
  236.         break;
  237.  
  238.     case WM_EXITMENULOOP:
  239.         m_dwTime = timeGetTime();    
  240.         break;
  241.     }
  242.     return Window::WindowProc(message, wParam, lParam);
  243. }
  244.  
  245. /*
  246.     Function:        EvActivate
  247.     Description:    Direct3D windowed-mode handler for WM_ACTIVATE message
  248. */
  249.  
  250. void DirectXWindow::EvActivate(WORD wActive, bool, HWND)
  251. {
  252.     LPDIRECT3DRMWINDEVICE lpd3drmWinDev;
  253.  
  254.         // The Direct3DRMWinDevice object is obtained by calling the 
  255.         // IDirect3DRMObject::QueryInterface method and specifying IID_IDirect3DRMWinDevice, or by 
  256.         // calling a method such as IDirect3DRM::CreateDeviceFromD3D. Its methods are inherited 
  257.         // from the IDirect3DRMDevice interface.
  258.        if(SUCCEEDED(m_pDevice->QueryInterface(IID_IDirect3DRMWinDevice, (void **)&lpd3drmWinDev))) 
  259.     {    // HandleActivate responds to a Windows WM_ACTIVATE message.
  260.         // This ensures that the colors are correct in the active rendering window.
  261.         lpd3drmWinDev->HandleActivate(wActive);
  262.         lpd3drmWinDev->Release();
  263.        }
  264. }
  265.  
  266. /*
  267.     Function:        EvActivate
  268.     Description:    Direct3D windowed-mode handler for WM_PAINT message
  269. */
  270.  
  271. void DirectXWindow::EvPaint()
  272. {
  273.     PAINTSTRUCT    ps;
  274.     LPDIRECT3DRMWINDEVICE lpd3drmWinDev;
  275.  
  276.         // The Direct3DRMWinDevice object is obtained by calling the 
  277.         // IDirect3DRMObject::QueryInterface method and specifying IID_IDirect3DRMWinDevice, or by 
  278.         // calling a method such as IDirect3DRM::CreateDeviceFromD3D. Its methods are inherited 
  279.         // from the IDirect3DRMDevice interface.
  280.        if(SUCCEEDED(m_pDevice->QueryInterface(IID_IDirect3DRMWinDevice, (void **)&lpd3drmWinDev))) 
  281.     {
  282.         BeginPaint(m_hWnd, &ps);
  283.         lpd3drmWinDev->HandlePaint (ps.hdc);
  284.         lpd3drmWinDev->Release();
  285.         EndPaint(m_hWnd, &ps);
  286.        }
  287. }
  288.  
  289. /*
  290.     Function:        EvActivate
  291.     Description:    WM_SIZE message handler
  292. */
  293.  
  294. void DirectXWindow::EvSize(UINT uSizeType, int cx, int cy)
  295. {
  296.     if((cx != 0) && (cy != 0))
  297.         Resize(cx, cy);
  298. }
  299.  
  300. /*
  301.     Function:        CheckMainMenuItem
  302.     Description:    Check or uncheck a menu item
  303.     Parameters:        id - id of the menu item to check or uncheck
  304.                     fChecked - true to check menu item, false to uncheck item
  305. */
  306.  
  307. void DirectXWindow::CheckMainMenuItem(UINT id, bool fChecked)
  308. {
  309.     if(m_hWnd) 
  310.     {
  311.         MENUITEMINFO item;
  312.  
  313.         item.cbSize = sizeof(MENUITEMINFO);
  314.         item.fMask = MIIM_STATE;
  315.         item.fState = fChecked ? MFS_CHECKED : MFS_UNCHECKED;
  316.         SetMenuItemInfo(GetMenu(m_hWnd), id, FALSE, &item);
  317.  
  318.         DrawMenuBar(m_hWnd);
  319.     }
  320. }
  321.  
  322. /*
  323.     Function:        EnableMainMenuItem
  324.     Description:    Enable or disable a menu item
  325.     Parameters:        id - id of the menu item to enable or disable
  326.                     fChecked - true to enable menu item, false to disable item
  327. */
  328.  
  329. void DirectXWindow::EnableMainMenuItem(UINT id, bool fEnabled)
  330. {
  331.     if(m_hWnd) 
  332.     {
  333.         MENUITEMINFO item;
  334.  
  335.         item.cbSize = sizeof(MENUITEMINFO);
  336.         item.fMask = MIIM_STATE;
  337.         item.fState = fEnabled ? MFS_ENABLED : MFS_GRAYED;
  338.         SetMenuItemInfo(GetMenu(m_hWnd), id, FALSE, &item);
  339.         DrawMenuBar(m_hWnd);
  340.     }
  341. }
  342.  
  343. /*
  344.     Function:        UpdateMainMenu
  345.     Description:    Set menu items to current DirectSound/Sensaura settings
  346. */
  347.  
  348. void DirectXWindow::UpdateMainMenu()
  349. {
  350.     DWORD dwConfig;
  351.  
  352.     if(m_pDS->GetSpeakerConfig(&dwConfig) != DS_OK)
  353.         return;
  354.  
  355.     CheckMainMenuItem(CM_SOFTWARE3D, m_DirectSoundType == DIRECTSOUND3D_SOFTWARE ? true : false);
  356.     CheckMainMenuItem(CM_STEREO, m_DirectSoundType == DIRECTSOUND3D_NONE ? true : false);
  357.  
  358.     if(m_bSensaura)
  359.     {
  360.         EnableMainMenuItem(CM_SENSAURA, true);
  361.         EnableMainMenuItem(CM_HARDWARE3D, false);
  362.         CheckMainMenuItem(CM_SENSAURA, m_DirectSoundType == DIRECTSOUND3D_SENSAURA ? true : false);
  363.     }
  364.     else
  365.     {
  366.         EnableMainMenuItem(CM_SENSAURA, false);
  367.         EnableMainMenuItem(CM_HARDWARE3D, true);
  368.         CheckMainMenuItem(CM_HARDWARE3D, m_DirectSoundType == DIRECTSOUND3D_HARDWARE ? true : false);
  369.     }
  370.     CheckMainMenuItem(CM_SPEAKERS, dwConfig == DSSPEAKER_HEADPHONE ? false : true);
  371.     CheckMainMenuItem(CM_HEADPHONES, dwConfig == DSSPEAKER_HEADPHONE ? true : false);
  372.  
  373.     if(m_DirectSoundType == DIRECTSOUND3D_NONE)
  374.     {
  375.         EnableMainMenuItem(CM_SPEAKERS, false);
  376.         EnableMainMenuItem(CM_HEADPHONES, false);
  377.     }
  378. }
  379.