summaryrefslogtreecommitdiffstats
path: root/src/audio/AudioScriptObject.cpp
blob: 03efdea9f1b0060b9e24a349c43623d2ede51c89 (plain) (blame)
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
99
100
101
#include "common.h"

#include "AudioScriptObject.h"
#include "Pools.h"
#include "DMAudio.h"
#include "SaveBuf.h"

cAudioScriptObject::cAudioScriptObject()
{
	Reset();
};

cAudioScriptObject::~cAudioScriptObject()
{
	Reset();
};

void
cAudioScriptObject::Reset()
{
	AudioId = SCRIPT_SOUND_INVALID;
	Posn = CVector(0.0f, 0.0f, 0.0f);
	AudioEntity = AEHANDLE_NONE;
}

void *
cAudioScriptObject::operator new(size_t sz) throw()
{
	return CPools::GetAudioScriptObjectPool()->New();
}

void *
cAudioScriptObject::operator new(size_t sz, int handle) throw()
{
	return CPools::GetAudioScriptObjectPool()->New(handle);
}

void
cAudioScriptObject::operator delete(void *p, size_t sz) throw()
{
	CPools::GetAudioScriptObjectPool()->Delete((cAudioScriptObject *)p);
}

void
cAudioScriptObject::operator delete(void *p, int handle) throw()
{
	CPools::GetAudioScriptObjectPool()->Delete((cAudioScriptObject *)p);
}

void
cAudioScriptObject::LoadAllAudioScriptObjects(uint8 *buf, uint32 size)
{
	INITSAVEBUF

	CheckSaveHeader(buf, 'A', 'U', 'D', '\0', size - SAVE_HEADER_SIZE);

	int32 pool_size;
	ReadSaveBuf(&pool_size, buf);
	for (int32 i = 0; i < pool_size; i++) {
		int32 handle;
		ReadSaveBuf(&handle, buf);
		cAudioScriptObject *p = new(handle) cAudioScriptObject;
		assert(p != nil);
		ReadSaveBuf(p, buf);
		p->AudioEntity = DMAudio.CreateLoopingScriptObject(p);
	}

	VALIDATESAVEBUF(size);
}

void
cAudioScriptObject::SaveAllAudioScriptObjects(uint8 *buf, uint32 *size)
{
	INITSAVEBUF

	int32 pool_size = CPools::GetAudioScriptObjectPool()->GetNoOfUsedSpaces();
	*size = SAVE_HEADER_SIZE + sizeof(int32) + pool_size * (sizeof(cAudioScriptObject) + sizeof(int32));
	WriteSaveHeader(buf, 'A', 'U', 'D', '\0', *size - SAVE_HEADER_SIZE);
	WriteSaveBuf(buf, pool_size);

	int32 i = CPools::GetAudioScriptObjectPool()->GetSize();
	while (i--) {
		cAudioScriptObject *p = CPools::GetAudioScriptObjectPool()->GetSlot(i);
		if (p != nil) {
			WriteSaveBuf(buf, CPools::GetAudioScriptObjectPool()->GetIndex(p));
			WriteSaveBuf(buf, *p);
		}
	}

	VALIDATESAVEBUF(*size);
}

void
PlayOneShotScriptObject(uint8 id, CVector const &pos)
{
	cAudioScriptObject *audioScriptObject = new cAudioScriptObject();
	audioScriptObject->Posn = pos;
	audioScriptObject->AudioId = id;
	audioScriptObject->AudioEntity = AEHANDLE_NONE;
	DMAudio.CreateOneShotScriptObject(audioScriptObject);
}