simplify the sprite loading

This commit is contained in:
Edouard Dupin 2012-06-26 10:16:11 +02:00
parent 7ed04e5ec6
commit 569b191389
8 changed files with 147 additions and 72 deletions

View File

@ -0,0 +1,40 @@
/**
*******************************************************************************
* @file etk/tool.cpp
* @brief Ewol Tool Kit : generique tools (Sources)
* @author Edouard DUPIN
* @date 26/06/2012
* @par Project
* Ewol TK
*
* @par Copyright
* Copyright 2011 Edouard DUPIN, all right reserved
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY.
*
* Licence summary :
* You can modify and redistribute the sources code and binaries.
* You can send me the bug-fix
*
* Term of the licence in in the file licence.txt.
*
*******************************************************************************
*/
#include <etk/tool.h>
// for the rand ...
#include <time.h>
#include <math.h>
float etk::tool::frand(float a, float b)
{
return ( rand()/(float)RAND_MAX ) * (b-a) + a;
}
int32_t etk::tool::irand(int32_t a, int32_t b)
{
return (int32_t)(( rand()/(float)RAND_MAX ) * ((float)b-(float)a) + (float)a);
}

37
Sources/libetk/etk/tool.h Normal file
View File

@ -0,0 +1,37 @@
/**
*******************************************************************************
* @file etk/tool.h
* @brief Ewol Tool Kit : generique tools (header)
* @author Edouard DUPIN
* @date 26/06/2012
* @par Project
* Ewol TK
*
* @par Copyright
* Copyright 2011 Edouard DUPIN, all right reserved
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY.
*
* Licence summary :
* You can modify and redistribute the sources code and binaries.
* You can send me the bug-fix
*
* Term of the licence in in the file licence.txt.
*
*******************************************************************************
*/
#ifndef __ETK_TOOL_H__
#define __ETK_TOOL_H__
#include <etk/Types.h>
namespace etk {
namespace tool {
float frand(float a, float b);
int32_t irand(int32_t a, int32_t b);
};
};
#endif

View File

@ -10,6 +10,7 @@ FILE_LIST = \
etk/Stream.cpp \
etk/File.cpp \
etk/RegExp.cpp \
etk/Color.cpp
etk/Color.cpp \
etk/tool.cpp

View File

@ -54,58 +54,6 @@ ewol::GameElement::GameElement(SceneElement & sceneElement, etk::UString& tmpNam
}
/**
* @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 ewol::GameElement::LoadSprite(etk::VectorType<ewol::Sprite*> listOfElement[NB_BOUBLE_BUFFER], etk::UString fileName, Vector2D<float> maxSize)
{
for (int32_t iii=0; iii<listOfElement[0].Size(); iii++) {
if (listOfElement[0][iii] != NULL) {
if (listOfElement[0][iii]->HasName(fileName) == true) {
// count the number of element registered ...
listOfElement[0][iii]->IncreaseLoadedTime();
return iii;
}
}
}
for(int32_t iii=0; iii<NB_BOUBLE_BUFFER; iii++) {
// we did not find the sprite ==> created it ...
ewol::Sprite* tmpSprite = new ewol::Sprite(fileName, maxSize.x, maxSize.y);
if (NULL == tmpSprite) {
EWOL_ERROR("Allocation error on the sprite : " << fileName);
return -1;
}
// add it :
listOfElement[iii].PushBack(tmpSprite);
}
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<ewol::Sprite*> 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<NB_BOUBLE_BUFFER; iii++) {
}
}
}
}
}
float quadDist(Vector2D<float> pos1, Vector2D<float> pos2)

View File

@ -143,21 +143,6 @@ namespace ewol {
virtual void RemoveElement(int32_t idOfElement) { };
virtual bool HaveImpact(int32_t group, int32_t type, Vector2D<float> position, float size);
virtual bool Explosion(int32_t group, int32_t type, Vector2D<float> position, float pxAtenuation, float power) { return false; } ;
/**
* @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<ewol::Sprite*> listOfElement[NB_BOUBLE_BUFFER], etk::UString fileName, Vector2D<float> 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<ewol::Sprite*> listOfElement[NB_BOUBLE_BUFFER], int32_t spriteId);
virtual void Message(etk::UString control, etk::UString message) { } ;
};

View File

@ -336,10 +336,7 @@ LUAMOD_API int lua_SpriteLoad(lua_State *L)
}
const char *filename = luaL_checkstring(L, 1);
int32_t maxSize = luaL_checkint(L, 2);
Vector2D<float> size;
size.x = maxSize;
size.y = size.x;
int32_t spriteID = tmpObj->LoadSprite( tmpScene->animated, filename, size);
int32_t spriteID = tmpScene->LoadSprite( filename, maxSize);
if (spriteID < 0) {
EWOL_ERROR("Error to load the sprite : " << filename);
}

View File

@ -313,3 +313,56 @@ void ewol::SceneElement::SetEventExternJoystick(uint32_t id, int32_t joyId, floa
EWOL_TODO("but when ...");
}
/**
* @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 ewol::SceneElement::LoadSprite(etk::UString fileName, float maxSize)
{
for (int32_t iii=0; iii<animated[0].Size(); iii++) {
if (animated[0][iii] != NULL) {
if (animated[0][iii]->HasName(fileName) == true) {
// count the number of element registered ...
animated[0][iii]->IncreaseLoadedTime();
return iii;
}
}
}
for(int32_t iii=0; iii<NB_BOUBLE_BUFFER; iii++) {
// we did not find the sprite ==> created it ...
ewol::Sprite* tmpSprite = new ewol::Sprite(fileName, maxSize, maxSize);
if (NULL == tmpSprite) {
EWOL_ERROR("Allocation error on the sprite : " << fileName);
return -1;
}
// add it :
animated[iii].PushBack(tmpSprite);
}
return animated[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::SceneElement::UnLoadSprite(int32_t spriteId)
{
if (spriteId >= 0 && spriteId < animated[0].Size()) {
if (animated[0][spriteId] != NULL) {
// count the number of element registered ...
if (true == animated[0][spriteId]->DecreaseLoadedTime() ) {
// must remove the sprite ==> pb with the double buffer ...
// TODO : ==> for all double buffer ...
for(int32_t iii=0; iii<NB_BOUBLE_BUFFER; iii++) {
}
}
}
}
}

View File

@ -71,6 +71,20 @@ namespace ewol {
void SetEventInput(uint32_t id, Vector2D<float> position);
void SetEventExternButton(uint32_t id, int32_t btId, int32_t state);
void SetEventExternJoystick(uint32_t id, int32_t joyId, float angle, float distance, int32_t state);
/**
* @brief Load or get a previous loaded sprite, it will be done on the current Sprite list
* @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::UString fileName, float 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(int32_t spriteId);
};
};