The HGE game engine is an open source 2D Game Engine Based on DirectX.
Its rendering and logic are frame callback-based framework modes,
It provides some basic image operations and input control functions.
I encapsulated the entire framework when I was writing a 2D game. It is quite disgusting in many places. Please forgive me for the nonstandard naming of variables.
For more information, see the general framework.
The frame-based callback of the HGE game engine makes me very uncomfortable, because our game logic is generally continuous and has nothing to do with the specific frame, so I ran the entire HGE in my own thread. In the game logic section, I send messages, notify the interface to update and obtain user input.
The HGE main framework consists of four parts:
1. Resource Management 2. Message Management 3. Sound Management 4. UI Management
The following figure shows gde_ui_hgeframework.h.
/* <Br/> * Copyright 2009-2010 GDE studio <br/> * game UI system-HGE engine framework class <br/> * ======== ========================================< br/> * the entire UI system calls the main framework of the HGE Game Engine <br/> * 2010/01/08 cgcreate <br/> */</P> <p> # ifndef gde_ui_hge_framework_h _ <br/> # define gde_ui_hge_framework_h _> <p> # include "gde_ui_resourcemanager.h" <br/> # include "gde_ui_uimanager.h" <br/> # include "inline" <br/> # include "gde_ui_soundmanag Er. H "</P> <p> # pragma comment (Lib," gde_ui/HGE/lib/HGE. lib ") <br/> # pragma comment (Lib," gde_ui/HGE/lib/hgehelp. lib ") <br/> # pragma comment (linker,"/nodefaultlib: libc. lib ") </P> <p> # include "gde_ui_thread_core.h" </P> <p> namespace GDE <br/>{< br/> namespace gde_ui <br/>{</P> <p> // HGE engine framework <br/> class hge_framework <br/>{< br/> public: <br/> // unique instance <br/> static hge_framework * instance () <br/> {<br/> static hge_fram Ework * instance _ = 0; <br/> If (! Instance _) <br/>{< br/> instance _ = new hge_framework (); <br/>}< br/> return instance _; <br/>}</P> <p> ~ Hge_framework () <br/>{< br/> Delete m_resmanager; <br/> Delete m_messagemanager; <br/> Delete m_soundmanager; <br/> Delete m_uimanager; <br/>}</P> <p> // start running <br/> void run () <br/>{< br/> // obtain the resource pointer <br/> phge = m_resmanager-> gethgeptr (); <br/> pgui = m_resmanager-> getguiptr (); </P> <p> // HGE core pointer in the initialization subclass <br/> m_messagemanager-> ready (); <br/> m_uimanager-> ready (); </P> <p> // start the main loop <br/> If (phge-> system_initiate () & phge) <br/>{< br/> phge-> system_start (); <br/>}< br/> else <br/> MessageBox (null, phge-> system_geterrormessage (), <br/> "error", mb_ OK | mb_iconerror | mb_systemmodal ); <br/>}</P> <p> // HGE framework logical functions <br/> static bool framefunc () <br/>{< br/> smart_lock (0) // thread lock </P> <p> hge_framework: instance ()-> m_messagemanager-> processmessage (); // process messages <br/> hge_framework: instance () -> m_uimanager-> handler (); // process user operations </P> <p> return false; <br/>}</P> <p> // HGE Framework rendering function <br/> static bool renderfunc () <br/>{< br/> smart_lock (0) // thread lock </P> <p> hge_framework: instance ()-> phge-> gfx_clear (0); // clear the screen <br/> hge_framework: instance () -> phge-> gfx_beginscene (); // start rendering </P> <p> hge_framework: instance ()-> m_uimanager-> render (); // draw </P> <p> hge_framework: instance ()-> phge-> gfx_endscene (); // end rendering </P> <p> return false; <br/>}</P> <p> // member sub-manager pointer <br/> ResourceManager * m_resmanager; <br/> messagemanager * m_messagemanager; <br/> soundmanager * m_soundmanager; <br/> uimanager * m_uimanager; </P> <p> // HGE system resource pointer <br/> HGE * phge; <br/> hgegui * pgui; </P> <p> PRIVATE: <br/> // Private structure. instantiation is not allowed. <br/> hge_framework () <br/>: phge (0) <br/>, pgui (0) <br/> {<br/> m_resmanager = new ResourceManager (); <br/> m_messagemanager = new messagemanager (); <br/> m_soundmanager = new soundmanager (); <br/> m_uimanager = new uimanager (); <br/>}< br/>}; </P> <p >}< br/> # endif;
Resource management is the core.
Gde_ui_resourcemanager.h
/* <Br/> * Copyright 2009-2010 GDE studio <br/> * game UI system-HGE engine framework-Resource Manager <br/> * ==== =============================< br/> * HGE engine resource management <br /> * <br/> * this type of resources mainly implement maintenance of HGE resources and provide a toolset for hge api encapsulation <br/> * and maintain the HGE configuration environment <br /> * <br/> * 2010/01/08 cgcreate <br/> */</P> <p> # ifndef required _ <br/> # define gde_ui_framework_resourcemanager_h _ </P> <p> # include ". /HGE/CN/gfxfont. H "<br/> # include ". /HGE/ CN/gfxedit. H "<br/> # include ". /HGE/include/HGE. H "<br/> # include ". /HGE/include/hgegui. H "<br/> # include ". /HGE/include/hgeguictrls. H "<br/> # include ". /HGE/include/hgeresource. H "</P> <p> # include <string> </P> <p> namespace GDE <br/> {<br/> namespace gde_ui <br/> {< /P> <p> // resource management <br/> class ResourceManager <br/> {<br/> public: <br/> // constructor to load HGE Global Resources <br/> ResourceManager (); </P> <p> // destructor, release HGE Global Resources <br/> ~ ResourceManager (); </P> <p> // obtain the HGE pointer <br/> HGE * gethgeptr () {return phge ;} </P> <p> // obtain the GUI pointer <br/> hgegui * getguiptr () {return pgui ;} </P> <p> // read the image <br/> void LoadImage (STD: String const & filename, hgesprite * & p_sprite, htexture & Tex ); </P> <p> PRIVATE: <br/> HGE * phge; <br/> hgegui * pgui; <br/> hgeresourcemanager m_hgeresmanager; </P> <p >}; </P> <p >}< br/>}</P> <p> # endif
Gde_ui_resourcemanager.cpp
# Include "gde_ui_hgeframework.h" <br/> # include "gde_ui_resourcemanager.h" </P> <p> namespace <br/>{< br/> STD :: string const res_manager_filename = "Res/res_s"; <br/> int const win_screenwidth = 800; <br/> int const win_screenheight = 600; <br/> STD :: string const win_titile = "HGE test window"; <br/> int const win_colormode = 32; <br/> int const win_maxfps = 100; <br/> int const win_windowed = true; </P> <p >}</P> <p> N Amespace GDE <br/>{< br/> namespace gde_ui <br/>{</P> <p> // constructor <br/> ResourceManager: ResourceManager () <br/>: m_hgeresmanager (res_manager_filename.c_str () <br/>{< br/> // create an HGE global instance <br/> phge = hgecreate (hge_version ); </P> <p> // Initialization Configuration <br/> phge-> system_setstate (hge_inifile, "hgedemo. ini "); <br/> phge-> system_setstate (hge_logfile," hgedemo. log "); <br/> phge-> system_setstate (hge_framefunc, & hge_framework:: Framefunc); <br/> phge-> system_setstate (hge_renderfunc, & hge_framework: renderfunc); <br/> phge-> system_setstate (hge_title, win_titile.c_str ()); <br/> phge-> system_setstate (hge_screenwidth, win_screenwidth); <br/> phge-> system_setstate (hge_screenheight, win_screenheight); <br/> phge-> system_setstate (callback, win_colormode); <br/> phge-> system_setstate (hge_fps, win_maxfps); <br/> phge-> system_setsta Te (hge_windowed, win_windowed); <br/> phge-> system_setstate (hge_hidemouse, false); <br/> phge-> system_setstate (hge_usesound, false ); <br/> phge-> system_setstate (hge_dontsuspend, true); // do not suspend if the focus is lost <br/> phge-> system_setstate (hgeintstate) 14, (INT) 0xface0ff ); // disable HGE logo </P> <p> // initialize the GUI <br/> pgui = new hgegui (); <br/> pgui-> setnavmode (hgegui_updown | hgegui_cycled); <br/> pgui-> enter (); <br/>}</P> <p> resourc Emanager ::~ ResourceManager () <br/>{< br/> // destroy GUI <br/> Delete pgui; </P> <p> // destroy the resource manager <br/> m_hgeresmanager.purge (); </P> <p> // HGE end <br/> phge-> system_shutdown (); <br/> phge-> release (); <br/>}</P> <p> void ResourceManager: LoadImage (STD: String const & filename, hgesprite * & p_sprite, htexture & Tex) <br/>{< br/> If (p_sprite) <br/> Delete p_sprite; // destroy the previous image </P> <p> Tex = phge-> texture_load (filename. c_str (); // load texture <br/> int W = phge-> texture_getwidth (Tex); <br/> int H = phge-> texture_getheight (Tex ); <br/> p_sprite = new hgesprite (Tex, 0, 0, W, H ); // cropping genie <br/>}</P> <p >}< br/>}
The most important thing is the message transmission mechanism. I have modeled the message macro definition mechanism of MFC, and the core code of the message module is given below.
// Message ing macro definition <br/> # define begin_message_map ()/<br/> typedef void (messagemanager: * messagefunction) ();/<br/> STD :: map <uimessage, messagefunction> message_functions _;/<br/> void initmessagemap ()/<br/>{< br/> # define on_message (ID, memberfuc) /<br/> message_functions _ [ID] = & messagemanager: memberfuc; <br/> # define end_message_map/<br/>}</P> <p> .... </P> <p> // here are some specific messages in my game </P> <p> // Message Type <br/> typedef int uimessage; </P> <p> uimessage const uimessage_updatebackimg = 0; // update the background image </P> <p> uimessage const uimessage_addrole = 10; // Add a role <br/> uimessage const uimessage_removerole = 11; // Delete the role <br/> uimessage const uimessage_updaterole = 12; // update the role list <br/> uimessage const uimessage_attackinfo = 13; // display the role blood loss </P> <p> uimessage const uimessage_updatedialog = 20; // update dialog box <br/> uimessage const uimessage_hidedialog = 21; // hide dialog box </P> <p> uimessage const uimessage_updatesmallmap = 30; // update the Map </P> <p> uimessage const uimessage_updategameinfo = 40; // update game prompt information </P> <p> uimessage const uimessage_updatebutton = 50; // Add button <br/> uimessage const uimessage_removebutton = 51; // delete button <br/> uimessage const uimessage_removeallbuttons = 52; // remove all buttons </P> <p> uimessage const uimessage_updatepopmenu = 60; // the pop-up menu <br/> uimessage const uimessage_hidepopmenu = 61; // hide the pop-up menu </P> <p> uimessage const uimessage_updatezoneblock = 70; // update the map block <br/> uimessage const uimessage_hidezoneblock = 71; // clear the map block </P> <p> // message ing implementation </P> <p> begin_message_map () <br/> on_message (uimessage_updatebackimg, updatebackimage) <br/> on_message (response, addrole) <br/> on_message (uimessage_removerole, removerole) <br/> on_message (response, updaterole) <br/> on_message (response, roleattackinfo) <br/> on_message (response, updatedialog) <br/> on_message (response, hidedialog) <br/> on_message (response, updatesmallmap) <br/> on_message (uimessage_updategameinfo, updategameinfo) <br/> on_message (response, updatebutton) <br/> on_message (uimessage_removebutton, removebutton) <br/> on_message (response, removeallbuttons) <br/> on_message (response, updatepopmenu) <br/> on_message (messages, hidepopmenu) <br/> on_message (uimessage_updatezoneblock, updatezoneblock) <br/> on_message (messages, hidezoneblock) <br/> end_message_map () </P> <p> // process the message <br/> void processmessage () <br/> {<br/> for (INT I = 0; I <m_messagelist.size (); ++ I) <br/>{< br/> (this-> * message_functions _ [m_messagelist [I]) (); <br/>}< br/> m_messagelist.clear (); <br/>}</P> <p>
Finally, the entire HGE engine is started.
Gde_ui_hge.h
# Ifndef gde_ui_hge_h _ <br/> # define gde_ui_hge_h _ </P> <p> # include <windows. h> <br/> # include <string> </P> <p> # include "gde_ui_hgeframework.h" </P> <p>/* <br/> gde hge Game Engine module 2009-11-21 </P> <p> var | author | brief <br/>================== ===========================< br/> 1.0 CG create <br/> */< /P> <p> namespace GDE <br/>{< br/> namespace gde_ui <br/>{</P> <p> // HGE thread entry <br/> static DWORD winapi hgethreadproc (lpvoid lpparameter) <br/>{< br/> hge_framework: instance ()-> Run (); <br/> return 0; <br/>}</P> <p >}< br/> # endif
Game Initialization
// Create an HGE thread
Hge_thread = createthread (null, 0,
Hgethreadproc, null, 0, null );
You can.