Sourcecodes - DirectX 9 Direct3D Klasse

Sprachenübersicht/C / C++/ C#/Spieleprogrammierung/DirectX

DirectX 9 Direct3D Klasse

Diese Seite wurde 7731 mal aufgerufen.

Dieser Artikel wurde in einem Wikiweb System geschrieben, das heißt, Sie können die Artikel jederzeit editieren, wenn Sie einen Fehler gefunden haben, oder etwas hinzufügen wollen.

Editieren Versionen Linkpartnerschaft Bottom Printversion

Keywords: DirectX 9, Direct3D, Klasse

Hallo,

ich mache hier meinen alten Quellcode zur DirectX Engine public.

Der Code kümmert sich um die Initialisierung von Direct3D.

Er ist schon etwas älter darum stimmt das Code design nicht ganz, aber ich hoffe dass er dem einen oder anderen helfen kann laugh

Die libs müssen natürlich eingebunden werden.

Die Adapter behandlung fehlt auch.

directx_graphics.h:


/**************************************************************************/
// /includes/graphic/direct_x_graphics.h
//
// This file manages DirectXGraphics
/**************************************************************************/

/**************************************************************************/
// Defines
/**************************************************************************/
#ifndef FILE_12_DXGRAPHICS
#define FILE_12_DXGRAPHICS
#define PI 3.141592653589793238

/**************************************************************************/
// Header
/**************************************************************************/
#include <d3d9.h>
#include <d3dx9.h>
#include <errorfile.h>

namespace bn_Core
{
    struct AdapterInformation
    {
        DWORD dwMaxTextureBlendStages;
        DWORD dwPixelShaderVersion;
        DWORD dwVertexShaderVersion;
    };

    //Class for DirectXGarhpics
    class DLLEXPORT bn_DirectXGraphics
    {
        public:
            //Initialise DirectXGraphics
            bn_DirectXGraphics();

            //Desturktor clears the class
            ~bn_DirectXGraphics();

            //Initialise DirectXGraphics
            void Init(HWND hWnd,unsigned int *pRefreshRate,bool bWindowed = true,int iBackBuffer = 0, bool bCPUCalculate = false,int ScreenWidth = 1024,int ScreenHeight = 768);

            //Deletes the Surface
            void Clear(D3DCOLOR Color = D3DCOLOR_XRGB(0,0,0),int iX = 0,int iY = 0,int iWidth = 0,int iHeight = 0);

            //Begins with the Scene
            void BeginScene();

            //Present the Scene
            HRESULT Present();

            //Gives the devices back
            LPDIRECT3DDEVICE9 GetDevice(){ return m_lpD3DDevice; }

            //Gives the backbuffer back
            LPDIRECT3DSURFACE9 GetBackBuffer();

            //Creates a Screenshot
            void CreateScreenshot(std::string strFileName);

            //Tests for lost device
            int bn_DirectXGraphics::TestCooperativeLevel();

            //Resets the object
            void RestoreObject();

            AdapterInformation* GetAdapterInformation(){ return &Adapter;}

        protected:

            //Direct3D Object 
            LPDIRECT3D9 m_lpD3D;

            //Display mode
            D3DDISPLAYMODE m_DisplayMode;

            //Pointer of a Device
            LPDIRECT3DDEVICE9 m_lpD3DDevice;

            //The Backbuffer
            int m_iBackBuffer;

            //The Present parameters
            D3DPRESENT_PARAMETERS m_PresentParams;

            AdapterInformation Adapter;
    };
}

#endif



directx_graphics.cpp:


//******************************* FOR MORE SEE THE .h file ********************************//
#include "direct_x_graphics.h"

//Initialise Cbn_DirectXGraphics
bn_Core::bn_DirectXGraphics::bn_DirectXGraphics()
{
    //Sets it 0
    m_lpD3D            = NULL;
    m_lpD3DDevice    = NULL;
    m_iBackBuffer    = NULL;
}
 
