Qt_DX

#ifndef MY_FRAME__HH__
#define MY_FRAME__HH__
#include <QtGui/QWidget> 

struct IDirect3D9;
struct IDirect3DDevice9; 

class QD3DWidget : public QWidget
{
    Q_OBJECT 

public:
    QD3DWidget( QWidget* pParent = NULL); 

    ~QD3DWidget(); 

    QSizePolicy sizePolicy() const { return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); }
    QPaintEngine *paintEngine() const { return NULL; } 

protected:
    void Setup(); 

    void Close(); 

    void paintEvent( QPaintEvent* pEvent); 

private:
    IDirect3D9*       mD3D;
    IDirect3DDevice9* mDevice;
};
#endif
#include <QMessageBox>
#define WIN32_LEAN_AND_MEAN
#include <d3d9.h>
#include <d3dx9.h>
#include "MyFrame.h"
#pragma comment(lib, "d3dx9d.lib")
#pragma comment(lib, "d3d9.lib")
QD3DWidget::QD3DWidget( QWidget* pParent)
    : QWidget( pParent)
{ 

    mD3D = NULL;
    mDevice = NULL; 

    setMinimumSize( 400, 400);
    setAttribute( Qt::WA_OpaquePaintEvent, true);
    setAttribute( Qt::WA_PaintOnScreen, true); 

    Setup();
} 

QD3DWidget::~QD3DWidget()
{
    Close();
} 

void QD3DWidget::Setup()
{
    HWND windowHandle = winId(); 

    mD3D = Direct3DCreate9( D3D_SDK_VERSION);
    if( NULL == mD3D)
        QMessageBox::critical(this,
        "ERROR",
        "Failed to create D3D object",
        QMessageBox::Ok);

    D3DPRESENT_PARAMETERS d3dpp;    

    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = windowHandle;    

    HRESULT hr = mD3D->CreateDevice(D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        windowHandle,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dpp,
        &mDevice);

    if( FAILED( hr))
        QMessageBox::critical(this,
        "ERROR",
        "Failed to create D3D device",
        QMessageBox::Ok);
} 

void QD3DWidget::Close()
{
    if( mDevice)
        mDevice->Release();
    if( mD3D)
        mD3D->Release();
} 

void QD3DWidget::paintEvent( QPaintEvent* pEvent)
{
    HRESULT hr = mDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(100, 0, 0), 1.0f, 0);
    if( FAILED( hr))
        QMessageBox::critical(this,
        "ERROR",
        "Failed to clear backbuffer.",
        QMessageBox::Ok);

    mDevice->BeginScene();    

    mDevice->EndScene();  

    hr = mDevice->Present(NULL, NULL, NULL, NULL);
    if( FAILED( hr))
        QMessageBox::critical(this,
        "ERROR",
        "Failed to Present().",
        QMessageBox::Ok);

    update();
} 

Qt_DX

时间: 2024-09-30 04:17:27

Qt_DX的相关文章