home *** CD-ROM | disk | FTP | other *** search
/ PC World 2006 July & August / PCWorld_2006-07-08_cd.bin / temacd / planearcade / planearcade.exe / Tank3.bmp / sound.cpp < prev    next >
C/C++ Source or Header  |  2004-10-30  |  8KB  |  421 lines

  1.  
  2.  
  3. #include "main.h"
  4.  
  5. //-----------
  6. //Global
  7. //-----------
  8. ALCcontext *Context = NULL;
  9. ALCdevice  *Device = NULL;
  10.  
  11.  
  12. //------------------------------------------------------------------
  13. // Name: InitializeSound()
  14. // Desc: Inicializacia zvuku
  15. //------------------------------------------------------------------
  16. void InitializeSound()
  17. {
  18.  
  19.     //DirectMusic
  20.     //--------------
  21.     if (MusicEnabled == 1)
  22.     {
  23.         CoInitialize(NULL);
  24.     }
  25.  
  26.     //OpenAL3D
  27.     //--------------
  28.     if (SoundEnabled == 1)
  29.     {
  30.         LogPrint("Inicializujem OpenAL3D");
  31.  
  32.         //Open device
  33.         if(SoundEnabled3D == 1)
  34.         {
  35.             if(HardwareSound == 1)
  36.                 Device = alcOpenDevice((ALubyte*)"DirectSound3D");
  37.             else
  38.                 Device = alcOpenDevice((ALubyte*)"DirectSound");
  39.         }
  40.         else
  41.             Device = alcOpenDevice((ALubyte*)"DirectSound");
  42.  
  43.  
  44.         if (Device == NULL)
  45.         {
  46.             //we failed to initialize the device
  47.             LogPrint(" chyba pri inicializacii");
  48.             MessageBox(hWnd,"Chyba pri iniciaσizßciφ zvuku",
  49.                             "ERROR",MB_OK|MB_ICONERROR);
  50.         }
  51.  
  52.         //Create context(s)
  53.         LogPrint("alcCreateContext");
  54.         Context=alcCreateContext(Device,NULL);
  55.  
  56.         //Set active context
  57.         LogPrint("alcMakeContextCurrentK");
  58.         alcMakeContextCurrent(Context);
  59.  
  60.         //alutInit(0,NULL);
  61.     
  62.     }
  63.  
  64. }
  65.  
  66. //------------------------------------------------------------------
  67. // Name: CleanUpSound()
  68. // Desc: Odstrani z pamati objekty zvuku
  69. //------------------------------------------------------------------
  70. void CleanUpSound()
  71. {
  72.  
  73.     //DirectMusic
  74.     //-------------
  75.  
  76.     if (MusicEnabled == 1)
  77.     {
  78.         //UnititializeCom
  79.         CoUninitialize();
  80.     }
  81.  
  82.     //OpenAL3D
  83.     //--------------
  84.  
  85.     if (SoundEnabled == 1)
  86.     {
  87.         //Get active context
  88.         Context=alcGetCurrentContext();
  89.         //Get device for active context
  90.         Device=alcGetContextsDevice(Context);
  91.         //Disable context
  92.         alcMakeContextCurrent(NULL);
  93.         //Release context(s)
  94.         alcDestroyContext(Context);
  95.         //Close device
  96.         alcCloseDevice(Device);
  97.     }
  98.  
  99. }
  100.  
  101.  
  102.  
  103. //------------------------------------------------------------------
  104. // Name: SOUND()
  105. // Desc: Konstruktor
  106. //------------------------------------------------------------------
  107. SOUND::SOUND()
  108. {    
  109.  
  110.     //MP3
  111.     m_pGraph = NULL;
  112.     m_pMediaControl = NULL;
  113.     m_pMediaPosition = NULL;
  114.  
  115.  
  116. }
  117.  
  118. //------------------------------------------------------------------
  119. // Name: ~SOUND()
  120. // Desc: Destruktor
  121. //------------------------------------------------------------------
  122. SOUND::~SOUND()
  123. {
  124.     Stop();
  125.  
  126.     //Direct Music
  127.     //-------------
  128.     if (m_enumFormat == MP3)
  129.     {
  130.         if(m_pGraph)
  131.             m_pGraph->Release();
  132.         if(m_pMediaControl)
  133.             m_pMediaControl->Release();
  134.         if(m_pMediaPosition)
  135.             m_pMediaPosition->Release();
  136.  
  137.     }
  138.  
  139.     //OpenAL3D
  140.     //-----------
  141.     if (m_enumFormat == WavMidi3D)
  142.     {
  143.         alDeleteSources(1,&alSource);
  144.         alDeleteBuffers(1,&alSampleSet);
  145.     }
  146.  
  147.     
  148. }
  149.  
  150.  
  151. //------------------------------------------------------------------
  152. // Name: InitialiseForWavMidi()
  153. // Desc: pripravy zvuk pre prehrßvanie MIDI alebo WAW
  154. //------------------------------------------------------------------
  155. void SOUND::InitialiseForWavMidi()
  156. {
  157.  
  158.     //ak je vypnuty zvuk skonci
  159.     if (SoundEnabled == 0)
  160.         return;
  161.  
  162.     LogPrint("Vytvaram zvuk WavMidi");
  163.  
  164.     m_enumFormat = WavMidi3D;
  165. }
  166.  
  167. //------------------------------------------------------------------
  168. // Name: InitialiseForWavMidi()
  169. // Desc: pripravy zvuk pre prehrßvanie MIDI alebo WAW
  170. //------------------------------------------------------------------
  171. void SOUND::InitialiseForWavMidi3D()
  172. {
  173.     //ak je vypnuty zvuk skonci
  174.     if (SoundEnabled == 0)
  175.         return;
  176.  
  177.     LogPrint("Vytvaram zvuk WavMidi3D");
  178.  
  179.     m_enumFormat = WavMidi3D;
  180.  
  181.     alSourcei(alSource,AL_SOURCE_RELATIVE,AL_TRUE);
  182.  
  183. }
  184.  
  185. //------------------------------------------------------------------
  186. // Name: InitialiseForMP3()
  187. // Desc: pripravy zvuk na prehravanie MP3
  188. //------------------------------------------------------------------
  189. void SOUND::InitialiseForMP3()
  190. {
  191.     //ak je vypnuty zvuk skonci
  192.     if (MusicEnabled == 0)
  193.         return;
  194.  
  195.     LogPrint("Inicializujem Direct Music");
  196.  
  197.     LogPrint("Vytvaram zvuk MP3");
  198.  
  199.     CoCreateInstance(CLSID_FilterGraph, NULL,
  200.                      CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph);
  201.     m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl);
  202.     m_pGraph->QueryInterface(IID_IMediaPosition, (void**)&m_pMediaPosition);
  203.     m_enumFormat = MP3;
  204. }
  205.  
  206.  
  207.  
  208. //------------------------------------------------------------------
  209. // Name: LoadSound()
  210. // Desc: Loadne zvuk
  211. //------------------------------------------------------------------
  212. bool SOUND::LoadSound(char* szSoundFileName)
  213. {
  214.  
  215.     WCHAR wstrSoundPath[MAX_PATH];
  216.  
  217.     //Log Info
  218.     char cBuf[80];
  219.     sprintf(cBuf,"  Zvuk: %s",szSoundFileName);
  220.     LogPrint(cBuf);
  221.  
  222.     switch(m_enumFormat)
  223.     {
  224.  
  225.         //MP3
  226.         //-------------
  227.         case MP3:
  228.             
  229.             //ak je vypnuta hudba - skonci
  230.             if (MusicEnabled == 0)
  231.                 return false;
  232.  
  233.             //Convert the path to unicode.
  234.             MultiByteToWideChar(CP_ACP, 0, szSoundFileName, -1, wstrSoundPath, MAX_PATH);
  235.  
  236.             m_pGraph->RenderFile(wstrSoundPath, NULL);
  237.         break;
  238.  
  239.         //Wav3D
  240.         //-------------
  241.         case WavMidi3D:
  242.  
  243.             //ak je vypnuty zvuk skonci
  244.             if (SoundEnabled == 0)
  245.                 return false;
  246.  
  247.             //load our sound
  248.             ALboolean    loop;
  249.             alutLoadWAVFile(szSoundFileName,&alFormatBuffer, (void **) &alBuffer,(unsigned int *)&alBufferLen, &alFreqBuffer, &loop);
  250.  
  251.             alGenSources(1, &alSource);
  252.             alGenBuffers(1, &alSampleSet);
  253.             alBufferData(alSampleSet, alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);
  254.             alSourcei(alSource, AL_BUFFER, alSampleSet);
  255.  
  256.             alutUnloadWAV(alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);            
  257.  
  258.             //set the pitch
  259.             alSourcef(alSource,AL_PITCH,1.0f);
  260.             //set the gain
  261.             alSourcef(alSource,AL_GAIN,1.0f);
  262.             
  263.             if (alSource == NULL)
  264.                 LogPrint("  chyba pri vytvarani zvuku");
  265.             
  266.         break;
  267.  
  268.         //Wav2D
  269.         //------
  270.         case WavMidi:
  271.  
  272.             
  273.  
  274.         break;
  275.  
  276.         default:
  277.             return false;
  278.     }
  279.  
  280.     return true;
  281. }
  282.  
  283. //------------------------------------------------------------------
  284. // Name: Play()
  285. // Desc: Prehra zvuk - dwNumOfRepeats - pocet prehrani
  286. //------------------------------------------------------------------
  287. bool SOUND::Play(bool Loop)
  288. {
  289.     switch(m_enumFormat)
  290.     {
  291.         //MP3
  292.         //-------------
  293.         case MP3:
  294.             
  295.             //ak je hudba vypnuta
  296.             if (MusicEnabled == 0)
  297.                 return false;
  298.  
  299.             //rewind
  300.             m_pMediaPosition->put_CurrentPosition(0);
  301.             
  302.             //Play mp3
  303.             m_pMediaControl->Run();
  304.         break;
  305.  
  306.         //Wav3D
  307.         //-------------
  308.         case WavMidi3D:
  309.  
  310.             //ak je vypnuty zvuk skonci
  311.             if (SoundEnabled == 0)
  312.                 return false;
  313.  
  314.             //set looping to true
  315.             alSourcei(alSource,AL_LOOPING,Loop);
  316.  
  317.             alSourcePlay(alSource);
  318.         break;
  319.  
  320.         //Wav2D
  321.         //-------------
  322.         case WavMidi:
  323.  
  324.                         
  325.         break;
  326.         default:
  327.             return false;
  328.     }
  329.  
  330.     return true;
  331. }
  332.  
  333. //------------------------------------------------------------------
  334. // Name: Stop()
  335. // Desc: Zastavy zvuk
  336. //------------------------------------------------------------------
  337. bool SOUND::Stop()
  338. {
  339.  
  340.     switch(m_enumFormat)
  341.     {
  342.         //MP3
  343.         //-------------
  344.         case MP3:
  345.             
  346.             //ak je vypnuty zvuk skonci
  347.             if (MusicEnabled == 0)
  348.                 return false;
  349.  
  350.             m_pMediaControl->Stop();
  351.         break;
  352.  
  353.         //Wav3D
  354.         //-------------
  355.         case WavMidi3D:
  356.  
  357.             //ak je vypnuty zvuk skonci
  358.             if (SoundEnabled == 0)
  359.                 return false;
  360.  
  361.             alSourceStop(alSource);
  362.         
  363.         break;
  364.  
  365.         //Wav2D
  366.         //-------------
  367.         case WavMidi:
  368.  
  369.         
  370.         break;
  371.  
  372.         default:
  373.             return false;
  374.     }
  375.  
  376.     return true;
  377. }
  378.  
  379. //------------------------------------------------------------------
  380. // Name: SetPosition()
  381. // Desc: Nastavi poziciu zvuku, zvuk sa prepoΦitava pod╛a pozicie kamery
  382. //------------------------------------------------------------------
  383. void SOUND::SetPosition(VECTOR3D Point,float Scale)
  384. {
  385.     //ak je vypnuty zvuk skonci
  386.     if (SoundEnabled == 0)
  387.         return;
  388.  
  389.     if (SoundEnabled3D == 0)
  390.         return;
  391.  
  392.     if (m_enumFormat != WavMidi3D)
  393.         return;
  394.  
  395.  
  396.     //Camera
  397.     //---------
  398.     alListener3f(AL_POSITION,0.0f,0.0f,0.0f);
  399.  
  400.     float vec[6];
  401.     vec[0] = 0.0f;
  402.     vec[1] = 0.0f;
  403.     vec[2] = 1.0f;
  404.     vec[3] = 0.0f;
  405.     vec[4] = 0.0f;
  406.     vec[5] = 1.0f;
  407.     alListenerfv(AL_ORIENTATION, vec);
  408.  
  409.  
  410.     //Sound
  411.     //----------
  412.     Point = TransformPoint(Point,matView);
  413.  
  414.     alSource3f(alSource,AL_POSITION,Point.X/Scale,-Point.Y/Scale,-Point.Z/Scale);
  415.     alSource3f(alSource,AL_VELOCITY,-20.0f,0.0f,20.0f);
  416.  
  417.  
  418. }
  419.  
  420.  
  421.