Subversion Repositories spk

Rev

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