Planeshift

data.h

Go to the documentation of this file.
00001 /*
00002  * data.h
00003  *
00004  * Copyright (C) 2001-2010 Atomic Blue ([email protected], http://www.planeshift.it)
00005  *
00006  * Credits : Saul Leite <[email protected]>
00007  *           Mathias 'AgY' Voeroes <[email protected]>
00008  *           and all past and present planeshift coders
00009  *
00010  * This program is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU General Public License
00012  * as published by the Free Software Foundation (version 2 of the License.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  */
00023 
00024 #ifndef _SOUND_DATA_H_
00025 #define _SOUND_DATA_H_
00026 
00027 /*
00028  * i still have to make this doxygen compatible .. please dont mock me
00029  * but i need to get this story out of my head and into this file
00030  *
00031  * How SoundData works: (Birth till Death)
00032  *
00033  * Birth:
00034  *
00035  * On Initialization it aquires references to vfs and a loader.
00036  * It returns false if it fails and no further checking is done.
00037  *
00038  * On succes the very first thing that may happen is that LoadSoundLib is called.
00039  * It fills libsoundfiles with the data it found in soundlib.xml
00040  * Even if not called we can still process requests for filenames within our vfs.
00041  *
00042  * We assume LoadSoundLib has been called and the library is filled.
00043  *
00044  * Now Someone calls LoadSoundFile to load a named resource or file
00045  * the one that calls expects a pointer to the SoundFile he requested
00046  * depending on how successfull this is LoadSoundFile will fill it in and return
00047  * true or false.
00048  *
00049  * GetSound is called and GetSound will search if theres a cached
00050  * SoundFile or if theres a unloaded resource in our soundlib.
00051  * if its cached then it will return that one. Thats best case
00052  * because LoadSoundFile can return a pointer to the cached data.
00053  * If its in our soundlib, then GetSound will make a copy and put that into our cache
00054  * (using PutSound) on succes it will return a Pointer to a valid SoundFile
00055  * on failure it will return NULL.
00056  *
00057  * GetSound may return NULL. In that case LoadSoundfile creates a new SoundFile
00058  * (object) with the given name as filename to check if we got a (dynamic)
00059  * file as name.
00060  * That happens if we are playing voicefiles. Those are downloaded from the server
00061  * and thus do not exist in our libraray. PutSound is used to add it to the cache.
00062  *
00063  * However theres always a valid SoundFile (object) for our loader
00064  * Now LoadSoundFile tries to load that file (finally).
00065  * There are two cases:
00066  * 1) file exists and is loadable (succes .. return true)
00067  * 2) file doesnt exist or ISNT loadable (failure .. return false)
00068  *
00069  * case 1) means that loaded (a bool) is set to true and snddata valid
00070  * case 2) loaded remains false and snddata is invalid / NULL
00071  *
00072  * LoadSoundFile has returned and the caller
00073  * might now do whatever he wanted todo with that snddata.
00074  *
00075  * Death:
00076  *
00077  * SoundData has a Update method that checks if there are expired SoundFiles
00078  * theres a hardcoded caching time of 300 seconds. After 300 seconds it will
00079  * check if there are still references on the snddata our SoundFile provides.
00080  * If theres only one then its the SoundFile object itself.
00081  * That means we go ahead and delete that object using DeleteSound.
00082  *
00083  * Now sometimes it isnt that easy ;) Maybe thats a looping background sound
00084  * in that case our RefCount is at last higher then one.
00085  * We set lasttouch to current ticks .. and check again .. in 300 seconds ;)
00086  *
00087  * Anyway UnloadSound is a public method and thus someone may call it for any
00088  * reason. Be aware!
00089  * It will crash your program if you unload data which is still in use ;)
00090  */
00091 
00092 // FIXME i should be an option ;)
00093 #define SOUNDFILE_CACHETIME     300000  ///<- number of milliseconds a file remains cached
00094 
00095 #include <cssysdef.h>
00096 #include <iutil/objreg.h>
00097 #include <csutil/csstring.h>
00098 #include <csutil/hash.h>
00099 #include <iutil/vfs.h>
00100 #include <isndsys/ss_data.h>
00101 #include <isndsys/ss_loader.h>
00102 
00109 class SoundFile
00110 {
00111     public:
00112     csString            name;           
00113     csString            filename;       
00114     csRef<iSndSysData>  snddata;        
00115     bool                loaded;         
00116     csTicks             lasttouch;      
00117 
00123     SoundFile(const char *newname, const char *newfilename);
00128     SoundFile (SoundFile* const &copythat);
00132     ~SoundFile();
00133 };
00134 
00141 class SoundData
00142 {
00143     public:
00144 
00150     SoundData ();
00155     ~SoundData ();
00156 
00162     bool Initialize(iObjectRegistry* objectReg);
00170     bool LoadSoundLib (const char* filename, iObjectRegistry* objectReg);
00175     void UnloadSoundLib ();
00176 
00183     bool LoadSoundFile (const char *name, csRef<iSndSysData> &snddata);
00190     void UnloadSoundFile (const char *name);
00197     void Update ();
00198 
00199     private:
00200     csRef<iSndSysLoader> sndloader;         
00201     csHash<SoundFile *> soundfiles;         
00202     csHash<SoundFile *> libsoundfiles;      
00203     csRef<iVFS> vfs;                        
00204     
00209     SoundFile *GetSound (const char *name);
00213     void PutSound (SoundFile* &sound);
00217     void DeleteSound (SoundFile* &sound);
00218 };
00219 
00220 #endif /*_SOUND_DATA_H_*/