home *** CD-ROM | disk | FTP | other *** search
- /*
- Company: Sensaura
- Copyright: (C) 1999
-
- Software Project: Sensaura DirectSound3D Demo
- File Name: dxwindow.cpp
- File Description: Source file for implementation of DirectXWindow class
- Author: Adam Philp
- Version: 1.02.000
- Last Update: 10-FEB-99
-
- Target Compiler: Microsoft Visual C++ Version 5.0
- */
-
- /////////////////////// Included files ////////////////////////////////////////////////////////////
-
- #include <windows.h>
-
- #include "dxwindow.h" // DirectXWindow class declaration
- #include "dxwldobj.h" // Classes for 3D objects
- #include "applicat.h" // Application object
- #include "sensaura.h" // Sensaura-specific DirectSound functions
- #include "directx.h" // DirectX error handling functions and macros
- #include "resource.h" // Menu item identifiers
-
- #include "debug.h" // Debugging support
-
- /////////////////////// Local functions ///////////////////////////////////////////////////////////
-
- /*
- Function: AboutBoxProc
- Description: Minimum dialog box procedure for modeless About Box
- Parameters: hwnd - Handle of the dialog box
- uMsg - Message ID
- wParam Message param
- Return value: FALSE if we processed the message, TRUE otherwise
- */
-
- BOOL FAR PASCAL AboutBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- HWND hwndParent;
-
- switch(uMsg)
- {
- case WM_INITDIALOG:
- hwndParent = GetParent(hwnd);
- EnableWindow(hwndParent, FALSE);
- return TRUE;
-
- case WM_COMMAND:
- if(LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- hwndParent = GetParent(hwnd);
- EnableWindow(hwndParent, TRUE);
- DestroyWindow(hwnd);
- }
- return FALSE;
-
- default:
- return FALSE;
- }
- }
-
- /////////////////////// DirectXWindow class implementation ////////////////////////////////////////
-
- DirectXWindow::DirectXWindow(Window* parent, LPCSTR name, Application* pApp)
- : Window(parent, name, pApp), DirectXWorld(pApp->GetInstance())
- {
- m_Attr.W = 400;
- m_Attr.H = 400;
- }
-
- DirectXWindow::~DirectXWindow()
- {
- }
-
- /*
- Function: Create
- Description: Create the main window object. Create the DirectX world and attach it to the
- window. Set the menu items to the current settings
- Return value: true if successful, false otherwise
- */
-
- bool DirectXWindow::Create()
- {
- if(!Window::Create())
- return false;
-
- if(!DirectXWorld::Create(m_hWnd)) // Failed to create our DirectX world, so close down the
- { // program
- PostMessage(WM_CLOSE);
- return false;
- }
- return true;
- }
-
- /*
- Function: GetClassName
- Description: Return the name of our derived window class
- Return value: Pointer to string containing the class name
- */
-
- LPSTR DirectXWindow::GetClassName() const
- {
- return "DirectXWindow"; // Name of our derived class
- }
-
- /*
- Function: GetWindowClass
- Description: Fill window class structure for our window class
- Parameters: wc - Window class structure
- */
-
- void DirectXWindow::GetWindowClass(WNDCLASS& wc) const
- {
- Window::GetWindowClass(wc);
-
- wc.style |= CS_HREDRAW|CS_VREDRAW;
- wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
- wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINMENU);
- }
-
- /*
- Function: EvCommand
- Description: Handler for WM_COMMAND messages sent to our window
- Parameters: id - id of the menu item or control which sent the message
- hwndCtl - handle of the control or NULL for a menu item or accelerator
- notifyCode - specifies the notification code if the message is from a control
- */
-
- LRESULT DirectXWindow::EvCommand(UINT id, HWND hwndCtl, UINT notifyCode)
- {
- HWND hWndTemp;
-
- switch(id)
- {
- case CM_SENSAURA:
- SetDirectSound(DIRECTSOUND3D_SENSAURA);
- if(m_Objects.First(ID_GLOBE))
- {
- DestroyObject(ID_GLOBE);
- CreateObject(ID_GLOBE);
- }
- break;
-
- case CM_HARDWARE3D:
- SetDirectSound(DIRECTSOUND3D_HARDWARE);
- if(m_Objects.First(ID_GLOBE))
- {
- DestroyObject(ID_GLOBE);
- CreateObject(ID_GLOBE);
- }
- break;
-
- case CM_SOFTWARE3D:
- SetDirectSound(DIRECTSOUND3D_SOFTWARE);
- if(m_Objects.First(ID_GLOBE))
- {
- DestroyObject(ID_GLOBE);
- CreateObject(ID_GLOBE);
- }
- break;
-
- case CM_STEREO:
- SetDirectSound(DIRECTSOUND3D_NONE);
- if(m_Objects.First(ID_GLOBE))
- {
- DestroyObject(ID_GLOBE);
- CreateObject(ID_GLOBE);
- }
- break;
-
- case CM_SPEAKERS:
- SetSpeakerConfig(DSSPEAKER_STEREO);
- break;
-
- case CM_HEADPHONES:
- SetSpeakerConfig(DSSPEAKER_HEADPHONE);
- break;
-
- case CM_ABOUT:
- hWndTemp = CreateDialog(GetApplication()->GetInstance(), MAKEINTRESOURCE(IDD_ABOUT), m_hWnd,
- (DLGPROC)AboutBoxProc);
- ::ShowWindow(hWndTemp, SW_SHOW);
- break;
-
- case CM_CONTROLS:
- hWndTemp = CreateDialog(GetApplication()->GetInstance(), MAKEINTRESOURCE(IDD_CONTROLS), m_hWnd,
- (DLGPROC)AboutBoxProc);
- ::ShowWindow(hWndTemp, SW_SHOW);
- break;
-
- default:
- return Window::EvCommand(id, hwndCtl, notifyCode);
- }
- return 0;
- }
-
- /*
- Function: IdleAction
- Description: Function called when no Windows messages are being processed. This is where
- our DirectX world is updated so that objects can be moved and user input
- responded to
- Return value: True if updated successfully, false otherwise
- */
-
- bool DirectXWindow::IdleAction(long)
- {
- return Update();
- }
-
- /*
- Function: WindowProc
- Description: Window procedure for our window
- Return value: 0 if we process the message, non-zero otherwise
- */
-
- LRESULT DirectXWindow::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_ACTIVATE:
- EvActivate(LOWORD(wParam), HIWORD(wParam) ? true : false, (HWND)lParam);
- return 0;
-
- case WM_PAINT:
- EvPaint();
- return 0;
-
- case WM_SIZE:
- EvSize(wParam, LOWORD(lParam), HIWORD(lParam));
- break;
-
- case WM_INITMENU:
- UpdateMainMenu();
- break;
-
- case WM_EXITMENULOOP:
- m_dwTime = timeGetTime();
- break;
- }
- return Window::WindowProc(message, wParam, lParam);
- }
-
- /*
- Function: EvActivate
- Description: Direct3D windowed-mode handler for WM_ACTIVATE message
- */
-
- void DirectXWindow::EvActivate(WORD wActive, bool, HWND)
- {
- LPDIRECT3DRMWINDEVICE lpd3drmWinDev;
-
- // The Direct3DRMWinDevice object is obtained by calling the
- // IDirect3DRMObject::QueryInterface method and specifying IID_IDirect3DRMWinDevice, or by
- // calling a method such as IDirect3DRM::CreateDeviceFromD3D. Its methods are inherited
- // from the IDirect3DRMDevice interface.
- if(SUCCEEDED(m_pDevice->QueryInterface(IID_IDirect3DRMWinDevice, (void **)&lpd3drmWinDev)))
- { // HandleActivate responds to a Windows WM_ACTIVATE message.
- // This ensures that the colors are correct in the active rendering window.
- lpd3drmWinDev->HandleActivate(wActive);
- lpd3drmWinDev->Release();
- }
- }
-
- /*
- Function: EvActivate
- Description: Direct3D windowed-mode handler for WM_PAINT message
- */
-
- void DirectXWindow::EvPaint()
- {
- PAINTSTRUCT ps;
- LPDIRECT3DRMWINDEVICE lpd3drmWinDev;
-
- // The Direct3DRMWinDevice object is obtained by calling the
- // IDirect3DRMObject::QueryInterface method and specifying IID_IDirect3DRMWinDevice, or by
- // calling a method such as IDirect3DRM::CreateDeviceFromD3D. Its methods are inherited
- // from the IDirect3DRMDevice interface.
- if(SUCCEEDED(m_pDevice->QueryInterface(IID_IDirect3DRMWinDevice, (void **)&lpd3drmWinDev)))
- {
- BeginPaint(m_hWnd, &ps);
- lpd3drmWinDev->HandlePaint (ps.hdc);
- lpd3drmWinDev->Release();
- EndPaint(m_hWnd, &ps);
- }
- }
-
- /*
- Function: EvActivate
- Description: WM_SIZE message handler
- */
-
- void DirectXWindow::EvSize(UINT uSizeType, int cx, int cy)
- {
- if((cx != 0) && (cy != 0))
- Resize(cx, cy);
- }
-
- /*
- Function: CheckMainMenuItem
- Description: Check or uncheck a menu item
- Parameters: id - id of the menu item to check or uncheck
- fChecked - true to check menu item, false to uncheck item
- */
-
- void DirectXWindow::CheckMainMenuItem(UINT id, bool fChecked)
- {
- if(m_hWnd)
- {
- MENUITEMINFO item;
-
- item.cbSize = sizeof(MENUITEMINFO);
- item.fMask = MIIM_STATE;
- item.fState = fChecked ? MFS_CHECKED : MFS_UNCHECKED;
- SetMenuItemInfo(GetMenu(m_hWnd), id, FALSE, &item);
-
- DrawMenuBar(m_hWnd);
- }
- }
-
- /*
- Function: EnableMainMenuItem
- Description: Enable or disable a menu item
- Parameters: id - id of the menu item to enable or disable
- fChecked - true to enable menu item, false to disable item
- */
-
- void DirectXWindow::EnableMainMenuItem(UINT id, bool fEnabled)
- {
- if(m_hWnd)
- {
- MENUITEMINFO item;
-
- item.cbSize = sizeof(MENUITEMINFO);
- item.fMask = MIIM_STATE;
- item.fState = fEnabled ? MFS_ENABLED : MFS_GRAYED;
- SetMenuItemInfo(GetMenu(m_hWnd), id, FALSE, &item);
- DrawMenuBar(m_hWnd);
- }
- }
-
- /*
- Function: UpdateMainMenu
- Description: Set menu items to current DirectSound/Sensaura settings
- */
-
- void DirectXWindow::UpdateMainMenu()
- {
- DWORD dwConfig;
-
- if(m_pDS->GetSpeakerConfig(&dwConfig) != DS_OK)
- return;
-
- CheckMainMenuItem(CM_SOFTWARE3D, m_DirectSoundType == DIRECTSOUND3D_SOFTWARE ? true : false);
- CheckMainMenuItem(CM_STEREO, m_DirectSoundType == DIRECTSOUND3D_NONE ? true : false);
-
- if(m_bSensaura)
- {
- EnableMainMenuItem(CM_SENSAURA, true);
- EnableMainMenuItem(CM_HARDWARE3D, false);
- CheckMainMenuItem(CM_SENSAURA, m_DirectSoundType == DIRECTSOUND3D_SENSAURA ? true : false);
- }
- else
- {
- EnableMainMenuItem(CM_SENSAURA, false);
- EnableMainMenuItem(CM_HARDWARE3D, true);
- CheckMainMenuItem(CM_HARDWARE3D, m_DirectSoundType == DIRECTSOUND3D_HARDWARE ? true : false);
- }
- CheckMainMenuItem(CM_SPEAKERS, dwConfig == DSSPEAKER_HEADPHONE ? false : true);
- CheckMainMenuItem(CM_HEADPHONES, dwConfig == DSSPEAKER_HEADPHONE ? true : false);
-
- if(m_DirectSoundType == DIRECTSOUND3D_NONE)
- {
- EnableMainMenuItem(CM_SPEAKERS, false);
- EnableMainMenuItem(CM_HEADPHONES, false);
- }
- }
-