C++小项目:directx11图形程序(二):systemclass

时间:2023-03-09 05:10:29
C++小项目:directx11图形程序(二):systemclass

先上代码:

systemclass.h

 #pragma once
#include"graphicsclass.h"
const bool FULLSCREEN = true;
class systemclass
{
public:
systemclass();
~systemclass();
bool Initialize();
void Run();
void Shutdown();
private:
graphicsclass *m_graphics;
HWND m_hwnd;
bool InitializeWindow(int screenwidth=, int screenheight=);
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
};

从这个类头文件里可以看到,这个类有:

  • 3个公有方法:前面mian.cpp里都调用了它们
  • 1个私有方法:这是用来初始化窗口的函数,在公有方法Initialize()里调用
  • 1个静态私有方法:这是windows窗口的消息回掉函数,只是windows程序规定要有的,其实没有做任何事
  • 2个数据成员,一个graphicsclass对象,一个窗口句柄

systemclass.cpp

 #include "systemclass.h"

 systemclass::systemclass()
{
} systemclass::~systemclass()
{
}
bool systemclass::Initialize()
{
int screenwidth, screenheight;
screenheight = ;
screenwidth = ;
InitializeWindow(); m_graphics = new graphicsclass; if (!m_graphics)
{
return false;
}
bool result = m_graphics->Initialize(m_hwnd);
if (!result)
{
MessageBox(m_hwnd, L"graphics initialize failed!", , MB_OK);
} return true;
}
void systemclass::Run()
{
MSG msg;
bool flag = true;
while (flag)
{
if (PeekMessage(&msg, NULL, , , PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} if (msg.message == WM_QUIT)
{
flag = false;
}
flag=m_graphics->Frame();
}
}
void systemclass::Shutdown()
{
if (m_graphics)
{
m_graphics->Shutdown();
delete m_graphics;
m_graphics = ;
}
if (m_hwnd)
{
m_hwnd = ;
}
}
bool systemclass::InitializeWindow(int screenwidth, int screenheight)
{
WNDCLASSEX wc; int posX, posY;
posX = ;
posY = ;
int screenWidth = screenwidth;
int screenHeight = screenheight; HINSTANCE hinstance = GetModuleHandle(NULL); LPCWSTR applicationName = L"Engine"; wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = ;
wc.cbWndExtra = ;
wc.hInstance = hinstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hIconSm = wc.hIcon;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = applicationName;
wc.cbSize = sizeof(WNDCLASSEX); RegisterClassEx(&wc);
if (FULLSCREEN==true)
{
screenWidth = GetSystemMetrics(SM_CXSCREEN);
screenHeight = GetSystemMetrics(SM_CYSCREEN);
} m_hwnd = CreateWindowEx(WS_EX_APPWINDOW, applicationName, applicationName,
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP,
posX, posY, screenWidth, screenHeight, NULL, NULL, hinstance, NULL); ShowWindow(m_hwnd, SW_SHOW); return true;
}
LRESULT CALLBACK systemclass::WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam)
{
switch (umessage)
{
case WM_DESTROY:
{
PostQuitMessage();
return ;
}
case WM_CLOSE:
{
PostQuitMessage();
return ;
}
default:
return DefWindowProc(hwnd, umessage, wparam, lparam);
}
return ;
}

Public: 

Initialize()函数:调用了InitializeWindow()函数,生成了graphicsclass对象,调用了graphics类的Initialize()方法

Run()函数:windows消息循环,循环里调用了graphics类的Frame()方法,只要该方法的返回值不为false就一直循环。这其实是经典windows窗口程序有的一部分,本来peekMessage()部分是很重要的一部分,但是在本程序中它并没有作用,如果读者还有疑问,可以参考windows窗口程序相关资料。这里只要知道它是不断循环调用graphics类的Frame()方法就好了。

Shutdown()函数:清理生成的graphicsclass对象。相当于是虚析构函数。

private:

InitialilzeWindow()函数:这是systemclass所做的比较实质性的工作,生成一个windows窗口,我们来看它的流程:

  • 声明一个WNDCLASS数据结构wc,这是描述你要生成的窗口的数据结构
  • 获取当前程序的实例句柄hinstance,它是程序的一个标识,在这个程序中使用它的地方不多,我就不详细介绍它了
  • 填充wc数据结构,重要的部分有style:窗口风格;lpfnWndProc,消息回掉函数;hInstance,程序实例句柄;lpszClassName,窗口类名字;cbSize,wndclass数据结构的大小
  • 注册窗口
  • 调用windowsAPI:CreateWindowEx()创建窗口,获得一个窗口句柄
  • 显示窗口

WndProc()函数:这是消息回掉函数,响应操作系统通知的消息,没有实质性作用

我们可以看到,systemclass主要工作就是创建一个窗口并添加消息循环,另外就是生成一个graphicsclass对象,调用graphicsclass对象的Initialize方法和Frame方法。 我们可以猜到,涉及到图形渲染的工作的部分还藏在graphicclass里。