C++小项目:directx11图形程序(三):graphicsclass

时间:2023-11-14 13:37:20

这是框架的第三层graphicsclass,这个类才真正可以说是整个程序的框架,因为它组织了后面所有的成员。

代码:

graphicsclass.h

 #pragma once
#include<Windows.h>
#include <d3d11.h>
#include <d3dcompiler.h>
#include <D3DX11.h>
#include <xnamath.h> #include"cameraclass.h"
#include"d3dclass.h"
#include"modelclass.h"
#include"shaderclass.h"
#include"inputclass.h"
#include"particleSysclass.h" #pragma comment(lib,"d3dx11.lib")
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d3dcompiler.lib")
class graphicsclass
{
public:
graphicsclass();
~graphicsclass();
bool Initialize(HWND hWnd );
void Shutdown();
bool Frame(); private:
d3dclass* m_d3d;
shaderclass* m_shader;
cameraclass* m_camera;
modelclass* m_model;
modelclass* m_model1;
inputclass* m_input;
particleSysclass *m_particles; ID3D11Device* m_device;
ID3D11DeviceContext* m_context;
};

从这个头文件里可以看到,首先包含了directx11开发需要用到的头文件和lib,其次包含了我自己写的也是框架出现过的类的头文件。从私有数据成员里也可窥见一斑。

但是多了两个

ID3D11Device* m_device;
ID3D11DeviceContext* m_context;

这样的指针。简单介绍一下,ID3D11Device是dx11的设备对象,ID3D11DeviceContext是dx11的设备上下文对象。在当前类里面并不涉及他们的创建。我们在这里申明它只是用来起一个桥梁作用。

graphicsclass.cpp

 #include "graphicsclass.h"

 graphicsclass::graphicsclass()
{
}
graphicsclass::~graphicsclass()
{
}
bool graphicsclass::Initialize(HWND hWnd)
{ m_d3d = new d3dclass;
m_d3d->Initialize(hWnd);
m_d3d->Getdevice(m_device);
m_d3d->Getcontext(m_context); m_shader = new shaderclass;
m_shader->Initialize(m_device, m_context); m_camera = new cameraclass; m_model = new modelclass;
m_model->Initialize(m_device, L"cube.txt", L"dx11.dds"); m_model1 = new modelclass;
m_model1->Initialize(m_device, L"cube.txt", L"stone.dds"); m_input = new inputclass;
m_input->Initialize(hWnd); m_particles = new particleSysclass;
m_particles->Initialize(m_device, L"star.dds", ); return true;
}
void graphicsclass::Shutdown()
{
m_input->Shutdown();
delete m_input; m_model1->Shutdown();
delete m_model1; m_model->Shutdown();
delete m_model; delete m_camera; m_shader->Shutdown();
delete m_shader; m_d3d->Shutdown();
delete m_d3d;
}
bool graphicsclass::Frame()
{
if (m_input->keydown(DIK_ESCAPE))
{
return false;
} static float t = ;
t += 0.002f; XMMATRIX view,pro;
m_d3d->Getpromtrx(pro); static float r = , n = , m = ;
int a = , b = , z = ;
if (m_input->mouseinput())
{
m_input->GetmouseI(a, b); }
r += a,n += b; m += z;
m_camera->SetPositon(, , -);
m_camera->SetRotation(n/, r/, );
m_camera->Getviewmatrix(view); XMVECTOR axis = { , , };
m_model->Setposition(, , );
m_model1->Setposition(, , );
m_model1->RotationAxis(axis, );
m_model->RotationAxis(axis, t); ID3D11VertexShader* vertexshader;
ID3D11PixelShader* pixelshader;
m_shader->GetShaders(vertexshader, pixelshader); m_d3d->Beginrender(, , , ); m_model->Render(m_context, view, pro, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, vertexshader, pixelshader);
m_model1->Render(m_context, view, pro, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, vertexshader, pixelshader);
m_particles->Render(m_context, view, pro, vertexshader, pixelshader); m_d3d->Endrender();
return true;
}

这段代码,我个人的感觉是非常的整齐。

Initialize()函数:就是创建对象,初始化,创建对象,初始化.....。不过与众不同的是,使用了m_d3d的Getdevice()和Getcontext()。为什么要使用呢?因为后面对象的初始化要用到ID3D11Device和ID3D11DeviceContext对象啊。这就是我所说的起到的桥梁的作用

Shutdown()函数:依然是调用组件的Shutdown()函数并释放他们,起到一个析构函数的作用

Frame()函数:调用组件的各个函数,可能是输入检测,可能是设置位置,可能是获取组件的成员,也可能是渲染。

依然好像没有涉及dx11的图形渲染。但是我们可以看到,涉及图形渲染的部分一定就在这个类所管理的组件里面了。还是继续看接下来的对各个组件的介绍吧。