Rev 79 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#ifndef __OVERLAY_H__#define __OVERLAY_H__class MyDirect3DDevice9;#define DIRECTINPUT_VERSION 0x08000#include <d3dx9.h>#include <mmsystem.h>#include <dinput.h>#define TYPERATE 200#define KEYDOWN(name,key) (name[key] & 0x80)#include <spk.h>#include <stdio.h>#ifdef FMOD#include <fmod.hpp>typedef struct SStoredSound {struct SStoredSound () { pSound = NULL; pChannel = NULL; }FMOD::Sound *pSound;FMOD::Channel *pChannel;CyString sFilename;} SStoredSound;#endifclass COverlay{public:COverlay (){m_lKeysDown = NULL;m_cKeyBuffer = NULL;m_lKeySize = 0;m_pKeyboardDevice = NULL;m_pMouseDevice = NULL;m_bGetKeyboard = false;m_iMouseX = m_iMouseY = 0;m_bHaltMouse = false;m_bSound = false;#ifdef FMODm_pSystem = NULL;#endif}virtual ~COverlay (){#ifdef FMODfor ( SStoredSound *sound = m_lSounds.First(); sound; sound = m_lSounds.Next() ){if ( sound->pSound )sound->pSound->release();delete sound;}m_lSounds.clear();if ( m_pSystem ){m_pSystem->close();m_pSystem->release();}#endif}bool GetKeyboard () { return m_bGetKeyboard; }virtual bool HaltMouse () { return m_bHaltMouse; }virtual bool MouseClick ( int but ) { return false; }virtual void MouseRelease ( int but ) { }virtual bool KeyDown ( int key ) { return false; }virtual void KeyReleased ( int key ) { }#ifdef FMODvirtual bool PlaySound ( FMOD::Sound *sound ){SStoredSound *s = NULL;for ( s = m_lSounds.First(); s; s = m_lSounds.Next() ){if ( s->pSound == sound )return (PlaySound (s));}return false;}#endifvirtual bool CreateSound(CyString filename){#ifdef FMODfilename.ToUpper();SStoredSound *s;for (s = m_lSounds.First(); s; s = m_lSounds.Next()){if (s->sFilename == filename)return s->pSound;}s = new SStoredSound;s->sFilename = filename;//TODO: update this to new FMOD lib to fix this/*FMOD_RESULT result = m_pSystem->createSound ( filename.c_str(), FMOD_HARDWARE, 0, &s->pSound );if ( result == FMOD_OK ){m_lSounds.push_back ( s );return true;}*/#endifreturn false;}virtual bool PlaySound ( CyString filename ){filename.ToUpper();#ifdef FMODSStoredSound *s = NULL;for ( s = m_lSounds.First(); s; s = m_lSounds.Next() ){if ( s->sFilename == filename )return (PlaySound (s));}#endifreturn false;}virtual void Send ( CyString data ) {}void MoveMouseY ( int y ) { m_iMouseY += y; if ( m_iMouseY > m_iHeight ) m_iMouseY = m_iHeight; if ( m_iMouseY < 0 ) m_iMouseY = 0; }void MoveMouseX ( int x ) { m_iMouseX += x; if ( m_iMouseX > m_iWidth ) m_iMouseX = m_iWidth; if ( m_iMouseX < 0 ) m_iMouseX = 0; }void SetKeyboardDevice ( IDirectInputDevice8 *device ){m_pKeyboardDevice = device;}void SetMouseDevice ( IDirectInputDevice8 *device ){m_pMouseDevice = device;}bool CheckKey(int key){if ( KEYDOWN ( m_cKeyBuffer, key ) )return true;return false;}virtual void render (MyDirect3DDevice9 *device) = 0;virtual void init (MyDirect3DDevice9 *device){#ifdef FMODm_bSound = true;FMOD_RESULT result;result = FMOD::System_Create( &m_pSystem ); // Create the main system object.if (result != FMOD_OK)m_bSound = false;else{result = m_pSystem->init(32, FMOD_INIT_NORMAL, 0);if (result != FMOD_OK)m_bSound = false;}if ( (!m_bSound) && (m_pSystem) ){m_pSystem->close();m_pSystem->release();}#elsem_bSound = false;#endif}virtual bool SetKeyBuffer ( DWORD size, char *buff ){// if old buffer is the wrong size, delete itif ( (m_cKeyBuffer) && (size != m_lKeySize) ){delete m_cKeyBuffer;if ( m_lKeysDown )delete m_lKeysDown;m_lKeysDown = NULL;m_cKeyBuffer = NULL;m_lKeySize = 0;}// if no buffer exists, create itif ( !m_cKeyBuffer ){m_lKeysDown = new DWORD[size];ZeroMemory (m_lKeysDown, size);m_cKeyBuffer = new char[size];m_lKeySize = size;}// copy keyboard state to buffermemcpy ( m_cKeyBuffer, buff, m_lKeySize );// check for keydownDWORD timeNow = timeGetTime();// first check if key is already downbool ret = false;for ( int i = 0; i < (int)m_lKeySize; i++ ){if ( (m_lKeysDown[i]) && (!CheckKey(i)) ){KeyReleased ( i );m_lKeysDown[i] = 0;}// time expiredif ( (m_lKeysDown[i]) && (timeNow > (m_lKeysDown[i] + TYPERATE)) )m_lKeysDown[i] = 0;}// now check if its been pushedfor ( int i = 0; i < (int)m_lKeySize; i++ ){if ( (!m_lKeysDown[i]) && (CheckKey (i)) ){m_lKeysDown[i] = timeNow;if ( KeyDown ( i ) )ret = true;}}return ret;}virtual void SetWindowSize ( int width, int height ) { m_iWidth = width; m_iHeight = height; m_iMouseX = (width / 2); m_iMouseY = (height / 2); }protected:#ifdef FMODvirtual bool PlaySound ( SStoredSound *sound ){if ( !sound )return false;//TODO: update to work with latest FMOD/*FMOD_RESULT result = m_pSystem->playSound ( FMOD_CHANNEL_FREE, sound->pSound, false, &sound->pChannel );if ( result == FMOD_OK )return true;*/return false;}#endifint m_iWidth, m_iHeight;char *m_cKeyBuffer;DWORD m_lKeySize;DWORD *m_lKeysDown;IDirectInputDevice8 *m_pKeyboardDevice;IDirectInputDevice8 *m_pMouseDevice;bool m_bGetKeyboard;bool m_bHaltMouse;int m_iMouseX, m_iMouseY;bool m_bSound;#ifdef FMODFMOD::System *m_pSystem;CLinkList<SStoredSound> m_lSounds;#endif};#endif //__OVERLAY_H__