//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;
}