aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8.1/examples/21.Quake3Explorer/sound.cpp
blob: 75615c1dba868662851f14ad784bbe7673effdff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*!
	Sound Factory.
	provides a sound interface

*/

#include "sound.h"


//#define USE_IRRKLANG

#ifdef USE_IRRKLANG

#include <irrKlang.h>
#ifdef _IRR_WINDOWS_
	#pragma comment (lib, "irrKlang.lib")
#endif

using namespace irrklang;

struct soundfile: public IFileReader
{
	soundfile ( io::IReadFile* f ): file (f ) {}
	virtual ~soundfile () { file->drop (); }
	
	virtual ik_s32 read(void* buffer, ik_u32 sizeToRead) { return file->read ( buffer, sizeToRead ); }
	virtual bool seek(ik_s32 finalPos, bool relativeMovement = false) { return file->seek ( finalPos, relativeMovement ); }
	virtual ik_s32 getSize(){ return file->getSize (); }
	virtual ik_s32 getPos()	{return file->getPos (); }
	virtual const ik_c8* getFileName() { return file->getFileName (); }
	io::IReadFile* file;
};

struct klangFactory : public irrklang::IFileFactory
{
	klangFactory ( IrrlichtDevice *device )	{ Device = device; }

	virtual irrklang::IFileReader* createFileReader(const ik_c8* filename)
	{
		io::IReadFile* file = Device->getFileSystem()->createAndOpenFile(filename);
		if ( 0 == file )
			return 0;

		return new soundfile ( file );
	}

	IrrlichtDevice *Device;
};
 
ISoundEngine *engine = 0;
ISound *backMusic = 0;

void sound_init ( IrrlichtDevice *device )
{
	engine = createIrrKlangDevice ();
	if ( 0 == engine )
		return;

	klangFactory *f = new klangFactory ( device );
	engine->addFileFactory ( f );
}

void sound_shutdown ()
{
	if ( backMusic )
		backMusic->drop ();

	if ( engine )
		engine->drop ();
}

void background_music ( const c8 * file )
{
	if ( 0 == engine )
		return;

	if ( backMusic )
	{
		backMusic->stop ();
		backMusic->drop ();
	}

	backMusic = engine->play2D ( file, true, false, true );

	if ( backMusic )
	{
		backMusic->setVolume ( 0.5f );
	}
}

#else

void sound_init ( IrrlichtDevice *device ) {}
void sound_shutdown () {}
void background_music ( const c8 * file ) {}

#endif