HGE引擎GUI对象的扩展

HGE引擎提供一个基本的GUI控件类,其不实现任何功能,但是提供一套虚函数和属性供派生类使用。

开发者可以在此基础之上开发属于自己的控件,总的来说还是比较方便的。

我在开发自己的游戏框架过程中,吸取了许多经验教训。

建议HGE引擎的使用者们不要直接在渲染过程中使用HGE提供的图形函数,而应该将他们全部都封装在GUI控件派生类中,

这样有几大好处。

1. 更贴近面向对象,使得程序框架清晰;

2. 更好管理资源,使用和释放都在同一个小模块中,即使规模扩大,也方便维护;

3. 易复用,我们可以整理一套统一的基类,然后逐步派生、组合实现我们需要的功能。

下面给出我在开发过程中自己使用的几个方便的基类

/* * CopyRight 2009 - 2010 GDE工作室 * 游戏UI系统 - HGE游戏引擎控件基类 * =================================== * 为了提高代码的复用性,提供一些常用的功能作为基类,在构建基于HGE的GUI控件时可以派生复用。 * * 2010/01/07 cg create */ #ifndef GDE_UI_BasicClasses_H_ #define GDE_UI_BasicClasses_H_ #include "../HGE/include/hge.h" #include "../HGE/include/hgesprite.h" #include "../HGE/include/hgefont.h" #include "../HGE/include/hgerect.h" #include "../HGE/include/hgegui.h" #include "../HGE/include/hgeguictrls.h" #include "../HGE/cn/GfxFont.h" #include "../HGE/cn/GfxEdit.h" #include <string> #include <string.h> #include <stdio.h> #include <stdlib.h> /****** 默认带中文字体的基类 ******/ class GDE_BASIC_GUIChineseFont : public hgeGUIObject { public: GDE_BASIC_GUIChineseFont() { font = new GfxFont("宋体",20,TRUE,FALSE,FALSE);// 宋体,粗体,非斜体,非平滑 font->SetColor(0xFFFFFFFF); // 设置像素字体颜色,默认白色 } //fonttype 字体,size 文字大小 GDE_BASIC_GUIChineseFont( std::string fonttype , int size ) { font = new GfxFont(fonttype.c_str(),size,TRUE,FALSE,FALSE); font->SetColor(0xFFFFFFFF); } ~GDE_BASIC_GUIChineseFont() { if( font ) delete font; } //以某种颜色显示一次,然后还原颜色 void FontPrintOnce( int x,int y, DWORD Color, std::string txt ) { DWORD oldcolor = font->GetColor(); font->SetColor( Color ); font->Print( x,y, txt.c_str() ); font->SetColor( oldcolor ); } protected: GfxFont* font;//中文字体指针 }; /****** 只用于显示,不用于控制的基类 ******/ class GDE_BASIC_GUIViewOnly : public hgeGUIObject { public: GDE_BASIC_GUIViewOnly() { this->rect.Set( 0,0,0,0 );//这样不会覆盖其他GUI控件的控制区 } }; /****** 感应鼠标移动的基类 ******/ class GDE_BASIC_GUIMouseSensitive : public hgeGUIObject { public: GDE_BASIC_GUIMouseSensitive() : mx_ ( 0 ) , my_ ( 0 ) , is_mouse_over_ ( FALSE ) { } virtual ~GDE_BASIC_GUIMouseSensitive(){} virtual void MouseOver( bool bOver ) { is_mouse_over_ = bOver; } virtual bool MouseMove(float x, float y) { mx_ = x; my_ = y; return false; } protected: float mx_,my_; //当前鼠标位置 bool is_mouse_over_; //鼠标是否悬停 }; #endif

具体的一个实例,比如我的界面角色管理类

