The HGE engine provides a basic GUI control class that does not implement any functions, but provides a set of virtual functions and attributes for use by Derived classes.
Developers can develop their own controls on this basis, which is more convenient in general.
I learned a lot from the Development of my own game framework.
It is recommended that the HGE engine users not use the graphic functions provided by HGE during rendering, but should encapsulate all of them in the GUI control derived class,
There are several benefits.
1. Closer to the object-oriented model, making the program framework clearer;
2. better resource management. The use and release of resources are in the same small module, which facilitates maintenance even if the scale is expanded;
3. Easy to reuse. we can organize a set of unified basic classes, and then gradually derive and combine them to implement the functions we need.
The following are some convenient basic classes used during the development process.
/* <Br/> * Copyright 2009-2010 GDE studio <br/> * game UI system-HGE game engine control base class <br/> * ====== ===========================< br/> * To improve code reusability, provides some common functions as the base class, which can be derived and reused when building HGE-based GUI controls. <Br/> * 2010/01/07 cgcreate <br/> */<br/> # ifndef gde_ui_basicclasses_h _ <br/> # define gde_ui_basicclasses_h _ </P> <p>> # include ".. /HGE/include/HGE. H "<br/> # include ".. /HGE/include/hgesprite. H "<br/> # include ".. /HGE/include/hgefont. H "<br/> # include ".. /HGE/include/hgerect. H "<br/> # include ".. /HGE/include/hgegui. H "<br/> # include ".. /HGE/include/hgeguictrls. H "</P> <p> # include ".. /HGE/CN/gfxfont. H "<B R/> # include ".. /HGE/CN/gfxedit. H "</P> <p> # include <string> <br/> # include <string. h> <br/> # include <stdio. h> <br/> # include <stdlib. h> </P> <p>/****** the default base class with Chinese fonts ******/<br/> class gde_basic_guichinesefont: public hgeguiobject <br/>{< br/> Public: <br/> gde_basic_guichinesefont () <br/>{< br/> font = new gfxfont ("", 20, true, false, false); //, bold, non-italic, non-smooth <br/> font-> setcolor (0 xffffffff); // set the pixel font color, white by default <br />}</P> <p> // fonttype font, size text size <br/> gde_basic_guichinesefont (STD: String fonttype, int size) <br/> {<br/> font = new gfxfont (fonttype. c_str (), size, true, false, false); <br/> font-> setcolor (0 xffffffff); <br/>}</P> <p> ~ Gde_basic_guichinesefont () <br/>{< br/> If (font) <br/> Delete font; <br/>}</P> <p> // display the color once, and then restore the color <br/> void fontprintonce (int x, int y, DWORD color, STD: String txt) <br/>{< br/> DWORD oldcolor = font-> getcolor (); <br/> font-> setcolor (color ); <br/> font-> Print (X, Y, TXT. c_str (); <br/> font-> setcolor (oldcolor); <br/>}</P> <p> protected: <br/> gfxfont * font; // Chinese font pointer <br/>}; </P> <p>/****** only for display and not for control Base Class *****/<br/> class gde_basic_guiviewonly: Public hgeguiobject <br/>{< br/> Public: <br/> gde_basic_guiviewonly () <br/>{< br/> This-> rect. set (,); // This will not overwrite the control area of other GUI controls <br/>}< br/> }; </P> <p>/******* base class for mouse movement ******/<br/> class gde_basic_guimousesensitive: public hgeguiobject <br/>{< br/> Public: <br/> gde_basic_guimousesensitive () <br/>: MX _ (0) <br/>, my _ (0) <br/>, is_mouse_over _ (Fals E) <br/>{< br/>}</P> <p> virtual ~ Gde_basic_guimousesensitive () {}</P> <p> virtual void Mouseover (bool bover) <br/>{< br/> is_mouse_over _ = bover; <br/>}</P> <p> virtual bool mousemove (float X, float y) <br/>{< br/> MX _ = X; <br/> my _ = y; <br/> return false; <br/>}</P> <p> protected: <br/> float MX _, my _; // current mouse position <br/> bool is_mouse_over _; // whether the mouse is hovering <br/>}; </P> <p> # endif
A specific instance, such as my interface role management class
/* <Br/> * Copyright 2009-2010 GDE studio <br/> * game UI system-hge gui control-role manager <br/> * ====== ==========================================< br/> * provides the role's resource management, role image Information Management and other functions <br/> * 2010/01/07 cgcreate <br/> */</P> <p> # ifndef gde_ui_role_manager_h _ <br/> # define gde_ui_role_manager_h _ </P> <p> # include "gde_ui_basicclasses.h" </P> <p> using namespace GDE; </P> <p> // role rendering Data Unit <br/> struct roleguiunit <br/> {<br/> roleguiunit (int id, STD: String filename, int PX, int py, HGE * pghge) <br/>{< br/> role_id = ID; <br/> img_filename = filename; <br/> X = px; <br/> Y = py; <br/> phge = pghge; </P> <p> // read the image <br/> Tex = phge-> texture_load (filename. c_str (); // load texture <br/> W = phge-> texture_getwidth (Tex); <br/> H = phge-> texture_getheight (Tex ); <br/> SPR = new hgesprite (Tex, 0, 0, W, H); <br/>}</P> <p> ~ Roleguiunit () <br/>{< br/> If (Tex) <br/> phge-> texture_free (Tex); <br/> If (SPR) <br/> Delete SPR; <br/>}</P> <p> void setattackinfo (STD: String txt) <br/>{ <br/> attackinfo = txt; <br/> attackinfo_show_count = 200; // FPS <br/>}</P> <p> int role_id; // role id <br/> STD: String img_filename; // role image file name <br/> int X, Y; // role coordinate (absolute coordinate) <br/> direction direct; // Role Orientation </P> <p> htexture Tex; <br/> hgesprite * SPR; <br/> Int W, H; // role image width and height <br/> HGE * phge; </P> <p> STD: String attackinfo; // Number of attacked blood loss <br/> int attackinfo_show_count; <br/> }; </P> <p> struct roleguiunitautoptr <br/> {<br/> roleguiunitautoptr (roleguiunit * r) <br/> {<br/> PTR = R; <br/>}</P> <p> ~ Roleguiunitautoptr () <br/>{< br/> Delete PTR; <br/>}</P> <p> roleguiunit * getptr () <br/>{< br/> return PTR; <br/>}</P> <p> PRIVATE: <br/> roleguiunit * PTR; <br/> }; </P> <p> // character role Manager GUI <br/> class gde_guirolemanager: Public gde_basic_guichinesefont <br/>{< br/> public: <br/> gde_guirolemanager (int id, HGE * pghge) <br/>: phge (pghge) <br/>{< br/> This-> id = ID; <br/> This-> rect. set (,); // The GUI control is only used to show Used for input <br/>}</P> <p> virtual ~ Gde_guirolemanager () <br/>{< br/> for (INT I = 0; I <roles _. size (); I ++) <br/>{< br/> Delete roles _ [I]; <br/>}< br/> roles _. clear (); <br/>}</P> <p> // Add a role <br/> void addrole (int x, int y, int role_id, STD :: string filename) <br/>{< br/> roleguiunit * TMP = new roleguiunit (role_id, filename, X, Y, phge ); <br/> // roleguiunitautoptr PTR (TMP); <br/> roles _. push_back (TMP); <br/>}</P> <p> // remove a role <br/> void Rem Overole (INT role_id) <br/>{< br/> STD: vector <roleguiunit * >:: iterator ITER; <br/> for (iter = roles _. begin (); iter! = Roles _. end (); ++ ITER) <br/>{< br/> If (* ITER)-> role_id = role_id) <br/> {<br/> roleguiunit * PTR = * ITER; <br/> roles _. erase (ITER); <br/> Delete PTR; <br/> return; <br/>}</P> <p> // set the character position <br/> void setpos (INT role_id, int X, int y) <br/>{< br/> STD: vector <roleguiunit * >:: iterator ITER; <br/> for (iter = roles _. begin (); iter! = Roles _. end (); ++ ITER) <br/>{< br/> If (* ITER)-> role_id = role_id) <br/>{< br/> (* ITER)-> X = x; <br/> (* ITER)-> Y = y; <br/> return; <br/>}</P> <p> // set the direction of a person <br/> void setdirection (INT role_id, direction D) <br/>{< br/> STD: vector <roleguiunit * >:: iterator ITER; <br/> for (iter = roles _. begin (); iter! = Roles _. end (); ++ ITER) <br/>{< br/> If (* ITER)-> role_id = role_id) <br/>{< br/> (* ITER)-> direct = D; <br/> return; <br/>}</P> <p> // whether role information is displayed <br/> void roleinfo (bool is_enable) <br/>{< br/> info_enable = is_enable; <br/>}</P> <p> // process the role's blood loss information <br/> void attackinfo (INT role_id, STD: String txt) <br/>{< br/> int X, Y; <br/> STD: vector <roleguiunit * >:: iterator ITER; <br/> for (iter = roles _. begi N (); iter! = Roles _. end (); ++ ITER) <br/>{< br/> If (* ITER)-> role_id = role_id) <br/>{< br/> (* ITER)-> setattackinfo (txt); <br/> break; <br/>}</P> <p> // render all roles <br/> virtual void render () <br/>{< br/> STD: vector <roleguiunit * >:: iterator ITER; <br/> for (iter = roles _. begin (); iter! = Roles _. end (); ++ ITER) <br/>{< br/> If (* ITER)-> SPR) <br/>{< br/> (* ITER) -> SPR-> renderstretch (* ITER)-> X, (* ITER)-> Y, <br/> (* ITER)-> X + (* ITER) -> W, <br/> (* ITER)-> Y + (* ITER)-> H ); <br/>}</P> <p> If (* ITER)-> attackinfo_show_count> 0) <br/> {<br/> // to do: Increase the Alpha and positional changes of the blood value. You can perform more operations. <br/> int x = (* ITER) -> X + 10; <br/> int y = (* ITER)-> Y-20; <br/> font-> Print (X, Y, (* ITER) -> attackinfo. c_str (); <br/> (* ITER)-> attackinfo_show_count --; <br/>}< br/> // virtual boolmouselbutton (bool bdown ); <br/> // virtual voidmouseover (bool bover); <br/> // virtual boolmousemove (float X, float y); </P> <p> PRIVATE: <br/> HGE * phge; </P> <p>/* <br/> by CG the problem was fixed for half an hour ~ Lessons learned .. <Br/> the reason why the container template members use pointers: <br/> the roleguiunit Member of the Vector Template contains content that cannot be copied. If no pointer is used, in the push_back operation of the vector, <br/> Run-time error occurs, so the pointer container is used. <Br/> when using pointer containers, you must manually delete the member pointers when using erase or clear its members. (Because vector calls the destructor of this class in erase or delete by default. If it is a pointer, it cannot release the content it points. <Br/> */<br/> STD: vector <roleguiunit *> roles _; <br/> bool info_enable; // whether role information is displayed </P> <p >}; </P> <p> # endif