//Destructor clears the class
bn_Core::bn_DirectXGraphics::~bn_DirectXGraphics()
{
    //Clean
    if (NULL != m_lpD3DDevice)
    {
        m_lpD3DDevice->Release();
        m_lpD3DDevice=NULL;
    }

    //Clean
    if (NULL != m_lpD3D)
    {
        m_lpD3D->Release();
        m_lpD3D=NULL;
    }
}

//Resets the object
void bn_Core::bn_DirectXGraphics::RestoreObject()
{
    //Resets the device
    DXTestForError(m_lpD3DDevice->Reset(&m_PresentParams),
                    "Error by reseting the Device",2,1);
}

//Tests for lost device
int bn_Core::bn_DirectXGraphics::TestCooperativeLevel()
{
    HRESULT hrLostDevice = m_lpD3DDevice->TestCooperativeLevel();    

    //Do nothing
    if (hrLostDevice == D3DERR_DEVICELOST)
        return 2;
    //Reset it
    else if (hrLostDevice == D3DERR_DEVICENOTRESET)
        return 1;

    //Shut down!
    else if (hrLostDevice == D3DERR_DRIVERINTERNALERROR)
        ExceptionGameError("",0,0,"Internal error -shuting down",1);

    return 0;
}


//Initialise Cbn_DirectXGraphics
void bn_Core::bn_DirectXGraphics::Init(HWND hWnd,unsigned int *pRefreshRate,bool bWindowed, int iBackBuffer, bool bCPUCalculate,int ScreenWidth,int ScreenHeight)
{
    //Creates a D3D Object
    if (FAILED(m_lpD3D = Direct3DCreate9(D3D_SDK_VERSION)))
        ExceptionGameError("",0,1,"Error, can't create a D3D Object.",1);

    //Gets gfx-card informations
    m_lpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&m_DisplayMode);

    //Zero Memory
    ZeroMemory(&m_PresentParams,sizeof(m_PresentParams));

    //Settings
    m_PresentParams.SwapEffect                = D3DSWAPEFFECT_DISCARD;
    m_PresentParams.hDeviceWindow            = hWnd;                    //Gives the windows to the struct
    m_PresentParams.Windowed                = bWindowed;            //Fullscreen / Windowed
    m_PresentParams.BackBufferFormat        = D3DFMT_A8R8G8B8;        //Sets 32 Bit
    m_PresentParams.BackBufferCount            = 1;   
    //m_PresentParams.PresentationInterval    = D3DPRESENT_INTERVAL_IMMEDIATE; 
    //m_PresentParams.FullScreen_RefreshRateInHz    = 0;

    *pRefreshRate = m_DisplayMode.RefreshRate;

    //If there is an backbuffer
    if (iBackBuffer)
    {
        m_iBackBuffer = iBackBuffer;

        m_PresentParams.EnableAutoDepthStencil = true;

        if (iBackBuffer == 16)
            m_PresentParams.AutoDepthStencilFormat = D3DFMT_D16;
        else if (iBackBuffer == 32)
            m_PresentParams.AutoDepthStencilFormat = D3DFMT_D32;
    }

    //Sets the screen width if bWindowed == true
    if (!bWindowed)
    {
        m_PresentParams.BackBufferWidth                = ScreenWidth;    //Sets the Width
        m_PresentParams.BackBufferHeight            = ScreenHeight;    //Sets the Height
        //m_PresentParams.FullScreen_RefreshRateInHz    = m_DisplayMode.RefreshRate;   
    }
    else
    {
        MoveWindow(hWnd,10,10,ScreenWidth,ScreenHeight,true);
    }

    //Typ of the device
    D3DDEVTYPE DeviceTyp;

    //If its CPU calculated
    if (bCPUCalculate)
    {
        DeviceTyp = D3DDEVTYPE_REF;

        //Errorfile
        ExceptionGameError("",0,11,"Starting DirectGraphics, in Reference Device Mode");
    }
    else DeviceTyp = D3DDEVTYPE_HAL;

    //Creates the device
    DXTestForError(m_lpD3D->CreateDevice(D3DADAPTER_DEFAULT,DeviceTyp,hWnd,
                    D3DCREATE_SOFTWARE_VERTEXPROCESSING,&m_PresentParams,&m_lpD3DDevice),
                    "Error by creating a Device",2,1);

    // This is how you find out how many texture stages your hardware will support
    D3DCAPS9 Caps;

    m_lpD3DDevice->GetDeviceCaps(&Caps);

    Adapter.dwMaxTextureBlendStages = Caps.MaxTextureBlendStages;
    Adapter.dwPixelShaderVersion    = Caps.PixelShaderVersion;
    Adapter.dwVertexShaderVersion    = Caps.VertexShaderVersion;

    if (Adapter.dwMaxTextureBlendStages < 4)
    {
        ucs::string        strBuffer = "Error, your hardware supports only ";
                        strBuffer += Adapter.dwMaxTextureBlendStages;
                        strBuffer += " textures per Object";

        ExceptionGameError("",0,1,(char*)strBuffer.c_str(),true);
    }
}

