what I want is to create drawable objects that "contain" some other drawable so that when something is changed betwen them, automatically redraw all the "contained" object on it's textures. I had various test, but this idea seemed the best because it allowed me to import methods from both classes.
here is the complete code:
#include <SFML/Graphics/RenderTexture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <vector>
#ifndef _OG_COMPDRW_HPP_
#define _OG_COMPDRW_HPP_
#ifndef _DESKTOP_SIZE_
#include <SFML/Window/VideoMode.hpp>
#define _DESKTOP_WIDTH_ sf::VideoMode::GetDesktopMode().Width
#define _DESKTOP_HEIGHT_ sf::VideoMode::GetDesktopMode().Height
#define _DESKTOP_SIZE_ sf::Vector2f(_DESKTOP_WIDTH_,_DESKTOP_HEIGHT_)
#endif
namespace og
{
class CompDrw : public sf::RenderTexture , public sf::Sprite
{
public :
CompDrw(){
sf::RenderTexture();
sf::Texture tmpTexture = sf::RenderTexture::GetTexture();
sf::Sprite::SetTexture(tmpTexture);
CompDrw::Show();
CompDrw::Updated = true;
CompDrw::Lightable = false;
}
CompDrw(bool isLghtble){
sf::RenderTexture();
sf::Texture tmpTexture = sf::RenderTexture::GetTexture();
sf::Sprite::SetTexture(tmpTexture);
CompDrw::Show();
CompDrw::Updated = true;
CompDrw::Lightable = isLghtble;
}
void Create(unsigned int width = _DESKTOP_WIDTH_, unsigned int height = _DESKTOP_HEIGHT_, bool depthBuffer = false) {
sf::RenderTexture::Create(width, height, depthBuffer);
CompDrw::Updated = false;
}
const bool IsLightable(){
return CompDrw::Lightable;
}
const bool IsOpen(){
return CompDrw::Shown;
}
void Show(bool showthis = true){
CompDrw::Shown = showthis;
}
void Hide(bool hidethis = true){
CompDrw::Show(!hidethis);
}
void Add(const sf::Drawable&){
CompDrw::myDrawableList.push_back(&object);
CompDrw::Updated = false;
}
void Swap(const sf::Drawable&, const sf::Drawable&){
int i1 = CompDrw::FindDrawable(obj1);
int i2 = CompDrw::FindDrawable(obj2);
if (i1==-1 || i2==-1) return;
std::swap( CompDrw::myDrawableList[i1], CompDrw::myDrawableList[i1] );
CompDrw::Updated = false;
}
void Cancel(const sf::Drawable&){
int i = CompDrw::FindDrawable(obj);
if (i==-1) return;
CompDrw::myDrawableList.erase( CompDrw::myDrawableList.begin() + i );
CompDrw::Updated = false;
}
void Empty(){
sf::RenderTexture::Clear();
CompDrw::myDrawableList.clear();
CompDrw::Updated = true;
}
void Clear(){
sf::RenderTexture::Clear();
CompDrw::Updated = false;
}
void Update(bool updating = true){
if (!updating)
{
CompDrw::Updated = false;
return;
}
if (CompDrw::Updated)
return;
CompDrw::UpdateTexture();
}
private :
void UpdateTexture(){
CompDrw::Clear();
int N = CompDrw::myDrawableList.size();
int i;
for ( i=0; i<N; i++ )
sf::RenderTarget::Draw(*CompDrw::myDrawableList[i]);
sf::RenderTexture::Display();
sf::Sprite::SetTexture(sf::RenderTexture::GetTexture(), true);
CompDrw::Updated = true;
}
int FindDrawable(const sf::Drawable&){
int N = CompDrw::myDrawableList.size();
int i;
for ( i=0; i<N; i++)
if ( CompDrw::myDrawableList[i] == &obj )
return i;
return -1;
}
bool Shown;
bool Lightable;
bool Updated;
std::vector<const sf::Drawable *> myDrawableList;
}; // end class CompostiteDrawable
} // end namespace og
#endif // end _OG_COMPDRW_HPP_
[/code]