C++ (Qt)class Direct3DWidget : public QWidget{ Q_OBJECT public: Direct3DWidget(QWidget *parent = 0); ~Direct3DWidget(); bool InitDirect3D();public slots: bool Rendering();private: void paintEvent(QPaintEvent *p) { Rendering(); } QPaintEngine* paintEngine() { return NULL; } LPDIRECT3D9 m_pD3D; LPDIRECT3DDEVICE9 m_pd3dDevice;};
C++ (Qt)Direct3DWidget::Direct3DWidget(QWidget *parent /* = 0 */){ setFixedSize(200, 200); setAutoFillBackground(false); setAttribute(Qt::WA_PaintOnScreen, true);}Direct3DWidget::~Direct3DWidget(){ if( m_pd3dDevice != NULL) m_pd3dDevice->Release(); if( m_pD3D != NULL) m_pD3D->Release();}bool Direct3DWidget::InitDirect3D(){ m_pD3D = Direct3DCreate9( D3D_SDK_VERSION); if( !m_pD3D) return false; D3DPRESENT_PARAMETERS d3dpp = {0}; d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; HRESULT hr = m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winId(), D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &m_pd3dDevice ); if( FAILED(hr) || !m_pd3dDevice) return false;}bool Direct3DWidget::Rendering(){ if(m_pd3dDevice == NULL) return false; m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 0), 1.0f, 0); if(SUCCEEDED(m_pd3dDevice->BeginScene())) { m_pd3dDevice->EndScene(); } m_pd3dDevice->Present( NULL, NULL, NULL, NULL ); return true;}
C++ (Qt)int main(int argc, char *argv[]){ QApplication a(argc, argv); QMainWindow window; Direct3DWidget* widget = new Direct3DWidget(&window); window.show(); widget->InitDirect3D(); return a.exec();}
C++ (Qt)class Direct3DWidget : public QWidget{ Q_OBJECTpublic: Direct3DWidget(QWidget *parent = 0); ~Direct3DWidget();public slots: bool Rendering();public: void InitDirect3D(); void paintEvent(QPaintEvent *p) { Rendering(); } QPaintEngine* paintEngine() { return 0; }private: CComPtr<IDirect3D9> m_pD3D; CComPtr<IDirect3DDevice9> m_pd3dDevice;};
C++ (Qt)Direct3DWidget::Direct3DWidget(QWidget *parent /* = 0 */){ /* setFixedSize(200, 200); setAutoFillBackground(false); setAttribute(Qt::WA_PaintOnScreen, true); setAttribute(Qt::WA_MSWindowsUseDirect3D, true); setAttribute(Qt::WA_NoSystemBackground, true);*/ //InitDirect3D();}Direct3DWidget::~Direct3DWidget(){}void Direct3DWidget::InitDirect3D(){ m_pD3D = Direct3DCreate9( D3D_SDK_VERSION); D3DPRESENT_PARAMETERS d3dpp = {0}; d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winId(), D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &m_pd3dDevice );}bool Direct3DWidget::Rendering(){ if(m_pd3dDevice == 0) return false; m_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 0), 1.0f, 0); if(SUCCEEDED(m_pd3dDevice->BeginScene())) { m_pd3dDevice->EndScene(); } m_pd3dDevice->Present( 0, 0, 0, 0 ); return true;}
C++ (Qt)int main(int argc, char *argv[]){ QApplication a(argc, argv); QMainWindow window; Direct3DWidget* widget = new Direct3DWidget(&window); //widget->setMinimumSize(20, 20); window.setAttribute(Qt::WA_PaintOnScreen); window.setAttribute(Qt::WA_NoSystemBackground); window.setCentralWidget(widget); widget->InitDirect3D(); window.show(); QTimer *timer = new QTimer(); QObject::connect(timer, SIGNAL( timeout() ), widget, SLOT( Rendering() ) ); timer->start(); return a.exec();}
C++ (Qt)/*window.setAttribute(Qt::WA_PaintOnScreen);window.setAttribute(Qt::WA_NoSystemBackground);window.setAttribute(Qt::WA_MSWindowsUseDirect3D, true);window.setAutoFillBackground(false);*/