home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Game_Audio / AudioTest / SoundScript.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-07-15  |  6.5 KB  |  276 lines

  1. /***********************************************************\
  2. Copyright (C) James Boer, 2002. 
  3. All rights reserved worldwide.
  4.  
  5. This software is provided "as is" without express or implied
  6. warranties. You may freely copy and compile this source into
  7. applications you distribute provided that the copyright text
  8. below is included in the resulting source code, for example:
  9. "Portions Copyright (C) James Boer, 2002"
  10. \***********************************************************/
  11.  
  12. #include "stdafx.h"
  13. #include "audiotest.h"
  14. #include "SoundScript.h"
  15.  
  16. #include "AudioTest.h"
  17. #include "Segment.h"
  18. #include "Audio.h"
  19.  
  20. #include "AudioTestDlg.h"
  21. #include "System.h"
  22. #include "Sound2D.h"
  23. #include "Sound3D.h"
  24.  
  25. using namespace Audio;
  26.  
  27. #ifdef _DEBUG
  28. #define new DEBUG_NEW
  29. #undef THIS_FILE
  30. static char THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33.  
  34. #define GET_CURRENT_SOUNDSCAPE() \
  35.     int iIndex = m_SoundScapes.GetCurSel();\
  36.     if(iIndex == LB_ERR) return;\
  37.     ISoundScape* pSoundScape = static_cast<ISoundScape*>(m_SoundScapes.GetItemDataPtr(iIndex));\
  38.     if(!pSoundScape) return;
  39.  
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // SoundScript property page
  43.  
  44. IMPLEMENT_DYNCREATE(CSoundScript, CPropertyPage)
  45.  
  46. CSoundScript::CSoundScript() : CPropertyPage(CSoundScript::IDD)
  47. {
  48.     //{{AFX_DATA_INIT(CSoundScript)
  49.         // NOTE: the ClassWizard will add member initialization here
  50.     //}}AFX_DATA_INIT
  51.     m_bInit = false;
  52. }
  53.  
  54. CSoundScript::~CSoundScript()
  55. {
  56. }
  57.  
  58. void CSoundScript::DoDataExchange(CDataExchange* pDX)
  59. {
  60.     CPropertyPage::DoDataExchange(pDX);
  61.     //{{AFX_DATA_MAP(CSoundScript)
  62.     DDX_Control(pDX, IDC_SLIDER_SSVOLUME, m_Volume);
  63.     DDX_Control(pDX, IDC_LIST_SS_SOUNDS, m_SoundScapes);
  64.     DDX_Control(pDX, IDC_PAUSESS, m_Pause);
  65.     DDX_Control(pDX, IDC_STOPSS, m_Stop);
  66.     DDX_Control(pDX, IDC_PLAYSS, m_Play);
  67.     //}}AFX_DATA_MAP
  68. }
  69.  
  70.  
  71. void CSoundScript::ClearList()
  72. {
  73.     if(m_bInit)
  74.     {
  75. /*
  76.         ISoundScape* pSoundScape;
  77.         int iSize = m_SoundScapes.GetCount();
  78.         for(int i = 0; i < iSize; i++)
  79.         {
  80.             pSoundScape = static_cast<ISoundScape*>(m_SoundScapes.GetItemDataPtr(0));
  81.             pSoundScape->Term();
  82.             m_SoundScapes.DeleteString(0);
  83.         }
  84. */
  85.         m_SoundScapes.ResetContent();
  86.     }
  87. }
  88.  
  89.  
  90. void CSoundScript::UpdateControls()
  91. {
  92.     GET_CURRENT_SOUNDSCAPE();
  93.     float fVal;
  94.     pSoundScape->GetVolume(fVal);
  95.     m_Volume.SetPos(static_cast<int>(fVal * 10000.0f));
  96. }
  97.  
  98.  
  99. BEGIN_MESSAGE_MAP(CSoundScript, CPropertyPage)
  100.     //{{AFX_MSG_MAP(CSoundScript)
  101.     ON_BN_CLICKED(IDC_SS_INITIALIZE, OnSSInitialize)
  102.     ON_BN_CLICKED(IDC_SS_DELETE, OnSSDelete)
  103.     ON_BN_CLICKED(IDC_LOADSS, OnLoadSS)
  104.     ON_BN_CLICKED(IDC_UNLOADSS, OnUnloadSS)
  105.     ON_BN_CLICKED(IDC_PLAYSS, OnPlaySS)
  106.     ON_BN_CLICKED(IDC_PAUSESS, OnPauseSS)
  107.     ON_BN_CLICKED(IDC_STOPSS, OnStopSS)
  108.     ON_WM_DESTROY()
  109.     ON_WM_HSCROLL()
  110.     ON_LBN_SELCHANGE(IDC_LIST_SS_SOUNDS, OnSelchangeListSSSounds)
  111.     ON_LBN_DBLCLK(IDC_LIST_SS_SOUNDS, OnDblclkListSsSounds)
  112.     //}}AFX_MSG_MAP
  113. END_MESSAGE_MAP()
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CSoundScript message handlers
  117.  
  118.  
  119.  
  120. BOOL CSoundScript::OnInitDialog() 
  121. {
  122.     CPropertyPage::OnInitDialog();
  123.     
  124.     // Load the play button icons
  125.     m_hPlayIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PLAY));
  126.     m_hPauseIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PAUSE));
  127.     m_hStopIcon = LoadIcon(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_STOP));
  128.  
  129.     // Set the master play control buttons
  130.     m_Play.SetIcon(m_hPlayIcon);
  131.     m_Pause.SetIcon(m_hPauseIcon);
  132.     m_Stop.SetIcon(m_hStopIcon);
  133.  
  134.     m_bInit = true;
  135.  
  136.     m_Volume.SetRange(0, 10000);
  137.     m_Volume.SetPos(10000);
  138.     m_Volume.Invalidate();
  139.  
  140.     return TRUE;  // return TRUE unless you set the focus to a control
  141.                   // EXCEPTION: OCX Property Pages should return FALSE
  142. }
  143.  
  144. void CSoundScript::OnSSInitialize() 
  145. {
  146.     CFileDialog dlg(TRUE, NULL, NULL, OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  147.         "Script Files (.txt)|*.txt|All Files (*.*)|*.*||");
  148.  
  149.     if(dlg.DoModal() != IDOK)
  150.         return;
  151.  
  152.     char drive[_MAX_DRIVE];
  153.     char dir[_MAX_DIR];
  154.     char fname[_MAX_FNAME];
  155.     char ext[_MAX_EXT];
  156.  
  157.     POSITION pos = dlg.GetStartPosition();
  158.     CString sPathName;
  159.     while(pos)
  160.     {
  161.         sPathName = dlg.GetNextPathName(pos);
  162.         _splitpath(sPathName, drive, dir, fname, ext);
  163.  
  164.         if(!SoundMgr()->LoadScript((const char*)sPathName))
  165.         {
  166.             AfxMessageBox("Error parsing sound script");
  167.             return;
  168.         }
  169.     }
  170.  
  171.     std::string sName;
  172.     SoundMgr()->ResetSoundItr();
  173.     while(SoundMgr()->GetNextSound(sName))
  174.     {
  175.         //DebugOut(1, "Registered Sound = %s", sName.c_str());
  176.     }
  177.     SoundMgr()->ResetSound3DItr();
  178.     while(SoundMgr()->GetNextSound3D(sName))
  179.     {
  180.         //DebugOut(1, "Registered Sound3D = %s", sName.c_str());
  181.     }
  182.     SoundMgr()->ResetSoundScapeItr();
  183.     while(SoundMgr()->GetNextSoundScape(sName))
  184.     {
  185.         //DebugOut(1, "Registered Soundscape = %s", sName.c_str());
  186.         ISoundScape* pSoundScape;
  187.         if(!SoundMgr()->CreateSoundScape(pSoundScape))
  188.             return;
  189.         if(!SoundMgr()->InitSoundScape(sName, pSoundScape))
  190.         {
  191.             AfxMessageBox("Error initializing audio file");
  192.             return;
  193.         }
  194.  
  195.         int iIndex = m_SoundScapes.AddString(sName.c_str());
  196.         if(iIndex != LB_ERR)
  197.             m_SoundScapes.SetItemDataPtr(iIndex, pSoundScape);
  198.     }
  199.  
  200. }
  201.  
  202. void CSoundScript::OnDestroy() 
  203. {
  204.     CPropertyPage::OnDestroy();
  205.     ClearList();
  206. }
  207.  
  208. void CSoundScript::OnSSDelete() 
  209. {
  210.     GET_CURRENT_SOUNDSCAPE();
  211.     pSoundScape->Destroy();
  212.     int iSize = m_SoundScapes.DeleteString(iIndex);
  213.     if(!iSize)
  214.         return;
  215.     if(iIndex > (iSize - 1))
  216.         iIndex = iSize - 1;
  217.     m_SoundScapes.SetCurSel(iIndex);
  218. }
  219.  
  220. void CSoundScript::OnLoadSS() 
  221. {
  222.     GET_CURRENT_SOUNDSCAPE();
  223.     pSoundScape->Load();
  224. }
  225.  
  226. void CSoundScript::OnUnloadSS() 
  227. {
  228.     GET_CURRENT_SOUNDSCAPE();
  229.     pSoundScape->Unload();
  230. }
  231.  
  232. void CSoundScript::OnPlaySS() 
  233. {
  234.     GET_CURRENT_SOUNDSCAPE();
  235.     pSoundScape->Play();
  236. }
  237.  
  238. void CSoundScript::OnPauseSS() 
  239. {
  240.     GET_CURRENT_SOUNDSCAPE();
  241.     pSoundScape->Pause();
  242. }
  243.  
  244. void CSoundScript::OnStopSS() 
  245. {
  246.     GET_CURRENT_SOUNDSCAPE();
  247.     pSoundScape->Stop();
  248. }
  249.  
  250.  
  251. void CSoundScript::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  252. {
  253.     if(pScrollBar->IsKindOf(RUNTIME_CLASS(CSliderCtrl)))
  254.     {
  255.         CSliderCtrl* pSlider = reinterpret_cast<CSliderCtrl*>(pScrollBar);
  256.         if(!pSlider)
  257.             return;
  258.         GET_CURRENT_SOUNDSCAPE();
  259.         if(pSlider == &m_Volume)
  260.         {
  261.             pSoundScape->SetVolume(static_cast<float>(pSlider->GetPos()) / 10000.0f);
  262.         }
  263.     }
  264.     CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar);
  265. }
  266.  
  267. void CSoundScript::OnSelchangeListSSSounds() 
  268. {
  269.     UpdateControls();    
  270. }
  271.  
  272. void CSoundScript::OnDblclkListSsSounds() 
  273. {
  274.     OnPlaySS();    
  275. }
  276.