码迷,mamicode.com
首页 > 其他好文 > 详细

Qt_DX

时间:2014-07-24 21:53:12      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   re   c   div   

#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,布布扣,bubuko.com

Qt_DX

标签:style   blog   color   os   io   re   c   div   

原文地址:http://www.cnblogs.com/nmgxbc/p/3865953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!