00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _VISUALCOMPONENT_
00019 #define _VISUALCOMPONENT_
00020
00021 #include <map>
00022
00023 #include "game/BaseComponent.h"
00024 #include "game/VisualFactory.h"
00025
00026 class AbstractRenderer;
00027
00028 class VisualComponent : public BaseComponent
00029 {
00030 private:
00031
00032 DEFINE_STL_TYPE(std::map<CONCAT(std::string, BaseVisual*)>, VisualMap);
00033
00034 VisualMap m_visuals;
00035
00036 public:
00037 DEFAULT_COMPONENT_CONSTRUCTOR(BaseComponent, VisualComponent, , ,);
00038
00039 template<class T>
00040 T * CreateVisual(const std::string & type, const std::string & name);
00041
00042 template<class T>
00043 T * GetVisual(const std::string & name);
00044
00045 void RemoveVisual(const std::string & name);
00046 void Render(AbstractRenderer* pRenderer, const int nDelta);
00047
00048 virtual void Update(const int nDelta) { }
00049
00050 virtual void ToStream(std::ostream & stream) const;
00051 virtual void FromStream(std::istream & stream);
00052 virtual bool FromAttribute(const Attribute* pRoot);
00053 };
00054
00055 template<class T>
00056 T * VisualComponent::CreateVisual(const std::string & type, const std::string & name)
00057 {
00058 if(m_visuals.find(name) != m_visuals.end())
00059 {
00060 assert(false && "Attempted to add visual with a name that has already been allocated");
00061 return false;
00062 }
00063
00064 T* pVisual = VisualFactoryCreate<T>(type);
00065
00066 assert(pVisual && "Invalid visual type");
00067
00068 m_visuals[name] = (BaseVisual*)pVisual;
00069
00070 pVisual->SetOwner(m_pGameEntity);
00071
00072 return pVisual;
00073 }
00074
00075 template<class T>
00076 T * VisualComponent::GetVisual(const std::string & name)
00077 {
00078 return m_visuals[name];
00079 }
00080
00081 REGISTER_COMPONENT(VisualComponent, "component_visual", "visual");
00082
00083 #endif