Subversion Repositories spk

Rev

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