home *** CD-ROM | disk | FTP | other *** search
- #include "Sound.h"
- #include "log.h"
- #include "SDL.h"
- //#include "fileio.h"
- //#include "camera.h"
- #include "Gui.h"
- #include "Game.h"
-
- #include <math.h>
-
- //Mix_Chunk* Sound::samples[SOUND_NUM_SAMPLES];
- int Sound::effectChannelsGroup=1;
-
-
- bool Sound::initialized=false;
-
- bool Sound::init(){
- if(initialized){
- error("(in Sound::init()): Sound is already initialized.\n\n");
- return false;
- }
-
- if(Gui::loadingMenu!=NULL)
- Gui::loadingMenu->updateStatusBar("initializing sound");
-
- log("\n");
- log("**************************\n");
- log("*** Initializing Sound ***\n");
- log("**************************\n");
- log("\n");
-
- info.cvar.sound_enabled->updateVar();
- info.cvar.sound_stereo->updateVar();
- info.cvar.sound_sampleVolume->updateVar();
- info.cvar.sound_numEffectChannels->updateVar();
- info.cvar.sound_sampleRate->updateVar();
- info.cvar.sound_chunkSize->updateVar();
-
- if(info.var.enabled==false){
- log("sound.enabled is set to false -> skipping sound initialization.\n");
- return true;
- }
-
- // for(int i=0;i<SOUND_NUM_SAMPLES;i++){
- // samples[i]=NULL;
- // }
-
- const SDL_version* link_version=Mix_Linked_Version();
- log("Running with SDL_mixer version %d.%d.%d.\n", link_version->major, link_version->minor, link_version->patch);
- // start SDL with audio support
- log("Starting SDL audio subsystem...\n");
- if(SDL_InitSubSystem(SDL_INIT_AUDIO)==-1) {
- error("(in Sound::init()): SDL_Init: %s. -> Sound disabled.\n", SDL_GetError());
- info.var.enabled=false;
- return false;
- }
-
- log("Opening audio device for 16 bit %s sound at %.2fkHz (chunkSize: %i)...\n"
- , info.var.stereo ? "stereo" : "mono", info.var.sampleRate/(float)1000, info.var.chunkSize );
- if(Mix_OpenAudio(info.var.sampleRate, MIX_DEFAULT_FORMAT, info.var.stereo ? 2 : 1, info.var.chunkSize)==-1) {
- error("(in Sound::init()): Mix_OpenAudio: %s. -> Sound disabled.\n", Mix_GetError());
- info.var.enabled=false;
- return false;
- }
-
- log("Allocating %i mixer channels (%i internal and %i for effects)...\n"
- , SOUND_FIRST_EFFECT_CHANNEL+info.var.numEffectChannels, SOUND_FIRST_EFFECT_CHANNEL, info.var.numEffectChannels);
- Mix_AllocateChannels(info.var.numEffectChannels);
- Mix_Volume(-1, info.var.sampleVolume);
-
- Mix_GroupChannels(SOUND_FIRST_EFFECT_CHANNEL, SOUND_FIRST_EFFECT_CHANNEL+info.var.numEffectChannels, effectChannelsGroup);
-
- // log("Loading samples...\n");
- // if(!loadSamples()){
- // return false;
- // }
-
- initialized=true;
-
- return true;
- }
-
- bool Sound::shutdown(){
- if(info.var.enabled==false){
- log("sound.enabled is set to false -> skipping sound shutdown.\n");
- return true;
- }
-
- if(!initialized){
- error("(in Sound::shutdown()): Sound is not initialized.\n\n");
- return false;
- }
-
- log("\n");
- log("===========================\n");
- log("=== Shutting down Sound ===\n");
- log("===========================\n");
- log("\n");
-
- log("Closing audio channels...\n");
- Mix_CloseAudio(); // FIXME: Probleme unter linux, wenn andere programme sound abspielen!!
- log("Closing SDL audio subsystem...\n");
- SDL_QuitSubSystem(SDL_INIT_AUDIO);
-
- // log("Freeing samples...\n");
- // freeSamples();
-
- initialized = false;
-
- return true;
- }
-
- bool Sound::wasInit(){
- return initialized;
- }
-
- bool Sound::registerCVarsAndCCmds(){
- return info.registerCVarsAndCCmds();
- }
-
- bool Sound::unregisterCVarsAndCCmds(){
- return info.unregisterCVarsAndCCmds();
- }
-
-
-
-
-
- Mix_Chunk* Sound::loadWAV(const char* filename){
- if( !info.var.enabled ){
- return NULL;
- }
-
- if(!File::exists(filename)){
- error("(in loadWAV()): file '%s' not found.\n", filename);
- return NULL;
- }
- Mix_Chunk *sample=Mix_LoadWAV(filename);
- if(!sample) {
- error("(in loadWAV()): Mix_LoadWAV(\"%s\") failed: %s.\n", filename, Mix_GetError());
- return NULL;
- }
-
- log("Sample '%s' loaded.\n", filename);
- return sample;
- }
-
-
-
- void Sound::playSample(int channel, Mix_Chunk* chunk, int numLoops/*=0*/){
- Mix_PlayChannel(channel, chunk, numLoops);
- }
-
-
- void Sound::playEffect(vec3_t pos, Mix_Chunk* chunk, int numLoops/*=0*/){
- int channel=aquireEffectChannel();
- if(channel==-1)
- return;
-
- //do pos // FIXME: ordentlich machen!!
- vec3_t toPos;
- vectorSub3d(pos, Game::cam.pos, toPos);
- vec2_t a, b;
- float al=(float)sqrt(Game::cam.dir[0]*Game::cam.dir[0]+Game::cam.dir[2]*Game::cam.dir[2]);
- float bl=(float)sqrt(toPos[0]*toPos[0]+toPos[2]*toPos[2]);
- vectorInit2d(Game::cam.dir[0]/al, Game::cam.dir[2]/al, a);
- vectorInit2d(toPos[0]/bl, toPos[2]/bl, b);
- float rad=(float)acos(a[0]*b[0]+a[1]*b[1]);
-
- int angle;
- vec3_t c;
- vectorCrossP3d(toPos, Game::cam.dir, c); // test if pos is right or left
- if(vectorDotP3d(Game::cam.up, c)>0.0f){
- angle=(int)(DEG(rad));
- }else{
- angle=360 - (int)(DEG(rad));
- }
-
- int distance = (int)vectorLength3d(toPos) * 7;
- distance = distance > 255 ? 255 : distance;
-
- if(!Mix_SetPosition(channel, angle, distance)){
- warn("(in sound::playEffect()): couldn't register pos-effect.\n\n");
- }
- //log("playEffect(): chan: %i, dist: %i, angle: %i\n.", channel, distance, angle);
-
- Mix_PlayChannel(channel, chunk, numLoops);
- }
-
- int Sound::aquireEffectChannel(){
- int channel;
- channel=Mix_GroupAvailable(effectChannelsGroup);
- if (channel==-1) {
- channel=Mix_GroupAvailable(effectChannelsGroup);
- if(channel==-1){
- // warn("(in aquireEffectChannel()): couldn't aquire ANY effect channel. sound.numEffectChannels set to 0?\n");
- }
- }
- return channel;
- }
-
-
-
-
- Mix_Music* Sound::loadMusic(const char* filename){
- Mix_Music *ret;
- ret=Mix_LoadMUS(filename);
- if(ret==NULL) {
- error("(in Sound::loadMusic()): Mix_LoadMUS(\"%s\") failed: %s.\n\n", filename, Mix_GetError());
- }else{
- log("Music '%s' loaded.\n", filename);
- }
- return ret;
- }
-
- void Sound::playMusic(Mix_Music *music, int numLoops){
- if(Mix_PlayMusic(music, numLoops)==-1) {
- warn("(in sound::playMusic()): Mix_PlayMusic: %s\n\n", Mix_GetError());
- }
- }
-
- void Sound::stopMusic(){
- Mix_HaltMusic();
- }
-