Subversion Repositories spk

Rev

Rev 11 | Rev 123 | 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>
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;
79 cycrow 104
		//TODO: update this to new FMOD lib to fix this
105
		/*
11 cycrow 106
		FMOD_RESULT result = m_pSystem->createSound ( filename.c_str(), FMOD_HARDWARE, 0, &s->pSound );
107
		if ( result == FMOD_OK )
108
		{
109
			m_lSounds.push_back ( s );
110
			return s->pSound;
79 cycrow 111
		}*/
11 cycrow 112
		delete s;
113
		return NULL;
114
	}
115
 
116
	void MoveMouseY ( int y ) { m_iMouseY += y; if ( m_iMouseY > m_iHeight ) m_iMouseY = m_iHeight; if ( m_iMouseY < 0 ) m_iMouseY = 0; }
117
	void MoveMouseX ( int x ) { m_iMouseX += x; if ( m_iMouseX > m_iWidth  ) m_iMouseX = m_iWidth;  if ( m_iMouseX < 0 ) m_iMouseX = 0; }
118
 
119
	void SetKeyboardDevice ( IDirectInputDevice8 *device )
120
	{
121
		m_pKeyboardDevice = device;
122
	}
123
	void SetMouseDevice ( IDirectInputDevice8 *device )
124
	{
125
		m_pMouseDevice = device;
126
	}
127
 
128
	bool CheckKey(int key)
129
	{
130
		if ( KEYDOWN ( m_cKeyBuffer, key ) )
131
			return true;
132
 
133
		return false;
134
	}
135
	virtual void render (MyDirect3DDevice9 *device) = 0;
136
	virtual void init (MyDirect3DDevice9 *device) 
137
	{
138
		m_bSound = true;
139
		FMOD_RESULT result;
140
 
141
		result = FMOD::System_Create( &m_pSystem );		// Create the main system object.
142
		if (result != FMOD_OK)
143
			m_bSound = false;
144
		else
145
		{
146
			result = m_pSystem->init(32, FMOD_INIT_NORMAL, 0);
147
			if (result != FMOD_OK)
148
				m_bSound = false;
149
		}
150
 
151
		if ( (!m_bSound) && (m_pSystem) )
152
		{
153
			m_pSystem->close();
154
			m_pSystem->release();
155
		}
156
	}
157
 
158
	virtual bool SetKeyBuffer ( DWORD size, char *buff )
159
	{
160
		// if old buffer is the wrong size, delete it
161
		if ( (m_cKeyBuffer) && (size != m_lKeySize) )
162
		{
163
			delete m_cKeyBuffer;
164
			if ( m_lKeysDown )
165
				delete m_lKeysDown;
166
			m_lKeysDown = NULL;
167
			m_cKeyBuffer = NULL;
168
			m_lKeySize = 0;
169
		}
170
 
171
		// if no buffer exists, create it
172
		if ( !m_cKeyBuffer )
173
		{
174
			m_lKeysDown = new DWORD[size];
175
			ZeroMemory (m_lKeysDown, size);
176
			m_cKeyBuffer = new char[size];
177
			m_lKeySize = size;
178
		}
179
 
180
		// copy keyboard state to buffer
181
		memcpy ( m_cKeyBuffer, buff, m_lKeySize );
182
 
183
		// check for keydown
184
		DWORD timeNow = timeGetTime();
185
 
186
		// first check if key is already down
187
		bool ret = false;
188
		for ( int i = 0; i < (int)m_lKeySize; i++ )
189
		{
190
			if ( (m_lKeysDown[i]) && (!CheckKey(i)) )
191
			{
192
				KeyReleased ( i );
193
				m_lKeysDown[i] = 0;
194
			}
195
 
196
			// time expired
197
			if ( (m_lKeysDown[i]) && (timeNow > (m_lKeysDown[i] + TYPERATE)) )
198
				m_lKeysDown[i] = 0;
199
		}
200
 
201
		// now check if its been pushed
202
		for ( int i = 0; i < (int)m_lKeySize; i++ )
203
		{
204
			if ( (!m_lKeysDown[i]) && (CheckKey (i)) )
205
			{
206
				m_lKeysDown[i] = timeNow;
207
				if ( KeyDown ( i ) )
208
					ret = true;
209
			}
210
		}
211
 
212
		return ret;
213
	}
214
 
215
	virtual void SetWindowSize ( int width, int height ) { m_iWidth = width; m_iHeight = height; m_iMouseX = (width / 2); m_iMouseY = (height / 2); }
216
 
217
protected:
218
	virtual bool PlaySound ( SStoredSound *sound )
219
	{
220
		if ( !sound )
221
			return false;
222
 
79 cycrow 223
		//TODO: update to work with latest FMOD
224
		/*
11 cycrow 225
		FMOD_RESULT result = m_pSystem->playSound ( FMOD_CHANNEL_FREE, sound->pSound, false, &sound->pChannel );
226
		if ( result == FMOD_OK )
227
			return true;
79 cycrow 228
			*/
11 cycrow 229
		return false;
230
	}
231
 
232
	int m_iWidth, m_iHeight;
233
 
234
	char *m_cKeyBuffer;
235
	DWORD m_lKeySize;
236
 
237
	DWORD *m_lKeysDown;
238
 
239
	IDirectInputDevice8 *m_pKeyboardDevice;	
240
	IDirectInputDevice8 *m_pMouseDevice;	
241
 
242
	bool m_bGetKeyboard;
243
 
244
	bool m_bHaltMouse;
245
	int m_iMouseX, m_iMouseY;
246
 
247
	bool m_bSound;
248
	FMOD::System *m_pSystem;
249
 
250
	CLinkList<SStoredSound> m_lSounds;
251
};
252
 
253
#endif //__OVERLAY_H__