/* * CopyRight 2009 - 2010 GDE工作室 * 游戏UI系统 - HGE GUI控件 - 角色管理器 * =================================== * 提供角色的资源管理、角色图片信息管理等功能 * * 2010/01/07 cg create */ #ifndef GDE_UI_ROLE_MANAGER_H_ #define GDE_UI_ROLE_MANAGER_H_ #include "GDE_UI_BasicClasses.h" using namespace GDE; //角色渲染资料单元 struct RoleGuiUnit { RoleGuiUnit( int id , std::string filename , int px, int py ,HGE* pgHGE ) { role_id = id; img_filename = filename; x = px; y = py; pHGE = pgHGE; 读取图片 tex = pHGE->Texture_Load( filename.c_str() ); //装载纹理 w = pHGE->Texture_GetWidth( tex ); h = pHGE->Texture_GetHeight( tex ); spr = new hgeSprite( tex, 0, 0, w, h ); } ~RoleGuiUnit() { if( tex ) pHGE->Texture_Free( tex ); if( spr ) delete spr; } void SetAttackInfo( std::string txt ) { attackinfo = txt; attackinfo_show_count = 200; //此处和FPS相关 } int role_id; //角色ID std::string img_filename;//角色图片文件名 int x,y; //角色坐标(绝对坐标) Direction direct; //角色朝向 HTEXTURE tex; hgeSprite* spr; int w,h; //角色图片宽高 HGE* pHGE; std::string attackinfo;//攻击掉血数 int attackinfo_show_count; }; struct RoleGuiUnitAutoPtr { RoleGuiUnitAutoPtr( RoleGuiUnit* r ) { ptr = r; } ~RoleGuiUnitAutoPtr() { delete ptr; } RoleGuiUnit* GetPtr() { return ptr; } private: RoleGuiUnit* ptr; }; //人物角色管理器GUI class GDE_GUIRoleManager : public GDE_BASIC_GUIChineseFont { public: GDE_GUIRoleManager( int id , HGE* pgHGE ) : pHGE( pgHGE ) { this->id = id; this->rect.Set( 0,0,0,0 ); //该GUI控件只用于显示不用于输入 } virtual ~GDE_GUIRoleManager() { for( int i = 0; i < roles_.size(); i++ ) { delete roles_[i]; } roles_.clear(); } //增加角色 void AddRole( int x, int y, int role_id, std::string filename ) { RoleGuiUnit* tmp = new RoleGuiUnit( role_id, filename, x, y, pHGE ); //RoleGuiUnitAutoPtr ptr( tmp ); roles_.push_back(tmp); } //移除角色 void RemoveRole( int role_id ) { std::vector<RoleGuiUnit*>::iterator iter; for(iter = roles_.begin(); iter != roles_.end(); ++iter ) { if( (*iter)->role_id == role_id ) { RoleGuiUnit* ptr = *iter; roles_.erase( iter ); delete ptr; return; } } } //设置人物位置 void SetPos( int role_id , int x, int y ) { std::vector<RoleGuiUnit*>::iterator iter; for(iter = roles_.begin(); iter != roles_.end(); ++iter ) { if( (*iter)->role_id == role_id ) { (*iter)->x = x; (*iter)->y = y; return; } } } //设置人物方向 void SetDirection( int role_id , Direction d ) { std::vector<RoleGuiUnit*>::iterator iter; for(iter = roles_.begin(); iter != roles_.end(); ++iter ) { if( (*iter)->role_id == role_id ) { (*iter)->direct = d; return; } } } //是否提示角色信息 void RoleInfo( bool is_enable ) { info_enable = is_enable; } //处理角色掉血信息 void AttackInfo( int role_id, std::string txt ) { int x,y; std::vector<RoleGuiUnit*>::iterator iter; for(iter = roles_.begin(); iter != roles_.end(); ++iter ) { if( (*iter)->role_id == role_id ) { (*iter)->SetAttackInfo( txt ); break; } } } //渲染所有角色 virtual void Render() { std::vector<RoleGuiUnit*>::iterator iter; for(iter = roles_.begin(); iter != roles_.end(); ++iter ) { if( (*iter)->spr ) { (*iter)->spr->RenderStretch( (*iter)->x, (*iter)->y, (*iter)->x + (*iter)->w, (*iter)->y + (*iter)->h ); } if( (*iter)->attackinfo_show_count > 0 ) { // TO DO 增加掉血值的alpha变化和位置变化,可以做的更绚 int x = (*iter)->x + 10; int y = (*iter)->y - 20; font->Print( x,y,(*iter)->attackinfo.c_str() ); (*iter)->attackinfo_show_count--; } } } //virtual bool MouseLButton(bool bDown); //virtual void MouseOver( bool bOver ); //virtual bool MouseMove(float x, float y); private: HGE* pHGE; /* by CG 2010-1-7 这个问题调了我半个小时~总结教训中。。 此处容器模板成员使用指针的原因: vector模板成员RoleGuiUnit内涉及到无法复制的内容,如果不使用指针的话,在vector的push_back操作中会 出现run-time error,所以使用指针容器。 使用指针容器的时候需要注意,在erase或者clear其成员的时候,需要手动delete成员指针。(因为vector默认 在erase或者delete的时候调用该类的析构函数,若是指针,则无法释放其指向的内容。 */ std::vector<RoleGuiUnit*> roles_; bool info_enable;//是否提示角色信息 }; #endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值