OMUSIC.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. // Filename : OMUSIC.CPP
  21. // Description : music
  22. #include <GAMEDEF.h>
  23. #include <OAUDIO.h>
  24. #include <OMUSIC.h>
  25. #include <OSYS.h>
  26. #include <OCONFIG.h>
  27. // -------- define constant --------//
  28. // random select 2 - 8 for background music
  29. #define LOW_RANDOM_SONG 2
  30. #define HIGH_RANDOM_SONG 8
  31. // -------- define song name --------//
  32. #ifdef DEMO
  33. static char *music_file[] =
  34. {
  35. "DEMO.WAV", // opening
  36. "DEMO.WAV",
  37. "DEMO.WAV",
  38. "DEMO.WAV",
  39. "DEMO.WAV",
  40. "DEMO.WAV",
  41. "DEMO.WAV",
  42. "DEMO.WAV",
  43. "DEMO.WAV",
  44. "DEMO.WAV",
  45. };
  46. #else
  47. static char *music_file[] =
  48. {
  49. "WAR.WAV", // opening
  50. "NORMAN.WAV",
  51. "MAYA.WAV",
  52. "GREEK.WAV",
  53. "VIKING.WAV",
  54. "PERSIAN.WAV",
  55. "CHINESE.WAV",
  56. "JAPANESE.WAV",
  57. "WIN.WAV",
  58. "LOSE.WAV",
  59. };
  60. #endif
  61. // -------- begin of function Music::Music ---------//
  62. Music::Music()
  63. {
  64. init_flag = 0;
  65. song_id = 0;
  66. music_channel = 0;
  67. }
  68. // -------- end of function Music::Music ---------//
  69. // -------- begin of function Music::~Music ---------//
  70. Music::~Music()
  71. {
  72. deinit();
  73. }
  74. // -------- end of function Music::~Music ---------//
  75. // -------- begin of function Music::init ---------//
  76. // note : call Music::init after audio.init
  77. void Music::init()
  78. {
  79. deinit();
  80. init_flag = audio.init_flag;
  81. }
  82. // -------- end of function Music::init ---------//
  83. // -------- begin of function Music::deinit ---------//
  84. // call deinit before audio.deinit
  85. void Music::deinit()
  86. {
  87. if( init_flag )
  88. {
  89. stop();
  90. }
  91. }
  92. // -------- end of function Music::deinit ---------//
  93. // -------- begin of function Music::stop ---------//
  94. int Music::stop()
  95. {
  96. if( init_flag )
  97. {
  98. if(music_channel)
  99. {
  100. if( play_type & MUSIC_PLAY_CD )
  101. {
  102. audio.stop_cd();
  103. }
  104. else
  105. {
  106. if( play_type & MUSIC_PLAY_LOOPED )
  107. audio.stop_loop_wav(music_channel);
  108. else
  109. audio.stop_long_wav(music_channel);
  110. }
  111. music_channel = 0;
  112. song_id = 0;
  113. }
  114. return 1;
  115. }
  116. else
  117. {
  118. return -1;
  119. }
  120. }
  121. // -------- end of function Music::stop ---------//
  122. // -------- begin of function Music::play ---------//
  123. // <int> songId
  124. // <int> playType 0 = non-looped, 1 = looped
  125. int Music::play(int songId, int playType)
  126. {
  127. if( !init_flag )
  128. return 0;
  129. stop();
  130. #ifdef BUNDLE
  131. // disable CD music
  132. playType &= ~MUSIC_CD_THEN_WAV & ~MUSIC_PLAY_CD;
  133. #endif
  134. if( playType & MUSIC_CD_THEN_WAV )
  135. {
  136. return play(songId, playType & ~MUSIC_CD_THEN_WAV | MUSIC_PLAY_CD)
  137. || play(songId, playType & ~MUSIC_CD_THEN_WAV & ~MUSIC_PLAY_CD);
  138. }
  139. else if( playType & MUSIC_PLAY_CD )
  140. {
  141. if( audio.cd_init_flag && audio.play_cd(songId +1, config.cd_music_volume) ) // skip the first data track
  142. {
  143. play_type = playType;
  144. song_id = songId;
  145. music_channel = 1;
  146. return 1;
  147. }
  148. return 0;
  149. }
  150. else
  151. {
  152. if( audio.wav_init_flag )
  153. {
  154. String waveFileStr(DIR_MUSIC);
  155. waveFileStr += music_file[songId-1];
  156. if( !DIR_MUSIC[0] || !m.is_file_exist(waveFileStr) || !audio.wav_init_flag )
  157. return 0;
  158. if( playType & MUSIC_PLAY_LOOPED )
  159. {
  160. AbsVolume absv(config.wav_music_volume,0);
  161. music_channel = audio.play_loop_wav(waveFileStr, 0, absv );
  162. }
  163. else
  164. {
  165. AbsVolume absv(config.wav_music_volume,0);
  166. music_channel = audio.play_long_wav(waveFileStr, absv );
  167. }
  168. play_type = playType;
  169. song_id = songId;
  170. return music_channel != 0;
  171. }
  172. return 0;
  173. }
  174. }
  175. // -------- end of function Music::play ---------//
  176. // -------- begin of function Music::is_playing ---------//
  177. // [int] songId id of the song (default=0, don't care)
  178. int Music::is_playing(int songId)
  179. {
  180. if( !init_flag )
  181. return 0;
  182. if( !music_channel )
  183. return 0;
  184. if( play_type & MUSIC_PLAY_CD )
  185. {
  186. return audio.is_cd_playing() && (!songId || songId == song_id);
  187. }
  188. else
  189. {
  190. if( play_type & MUSIC_PLAY_LOOPED )
  191. {
  192. return (!songId || songId == song_id); // loop wav always playing
  193. }
  194. else
  195. {
  196. return audio.is_long_wav_playing(music_channel) && (!songId || songId == song_id);
  197. }
  198. }
  199. return 0;
  200. }
  201. // -------- end of function Music::is_playing ---------//
  202. // -------- begin of function Music::max_song --------//
  203. // return no. of songs
  204. int Music::max_song()
  205. {
  206. return sizeof(music_file) / sizeof(char *);
  207. }
  208. // -------- end of function Music::max_song --------//
  209. // -------- begin of function Music::random_bgm_track --------//
  210. int Music::random_bgm_track(int excludeSong)
  211. {
  212. int s = LOW_RANDOM_SONG + m.get_time() % (HIGH_RANDOM_SONG - LOW_RANDOM_SONG + 1);
  213. if( s == excludeSong )
  214. {
  215. // avoid selecting excludeSong if possible
  216. if( ++s > HIGH_RANDOM_SONG )
  217. s = LOW_RANDOM_SONG;
  218. }
  219. err_when(s < 1 || s > max_song() );
  220. return s;
  221. }
  222. // -------- end of function Music::random_bgm_track --------//
  223. // -------- begin of function Music::change_volume --------//
  224. void Music::change_volume(int vol)
  225. {
  226. if( !init_flag )
  227. return;
  228. if( is_playing() )
  229. {
  230. if( play_type & MUSIC_PLAY_CD )
  231. {
  232. audio.set_cd_volume(vol);
  233. }
  234. else
  235. {
  236. AbsVolume absv(vol,0);
  237. audio.volume_long_wav(music_channel, DsVolume(absv));
  238. }
  239. }
  240. }
  241. //-------- end of function Music::change_volume --------//
  242. //-------- begin of function Music::yield --------//
  243. void Music::yield()
  244. {
  245. if( !init_flag )
  246. return;
  247. int oldSongId = song_id;
  248. if( config.music_flag )
  249. {
  250. if( !is_playing() )
  251. play(random_bgm_track(oldSongId), sys.cdrom_drive ? MUSIC_CD_THEN_WAV : 0 );
  252. }
  253. else
  254. {
  255. stop(); // stop any music playing
  256. }
  257. }
  258. //-------- end of function Music::yield --------//