📄 d3dwnd.cpp
字号:
// D3DWnd.cpp : implementation file
//
#include "stdafx.h"
#include "Ex_D3D.h"
#include "D3DWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CD3DWnd
CD3DWnd::CD3DWnd()
{
}
CD3DWnd::~CD3DWnd()
{
}
BEGIN_MESSAGE_MAP(CD3DWnd, CWnd)
//{{AFX_MSG_MAP(CD3DWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CD3DWnd message handlers
int CD3DWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
// 创建Direct3D对象,并获取接口IDirect3D9的指针,
// 我们将通过该指针操作Direct3D对象。
m_pD3D = ::Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
::ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE; // 创建窗口模式的Direct3D程序
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// 调用方法IDirect3D9::CreateDevice创建设备对象,并获取
// 接口IDirect3DDevice9的指针,我们将通过该指针操作设备对象
m_pD3D->CreateDevice(
D3DADAPTER_DEFAULT, // 使用缺省的显卡
D3DDEVTYPE_HAL, // 指定设备类型为HAL
m_hWnd, // Direct3D窗口的句柄
D3DCREATE_SOFTWARE_VERTEXPROCESSING,//软件顶点处理
&d3dpp, &m_pDevice);
return 0;
}
void CD3DWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// 用指定颜色清除后备缓存区
m_pDevice->Clear(
0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,0,255), // 指定使用蓝色
1.0f, 0);
// Direct3D规定在渲染前必须调用方法IDirect3DDevice9::BeginScene,
// 结束时要调用IDirect3DDevice9::EndScene。
m_pDevice->BeginScene();
// 实际的渲染代码放在此处。因为本节只是为了演示如何初始化Direct3D,
// 所以这里为空,生成的Direct3D窗口将是一个蓝色背景的空白窗口
m_pDevice->EndScene();
// 交换当前/后备缓存区,刷新窗口
m_pDevice->Present(NULL, NULL, NULL, NULL);
// Do not call CWnd::OnPaint() for painting messages
}
void CD3DWnd::OnDestroy()
{
CWnd::OnDestroy();
// TODO: Add your message handler code here
m_pDevice->Release(); // 释放设备对象
m_pD3D->Release(); // 释放Direct3D对象
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -