目的:
编写启动渲染系统的代码,用于初始化Direct3D,将屏幕清屏为指定的颜色以及关闭系统。
//main.h
#ifndef _UGP_MAIN_H_
#define _UGP_MAIN_H_
#include "StrandedEngine/engine.h"
#pragma comment (lib,"lib/StrandedEngine.lib")
//窗口类名
#define WINDOW_CLASS "StrandedGame"
//窗口名
#define WINDOW_NAME "Stranded"
//窗口宽
#define WIN_WIDTH 800
//窗口高
#define WIN_HEIGHT 600
//全屏
#define FULLSCREEN 1
//初始化引擎
bool InitializeEngine();
//关闭引擎
void ShutdownEngine();
//游戏初始化
bool GameInitialize();
//游戏循环
void GameLoop();
//游戏结束
void GameShutdown();
#endif
//main.cpp
#include "main.h"
HWND g_hwnd;
CRenderInterface *g_Render = NULL;
//消息过程处理函数
LRESULT WINAPI MsgProc(HWND hd, UINT msg, WPARAM wp, LPARAM lp){
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
if (wp == VK_ESCAPE) PostQuitMessage(0);
break;
}
return DefWindowProc(hd, msg, wp, lp);
}
//主函数入口
int WINAPI WinMain(HINSTANCE h, HINSTANCE p, LPSTR cmd, int show)
{
//设置窗口
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc,
0L, 0L, GetModuleHandle(NULL), NULL, NULL,
NULL, NULL, WINDOW_CLASS, NULL };
//注册窗口
RegisterClassEx(&wc);
//判断是否全屏
if (FULLSCREEN)
{
//创建窗口
g_hwnd = CreateWindowEx(NULL, WINDOW_CLASS, WINDOW_NAME,
WS_POPUP | WS_SYSMENU | WS_VISIBLE, 0, 0,
NULL, NULL, h, NULL);
}
else
{
g_hwnd = CreateWindowEx(NULL, WINDOW_CLASS, WINDOW_NAME,
WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,
0, WIN_WIDTH, WIN_HEIGHT,
NULL, NULL, h, NULL);
}
if (g_hwnd)
{
//显示窗口
ShowWindow(g_hwnd, SW_SHOWDEFAULT);
//更新窗口
UpdateWindow(g_hwnd);
}
//初始化引擎
if (InitializeEngine())
{
//游戏初始化
if (GameInitialize())
{
//消息
MSG msg;
//清空消息
ZeroMemory(&msg, sizeof(msg));
//设置鼠标位置
SetCursorPos(0, 0);
//消息不退出时
while (msg.message != WM_QUIT)
{
//捕捉消息
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
//转换消息
TranslateMessage(&msg);
//发送消息
DispatchMessage(&msg);
}
else
//游戏循环
GameLoop();
}
}
}
//关闭游戏
GameShutdown();
//关闭游戏引擎
ShutdownEngine();
//取消注册窗口类
UnregisterClass(WINDOW_CLASS, wc.hInstance);
}
//初始化引擎
bool InitializeEngine()
{
if (!CreateD3DRender(&g_Render))return false;
if (!g_Render->Initialize(WIN_WIDTH, WIN_HEIGHT,
g_hwnd, FULLSCREEN))return false;
g_Render->SetClearCol(0, 0, 0);
return true;
}
//关闭引擎
void ShutdownEngine()
{
if (g_Render)
{
g_Render->Shutdown();
delete g_Render;
g_Render = NULL;
}
}
//初始化游戏
bool GameInitilize()
{
return true;
}
//游戏循环
void GameLoop()
{
if (!g_Render)return;
g_Render->StartRender(1, 1, 0);
g_Render->EndRendering();
}
//关闭游戏
void GameShutdown()
{
}
//engine.h
#ifndef _UGP_ENGINE_H_
#define _UGP_ENGINE_H_
#include "RenderInterface.h"
#include "D3DRenderer.h"
#endif
//defines.h
#ifndef _UGP_DEFINES_H_
#define _UGP_DEFINES_H_
#include
#define UGP_INVALID -1
#define UGP_OK 1
#define UGP_FAIL 0
#define WinHWND HWND
typedef long VertexType;
enum PrimType
{
NULL_TYPE,
POINT_LIST,
TRIANGLE_LIST,
TRIANGLE_STRIP,
TRIANGLE_FAN,
LINE_LIST,
LINT_STRIP
};
#define UGPCOLOR_ARGB(a,r,g,b) ((unsigned long)((((a)&0xff)<<24)|\
(((r)& 0xff) << 16) | (((g)& 0xff) << 8) | \
((b)& 0xff)))