Directx11教程(3) 一個最基本D3D應用程序(1)
在前一篇教程程序代碼的基礎上,這次我們將增加2個類:
InputClass,鍵盤處理的代碼將放在這個類裏面,GraphicsClass類,D3D渲染的代碼放在這個類裏,這兩個類都是SystemClass類的成員變量,SystemClass類中會調用這2個類實例的初始化、渲染以及shutdown函數。
增加這個兩個類後,應用的程序的框架如下:
System Class類有點小變動,增加了兩個成員變量m_Input,m_Graphics,分別處理輸入和渲染的操作。
SystemClass.h改變的代碼如下:
class SystemClass{
public:
SystemClass(void);
…
HWND m_hwnd;
//InputClass和GraphicsClass是SystemClass的兩個成員變量
InputClass* m_Input;
GraphicsClass* m_Graphics;
};
SystemClass::SystemClass(void)
{
m_Input = 0;
m_Graphics = 0;
}
SystemClass::SystemClass(const SystemClass &)
{
}
SystemClass::~SystemClass(void)
{
}
//調用窗口初始化函數和其它一些類的初始化函數
bool SystemClass::Initialize()
{
int screenWidth = 0, screenHeight = 0;
// 初始化窗口
InitializeWindows(screenWidth, screenHeight);
//創建input對象處理鍵盤輸入
m_Input = new InputClass;
if(!m_Input)
{
return false;
}
// 初始化輸入對象.
m_Input->Initialize();
// 創建圖形對象,這個對象將渲染應用程序中的所有物體
m_Graphics = new GraphicsClass;
if(!m_Graphics)
{
return false;
}
// 初始化圖形對象
result = m_Graphics->Initialize(screenWidth, screenHeight, m_hwnd);
if(!result)
{
return false;
}
return true;
}
void SystemClass::Shutdown()
{
//其它類的一些銷毀工作
if(m_Graphics)
{
m_Graphics->Shutdown();
delete m_Graphics;
m_Graphics = 0;
}
if(m_Input)
{
delete m_Input;
m_Input = 0;
}
// 執行一些銷毀工作.
ShutdownWindows();
}
在Frame函數中檢測用戶按鍵以及調用圖形渲染函數。
bool SystemClass::Frame(){
bool result;
//檢測用戶是否按下ESC鍵,如果按下,退出程序.
if(m_Input->IsKeyDown(VK_ESCAPE))
{
return false;
}
// 執行幀渲染函數.
bool result = m_Graphics->Frame();
if(!result)
{
return false;
}
return true;
}
{
switch(umsg)
{
// 檢測按鍵消息.
case WM_KEYDOWN:
{
m_Input->KeyDown((unsigned int)wparam);
return 0;
}
case WM_KEYUP:
{
m_Input->KeyUp((unsigned int)wparam);
return 0;
}
//任何其它消息發送到windows缺省處理.
default:
{
return DefWindowProc(hwnd, umsg, wparam, lparam);
}
}
}
#pragma once
class InputClass
{
public:
InputClass(void);
InputClass(const InputClass&);
~InputClass(void);
void Initialize();
void KeyDown(unsigned int);
void KeyUp(unsigned int);
bool IsKeyDown(unsigned int);
private:
bool m_keys[256];//用來存放256個虛擬鍵是否被按下
};
InputClass.cpp代碼如下:
#include "InputClass.h"
InputClass::InputClass(void)
{
}
InputClass::InputClass(const InputClass&)
{
}
InputClass::~InputClass(void)
{
}
void InputClass::Initialize()
{
int i;
// 初始所有的鍵都是非按下狀態.
for(i=0; i<256; i++)
{
m_keys[i] = false;
}
return;
}
void InputClass::KeyDown(unsigned int input)
{
//鍵被按下.
m_keys[input] = true;
return;
}
void InputClass::KeyUp(unsigned int input)
{
//釋放按鍵.
m_keys[input] = false;
return;
}
bool InputClass::IsKeyDown(unsigned int key)
{
// 判斷鍵是否被按下?
return m_keys[key];
}
GraphicsClass.h代碼如下:
#pragma once
#include <windows.h>
/////////////
// GLOBALS //
/////////////
const bool FULL_SCREEN = false; //是否全屏
const bool VSYNC_ENABLED = true; //是否垂直同步
const float SCREEN_DEPTH = 1000.0f; //深度,遠點
const float SCREEN_NEAR = 0.1f; //深度,近點
class GraphicsClass
{
public:
GraphicsClass(void);
GraphicsClass(const GraphicsClass&);
~GraphicsClass(void);
bool Initialize(int, int, HWND);
void Shutdown();
bool Frame();
private:
bool Render();
};
GraphicsClass.cpp代碼如下,註意現在只是一個框架,並沒有實際的渲染代碼,下一篇日誌中,我們將加入D3D的初始化操作代碼:
#include "GraphicsClass.h"
GraphicsClass::GraphicsClass(void)
{
}
GraphicsClass::GraphicsClass(const GraphicsClass&)
{
}
GraphicsClass::~GraphicsClass(void)
{
}
bool GraphicsClass:: Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
return true;
}
void GraphicsClass::Shutdown()
{
return;
}
bool GraphicsClass::Frame()
{
return true;
}
bool GraphicsClass::Render()
{
return true;
}
程序執行後,界面和前兩篇教程給出的是一樣的。
工程文件myTutorialD3D11_2
代碼下載:
http://files.cnblogs.com/mikewolf2002/myTutorialD3D11.zip
Directx11教程(3) 一個最基本D3D應用程序(1)