//Deletes the Surface
void bn_Core::bn_DirectXGraphics::Clear(D3DCOLOR Color,int iX,int iY,int iWidth,int iHeight)
{
    D3DRECT rRect;

    if ((iWidth == 0) || (iHeight == 0))
    {
        //If there is a backbuffer
        if (m_iBackBuffer)
            m_lpD3DDevice->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,Color,1.0f,0);
        else m_lpD3DDevice->Clear(0,0,D3DCLEAR_TARGET,Color,0,0);
    }
    else
    {
        rRect.x1 = iX;
        rRect.y1 = iY;
        rRect.x2 = iX + iWidth;
        rRect.y2 = iY + iHeight;

        //If there is a backbuffer
        if (m_iBackBuffer)
            m_lpD3DDevice->Clear(1,&rRect,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,Color,1.0f,0);
        else m_lpD3DDevice->Clear(1,&rRect,D3DCLEAR_TARGET,Color,0,0);
    }
}

//Begins with the Scene
void bn_Core::bn_DirectXGraphics::BeginScene()
{
    m_lpD3DDevice->BeginScene();
}

//Present the Scene
HRESULT bn_Core::bn_DirectXGraphics:resent()
{
    //Presents it
    m_lpD3DDevice->EndScene();
    return m_lpD3DDevice->Present(0,0,0,0);
}

//Gives the backbuffer back
LPDIRECT3DSURFACE9 bn_Core::bn_DirectXGraphics::GetBackBuffer()
{
    LPDIRECT3DSURFACE9 BackBuffer;

    m_lpD3DDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&BackBuffer);

    return BackBuffer;
}

        //Creates a Screenshot
void bn_Core::bn_DirectXGraphics::CreateScreenshot(ucs::string strFileName)
{
    //Creates a surface for the screenshot
    LPDIRECT3DSURFACE9 lpSurface;

    m_lpD3DDevice->CreateOffscreenPlainSurface(
                                  1024,768,
                                  D3DFMT_A8R8G8B8,
                                  D3DPOOL_SYSTEMMEM,
                                  &lpSurface,0);
    
    //Copys the screenshot into the front buffer
    m_lpD3DDevice->GetFrontBufferData(0,lpSurface);
    
    //Saves it in a file
    D3DXSaveSurfaceToFile(strFileName.c_str(),D3DXIFF_BMP,
                          lpSurface,0,NULL);

    //Cleans the surface
    lpSurface->Release();
}

Gibt es noch irgendwelche Fragen, oder wollen Sie über den Artikel diskutieren?

Editieren Versionen Linkpartnerschaft Top Printversion

Haben Sie einen Fehler gefunden? Dann klicken Sie doch auf Editieren, und beheben den Fehler, keine Angst, Sie können nichts zerstören, der Artikel kann wiederhergestellt werden.

Sprachenübersicht/C / C++/ C#/Spieleprogrammierung/DirectX/DirectX 9 Direct3D Klasse