sound.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*!
  2. Sound Factory.
  3. provides a sound interface
  4. */
  5. #include "sound.h"
  6. //#define USE_IRRKLANG
  7. #ifdef USE_IRRKLANG
  8. #include <irrKlang.h>
  9. #ifdef _MSC_VER
  10. #pragma comment (lib, "irrKlang.lib")
  11. #endif
  12. using namespace irrklang;
  13. struct soundfile: public IFileReader
  14. {
  15. soundfile ( io::IReadFile* f ): file (f ) {}
  16. virtual ~soundfile () { file->drop (); }
  17. virtual ik_s32 read(void* buffer, ik_u32 sizeToRead) { return file->read ( buffer, sizeToRead ); }
  18. virtual bool seek(ik_s32 finalPos, bool relativeMovement = false) { return file->seek ( finalPos, relativeMovement ); }
  19. virtual ik_s32 getSize(){ return file->getSize (); }
  20. virtual ik_s32 getPos() {return file->getPos (); }
  21. virtual const ik_c8* getFileName() { return file->getFileName().c_str(); }
  22. io::IReadFile* file;
  23. };
  24. struct klangFactory : public irrklang::IFileFactory
  25. {
  26. klangFactory ( IrrlichtDevice *device ) { Device = device; }
  27. virtual irrklang::IFileReader* createFileReader(const ik_c8* filename)
  28. {
  29. io::IReadFile* file = Device->getFileSystem()->createAndOpenFile(filename);
  30. if ( 0 == file )
  31. return 0;
  32. return new soundfile ( file );
  33. }
  34. IrrlichtDevice *Device;
  35. };
  36. ISoundEngine *engine = 0;
  37. ISound *backMusic = 0;
  38. void sound_init ( IrrlichtDevice *device )
  39. {
  40. engine = createIrrKlangDevice ();
  41. if ( 0 == engine )
  42. return;
  43. klangFactory *f = new klangFactory ( device );
  44. engine->addFileFactory ( f );
  45. }
  46. void sound_shutdown ()
  47. {
  48. if ( backMusic )
  49. backMusic->drop ();
  50. if ( engine )
  51. engine->drop ();
  52. }
  53. void background_music ( const c8 * file )
  54. {
  55. if ( 0 == engine )
  56. return;
  57. if ( backMusic )
  58. {
  59. backMusic->stop ();
  60. backMusic->drop ();
  61. }
  62. backMusic = engine->play2D ( file, true, false, true );
  63. if ( backMusic )
  64. {
  65. backMusic->setVolume ( 0.5f );
  66. }
  67. }
  68. #else
  69. void sound_init ( IrrlichtDevice *device ) {}
  70. void sound_shutdown () {}
  71. void background_music ( const c8 * file ) {}
  72. #endif