|
本帖最后由 小生怕怕啊 于 2015-6-11 10:03 编辑
继上一篇,增加了一个微小的引擎类,此类还不具备基本的引擎功能,后面慢慢填充。
首先是引擎类的头文件声明
- #ifndef _ENGINE_H_
- #define _ENGINE_H_
- #include <windows.h>
- #include <d3d9.h>
- #include <d3dx9.h>
- #include "Functions.h"
- #pragma comment(lib,"d3d9.lib")
- #pragma comment(lib,"d3dx9.lib")
- //引擎类 ~引擎虽小,五脏俱全
- class Engine
- {
- public:
- Engine();
- ~Engine();
- bool Init(HWND hwnd,UINT uWidth,UINT uHeight,bool bFullscreen=false );//初始化 ~看字明意
- void ClearScreen(D3DCOLOR color=D3DCOLOR_XRGB(0,0,0)); //清屏 ~简洁明了
- void StartRender(); //开始渲染 ~看字明意
- void EndRender();//结束渲染 ~看字明意
- void Shutdown();//关闭 ~看字明意
- private:
- LPDIRECT3D9 m_D3d; //D3D接口指针
- LPDIRECT3DDEVICE9 m_Dev; //D3D设备
- HWND hMainHwnd; //窗口句柄 ~不知道要不要用
- };
- #endif
复制代码
然后是引擎类的实现
- #include "Engine.h"
- Engine::Engine()
- {
- m_Dev=NULL;
- m_D3d=NULL;
- hMainHwnd=NULL;
- }
- Engine::~Engine()
- {
- Shutdown(); //关闭引擎
- }
- bool Engine::Init( HWND hwnd,UINT uWidth,UINT uHeight,bool bFullscreen )
- {
- if (NULL==(m_D3d=Direct3DCreate9(D3D_SDK_VERSION))) //获取D3D接口指针
- {
- ShowMessage("创建D3D接口失败!");
- return false;
- }
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory(&d3dpp,sizeof(d3dpp));
- d3dpp.BackBufferCount=1; //缓冲数量
- d3dpp.BackBufferWidth=uWidth; //缓冲宽度
- d3dpp.BackBufferHeight=uHeight; //缓冲高度
- d3dpp.BackBufferFormat=D3DFMT_X8R8G8B8; //缓冲格式
- d3dpp.EnableAutoDepthStencil=TRUE; //开启自动深度缓冲
- d3dpp.AutoDepthStencilFormat=D3DFMT_D24S8; //深度缓冲格式
- d3dpp.Windowed=!bFullscreen; //是否全屏
- d3dpp.hDeviceWindow=hwnd; //设备窗口
- d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD; //交换效果
- //创建D3D设备
- if (FAILED(m_D3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&m_Dev)))
- {
- ShowMessage("创建D3D设备失败!");
- return false;
- }
- return true;
- }
- void Engine::Shutdown()
- {
- SAFE_RELEASE(m_Dev);
- SAFE_RELEASE(m_D3d);
- }
- void Engine::ClearScreen( D3DCOLOR color/*=D3DCOLOR_XRGB(0,0,0)*/ )
- {
- m_Dev->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,color,1.0f,0);
- }
- void Engine::StartRender()
- {
- m_Dev->BeginScene();
- }
- void Engine::EndRender()
- {
- m_Dev->EndScene();
- m_Dev->Present(NULL,NULL,NULL,NULL);
- }
复制代码
另外增加了一个公共函数文件
- #ifndef _FUNCTIONS_H_
- #define _FUNCTIONS_H_
- #include <windows.h>
- #define SAFE_RELEASE(P) {if (P!=NULL){P->Release();P=NULL;}}
- void ShowMessage(LPCSTR lpStr);
- #endif
- <BLOCKQUOTE>
复制代码- #include "Functions.h"
- void ShowMessage( LPCSTR lpStr )
- {
- MessageBox(NULL,lpStr,"Info",MB_OK);
- }
- <BLOCKQUOTE>
复制代码 最后是WinMain窗体的代码,继上一篇的代码,修改部分代码
- <P>#include <windows.h>
- #include "Engine.h"</P>
- <P>#define WIN_CLASS "dx01"//窗体类名
- #define WIN_NAME "dx01" //窗体标题
- #define WIN_WIDTH 800 //窗体宽度
- #define WIN_HEIGHT 600 //窗体高度
- #define WIN_FULLSCREEN false //是否全屏</P>
- <P>
- Engine engine; //引擎对象</P>
- <P>//循环
- void GameLoop();</P>
- <P>//Windows消息回调函数
- LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
- {
- switch(msg)
- {
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_KEYUP:
- if (wparam==VK_ESCAPE)
- {
- PostQuitMessage(0);
- }
- break;
- }
- return DefWindowProc(hwnd,msg,wparam,lparam);
- }</P>
- <P>//Window入口函数
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
- {
- //设置窗口结构类属性
- WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,WndProc,0,0,hInstance,NULL,NULL,NULL,NULL,WIN_CLASS,NULL};
- //注册窗口类型
- RegisterClassEx(&wc);
- //创建窗口
- HWND hwnd= CreateWindow(WIN_CLASS,WIN_NAME,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,WIN_WIDTH,WIN_HEIGHT,NULL,NULL,hInstance,NULL);
- if (hwnd)
- {
- ShowWindow(hwnd,nShowCmd); //显示窗口
- UpdateWindow(hwnd); //更新窗口
-
- if (engine.Init(hwnd,WIN_WIDTH,WIN_HEIGHT))
- {
- MSG msg;
- ZeroMemory(&msg,sizeof(msg));
- //窗口消息循环
- while (msg.message!=WM_QUIT)
- {
- if (PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
- {
- TranslateMessage(&msg); //转换消息
- DispatchMessage(&msg); //分派消息
- }
- else
- {
- GameLoop();
- }
- }
- engine.Shutdown();
- }
- }
- UnregisterClass(WIN_CLASS,wc.hInstance);
- return 0;
- }</P>
- <P>void GameLoop()
- {
- engine.StartRender(); //开始渲染</P>
- <P> engine.EndRender(); //结束渲染
- }</P>
复制代码
运行截图
源代码下载:
dx01.rar
(1.77 MB, 下载次数: 2)
|
|