从零开始开发3D游戏引擎 - 在QtWidget中进行Direct3D11渲染,并捕捉Windows消息

DirectX 渲染

创建一个继承QWidget的类,重写paintEvent,resizeEvent
但是仅仅这样还不够,画面会一直闪烁,需要重写以下函数
virtual QPaintEngine paintEngine() const { return NULL; }*
并设置窗口属性
this->setAttribute(Qt::WA_PaintOnScreen, true);
this->setAttribute(Qt::WA_NativeWindow, true);

捕捉Windows的消息

重写nativeEventFilter,继承 public QAbstractNativeEventFilter 接口
并砸application里面install

QApplication a(argc, argv);
BugattiEngine w;
w.show();
a.installNativeEventFilter(&w);
a.installNativeEventFilter(w.GetRenderWidget());
#pragma once
#include <qwidget.h>
#include "Render/D3DX11Entry.h"
class RenderWidget :
	public QWidget, public QAbstractNativeEventFilter
{
public:
	RenderWidget(QWidget* parent = nullptr);
	virtual ~RenderWidget();
	D3DX11Entry d3d11_entry;
	QWidget* main_window;
	HWND hwnd;
	bool has_init;
	virtual QPaintEngine* paintEngine() const { return NULL; }
	void Init(UINT width = 800, UINT height = 600);
protected:
	//virtual void resizeEvent(QResizeEvent* evt);
	virtual void paintEvent(QPaintEvent* evt); 
	virtual void timerEvent(QTimerEvent *event);
	virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE;
};


RenderWidget::RenderWidget(QWidget* parent) :QWidget(parent)
, d3d11_entry((HWND)this->winId())
, has_init(false)
{
	hwnd = (HWND)this->winId();
	this->setAttribute(Qt::WA_PaintOnScreen, true);
	this->setAttribute(Qt::WA_NativeWindow, true);
	startTimer(1000);
	main_window = parentWidget()->parentWidget();
}


RenderWidget::~RenderWidget()
{
	d3d11_entry.CleanupDevice();
}

void RenderWidget::Init(UINT width, UINT height)
{
	setFixedSize(width, height);
	d3d11_entry.SetSize(width, height);
	d3d11_entry.InitDevice();
}

//QPainter painter(this);
//painter.drawLine(80, 100, 650, 500);
void RenderWidget::paintEvent(QPaintEvent * event)
{
	d3d11_entry.Render();
	update();
}

void RenderWidget::timerEvent(QTimerEvent * event)
{
	main_window->setWindowTitle("FPS = " + QString::number(d3d11_entry.fps));
}

bool RenderWidget::nativeEventFilter(const QByteArray &eventType, void *message, long *)
{
	if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG")
	{
		MSG* pMsg = reinterpret_cast<MSG*>(message);
		if (pMsg->message == WM_KEYDOWN)
		{
			qDebug() << "RenderWidget windows system message WM_KEYDOWN";
		}
	}
	return false;
}
发布了34 篇原创文章 · 获赞 1 · 访问量 7575
展开阅读全文

Direct3D学习 窗口无显示内容

05-21

我是Direct3D的初学者,运行书上的代码时窗口为一片空白,没有显示。校验过几遍代码了,应该没有错,希望有大佬帮忙解答一下 代码如下: #include<Windows.h> #include<d3d9.h> #include<time.h> #include<iostream> using namespace std; #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") //program settings const string APPTITLE = "Direct3D_Windowed"; const int SCREENW = 1024; const int SCREENH = 768; //Direct3D objects LPDIRECT3D9 d3d = NULL; LPDIRECT3DDEVICE9 d3ddev = NULL; bool gameover = false; //macro to detect key presses #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code)&0x8000)?1:0) /* Game initialization function */ bool Game_Init(HWND window) { //MessageBox(window, "Game_Init", "BREAKPOINT", 0); //initialize Direct3D d3d = Direct3DCreate9(D3D_SDK_VERSION); if (d3d == NULL) { MessageBox(window, "Error initializing Direct3D", "Error", MB_OK); return 0; } //set Direct3D presentation parameters D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = SCREENW; d3dpp.BackBufferHeight = SCREENH; d3dpp.hDeviceWindow = window; //create Direct3D device d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev ); if (d3ddev == NULL) { MessageBox(window, "Error creating Direct3D device", "Error", MB_OK); return 0; } return true; } /* Game update function */ void Game_Run(HWND hwnd) { //make sure the Direct3D device is valid if (!d3ddev) return; //clear the backbuffer to bright green d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0); //start rendering if (d3ddev->BeginScene()) { //do something? //stop rendering d3ddev->EndScene(); //copy back buffer on the screen d3ddev->Present(NULL, NULL, NULL, NULL); } //check for escape key(to exit program) if (KEY_DOWN(VK_ESCAPE)) PostMessage(hwnd, WM_DESTROY, 0, 0); } /* Game shutdown function */ void Game_End(HWND hwnd) { //display close message MessageBox(hwnd, "Program is about to end", "Game_End", MB_OK); //free memory if (d3ddev) d3ddev->Release(); if (d3d) d3d->Release(); } /* Windows event handing function */ LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_DESTROY: gameover = true; PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); } /* Main Windows entry function */ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { //set the new window's properties WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WinProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = APPTITLE.c_str(); wc.hIconSm = NULL; RegisterClassEx(&wc); //create a new window HWND window = CreateWindow(APPTITLE.c_str(), APPTITLE.c_str(), //WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT, SCREENW,SCREENH, NULL,NULL,hInstance,NULL ); //was there an error creating the window? if (window == 0) return 0; //display the window ShowWindow(window, nCmdShow); UpdateWindow(window); //initialize the game if (!Game_Init(window)) return 0; //main message loop MSG message; while (!gameover) { if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&message); DispatchMessage(&message); } Game_Run(window); } Game_End(window); return message.wParam; } 这是运行结果: ![图片说明](https://img-ask.csdn.net/upload/201905/21/1558407087_94690.png) 另外还有一个小问题,为什么在改变了其分辨率的值以后程序就不能运行了?分辨率的设置有什么特殊要求吗?谢谢大神 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览