Subversion Repositories spk

Rev

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;
#endif

class 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 FMOD
                m_pSystem = NULL;
#endif
        }

        virtual ~COverlay ()
        {
#ifdef FMOD
                for ( 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 FMOD
        virtual 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;

        }
#endif

        virtual bool CreateSound(CyString filename)
        {
#ifdef FMOD
                filename.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;
                }*/
#endif
                return false;
        }

        virtual bool PlaySound ( CyString filename )
        {
                filename.ToUpper();
#ifdef FMOD
                SStoredSound *s = NULL;
                for ( s = m_lSounds.First(); s; s = m_lSounds.Next() )
                {
                        if ( s->sFilename == filename )
                                return (PlaySound (s));
                }
#endif
                return 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 FMOD
                m_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();
                }
#else
                m_bSound = false;
#endif
        }

        virtual bool SetKeyBuffer ( DWORD size, char *buff )
        {
                // if old buffer is the wrong size, delete it
                if ( (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 it
                if ( !m_cKeyBuffer )
                {
                        m_lKeysDown = new DWORD[size];
                        ZeroMemory (m_lKeysDown, size);
                        m_cKeyBuffer = new char[size];
                        m_lKeySize = size;
                }

                // copy keyboard state to buffer
                memcpy ( m_cKeyBuffer, buff, m_lKeySize );

                // check for keydown
                DWORD timeNow = timeGetTime();

                // first check if key is already down
                bool ret = false;
                for ( int i = 0; i < (int)m_lKeySize; i++ )
                {
                        if ( (m_lKeysDown[i]) && (!CheckKey(i)) )
                        {
                                KeyReleased ( i );
                                m_lKeysDown[i] = 0;
                        }

                        // time expired
                        if ( (m_lKeysDown[i]) && (timeNow > (m_lKeysDown[i] + TYPERATE)) )
                                m_lKeysDown[i] = 0;
                }

                // now check if its been pushed
                for ( 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 FMOD
        virtual 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;
        }
#endif

        int 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 FMOD
        FMOD::System *m_pSystem;

        CLinkList<SStoredSound> m_lSounds;
#endif
};

#endif //__OVERLAY_H__