All pastes #719624 Raw Edit

Etwas

public text v1 · immutable
#719624 ·published 2007-09-29 13:27 UTC
rendered paste body
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(); 
}