Subversion Repositories spk

Rev

Rev 79 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 cycrow 1
#ifndef __OVERLAY_H__
2
#define __OVERLAY_H__
3
 
4
class MyDirect3DDevice9;
5
 
6
#define DIRECTINPUT_VERSION 0x08000
7
#include <d3dx9.h>
8
#include <mmsystem.h>
9
#include <dinput.h>
10
 
11
#define TYPERATE 200
12
#define KEYDOWN(name,key) (name[key] & 0x80)
13
 
14
#include <spk.h>
15
#include <stdio.h>
16
#include <fmod.hpp>
17
 
18
typedef struct SStoredSound {
19
	struct SStoredSound () { pSound = NULL; pChannel = NULL; }
20
	FMOD::Sound *pSound;
21
	FMOD::Channel *pChannel;
22
	CyString sFilename;
23
} SStoredSound;
24
 
25
class COverlay
26
{
27
public:
28
	COverlay ()
29
	{
30
		m_lKeysDown = NULL;
31
		m_cKeyBuffer = NULL;
32
		m_lKeySize = 0;
33
		m_pKeyboardDevice = NULL;
34
		m_pMouseDevice = NULL;
35
		m_bGetKeyboard = false;
36
		m_iMouseX = m_iMouseY = 0;
37
		m_bHaltMouse = false;
38
		m_bSound = false;
39
		m_pSystem = NULL;
40
	}
41
 
42
	virtual ~COverlay ()
43
	{
44
		for ( SStoredSound *sound = m_lSounds.First(); sound; sound = m_lSounds.Next() )
45
		{
46
			if ( sound->pSound )
47
				sound->pSound->release();
48
			delete sound;
49
		}
50
		m_lSounds.clear();
51
 
52
		if ( m_pSystem )
53
		{
54
			m_pSystem->close();
55
			m_pSystem->release();
56
		}
57
	}
58
 
59
 
60
	bool GetKeyboard () { return m_bGetKeyboard; }
61
 
62
	virtual bool HaltMouse () { return m_bHaltMouse; }
63
	virtual bool MouseClick ( int but ) { return false; }
64
	virtual void MouseRelease ( int but ) { }
65
	virtual bool KeyDown ( int key ) { return false; }
66
	virtual void KeyReleased ( int key ) { }
67
 
68
	virtual bool PlaySound ( FMOD::Sound *sound )
69
	{
70
		SStoredSound *s = NULL;
71
		for ( s = m_lSounds.First(); s; s = m_lSounds.Next() )
72
		{
73
			if ( s->pSound == sound )
74
				return (PlaySound (s));
75
		}
76
		return false;
77
 
78
	}
79
	virtual bool PlaySound ( CyString filename )
80
	{
81
		filename.ToUpper();
82
		SStoredSound *s = NULL;
83
		for ( s = m_lSounds.First(); s; s = m_lSounds.Next() )
84
		{
85
			if ( s->sFilename == filename )
86
				return (PlaySound (s));
87
		}
88
		return false;
89
	}
90
 
91
	virtual void Send ( CyString data ) {}
92
	virtual FMOD::Sound *CreateSound ( CyString filename )
93
	{
94
		filename.ToUpper();
95
		SStoredSound *s;
96
		for ( s = m_lSounds.First(); s; s = m_lSounds.Next() )
97
		{
98
			if ( s->sFilename == filename )
99
				return s->pSound;
100
		}
101
 
102
		s = new SStoredSound;
103
		s->sFilename = filename;
104
		FMOD_RESULT result = m_pSystem->createSound ( filename.c_str(), FMOD_HARDWARE, 0, &s->pSound );
105
		if ( result == FMOD_OK )
106
		{
107
			m_lSounds.push_back ( s );
108
			return s->pSound;
109
		}
110
		delete s;
111
		return NULL;
112
	}
113
 
114
	void MoveMouseY ( int y ) { m_iMouseY += y; if ( m_iMouseY > m_iHeight ) m_iMouseY = m_iHeight; if ( m_iMouseY < 0 ) m_iMouseY = 0; }
115
	void MoveMouseX ( int x ) { m_iMouseX += x; if ( m_iMouseX > m_iWidth  ) m_iMouseX = m_iWidth;  if ( m_iMouseX < 0 ) m_iMouseX = 0; }
116
 
117
	void SetKeyboardDevice ( IDirectInputDevice8 *device )
118
	{
119
		m_pKeyboardDevice = device;
120
	}
121
	void SetMouseDevice ( IDirectInputDevice8 *device )
122
	{
123
		m_pMouseDevice = device;
124
	}
125
 
126
	bool CheckKey(int key)
127
	{
128
		if ( KEYDOWN ( m_cKeyBuffer, key ) )
129
			return true;
130
 
131
		return false;
132
	}
133
	virtual void render (MyDirect3DDevice9 *device) = 0;
134
	virtual void init (MyDirect3DDevice9 *device) 
135
	{
136
		m_bSound = true;
137
		FMOD_RESULT result;
138
 
139
		result = FMOD::System_Create( &m_pSystem );		// Create the main system object.
140
		if (result != FMOD_OK)
141
			m_bSound = false;
142
		else
143
		{
144
			result = m_pSystem->init(32, FMOD_INIT_NORMAL, 0);
145
			if (result != FMOD_OK)
146
				m_bSound = false;
147
		}
148
 
149
		if ( (!m_bSound) && (m_pSystem) )
150
		{
151
			m_pSystem->close();
152
			m_pSystem->release();
153
		}
154
	}
155
 
156
	virtual bool SetKeyBuffer ( DWORD size, char *buff )
157
	{
158
		// if old buffer is the wrong size, delete it
159
		if ( (m_cKeyBuffer) && (size != m_lKeySize) )
160
		{
161
			delete m_cKeyBuffer;
162
			if ( m_lKeysDown )
163
				delete m_lKeysDown;
164
			m_lKeysDown = NULL;
165
			m_cKeyBuffer = NULL;
166
			m_lKeySize = 0;
167
		}
168
 
169
		// if no buffer exists, create it
170
		if ( !m_cKeyBuffer )
171
		{
172
			m_lKeysDown = new DWORD[size];
173
			ZeroMemory (m_lKeysDown, size);
174
			m_cKeyBuffer = new char[size];
175
			m_lKeySize = size;
176
		}
177
 
178
		// copy keyboard state to buffer
179
		memcpy ( m_cKeyBuffer, buff, m_lKeySize );
180
 
181
		// check for keydown
182
		DWORD timeNow = timeGetTime();
183
 
184
		// first check if key is already down
185
		bool ret = false;
186
		for ( int i = 0; i < (int)m_lKeySize; i++ )
187
		{
188
			if ( (m_lKeysDown[i]) && (!CheckKey(i)) )
189
			{
190
				KeyReleased ( i );
191
				m_lKeysDown[i] = 0;
192
			}
193
 
194
			// time expired
195
			if ( (m_lKeysDown[i]) && (timeNow > (m_lKeysDown[i] + TYPERATE)) )
196
				m_lKeysDown[i] = 0;
197
		}
198
 
199
		// now check if its been pushed
200
		for ( int i = 0; i < (int)m_lKeySize; i++ )
201
		{
202
			if ( (!m_lKeysDown[i]) && (CheckKey (i)) )
203
			{
204
				m_lKeysDown[i] = timeNow;
205
				if ( KeyDown ( i ) )
206
					ret = true;
207
			}
208
		}
209
 
210
		return ret;
211
	}
212
 
213
	virtual void SetWindowSize ( int width, int height ) { m_iWidth = width; m_iHeight = height; m_iMouseX = (width / 2); m_iMouseY = (height / 2); }
214
 
215
protected:
216
	virtual bool PlaySound ( SStoredSound *sound )
217
	{
218
		if ( !sound )
219
			return false;
220
 
221
		FMOD_RESULT result = m_pSystem->playSound ( FMOD_CHANNEL_FREE, sound->pSound, false, &sound->pChannel );
222
		if ( result == FMOD_OK )
223
			return true;
224
		return false;
225
	}
226
 
227
	int m_iWidth, m_iHeight;
228
 
229
	char *m_cKeyBuffer;
230
	DWORD m_lKeySize;
231
 
232
	DWORD *m_lKeysDown;
233
 
234
	IDirectInputDevice8 *m_pKeyboardDevice;	
235
	IDirectInputDevice8 *m_pMouseDevice;	
236
 
237
	bool m_bGetKeyboard;
238
 
239
	bool m_bHaltMouse;
240
	int m_iMouseX, m_iMouseY;
241
 
242
	bool m_bSound;
243
	FMOD::System *m_pSystem;
244
 
245
	CLinkList<SStoredSound> m_lSounds;
246
};
247
 
248
#endif //__OVERLAY_H__