HGE Engine GUI Object extension _ engine

Source: Internet
Author: User
Tags int size

The HGE engine provides a basic GUI control class that does not implement any functionality, but provides a set of virtual functions and properties for use by derived classes.

Developers can develop their own controls on this basis, which is generally more convenient.

I have learned a lot from the development of my own game framework.

It is recommended that users of the HGE engine not use the graphics functions provided by HGE directly in the rendering process, but should encapsulate all of them in GUI control derived classes.

There are several great benefits.

1. More close to the object-oriented, make the program framework clear;

2. Better management of resources, use and release are in the same small module, even if the scale of expansion, but also facilitate maintenance;

3. Easy to reuse, we can organize a set of unified base class, and then gradually derive, combined to achieve the functions we need.

Here are a few handy base classes I used myself during the development process

* * CopyRight 2009-2010 Gde Studio * Game UI System-HGE game engine Control base class * =================================== * To improve the reusability of code, some common functions are provided as base classes, You can derive reuse when building a HGE-based GUI control. * * 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 >/****** default ******/class Gde_basic_guichinesefont:public Hgeguiobject {Public:gde_basic_guichinesefont () with Chinese fonts {font = new Gfxfont ("Arial", 20,true,false,false);//Arial, bold, non italic, not smooth font->setcolor (0xFFFFFFFF);//set pixel font color, default white}//font Type font, size type Gde_basic_guichinesefont (std::string fonttype, int size) {font = new Gfxfont (Fonttype.c_str (), size,t Rue,false,false); Font-> SetColor (0xFFFFFFFF); ~gde_basic_guichinesefont () {if (font) Delete font is displayed once in a color, then restore the color 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;//Chinese font pointer}; /****** is used only for display, not for control of base class ******/class Gde_basic_guiviewonly:public Hgeguiobject {public:gde_basic_guiviewonly () {this- >rect. Set (0,0,0,0);//This does not overwrite the control area of other GUI controls}}; /****** Induction Mouse movement base class ******/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;} prot Ected:float Mx_,my_; Current mouse position bool Is_mouse_over_; Whether the mouse hovers}; #endif

A specific example, such as my interface role management class

* * CopyRight 2009-2010 Gde Studio * Game UI System-HGE GUI Control-Role Manager * =================================== * provides role resource management, role picture information management, etc. function * * 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; Role rendering Data unit 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; Read Picture Tex = Phge->texture_load (Filename.c_str ()); Load Texture 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; Here and FPS related} int role_id; Role ID std::string img_filename;//role picture filename int x,y; Role coordinates (absolute coordinates) Direction direct; Role toward Htexture Tex; hgesprite* SPR; int w,h; Role picture wide-high hge* phge; std::string attackinfo;//attack offblood count int attackinfo_show_count; }; struct Roleguiunitautoptr {roleguiunitautoptr (roleguiunit* r) {ptr = R;} ~roleguiunitautoptr () {delete ptr;} roleguiunit* getptr () {return ptr;} private:roleguiunit* ptr; }; Personas manager 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); This GUI control is used only to display not for input} virtual ~gde_guirolemanager () {for (int i = 0; i < roles_.size (); i++) {delete roles_[i];} rol Es_.clear (); }//Add role void Addrole (int x, int y, int role_id, std::string filename) {roleguiunit* tmp = new Roleguiunit (role_id, fi Lename, x, Y, phge); Roleguiunitautoptr PTR (TMP); Roles_.push_back (TMP); }//Remove role 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;} }//Set character position 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 }}//set character direction 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;}}} Do you want to prompt for role information void Roleinfo (bool is_enable) {info_enable = is_enable}//process role bleed information void attackinfo (int role_id, Std::stri ng txt) {int x,y; std::vector<roleguiunit*>::iterator iter; for (iter = Roles_.begin (); Iter!= roles_.end (); ++ite R) {if ((*iter)->role_id = = role_id) {(*iter)->setattackinfo (TXT); break;}}} Render all roles virtual void Render () {std::vector<roleguiunit*>::iterator iter; for (iter = Roles_.begin (); ITER!= Roles_ . end (); ++iter) {if (*iter)-&GT;SPR) { (*iter)->spr->renderstretch (*iter)->x, (*iter)->y, (*iter)->x + (*iter)->w, (*iter)->y + (*iter )->h); } if ((*iter)->attackinfo_show_count > 0) {//To do increase the drop in the blood value of alpha change and position change, can be done more gorgeous int x = (*iter)->x +; 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 This problem tuned me half an hour ~ summing up the lesson. The reason that container template members use pointers here: Vector template member Roleguiunit contains content that cannot be replicated, and if the pointer is not used, run-time error appears in the vector's push_back operation, so the pointer container is used. When using a pointer container, it is necessary to note that when erase or clear its members, you need to manually delete the member pointer. (Because vectors call the destructor of the class by default when erase or delete), if it is a pointer, it cannot be freed of the content it points to. * * Std::vector<roleguiunit*> Roles_; BOOL info_enable;//Whether the role information is prompted}; #endif

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.