对HGE游戏引擎的一次封
本文转载至http://blog.csdn.net/rcfalcon/article/details/5307367
HGE游戏引擎是一个开源2D游戏引擎,基于directX。
它的渲染及逻辑是基于帧回调的框架模式,
其提供一些基本的图像操作和输入控制功能。
我在之前写一个2D游戏的时候对它整个框架进行了一次封装,很多地方写得比较恶心,请原谅我在变量上命名的不规范。
在此分享出来,主要可以参考一下大体框架吧。
HGE游戏引擎基于帧的回调让我很不爽,因为咱的游戏逻辑一般是连续的,跟具体帧无关,所以我把整个HGE跑在自己的一个线程之中。我的游戏逻辑部分通过发消息,通知界面更新 以及获取用户输入。
在HGE的主框架下分四个部分
1. 资源管理 2.消息管理 3.声音管理 4.UI管理
下面是GDE_UI_HGEFramework.h
1 /*
2 * CopyRight 2009 - 2010 GDE工作室
3 * 游戏UI系统 - HGE 引擎框架类
4 * ===================================
5 * 整个UI系统调用HGE游戏引擎的主框架
6 *
7 * 2010/01/08 cg create
8 */
9
10 #ifndef GDE_UI_HGE_FRAMEWORK_H_
11 #define GDE_UI_HGE_FRAMEWORK_H_
12
13 #include "GDE_UI_ResourceManager.h"
14 #include "GDE_UI_UIManager.h"
15 #include "GDE_UI_MessageManager.h"
16 #include "GDE_UI_SoundManager.h"
17
18
19 #pragma comment(lib,"GDE_UI/HGE/lib/hge.lib")
20 #pragma comment(lib,"GDE_UI/HGE/lib/hgehelp.lib")
21 #pragma comment(linker, "/NODEFAULTLIB:libc.lib")
22
23 #include "GDE_UI_thread_core.h"
24
25 namespace GDE
26 {
27 namespace GDE_UI
28 {
29
30 //HGE引擎框架
31 class HGE_Framework
32 {
33 public:
34 //唯一实例
35 static HGE_Framework* Instance()
36 {
37 static HGE_Framework* instance_ = 0;
38 if( !instance_ )
39 {
40 instance_ = new HGE_Framework();
41 }
42 return instance_;
43 }
44
45 ~HGE_Framework()
46 {
47 delete m_ResManager;
48 delete m_MessageManager;
49 delete m_SoundManager;
50 delete m_UIManager;
51 }
52
53 //开始运行
54 void Run()
55 {
56 //获取资源指针
57 pHGE = m_ResManager->GetHGEPtr();
58 pGUI = m_ResManager->GetGuiPtr();
59
60 //初始化子类中的HGE核心指针
61 m_MessageManager->Ready();
62 m_UIManager->Ready();
63
64 //开始主循环
65 if(pHGE->System_Initiate() && pHGE)
66 {
67 pHGE->System_Start();
68 }
69 else
70 MessageBox(NULL, pHGE->System_GetErrorMessage(),
71 "Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
72 }
73
74 //HGE框架逻辑函数
75 static bool FrameFunc()
76 {
77 SMART_LOCK( 0 )//线程锁
78
79 HGE_Framework::Instance()->m_MessageManager->ProcessMessage();//处理消息
80 HGE_Framework::Instance()->m_UIManager->Handler();//处理用户操作
81
82 return FALSE;
83 }
84
85 //HGE框架渲染函数
86 static bool RenderFunc()
87 {
88 SMART_LOCK( 0 )//线程锁
89
90 HGE_Framework::Instance()->pHGE->Gfx_Clear(0); //清屏
91 HGE_Framework::Instance()->pHGE->Gfx_BeginScene(); //开始渲染
92
93 HGE_Framework::Instance()->m_UIManager->Render();//绘制
94
95 HGE_Framework::Instance()->pHGE->Gfx_EndScene(); //结束渲染
96
97 return FALSE;
98 }
99
100 //成员子管理器指针
101 ResourceManager* m_ResManager;
102 MessageManager* m_MessageManager;
103 SoundManager* m_SoundManager;
104 UIManager* m_UIManager;
105
106 //HGE系统相关资源指针
107 HGE* pHGE;
108 hgeGUI* pGUI;
109
110 private:
111 //私有构造,不允许实例化
112 HGE_Framework()
113 :pHGE(0)
114 ,pGUI(0)
115 {
116 m_ResManager = new ResourceManager();
117 m_MessageManager = new MessageManager();
118 m_SoundManager = new SoundManager();
119 m_UIManager = new UIManager();
120 }
121 };
122
123
124 }
125 }
126 #endif;
比较核心的是资源管理类,下面给出
GDE_UI_ResourceManager.h
/*
* CopyRight 2009 - 2010 GDE工作室
* 游戏UI系统 - HGE 引擎框架类 - 资源管理器
* ===================================
* HGE引擎资源管理类
*
* 本类的资源主要实现对HGE资源的维护 和对HGE API封装提供工具集
* 并维护HGE的配置环境
*
* 2010/01/08 cg create
*/
#ifndef GDE_UI_FRAMEWORK_RESOURCEMANAGER_H_
#define GDE_UI_FRAMEWORK_RESOURCEMANAGER_H_
#include "./HGE/cn/GfxFont.h"
#include "./HGE/cn/GfxEdit.h"
#include "./HGE/include/hge.h"
#include "./HGE/include/hgegui.h"
#include "./HGE/include/hgeguictrls.h"
#include "./HGE/include/hgeresource.h"
#include <string>
namespace GDE
{
namespace GDE_UI
{
//资源管理类
class ResourceManager
{
public:
//构造函数,加载HGE全局资源
ResourceManager();
//析构函数,释放HGE全局资源
~ResourceManager();
//获取HGE指针
HGE* GetHGEPtr() { return pHGE; }
//获取GUI指针
hgeGUI* GetGuiPtr() { return pGUI; }
//读取图片
void LoadImage( std::string const& filename, hgeSprite*& p_sprite, HTEXTURE& tex );
private:
HGE* pHGE;
hgeGUI* pGUI;
hgeResourceManager m_HgeResManager;
};
}
}
#endif
GDE_UI_ResourceManager.cpp
1 #include "GDE_UI_HGEFramework.h"
2 #include "GDE_UI_ResourceManager.h"
3
4 namespace
5 {
6 std::string const RES_MANAGER_FILENAME = "res/res_s";
7 int const WIN_SCREENWIDTH = 800;
8 int const WIN_SCREENHEIGHT = 600;
9 std::string const WIN_TITILE = "HGE TEST WINDOW";
10 int const WIN_COLORMODE = 32;
11 int const WIN_MAXFPS = 100;
12 int const WIN_WINDOWED = true;
13
14 }
15
16 namespace GDE
17 {
18 namespace GDE_UI
19 {
20
21 //构造函数
22 ResourceManager::ResourceManager()
23 : m_HgeResManager( RES_MANAGER_FILENAME.c_str() )
24 {
25 //建立HGE全局实例
26 pHGE = hgeCreate(HGE_VERSION);
27
28 //初始化配置
29 pHGE->System_SetState(HGE_INIFILE, "hgedemo.ini");
30 pHGE->System_SetState(HGE_LOGFILE, "hgedemo.log");
31 pHGE->System_SetState(HGE_FRAMEFUNC, &HGE_Framework::FrameFunc);
32 pHGE->System_SetState(HGE_RENDERFUNC, &HGE_Framework::RenderFunc);
33 pHGE->System_SetState(HGE_TITLE, WIN_TITILE.c_str());
34 pHGE->System_SetState(HGE_SCREENWIDTH, WIN_SCREENWIDTH);
35 pHGE->System_SetState(HGE_SCREENHEIGHT, WIN_SCREENHEIGHT);
36 pHGE->System_SetState(HGE_SCREENBPP, WIN_COLORMODE);
37 pHGE->System_SetState(HGE_FPS,WIN_MAXFPS);
38 pHGE->System_SetState(HGE_WINDOWED, WIN_WINDOWED);
39 pHGE->System_SetState(HGE_HIDEMOUSE, false);
40 pHGE->System_SetState(HGE_USESOUND, false);
41 pHGE->System_SetState(HGE_DONTSUSPEND, true);//失去焦点不挂起
42 pHGE->System_SetState((hgeIntState)14, (int)0xFACE0FF); //禁用HGE LOGO
43
44 //初始化GUI
45 pGUI = new hgeGUI();
46 pGUI->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
47 pGUI->Enter();
48 }
49
50 ResourceManager::~ResourceManager()
51 {
52 // 销毁gui
53 delete pGUI;
54
55 // 销毁资源管理器
56 m_HgeResManager.Purge();
57
58 // HGE结束
59 pHGE->System_Shutdown();
60 pHGE->Release();
61 }
62
63 void ResourceManager::LoadImage( std::string const& filename, hgeSprite*& p_sprite, HTEXTURE& tex )
64 {
65 if( p_sprite )
66 delete p_sprite; //销毁之前的图片
67
68 tex = pHGE->Texture_Load( filename.c_str() ); //装载纹理
69 int w = pHGE->Texture_GetWidth( tex );
70 int h = pHGE->Texture_GetHeight( tex );
71 p_sprite = new hgeSprite( tex, 0, 0, w, h ); //剪裁精灵
72 }
73
74
75 }
76 }
比较重要的是消息传递机制,我仿照了MFC的消息宏定义机制,下面给出消息模块的核心代码
1 //消息映射宏定义
2 #define BEGIN_MESSAGE_MAP()/
3 typedef void (MessageManager::*MessageFunction)();/
4 std::map<UIMessage, MessageFunction> message_functions_;/
5 void InitMessageMap()/
6 {
7 #define ON_MESSAGE( ID, memberFuc )/
8 message_functions_[ID] = &MessageManager::memberFuc;
9 #define END_MESSAGE_MAP/
10 }
11
12
13
14 ....
15
16
17
18 //这里是我游戏中具体的一些消息
19
20 //消息类型
21 typedef int UIMessage;
22
23 UIMessage const UIMessage_UpdateBackImg = 0; //更新背景图片
24
25 UIMessage const UIMessage_AddRole = 10; //增加角色
26 UIMessage const UIMessage_RemoveRole = 11; //删除角色
27 UIMessage const UIMessage_UpdateRole = 12; //更新角色列表
28 UIMessage const UIMessage_AttackInfo = 13; //角色掉血显示
29
30 UIMessage const UIMessage_UpdateDialog = 20; //更新对话框
31 UIMessage const UIMessage_HideDialog = 21; //隐藏对话框
32
33 UIMessage const UIMessage_UpdateSmallMap = 30; //更新小地图
34
35 UIMessage const UIMessage_UpdateGameInfo = 40; //更新游戏提示信息
36
37 UIMessage const UIMessage_UpdateButton = 50; //增加按钮
38 UIMessage const UIMessage_RemoveButton = 51; //删除按钮
39 UIMessage const UIMessage_RemoveAllButtons = 52; //移除所有按钮
40
41 UIMessage const UIMessage_UpdatePopMenu = 60; //弹出菜单
42 UIMessage const UIMessage_HidePopMenu = 61; //隐藏弹出菜单
43
44 UIMessage const UIMessage_UpdateZoneBlock= 70; //更新地图块
45 UIMessage const UIMessage_HideZoneBlock = 71; //清除地图块
46
47
48 //消息映射实现
49
50 BEGIN_MESSAGE_MAP()
51 ON_MESSAGE( UIMessage_UpdateBackImg, UpdateBackImage )
52 ON_MESSAGE( UIMessage_AddRole, AddRole )
53 ON_MESSAGE( UIMessage_RemoveRole, RemoveRole )
54 ON_MESSAGE( UIMessage_UpdateRole, UpdateRole )
55 ON_MESSAGE( UIMessage_AttackInfo, RoleAttackInfo )
56 ON_MESSAGE( UIMessage_UpdateDialog, UpdateDialog )
57 ON_MESSAGE( UIMessage_HideDialog, HideDialog )
58 ON_MESSAGE( UIMessage_UpdateSmallMap, UpdateSmallMap )
59 ON_MESSAGE( UIMessage_UpdateGameInfo, UpdateGameInfo )
60 ON_MESSAGE( UIMessage_UpdateButton, UpdateButton )
61 ON_MESSAGE( UIMessage_RemoveButton, RemoveButton )
62 ON_MESSAGE( UIMessage_RemoveAllButtons, RemoveAllButtons )
63 ON_MESSAGE( UIMessage_UpdatePopMenu, UpdatePopMenu )
64 ON_MESSAGE( UIMessage_HidePopMenu, HidePopMenu )
65 ON_MESSAGE( UIMessage_UpdateZoneBlock, UpdateZoneBlock )
66 ON_MESSAGE( UIMessage_HideZoneBlock, HideZoneBlock )
67 END_MESSAGE_MAP()
68
69
70 //处理消息
71 void ProcessMessage()
72 {
73 for( int i = 0; i < m_MessageList.size(); ++i )
74 {
75 ( this->*message_functions_[m_MessageList[i]] )();
76 }
77 m_MessageList.clear();
78 }
最后是整个HGE引擎启动的方式
GDE_UI_HGE.h
1 #ifndef GDE_UI_HGE_H_
2 #define GDE_UI_HGE_H_
3
4 #include <windows.h>
5 #include <string>
6
7 #include "GDE_UI_HGEFramework.h"
8
9 /*
10 GDE HGE游戏引擎模块 2009-11-21
11
12 var | author | brief
13 ================================================
14 1.0 cg create
15 */
16
17 namespace GDE
18 {
19 namespace GDE_UI
20 {
21
22 //HGE线程入口
23 static DWORD WINAPI HGEThreadProc(LPVOID lpParameter)
24 {
25 HGE_Framework::Instance()->Run();
26 return 0;
27 }
28
29 }
30 }
31 #endif
游戏初始化的时候
//创建hge线程
hge_thread = CreateThread( NULL, 0,
HGEThreadProc, NULL, 0, NULL );
就可以了