home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / gamesystem / source / gs_soundsystem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  4.7 KB  |  234 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    gsCSoundSystem
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    12/03/00
  8. //
  9. // Base:    gsCObject
  10. //
  11. // Derived:    None
  12. //
  13. //-------------------------------------------------------------
  14.  
  15. #include "gamesystem.h"
  16.  
  17. //-------------------------------------------------------------
  18.  
  19. gsCSoundSystem::gsCSoundSystem()
  20. {
  21.     m_active = false;
  22.     m_current_music = 0;
  23. }
  24.  
  25. //-------------------------------------------------------------
  26.  
  27. gsCSoundSystem::~gsCSoundSystem()
  28. {
  29. }
  30.  
  31. //-------------------------------------------------------------
  32.  
  33. bool gsCSoundSystem::create()
  34. {
  35.     if (!m_active) {
  36.         if (BASS_GetVersion() != (MAKELONG(0,8))) {
  37.             gsERROR("Wrong version of BASSMOD");
  38.             return false;
  39.             }
  40.  
  41.         if (!BASS_Init(-1,44100,BASS_DEVICE_NOSYNC,gsCApplication::getWindow())) {
  42.             gsERROR("gsCSound::create can't initialize digital sound system");
  43.             return false;
  44.             }
  45.  
  46.         BASS_Start();
  47.  
  48.         gsREPORT("gsCSoundSystem created");
  49.  
  50.         m_active = true;
  51.         }
  52.  
  53.     return true;
  54. }
  55.  
  56. //-------------------------------------------------------------
  57.  
  58. bool gsCSoundSystem::destroy()
  59. {
  60.     if (m_active) {
  61.         stopMusic();
  62.         stopSamples();
  63.  
  64.         BASS_Stop();
  65.  
  66.         clearMusicList();
  67.         clearSampleList();
  68.  
  69.         BASS_Free();
  70.  
  71.         gsREPORT("gsCSoundSystem destroyed");
  72.  
  73.         m_active = false;
  74.         }
  75.  
  76.     return true;
  77. }
  78.  
  79. //-------------------------------------------------------------
  80.  
  81. void gsCSoundSystem::clearMusicList()
  82. {
  83.     stopMusic();
  84.  
  85.     for (int i = 0; i < m_music_list.getSize(); i++)
  86.         delete m_music_list[i];
  87.     
  88.     m_music_list.clear();
  89. }
  90.  
  91. //-------------------------------------------------------------
  92.  
  93. int gsCSoundSystem::getNumberOfMusics()
  94. {
  95.     return m_music_list.getSize();
  96. }
  97.  
  98. //-------------------------------------------------------------
  99.  
  100. bool gsCSoundSystem::addMusic(const char *filename)
  101. {
  102.     if (m_active) {
  103.         gsCMusic *music = new gsCMusic;
  104.         if (!music->load(filename)) {
  105.             delete music;
  106.             return false;
  107.             }
  108.         m_music_list.addItem(music);
  109.         return true;
  110.         }
  111.  
  112.     return false;
  113. }
  114.  
  115. //-------------------------------------------------------------
  116.  
  117. bool gsCSoundSystem::playMusic(int index)
  118. {
  119.     if (m_active) {
  120.         stopMusic();
  121.         if (index >= 0 && index < m_music_list.getSize()) {
  122.             gsCMusic *music = m_music_list[index];
  123.             if (music && BASS_StreamPlay(music->getHandle(),TRUE,0)) {
  124.                 m_current_music = music;
  125.                 return true;
  126.                 }
  127.             }
  128.         }
  129.  
  130.     return false;
  131. }
  132.  
  133. //-------------------------------------------------------------
  134.  
  135. void gsCSoundSystem::stopMusic()
  136. {
  137.     if (m_active && m_current_music) {
  138.         BASS_ChannelStop(m_current_music->getHandle());
  139.         m_current_music = 0;
  140.         }
  141. }
  142.  
  143. //-------------------------------------------------------------
  144. // Note: if music turned off this returns false
  145.  
  146. bool gsCSoundSystem::isMusicFinished()
  147. {
  148.     if (m_active)
  149.         return m_current_music && !BASS_ChannelIsActive(m_current_music->getHandle());
  150.     else
  151.         return false;
  152. }
  153.  
  154. //-------------------------------------------------------------
  155.  
  156. void gsCSoundSystem::clearSampleList()
  157. {
  158.     stopSamples();
  159.  
  160.     for (int i = 0; i < m_sample_list.getSize(); i++)
  161.         delete m_sample_list[i];
  162.     
  163.     m_sample_list.clear();
  164. }
  165.  
  166. //-------------------------------------------------------------
  167.  
  168. int gsCSoundSystem::getNumberOfSamples()
  169. {
  170.     return m_sample_list.getSize();
  171. }
  172.  
  173. //-------------------------------------------------------------
  174.  
  175. bool gsCSoundSystem::addSample(const char *filename)
  176. {
  177.     if (m_active) {
  178.         gsCSample *sample = new gsCSample;
  179.         if (!sample->load(filename)) {
  180.             delete sample;
  181.             return false;
  182.             }
  183.         m_sample_list.addItem(sample);
  184.         return true;
  185.         }
  186.  
  187.     return false;
  188. }
  189.  
  190. //-------------------------------------------------------------
  191.  
  192. bool gsCSoundSystem::playSample(int index,int panning)
  193. {
  194.     if (m_active) {
  195.         if (index >= 0 && index < m_sample_list.getSize()) {
  196.             if (panning < -100)
  197.                 panning = -100;
  198.             if (panning > 100)
  199.                 panning = 100;
  200.  
  201.             gsCSample *sample = m_sample_list[index];
  202.             if (sample && BASS_SamplePlayEx(sample->getHandle(),0,-1,50,panning,false))
  203.                 return true;
  204.             }
  205.         }
  206.  
  207.     return false;
  208. }
  209.  
  210. //-------------------------------------------------------------
  211.  
  212. void gsCSoundSystem::stopSamples()
  213. {
  214.     for (int i = 0; i < m_sample_list.getSize(); i++)
  215.         BASS_SampleStop(m_sample_list[i]->getHandle());
  216. }
  217.  
  218. //-------------------------------------------------------------
  219.  
  220. bool gsCSoundSystem::isActive()
  221. {
  222.     return m_active;
  223. }
  224.  
  225. //-------------------------------------------------------------
  226.  
  227. void gsCSoundSystem::setVolume(int music_percent,int sample_percent)
  228. {
  229.     if (m_active)
  230.         BASS_SetGlobalVolumes(0,sample_percent,music_percent);
  231. }
  232.  
  233. //-------------------------------------------------------------
  234.