///////////////////////////////////////////////////////////////////////////////////////////
// Device.h
//
//  Definition of the Device class, which wraps a Direct3D 8 device.

#pragma once

#include "Direct3D.h"

using namespace System;
using namespace System::ComponentModel;

namespace Sunlight
{
    namespace DirectX
    {
        namespace Graphics
        {
            // Wraps a Direct3D 8 device, providing 2D graphics functionality.
            __gc public class Device
            {
            protected:
                D3DFORMAT                   m_nFormat;
                int                         m_nRefreshRate;
                bool                        m_bCreated;
                IDirect3DDevice8 __nogc     *m_pDevice;
                System::Windows::Forms::Form *m_pParentWindow;
                double                      m_nFrameRate, m_nFilteredFrameRate;
                DWORD                       m_dwFrameStartTime, m_dwFilteredFrameStartTime, m_nFilterFrames;

                // Perform the device initialisation or reset.
                void Create(bool bReset);
            
                // Fills a D3DPRESENT_PARAMETERS for the device.
                virtual void FillD3DPP(D3DPRESENT_PARAMETERS *pd3dpp);
                // Sets the device render states.
                virtual void SetupDevice();
                // Sets the window styles.
                virtual void SetupWindow();
                // Called when the ParentWindow is closed.
                void OnFormClosed(Object *sender, EventArgs *e);

            public:
                Device(Direct3D *d3d);
                ~Device();

                // Sets/returns the width of the device.
                int Width;
                // Sets/returns the height of the device.
                int Height;
                // Sets/returns the colour depth of the device.
                int BitsPerPixel;
                // Sets/returns if the device should be displayed in a window.
                bool Windowed;
                // Sets/returns the Direct3D object used to create this device.
                Direct3D *Direct3DObject;
                // Sets/returns the window which will represent this device.
                __property System::Windows::Forms::Form *get_ParentWindow();
                __property void set_ParentWindow(System::Windows::Forms::Form *);

                // Returns the format of the device.
                __property DWORD get_Format();
                // The Direct3D device object underlying this object.
                __property IDirect3DDevice8 __nogc *get_Direct3DDevice();
                // Returns whether it is safe for the application to execute its logic and drawing.
                __property bool get_Paused();
                // Returns whether the device has been successfully created.
                __property bool get_IsCreated();
                // Returns the current frame rate in frames per second.
                __property double get_FrameRate();
                // Returns the filtered frame rate in frames per second.
                __property double get_FilteredFrameRate();
                // Sets/returns the time period over which the frame rate will be filtered, in milliseconds.
                int FrameRateFilterTime;
                // Returns the display refresh rate in Hz.
                __property int get_RefreshRate();

                // Called when DirectX Graphics objects may be (re-)created.
                __event EventHandler    *Created;
                // Called when video-memory objects may be (re-)created.
                __event EventHandler    *Initialized;
                // Called when the device is lost.
                __event EventHandler    *Lost;
                // Called when DirectX Graphics objects must be released.
                __event EventHandler    *Releasing;

                // Creates the device object.
                void Create();
                // Resets the device, allowing changes in device parameters.
                void Reset();
                // Destroys this device and related DirectX Graphics objects.
                void Destroy();

                // Begin a scene, after which drawing commands may occur.
                void BeginScene();
                // End a scene, after which drawing commands may not occur.
                void EndScene();
                // Draw the current scene to the device.
                void Flip();
            };
        }
    }
}