From c79473d01239d7192ae1b21287ba7e563cf4738f Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sat, 24 Aug 2013 22:03:34 +0200 Subject: [PATCH] [DEV] modify the use of the element to permit optimisation --- ege/ElementGame.h | 31 +++++++- ege/Environement.cpp | 184 +++++++++++++++++++++---------------------- ege/Environement.h | 10 +-- 3 files changed, 121 insertions(+), 104 deletions(-) diff --git a/ege/ElementGame.h b/ege/ElementGame.h index 8987f09..0d8a36f 100644 --- a/ege/ElementGame.h +++ b/ege/ElementGame.h @@ -40,7 +40,9 @@ namespace ege btRigidBody* m_body; //!< all the element have a body ==> otherwise it will be not manage with this system... public: /** - * @brief Constructor + * @brief Constructor (when constructer is called just add element that did not change. + * The objest will be stored in a pool of element and keep a second time if needed ==> redure memory allocation, + * when needed, the system will call the Init and un-init function... */ ElementGame(ege::Environement& _env); /** @@ -48,10 +50,33 @@ namespace ege */ virtual ~ElementGame(void); /** - * @brief Get the element Type description string - * @return A pointer on the descriptive string (Do not free it ==> it might be a const) + * @brief Get the element Type description string. + * @return A reference on the descriptive string. */ virtual const etk::UString& GetType(void) const; + /** + * @brief Init the element with the defined properties + * @param[in] _description String properties that the element know how to parse it... + * @note : When the useer reques no parameter ==> if will call this one. + * @note : In a game developpement the user choice a transmission mode (string, json or xml) and use only one ... + * @return true, the element is corectly initialized. + */ + virtual bool Init(const etk::UString& _description) { return false; }; + /** + * @brief Init the element with the defined properties + * @param[in] _value json properties that the element know how to parse it... + * @note : in a game developpement the user choice a transmission mode (string, json or xml) and use only one ... + * @return true, the element is corectly initialized. + */ + virtual bool Init(ejson::Value* _value) { return false; }; + /** + * @brief Init the element with the defined properties + * @param[in] _node xml properties that the element know how to parse it... + * @note : in a game developpement the user choice a transmission mode (string, json or xml) and use only one ... + * @return true, the element is corectly initialized. + */ + virtual bool Init(exml::Node* _value) { return false; }; + virtual bool UnInit(void) { return true; }; private: uint32_t m_uID; //!< This is a reference on a basic element ID public: diff --git a/ege/Environement.cpp b/ege/Environement.cpp index 30d94ec..c6f012d 100644 --- a/ege/Environement.cpp +++ b/ege/Environement.cpp @@ -111,115 +111,47 @@ void ege::Environement::GetElementNearestFixed(const vec3& _sourcePosition, } } -class CallBackCreator -{ - public: - ege::createElement_string_tf m_string; - ege::createElement_ejson_tf m_ejson; - ege::createElement_exml_tf m_exml; - CallBackCreator(void) : m_string(NULL), m_ejson(NULL), m_exml(NULL) {}; -}; -static etk::Hash& GetHachTableCreating(void) +static etk::Hash& GetHachTableCreating(void) { - static etk::Hash s_table; + static etk::Hash s_table; return s_table; } -void ege::Environement::AddCreator(const etk::UString& _type, ege::createElement_string_tf _creator) +void ege::Environement::AddCreator(const etk::UString& _type, ege::createElement_tf _creator) { if (NULL == _creator) { EGE_ERROR("Try to add an empty CREATOR ..."); return; } if (GetHachTableCreating().Exist(_type)==true) { - GetHachTableCreating()[_type].m_string = _creator; + GetHachTableCreating()[_type] = _creator; return; } - CallBackCreator tmpp; - tmpp.m_string = _creator; - GetHachTableCreating().Add(_type, tmpp); + GetHachTableCreating().Add(_type, _creator); } -void ege::Environement::AddCreator(const etk::UString& _type, ege::createElement_ejson_tf _creator) -{ - if (NULL == _creator) { - EGE_ERROR("Try to add an empty CREATOR ..."); - return; - } - if (GetHachTableCreating().Exist(_type)==true) { - GetHachTableCreating()[_type].m_ejson = _creator; - return; - } - CallBackCreator tmpp; - tmpp.m_ejson = _creator; - GetHachTableCreating().Add(_type, tmpp); -} - -void ege::Environement::AddCreator(const etk::UString& _type, ege::createElement_exml_tf _creator) -{ - if (NULL == _creator) { - EGE_ERROR("Try to add an empty CREATOR ..."); - return; - } - if (GetHachTableCreating().Exist(_type)==true) { - GetHachTableCreating()[_type].m_exml = _creator; - return; - } - CallBackCreator tmpp; - tmpp.m_exml = _creator; - GetHachTableCreating().Add(_type, tmpp); -} - -ege::ElementGame* ege::Environement::CreateElement( - const etk::UString& _type, - const etk::UString* _description, - ejson::Value* _value, - exml::Node* _node, - bool _autoAddElement) +ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, bool _autoAddElement) { if (false==GetHachTableCreating().Exist(_type)) { EGE_ERROR("Request creating of an type that is not known '" << _type << "'"); return NULL; } - CallBackCreator tmpp = GetHachTableCreating()[_type]; - ege::ElementGame* tmpElement = NULL; - if (NULL != _value) { - if (NULL == tmpp.m_ejson) { - EGE_ERROR("ejson NULL pointer creator ==> internal error... '" << _type << "'"); - // internal error - return NULL; - } - tmpElement = tmpp.m_ejson(*this, _value); - } else if (NULL != _node) { - if (NULL == tmpp.m_exml) { - EGE_ERROR("exml NULL pointer creator ==> internal error... '" << _type << "'"); - // internal error - return NULL; - } - tmpElement = tmpp.m_exml(*this, _node); - } else if (NULL != _description) { - if (NULL == tmpp.m_string) { - EGE_ERROR("string NULL pointer creator ==> internal error... '" << _type << "'"); - // internal error - return NULL; - } - tmpElement = tmpp.m_string(*this, *_description); - } else { - if (NULL != tmpp.m_ejson) { - tmpElement = tmpp.m_ejson(*this, NULL); - } else if (NULL != tmpp.m_exml) { - tmpElement = tmpp.m_exml(*this, NULL); - } else if (NULL != tmpp.m_string) { - tmpElement = tmpp.m_string(*this, ""); - } else { - EGE_ERROR("No callback availlable !!! '" << _type << "'"); - return NULL; - } + ege::createElement_tf creatorPointer = GetHachTableCreating()[_type]; + if (NULL == creatorPointer) { + EGE_ERROR("NULL pointer creator ==> internal error... '" << _type << "'"); + return NULL; } + ege::ElementGame* tmpElement = creatorPointer(*this); if (NULL == tmpElement) { - EGE_ERROR("Sub creator han an error when creating element : '" << _type << "'"); + EGE_ERROR("allocation error '" << _type << "'"); + return NULL; + } + if (false==tmpElement->Init("")) { + EGE_ERROR("Init error ... '" << _type << "'"); + // remove created element ... + delete(tmpElement); return NULL; } if (_autoAddElement==true) { @@ -227,24 +159,89 @@ ege::ElementGame* ege::Environement::CreateElement( } return tmpElement; } -ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, bool _autoAddElement) -{ - return CreateElement(_type, NULL, NULL, NULL, _autoAddElement); -} ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, const etk::UString& _description, bool _autoAddElement) { - return CreateElement(_type, &_description, NULL, NULL, _autoAddElement); + if (false==GetHachTableCreating().Exist(_type)) { + EGE_ERROR("Request creating of an type that is not known '" << _type << "'"); + return NULL; + } + ege::createElement_tf creatorPointer = GetHachTableCreating()[_type]; + if (NULL == creatorPointer) { + EGE_ERROR("NULL pointer creator ==> internal error... '" << _type << "'"); + return NULL; + } + ege::ElementGame* tmpElement = creatorPointer(*this); + if (NULL == tmpElement) { + EGE_ERROR("allocation error '" << _type << "'"); + return NULL; + } + if (false==tmpElement->Init(_description)) { + EGE_ERROR("Init error ... '" << _type << "'"); + // remove created element ... + delete(tmpElement); + return NULL; + } + if (_autoAddElement==true) { + AddElementGame(tmpElement); + } + return tmpElement; } ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, ejson::Value* _value, bool _autoAddElement) { - return CreateElement(_type, NULL, _value, NULL, _autoAddElement); + if (false==GetHachTableCreating().Exist(_type)) { + EGE_ERROR("Request creating of an type that is not known '" << _type << "'"); + return NULL; + } + ege::createElement_tf creatorPointer = GetHachTableCreating()[_type]; + if (NULL == creatorPointer) { + EGE_ERROR("NULL pointer creator ==> internal error... '" << _type << "'"); + return NULL; + } + ege::ElementGame* tmpElement = creatorPointer(*this); + if (NULL == tmpElement) { + EGE_ERROR("allocation error '" << _type << "'"); + return NULL; + } + if (false==tmpElement->Init(_value)) { + EGE_ERROR("Init error ... '" << _type << "'"); + // remove created element ... + delete(tmpElement); + return NULL; + } + if (_autoAddElement==true) { + AddElementGame(tmpElement); + } + return tmpElement; } ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, exml::Node* _node, bool _autoAddElement) { - return CreateElement(_type, NULL, NULL, _node, _autoAddElement); + if (false==GetHachTableCreating().Exist(_type)) { + EGE_ERROR("Request creating of an type that is not known '" << _type << "'"); + return NULL; + } + ege::createElement_tf creatorPointer = GetHachTableCreating()[_type]; + if (NULL == creatorPointer) { + EGE_ERROR("NULL pointer creator ==> internal error... '" << _type << "'"); + return NULL; + } + ege::ElementGame* tmpElement = creatorPointer(*this); + if (NULL == tmpElement) { + EGE_ERROR("allocation error '" << _type << "'"); + return NULL; + } + if (false==tmpElement->Init(_node)) { + EGE_ERROR("Init error ... '" << _type << "'"); + // remove created element ... + delete(tmpElement); + return NULL; + } + if (_autoAddElement==true) { + AddElementGame(tmpElement); + } + return tmpElement; } @@ -280,6 +277,7 @@ void ege::Environement::RmElementGame(ege::ElementGame* _removeElement) for (int32_t iii=0; iiiDynamicDisable(); + m_listElementGame[iii]->UnInit(); delete(m_listElementGame[iii]); m_listElementGame[iii] = NULL; } diff --git a/ege/Environement.h b/ege/Environement.h index fec1764..a1e77cc 100644 --- a/ege/Environement.h +++ b/ege/Environement.h @@ -21,9 +21,7 @@ class btDynamicsWorld; namespace ege { class ElementGame; class Environement; - typedef ege::ElementGame* (*createElement_string_tf)(ege::Environement& _env, const etk::UString& _description); - typedef ege::ElementGame* (*createElement_ejson_tf)(ege::Environement& _env, ejson::Value* _value); - typedef ege::ElementGame* (*createElement_exml_tf)(ege::Environement& _env, exml::Node* _node); + typedef ege::ElementGame* (*createElement_tf)(ege::Environement& _env); class ElementInteraction { @@ -68,9 +66,7 @@ namespace ege { * @param[in] _type Type of the element. * @param[in] _creator Function pointer that reference the element creating. */ - static void AddCreator(const etk::UString& _type, ege::createElement_string_tf _creator); - static void AddCreator(const etk::UString& _type, ege::createElement_ejson_tf _creator); - static void AddCreator(const etk::UString& _type, ege::createElement_exml_tf _creator); + static void AddCreator(const etk::UString& _type, ege::createElement_tf _creator); /** * @brief Create an element on the curent scene. * @param[in] _type Type of the element that might be created. @@ -83,8 +79,6 @@ namespace ege { ege::ElementGame* CreateElement(const etk::UString& _type, const etk::UString& _description, bool _autoAddElement=true); ege::ElementGame* CreateElement(const etk::UString& _type, ejson::Value* _value, bool _autoAddElement=true); ege::ElementGame* CreateElement(const etk::UString& _type, exml::Node* _node, bool _autoAddElement=true); - protected: - ege::ElementGame* CreateElement(const etk::UString& _type, const etk::UString* _description, ejson::Value* _value, exml::Node* _node, bool _autoAddElement); public: class ResultNearestElement {