From 1c583aafddd42a5bd3c8ab5081b842a4a6f10565 Mon Sep 17 00:00:00 2001 From: Edouard Dupin Date: Mon, 11 Jun 2012 16:47:02 +0200 Subject: [PATCH] simplify scene sprite management --- Sources/libewol/ewol/Game/GameElement.cpp | 27 ++++++++- Sources/libewol/ewol/Game/GameElement.h | 59 +++++++++++--------- Sources/libewol/ewol/Game/GameElementLua.cpp | 13 +---- Sources/libewol/ewol/Game/GameElementLua.h | 2 +- Sources/libewol/ewol/Game/SceneElement.cpp | 18 ------ Sources/libewol/ewol/Game/SceneElement.h | 10 ++-- Sources/libewol/ewol/OObject.cpp | 1 + Sources/libewol/ewol/OObject.h | 10 ++++ Sources/libewol/ewol/OObject/Sprite.cpp | 53 +++++++++++++++++- Sources/libewol/ewol/OObject/Sprite.h | 9 +-- Sources/libewol/ewol/widget/Scene.cpp | 16 +----- 11 files changed, 133 insertions(+), 85 deletions(-) diff --git a/Sources/libewol/ewol/Game/GameElement.cpp b/Sources/libewol/ewol/Game/GameElement.cpp index d23eac5e..b7d0540c 100644 --- a/Sources/libewol/ewol/Game/GameElement.cpp +++ b/Sources/libewol/ewol/Game/GameElement.cpp @@ -54,7 +54,7 @@ ewol::GameElement::GameElement(SceneElement & sceneElement, etk::UString& tmpNam /** - * @brief Requuest the draw of the current element, it will be done on the current Sprite list + * @brief Load or get a previous loaded sprite, it will be done on the current Sprite list * @param[in,out] listOfElement Reference on the list of sprite that we need to find if it exist or added a new one * @param[in] fileName Sprite name * @param[in] maxSize maximum size of the sprite @@ -65,7 +65,8 @@ int32_t ewol::GameElement::LoadSprite(etk::VectorType listOfEleme for (int32_t iii=0; iiiHasName(fileName) == true) { - // TODO : control the size ... + // count the number of element registered ... + listOfElement[0][iii]->IncreaseLoadedTime(); return iii; } } @@ -83,6 +84,28 @@ int32_t ewol::GameElement::LoadSprite(etk::VectorType listOfEleme return listOfElement[0].Size() -1; } +/** + * @brief UnLoad or not(if needed) the sprite selected, it will be done on the current Sprite list + * @param[in,out] listOfElement Reference on the list of sprite that we need to find if it exist or added a new one + * @param[in] spriteId Sprite registered id + * @return --- + */ +void ewol::GameElement::UnLoadSprite(etk::VectorType listOfElement[NB_BOUBLE_BUFFER], int32_t spriteId) +{ + if (spriteId >= 0 && spriteId < listOfElement[0].Size()) { + if (listOfElement[0][spriteId] != NULL) { + // count the number of element registered ... + if (true == listOfElement[0][spriteId]->DecreaseLoadedTime() ) { + // must remove the sprite ==> pb with the double buffer ... + // TODO : ==> for all double buffer ... + for(int32_t iii=0; iii pos1, Vector2D pos2) { diff --git a/Sources/libewol/ewol/Game/GameElement.h b/Sources/libewol/ewol/Game/GameElement.h index b1f54ba0..e0234beb 100644 --- a/Sources/libewol/ewol/Game/GameElement.h +++ b/Sources/libewol/ewol/Game/GameElement.h @@ -70,32 +70,32 @@ namespace ewol { virtual void Init(void) { }; virtual void UnInit(void) { }; - uint16_t GetUniqueId(void) { return m_uniqueId; }; + uint16_t GetUniqueId(void) { return m_uniqueId; }; - bool HasName(etk::UString tmpName) { return (tmpName == m_fileNameConfig); }; - bool IsVisible(void) { return m_visible; }; - void SetVisible(bool state) { m_visible = state; }; - Vector2D PositionGet(void) { return m_position; }; - void PositionSet(Vector2D state) { m_position = state; }; - void PositionSet(float xxx, float yyy) { m_position.x = xxx; m_position.y = yyy; }; - Vector2D SpeedGet(void) { return m_speed; }; - void SpeedSet(Vector2D state) { m_speed = state; }; - void SpeedSet(float xxx, float yyy) { m_speed.x = xxx; m_speed.y = yyy; }; - float SizeGet(void) { return m_size; }; - void SizeSet(float state) { m_size = state; }; - float AngleGet(void) { return m_angle; }; - void AngleSet(float state) { m_angle = state; }; - float GravityGet(void) { return m_gravity; }; - void GravitySet(float state) { m_gravity = state; }; - int32_t PowerGet(void) { return m_power; }; - void PowerSet(int32_t state) { m_power = state; }; - bool CanBeCibledGet(void) { return m_canBeCibled; }; - void CanBeCibledSet(bool state) { m_canBeCibled = state; }; + bool HasName(etk::UString tmpName) { return (tmpName == m_fileNameConfig); }; + bool IsVisible(void) { return m_visible; }; + void SetVisible(bool state) { m_visible = state; }; + Vector2D PositionGet(void) { return m_position; }; + void PositionSet(Vector2D state) { m_position = state; }; + void PositionSet(float xxx, float yyy) { m_position.x = xxx; m_position.y = yyy; }; + Vector2D SpeedGet(void) { return m_speed; }; + void SpeedSet(Vector2D state) { m_speed = state; }; + void SpeedSet(float xxx, float yyy) { m_speed.x = xxx; m_speed.y = yyy; }; + float SizeGet(void) { return m_size; }; + void SizeSet(float state) { m_size = state; }; + float AngleGet(void) { return m_angle; }; + void AngleSet(float state) { m_angle = state; }; + float GravityGet(void) { return m_gravity; }; + void GravitySet(float state) { m_gravity = state; }; + int32_t PowerGet(void) { return m_power; }; + void PowerSet(int32_t state) { m_power = state; }; + bool CanBeCibledGet(void) { return m_canBeCibled; }; + void CanBeCibledSet(bool state) { m_canBeCibled = state; }; - int32_t GetType(void) { return m_type; }; // DEPRECATED ... - int32_t TypeGet(void) { return m_type; }; - uint16_t GroupGet(void) { return m_group; }; - void GroupSet(uint16_t state) { m_group = state; }; + int32_t GetType(void) { return m_type; }; // TODO : DEPRECATED ... + int32_t TypeGet(void) { return m_type; }; + uint16_t GroupGet(void) { return m_group; }; + void GroupSet(uint16_t state) { m_group = state; }; void GetElementProperty(gameElementGenericProperty_ts &element); /** @@ -115,7 +115,7 @@ namespace ewol { * @param[in,out] listOfEffects Reference on the list where the display must be done for every effects * @return --- */ - virtual void Draw(etk::VectorType & listOfSprite, etk::VectorType & listOfEffects) { }; + virtual void Draw(int32_t currentCreateId) { }; /** * @brief an element has been remove, just remove reference on it or ID on IT, it can be replace whith an other that have no link * @param[in] idOfElement Id of the element that has been removed @@ -125,13 +125,20 @@ namespace ewol { virtual bool HaveImpact(int32_t group, int32_t type, Vector2D position, float size); virtual bool Explosion(int32_t group, int32_t type, Vector2D position, float pxAtenuation, float power) { return false; } ; /** - * @brief Requuest the draw of the current element, it will be done on the current Sprite list + * @brief Load or get a previous loaded sprite, it will be done on the current Sprite list * @param[in,out] listOfElement Reference on the list of sprite that we need to find if it exist or added a new one * @param[in] fileName Sprite name * @param[in] maxSize maximum size of the sprite * @return the id of the sprite requested or -1 if it does not existed */ int32_t LoadSprite(etk::VectorType listOfElement[NB_BOUBLE_BUFFER], etk::UString fileName, Vector2D maxSize); + /** + * @brief UnLoad or not(if needed) the sprite selected, it will be done on the current Sprite list + * @param[in,out] listOfElement Reference on the list of sprite that we need to find if it exist or added a new one + * @param[in] spriteId Sprite registered id + * @return --- + */ + void UnLoadSprite(etk::VectorType listOfElement[NB_BOUBLE_BUFFER], int32_t spriteId); virtual void Message(etk::UString control, etk::UString message) { } ; }; diff --git a/Sources/libewol/ewol/Game/GameElementLua.cpp b/Sources/libewol/ewol/Game/GameElementLua.cpp index 40d4c801..fc017f15 100644 --- a/Sources/libewol/ewol/Game/GameElementLua.cpp +++ b/Sources/libewol/ewol/Game/GameElementLua.cpp @@ -35,7 +35,6 @@ static ewol::GameElementLua * tmpObj = NULL; static etk::VectorType * tmpSprite = NULL; -static etk::VectorType * tmpEffects = NULL; static ewol::SceneElement * tmpScene = NULL; template int index(lua_State* L); @@ -410,12 +409,10 @@ LUAMOD_API int lua_ElementAdd(lua_State *L) // TODO : Remove this when find an other way do do it ... ewol::GameElementLua * ttmpObj = tmpObj; etk::VectorType * ttmpSprite = tmpSprite; - etk::VectorType * ttmpEffects = tmpEffects; ewol::SceneElement * ttmpScene = tmpScene; uint32_t elementId = tmpScene->AddElementNamed(group, elementName); tmpObj = ttmpObj; tmpSprite = ttmpSprite; - tmpEffects = ttmpEffects; tmpScene = ttmpScene; if (0==elementId) { @@ -555,14 +552,12 @@ LUAMOD_API int lua_HaveImpact(lua_State *L) // TODO : Remove this when find an other way do do it ... ewol::GameElementLua * ttmpObj = tmpObj; etk::VectorType * ttmpSprite = tmpSprite; - etk::VectorType * ttmpEffects = tmpEffects; ewol::SceneElement * ttmpScene = tmpScene; bool result = tmpScene->HaveImpact(tmpObj->GroupGet(), tmpObj->TypeGet(), tmpObj->PositionGet(), tmpObj->SizeGet()); tmpObj = ttmpObj; tmpSprite = ttmpSprite; - tmpEffects = ttmpEffects; tmpScene = ttmpScene; lua_pushboolean(L, result ); @@ -581,14 +576,12 @@ LUAMOD_API int lua_Explosion(lua_State *L) // TODO : Remove this when find an other way do do it ... ewol::GameElementLua * ttmpObj = tmpObj; etk::VectorType * ttmpSprite = tmpSprite; - etk::VectorType * ttmpEffects = tmpEffects; ewol::SceneElement * ttmpScene = tmpScene; tmpScene->Explosion(tmpObj->GroupGet(), tmpObj->TypeGet(), tmpObj->PositionGet(), 0.01, tmpObj->PowerGet()); tmpObj = ttmpObj; tmpSprite = ttmpSprite; - tmpEffects = ttmpEffects; tmpScene = ttmpScene; // return number of parameters @@ -803,11 +796,10 @@ bool ewol::GameElementLua::Process(int64_t time, int32_t deltaTime) } -void ewol::GameElementLua::Draw(etk::VectorType & listOfSprite, etk::VectorType & listOfEffects) +void ewol::GameElementLua::Draw(int32_t currentCreateId) { tmpObj = this; - tmpSprite = &listOfSprite; - tmpEffects = &listOfEffects; + tmpSprite = &m_sceneElement.animated[currentCreateId]; if (NULL != m_luaState) { // call the Draw function lua_getglobal(m_luaState, "Draw"); @@ -823,7 +815,6 @@ void ewol::GameElementLua::Draw(etk::VectorType & listOfSprite, e } tmpObj = NULL; tmpSprite = NULL; - tmpEffects = NULL; } diff --git a/Sources/libewol/ewol/Game/GameElementLua.h b/Sources/libewol/ewol/Game/GameElementLua.h index 31a896b9..3d2af025 100644 --- a/Sources/libewol/ewol/Game/GameElementLua.h +++ b/Sources/libewol/ewol/Game/GameElementLua.h @@ -46,7 +46,7 @@ namespace ewol { virtual void Init(void); virtual void UnInit(void); virtual bool Process(int64_t time, int32_t deltaTime); - virtual void Draw(etk::VectorType & listOfSprite, etk::VectorType & listOfEffects); + virtual void Draw(int32_t currentCreateId); virtual bool HaveImpact(int32_t group, int32_t type, Vector2D position, float size); virtual bool Explosion(int32_t group, int32_t type, Vector2D position, float pxAtenuation, float power); virtual void Message(etk::UString control, etk::UString message); diff --git a/Sources/libewol/ewol/Game/SceneElement.cpp b/Sources/libewol/ewol/Game/SceneElement.cpp index 2d365ce6..438ac2f8 100644 --- a/Sources/libewol/ewol/Game/SceneElement.cpp +++ b/Sources/libewol/ewol/Game/SceneElement.cpp @@ -79,15 +79,6 @@ ewol::SceneElement::~SceneElement(void) } listAnimatedElements[iii].Clear(); } - for (int32_t iii=0; iii position; //!< current position - Vector2D speed; //!< current speed - Vector2D size; //!< curent size of the element - float angle; //!< element angle + Vector2D position; //!< current position + Vector2D speed; //!< current speed + Vector2D size; //!< curent size of the element + float angle; //!< element angle } gameElementGenericProperty_ts; typedef ewol::GameElement* (creatorElement_tf)(SceneElement & sceneElement, etk::UString& tmpName, etk::UString& userString); @@ -64,9 +64,7 @@ namespace ewol { int32_t numberOfGroup; //!< curent scene number of group etk::UString groupDescription[MAX_GROUP_NUMBER]; //!< name of all the groups int32_t groupEnemy[MAX_GROUP_NUMBER][MAX_GROUP_NUMBER]; //!< list of the ennemy - etk::VectorType backgroundElements[NB_BOUBLE_BUFFER]; //!< element that must be display the first etk::VectorType animated[NB_BOUBLE_BUFFER]; //!< element that must be display the second - etk::VectorType effects[NB_BOUBLE_BUFFER]; //!< element that must be display the third etk::VectorType listAnimatedElements[MAX_GROUP_NUMBER]; //!< generic element to display order in the diffferent group etk::VectorType listGarbage; //!< garbage of the old element allocated ==> prevent multiple alloc and free etk::VectorType listCreatorElement; //!< list of all creatable elements diff --git a/Sources/libewol/ewol/OObject.cpp b/Sources/libewol/ewol/OObject.cpp index e3233295..2bdc91d0 100644 --- a/Sources/libewol/ewol/OObject.cpp +++ b/Sources/libewol/ewol/OObject.cpp @@ -37,6 +37,7 @@ ewol::OObject::OObject(void) m_hasClipping = false; m_scaling.x = 1.0; m_scaling.y = 1.0; + m_nbLoadedTime = 1; } diff --git a/Sources/libewol/ewol/OObject.h b/Sources/libewol/ewol/OObject.h index d9e8f03b..823d6934 100644 --- a/Sources/libewol/ewol/OObject.h +++ b/Sources/libewol/ewol/OObject.h @@ -43,6 +43,7 @@ namespace ewol { class OObject { protected: + int32_t m_nbLoadedTime; //!< specific in case of non multiple allocation bool m_hasClipping; clipping_ts m_clipping; Vector2D m_scaling; //!< scaling ol the object @@ -54,6 +55,15 @@ namespace ewol { void clippingEnable(void) {m_hasClipping = true;}; void scalingSet(Vector2D scale) {m_scaling = scale;}; virtual void Draw(void) = 0; + /** + * @brief Increase the number of element registered on this class ==> this is specific to decrese the memory usage in special case (scene) + */ + void IncreaseLoadedTime(void) { m_nbLoadedTime++;}; + /** + * @brief Decrease the number of element registered on this class ==> this is specific to decrese the memory usage in special case (scene) + * @return true, if no more element registered on it ... + */ + bool DecreaseLoadedTime(void) { m_nbLoadedTime--; if (m_nbLoadedTime <= 0) { return true;} else { return false;} }; }; }; diff --git a/Sources/libewol/ewol/OObject/Sprite.cpp b/Sources/libewol/ewol/OObject/Sprite.cpp index d941afc8..35bbade1 100644 --- a/Sources/libewol/ewol/OObject/Sprite.cpp +++ b/Sources/libewol/ewol/OObject/Sprite.cpp @@ -69,7 +69,7 @@ void ewol::Sprite::Draw(void) glBindTexture(GL_TEXTURE_2D, ewol::texture::GetGLID(m_textureId)); glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays - glVertexPointer( 2, GL_FLOAT, 0, &m_coord[0] ); + glVertexPointer( 3, GL_FLOAT, 0, &m_coord[0] ); glTexCoordPointer( 2, GL_FLOAT, 0, &m_coordTex[0] ); glDrawArrays( GL_TRIANGLES, 0, m_coord.Size()); //EWOL_DEBUG("request draw of " << m_coord.Size() << " elements"); @@ -98,7 +98,56 @@ void ewol::Sprite::Element(Vector2D pos, float size, float angle) texD.u = 1.0; texD.v = 0.0; - Vector2D point; + Vector3D point; + point.y = 0; + float yyySin = sin(angle) * size; + float xxxCos = cos(angle) * size; + + point.x = xxxCos + pos.x; + point.y = yyySin + pos.y; + m_coord.PushBack(point); + m_coordTex.PushBack(texB); + + point.x = yyySin + pos.x; + point.y = -xxxCos + pos.y; + m_coord.PushBack(point); + m_coordTex.PushBack(texC); + + point.x = -xxxCos + pos.x; + point.y = -yyySin + pos.y; + m_coord.PushBack(point); + m_coordTex.PushBack(texD); + + m_coord.PushBack(point); + m_coordTex.PushBack(texD); + + point.x = -yyySin + pos.x; + point.y = xxxCos + pos.y; + m_coord.PushBack(point); + m_coordTex.PushBack(texA); + + point.x = xxxCos + pos.x; + point.y = yyySin + pos.y; + m_coord.PushBack(point); + m_coordTex.PushBack(texB); +} + + +void ewol::Sprite::Element(Vector3D pos, float size, float angle) +{ + angle -= M_PI/4; + size *= 0.7; + texCoord_ts texA, texB, texC, texD; + texA.u = 0.0; + texA.v = 0.0; + texB.u = 0.0; + texB.v = 1.0; + texC.u = 1.0; + texC.v = 1.0; + texD.u = 1.0; + texD.v = 0.0; + + Vector3D point = pos; float yyySin = sin(angle) * size; float xxxCos = cos(angle) * size; diff --git a/Sources/libewol/ewol/OObject/Sprite.h b/Sources/libewol/ewol/OObject/Sprite.h index 705fd38b..fc3fae59 100644 --- a/Sources/libewol/ewol/OObject/Sprite.h +++ b/Sources/libewol/ewol/OObject/Sprite.h @@ -38,12 +38,13 @@ namespace ewol { virtual ~Sprite(void); virtual void Draw(void); void Clear(void); - void Element(Vector2D pos, float size, float angle); + void Element(Vector2D pos, float size, float angle); + void Element(Vector3D pos, float size, float angle); bool HasName(etk::UString& name) { return name == m_name; }; protected: - int32_t m_textureId; //!< texture internal ID - etk::VectorType > m_coord; //!< internal coord of the object - etk::VectorType m_coordTex; //!< internal texture coordinate for every point + int32_t m_textureId; //!< texture internal ID + etk::VectorType > m_coord; //!< internal coord of the object + etk::VectorType m_coordTex; //!< internal texture coordinate for every point }; }; diff --git a/Sources/libewol/ewol/widget/Scene.cpp b/Sources/libewol/ewol/widget/Scene.cpp index b3711851..6ca4cfb2 100644 --- a/Sources/libewol/ewol/widget/Scene.cpp +++ b/Sources/libewol/ewol/widget/Scene.cpp @@ -104,17 +104,11 @@ void ewol::Scene::OnRegenerateDisplay(void) m_sceneElement.animated[m_currentCreateId][iii]->Clear(); } } - // clean effects - for (int32_t iii=0; iiiClear(); - } - } for (int32_t jjj=0; jjjDraw(m_sceneElement.animated[m_currentCreateId], m_sceneElement.effects[m_currentCreateId]); + m_sceneElement.listAnimatedElements[jjj][iii]->Draw(m_currentCreateId); } } } @@ -131,20 +125,12 @@ void ewol::Scene::OnRegenerateDisplay(void) void ewol::Scene::OnDraw(void) { //EWOL_ERROR(" On draw : " << m_currentDrawId); - // draw background - // TODO : ... // draw elements for (int32_t iii=0; iiiDraw(); } } - // draw effects - for (int32_t iii=0; iiiDraw(); - } - } }