home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / Sound.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-06  |  5.9 KB  |  225 lines

  1. #include "Sound.h"
  2. #include "log.h"
  3. #include "SDL.h"
  4. //#include "fileio.h"
  5. //#include "camera.h"
  6. #include "Gui.h"
  7. #include "Game.h"
  8.  
  9. #include <math.h>
  10.  
  11. //Mix_Chunk* Sound::samples[SOUND_NUM_SAMPLES];
  12. int Sound::effectChannelsGroup=1;
  13.  
  14.  
  15. bool Sound::initialized=false;
  16.  
  17. bool Sound::init(){
  18.     if(initialized){
  19.         error("(in Sound::init()): Sound is already initialized.\n\n");
  20.         return false;
  21.     }
  22.  
  23.     if(Gui::loadingMenu!=NULL)
  24.         Gui::loadingMenu->updateStatusBar("initializing sound");
  25.  
  26.     log("\n");
  27.     log("**************************\n");
  28.     log("*** Initializing Sound ***\n");
  29.     log("**************************\n");
  30.     log("\n");
  31.  
  32.     info.cvar.sound_enabled->updateVar();
  33.     info.cvar.sound_stereo->updateVar();
  34.     info.cvar.sound_sampleVolume->updateVar();
  35.     info.cvar.sound_numEffectChannels->updateVar();
  36.     info.cvar.sound_sampleRate->updateVar();
  37.     info.cvar.sound_chunkSize->updateVar();
  38.  
  39.     if(info.var.enabled==false){
  40.         log("sound.enabled is set to false -> skipping sound initialization.\n");
  41.         return true;
  42.     }
  43.  
  44. //    for(int i=0;i<SOUND_NUM_SAMPLES;i++){
  45. //        samples[i]=NULL;
  46. //    }
  47.  
  48.     const SDL_version* link_version=Mix_Linked_Version();
  49.     log("Running with SDL_mixer version %d.%d.%d.\n", link_version->major, link_version->minor, link_version->patch);
  50.     // start SDL with audio support
  51.     log("Starting SDL audio subsystem...\n");
  52.     if(SDL_InitSubSystem(SDL_INIT_AUDIO)==-1) {
  53.         error("(in Sound::init()): SDL_Init: %s. -> Sound disabled.\n", SDL_GetError());
  54.         info.var.enabled=false;
  55.         return false;
  56.     }
  57.  
  58.     log("Opening audio device for 16 bit %s sound at %.2fkHz (chunkSize: %i)...\n"
  59.         , info.var.stereo ? "stereo" : "mono", info.var.sampleRate/(float)1000, info.var.chunkSize );
  60.     if(Mix_OpenAudio(info.var.sampleRate, MIX_DEFAULT_FORMAT, info.var.stereo ? 2 : 1, info.var.chunkSize)==-1) {
  61.         error("(in Sound::init()): Mix_OpenAudio: %s. -> Sound disabled.\n", Mix_GetError());
  62.         info.var.enabled=false;
  63.         return false;
  64.     }
  65.  
  66.     log("Allocating %i mixer channels (%i internal and %i for effects)...\n"
  67.         , SOUND_FIRST_EFFECT_CHANNEL+info.var.numEffectChannels, SOUND_FIRST_EFFECT_CHANNEL, info.var.numEffectChannels);
  68.     Mix_AllocateChannels(info.var.numEffectChannels);
  69.     Mix_Volume(-1, info.var.sampleVolume);
  70.  
  71.     Mix_GroupChannels(SOUND_FIRST_EFFECT_CHANNEL, SOUND_FIRST_EFFECT_CHANNEL+info.var.numEffectChannels, effectChannelsGroup);
  72.  
  73. //    log("Loading samples...\n");
  74. //    if(!loadSamples()){
  75. //        return false;
  76. //    }
  77.     
  78.     initialized=true;
  79.  
  80.     return true;
  81. }
  82.  
  83. bool Sound::shutdown(){
  84.     if(info.var.enabled==false){
  85.         log("sound.enabled is set to false -> skipping sound shutdown.\n");
  86.         return true;
  87.     }
  88.  
  89.     if(!initialized){
  90.         error("(in Sound::shutdown()): Sound is not initialized.\n\n");
  91.         return false;
  92.     }
  93.  
  94.     log("\n");
  95.     log("===========================\n");
  96.     log("=== Shutting down Sound ===\n");
  97.     log("===========================\n");
  98.     log("\n");
  99.  
  100.     log("Closing audio channels...\n");
  101.     Mix_CloseAudio();    // FIXME: Probleme unter linux, wenn andere programme sound abspielen!!
  102.     log("Closing SDL audio subsystem...\n");
  103.     SDL_QuitSubSystem(SDL_INIT_AUDIO);
  104.  
  105. //    log("Freeing samples...\n");
  106. //    freeSamples();
  107.     
  108.     initialized = false;
  109.  
  110.     return true;
  111. }
  112.  
  113. bool Sound::wasInit(){
  114.     return initialized;
  115. }
  116.  
  117. bool Sound::registerCVarsAndCCmds(){
  118.     return info.registerCVarsAndCCmds();
  119. }
  120.  
  121. bool Sound::unregisterCVarsAndCCmds(){
  122.     return info.unregisterCVarsAndCCmds();
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. Mix_Chunk* Sound::loadWAV(const char* filename){
  130.     if( !info.var.enabled ){
  131.         return NULL;
  132.     }
  133.  
  134.     if(!File::exists(filename)){
  135.         error("(in loadWAV()): file '%s' not found.\n", filename);
  136.         return NULL;
  137.     }
  138.     Mix_Chunk *sample=Mix_LoadWAV(filename);
  139.     if(!sample) {
  140.         error("(in loadWAV()): Mix_LoadWAV(\"%s\") failed: %s.\n", filename, Mix_GetError());
  141.         return NULL;
  142.     }
  143.  
  144.     log("Sample '%s' loaded.\n", filename);
  145.     return sample;
  146. }
  147.  
  148.  
  149.  
  150. void Sound::playSample(int channel, Mix_Chunk* chunk, int numLoops/*=0*/){
  151.     Mix_PlayChannel(channel, chunk, numLoops);
  152. }
  153.  
  154.  
  155. void Sound::playEffect(vec3_t pos, Mix_Chunk* chunk, int numLoops/*=0*/){
  156.     int channel=aquireEffectChannel();
  157.     if(channel==-1)
  158.         return;
  159.  
  160.     //do pos    // FIXME: ordentlich machen!!
  161.     vec3_t toPos;
  162.     vectorSub3d(pos, Game::cam.pos, toPos);
  163.     vec2_t a, b;
  164.     float al=(float)sqrt(Game::cam.dir[0]*Game::cam.dir[0]+Game::cam.dir[2]*Game::cam.dir[2]);
  165.     float bl=(float)sqrt(toPos[0]*toPos[0]+toPos[2]*toPos[2]);
  166.     vectorInit2d(Game::cam.dir[0]/al, Game::cam.dir[2]/al, a);
  167.     vectorInit2d(toPos[0]/bl, toPos[2]/bl, b);
  168.     float rad=(float)acos(a[0]*b[0]+a[1]*b[1]);
  169.  
  170.     int angle;
  171.     vec3_t c;
  172.     vectorCrossP3d(toPos, Game::cam.dir, c);    // test if pos is right or left
  173.     if(vectorDotP3d(Game::cam.up, c)>0.0f){
  174.         angle=(int)(DEG(rad));
  175.     }else{
  176.         angle=360 - (int)(DEG(rad));
  177.     }
  178.  
  179.     int distance = (int)vectorLength3d(toPos) * 7;
  180.     distance = distance > 255 ? 255 : distance;
  181.  
  182.     if(!Mix_SetPosition(channel, angle, distance)){
  183.         warn("(in sound::playEffect()): couldn't register pos-effect.\n\n");
  184.     }
  185.     //log("playEffect(): chan: %i, dist: %i, angle: %i\n.", channel, distance, angle);
  186.  
  187.     Mix_PlayChannel(channel, chunk, numLoops);
  188. }
  189.  
  190. int Sound::aquireEffectChannel(){
  191.     int channel;
  192.     channel=Mix_GroupAvailable(effectChannelsGroup);
  193.     if (channel==-1) {
  194.         channel=Mix_GroupAvailable(effectChannelsGroup);
  195.         if(channel==-1){
  196. //            warn("(in aquireEffectChannel()): couldn't aquire ANY effect channel. sound.numEffectChannels set to 0?\n");
  197.         }
  198.     }
  199.     return channel;
  200. }
  201.  
  202.  
  203.  
  204.  
  205. Mix_Music* Sound::loadMusic(const char* filename){
  206.     Mix_Music *ret;
  207.     ret=Mix_LoadMUS(filename);
  208.     if(ret==NULL) {
  209.         error("(in Sound::loadMusic()): Mix_LoadMUS(\"%s\") failed: %s.\n\n", filename, Mix_GetError());
  210.     }else{
  211.         log("Music '%s' loaded.\n", filename);
  212.     }
  213.     return ret;
  214. }
  215.  
  216. void Sound::playMusic(Mix_Music *music, int numLoops){
  217.     if(Mix_PlayMusic(music, numLoops)==-1) {
  218.         warn("(in sound::playMusic()): Mix_PlayMusic: %s\n\n", Mix_GetError());
  219.     }
  220. }
  221.  
  222. void Sound::stopMusic(){
  223.     Mix_HaltMusic();
  224. }
  225.