DX開發框架封裝及Win32框架封裝(個人練習版)

//Utility.h


#ifndef WINDOWS_TOOLS_
#define WINDOWS_TOOLS_
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#endif
#ifndef D3D_TOOLS_
#define D3D_TOOLS_
#include <d3d9.h>
#pragma comment(lib,"d3d9.lib")
#include <d3dx9.h>
#pragma comment(lib,"d3dx9.lib")
#endif


//GameEngine.h


#ifndef GAMEENGINE_H_
#define GAMEENGINE_H_
#include "Utility.h"
#pragma once
class CGameEngine
{
public:
CGameEngine();
~CGameEngine();
VOID onInitD3D();
VOID onInitCamera();
VOID onLogic();
VOID onRender();
VOID onCleanup();
};
#endif


//GameEngine.cpp


#include "GameEngine.h"
#include "MyWinFrame.h"
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pDevice = NULL;
HWND g_hWnd = NULL;


;
CGameEngine::CGameEngine()
{
}




CGameEngine::~CGameEngine()
{
}
VOID CGameEngine::onInitD3D()
{//創建D3D的接口指針
//用來獲取當前顯卡顯示模式的結構體
//將獲取到的顯卡信息存儲到該結構體中
//---用系統定義好的結構體來描述當前設備所具有的特點
//創建接口指針
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);


if (!g_pD3D)
{
MessageBox(NULL, TEXT("Create D3D Error"), TEXT("Error"), MB_OK);
return;
}


//D3D的顯示模式
D3DDISPLAYMODE d3ddm;
ZeroMemory(&d3ddm, sizeof(d3ddm));
//獲取當前顯卡顯示模式
g_pD3D->GetAdapterDisplayMode(
D3DADAPTER_DEFAULT,//默認顯卡
&d3ddm //顯示模式信息存儲在這裏
);


D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));


//--------窗口模式的寫法----------
//d3dpp.Windowed = true;


////--------全屏模式的寫法----------
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = d3ddm.Width;
d3dpp.BackBufferHeight = d3ddm.Height;


d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = d3ddm.Format;


d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.EnableAutoDepthStencil = TRUE;


d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;


//創建設備指針
g_pD3D->CreateDevice(
D3DADAPTER_DEFAULT,//默認顯卡
D3DDEVTYPE_HAL,//設備類型
g_hWnd, //窗口句柄
D3DCREATE_SOFTWARE_VERTEXPROCESSING,//定點軟處理
&d3dpp, //設備屬性
&g_pDevice //返回的設備指針
);
if (!g_pDevice)
{
MessageBox(NULL, TEXT("Create D3DDevice Error"), TEXT("Error"), MB_OK);
return;
}


g_pDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);


}
VOID CGameEngine::onInitCamera()
{
D3DXMATRIX matView;
D3DXMatrixIdentity(&matView);
D3DXVECTOR3 vCameraPos(0,3,-15);
D3DXVECTOR3 vLookAt(0,0,0);
D3DXVECTOR3 vUp(0,1,0);
D3DXMatrixLookAtLH(&matView, &vCameraPos, &vLookAt, &vUp);
g_pDevice->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIX matPoj;
D3DXMatrixIdentity(&matPoj);
D3DXMatrixPerspectiveFovLH(&matPoj,D3DX_PI / 3, 16.0f / 9.0f,1.0f,5000.0f);
g_pDevice->SetTransform(D3DTS_PROJECTION,&matPoj);
}
VOID CGameEngine::onLogic()
{
}
VOID CGameEngine::onRender()
{
g_pDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, D3DCOLOR_XRGB(123, 125, 234), 1.0f, 0);
g_pDevice->BeginScene();
g_pDevice->EndScene();
g_pDevice->Present(NULL, NULL, NULL, NULL);
}
VOID CGameEngine::onCleanup()
{
if (g_pDevice)
{
g_pDevice->Release();
g_pDevice = NULL;
}
if (g_pD3D)
{
g_pD3D->Release();
g_pD3D = NULL;
}
}


//MyWinFrame.h


#ifndef MYWINFRAME_H_
#define MYWINFRAME_H_
#pragma once
#include "Utility.h"
#include "GameEngine.h"
class CMyWinFrame
{
private:
MSG m_uMsg;
WNDCLASS wc;
CGameEngine* m_game;
public:
CMyWinFrame();
~CMyWinFrame();
BOOL RegisterGLWindow(char *title);
BOOL CreateGLWindow(char *title);
MSG getMessage(){ return m_uMsg; };
static LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
};
#endif


//MyWinFrame.cpp


#include "MyWinFrame.h"


extern HWND g_hWnd;
HINSTANCE g_hInstance=NULL;


CMyWinFrame::CMyWinFrame()
{
m_game =new CGameEngine();
ZeroMemory(&wc,sizeof(wc));
}


CMyWinFrame::~CMyWinFrame()
{
if (m_game)
{
delete m_game;
m_game = NULL;
}
}
BOOL CMyWinFrame::RegisterGLWindow(char *title)
{
wc.style = CS_HREDRAW | CS_VREDRAW|CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WndProc; // WndProc處理消息
wc.cbClsExtra = 0; // 無額外窗口數據
wc.cbWndExtra = 0; // 無額外窗口數據
wc.hInstance = g_hInstance; // 設置實例
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // 裝入缺省圖標
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // 裝入鼠標指針
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // GL不需要背景
wc.lpszMenuName = NULL; // 不需要菜單
wc.lpszClassName = TEXT("DXFrame"); // 設定類名字
if (!RegisterClass(&wc)) // 嘗試註冊窗口類
{
MessageBox(NULL, "註冊窗口失敗", "錯誤", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // 退出並返回FALSE
}
return TRUE;
}
BOOL CMyWinFrame::CreateGLWindow(char *title)
{
g_hWnd = CreateWindow(
TEXT("OpenGL"),TEXT("Microsmile"),WS_POPUP|WS_THICKFRAME,
0,0,800,600,NULL,NULL,g_hInstance,NULL
);
ShowWindow(g_hWnd, SW_SHOWNORMAL);
UpdateWindow(g_hWnd);
m_game->onInitD3D();
m_game->onInitCamera();
while (m_uMsg.message!=WM_QUIT)
{
if (PeekMessage(&m_uMsg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&m_uMsg);
DispatchMessage(&m_uMsg);
}
else
{
m_game->onLogic();
m_game->onRender();
}
m_game->onCleanup();
}
UnregisterClass(wc.lpszClassName,g_hInstance);
return TRUE;
}
LRESULT CALLBACK CMyWinFrame::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
PlaySound(TEXT("OrcTheme.wav"),NULL,SND_FILENAME|SND_ASYNC);
return 0;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
DestroyWindow(hWnd);
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_PAINT:
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}


//FSMain.cpp


#include "Utility.h"
#include "MyWinFrame.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
CMyWinFrame CF;
if (!CF.RegisterGLWindow(TEXT("Microsmile")))
{
MessageBox(NULL,TEXT("註冊窗口類失敗!"),TEXT("錯誤"),MB_ICONWARNING|MB_OK);
return FALSE;
}
if (!CF.CreateGLWindow(TEXT("Microsmile")))
{
MessageBox(NULL, TEXT("創建窗口類失敗!"), TEXT("錯誤"), MB_ICONWARNING | MB_OK);
return FALSE;
}
return CF.getMessage().wParam;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章