1 #ifndef RENDERCOMPONENT_H_
2 #define RENDERCOMPONENT_H_
3 
4 #include "../../SharedDefines.h"
5 #include "../ActorComponent.h"
6 
7 class Image;
8 typedef std::map<std::string, shared_ptr<Image>> ImageMap;
9 
10 //=================================================================================================
11 // BaseRenderComponent Declaration
12 //
13 
14 class PositionComponent;
15 class SceneNode;
16 class BaseRenderComponent : public ActorComponent
17 {
18 public:
19     virtual bool VInit(TiXmlElement* data) override;
20     virtual TiXmlElement* VGenerateXml() override;
21     virtual void VPostInit() override;
22     virtual void VOnChanged() override;
23 
24     weak_ptr<Image> GetImage(std::string imageName);
25     weak_ptr<Image> GetImage(uint32 imageId);
26     bool HasImage(std::string imageName);
27     bool HasImage(int32 imageId);
28 
GetImagesCount()29     uint32 GetImagesCount() const { return m_ImageMap.size(); }
30 
31     // Gets actor's X-Y-W-H
32     virtual SDL_Rect VGetPositionRect() const = 0;
33 
GetScneNodePublicTest()34     shared_ptr<SceneNode> GetScneNodePublicTest() { return GetSceneNode(); }
35 
36 protected:
37     // loads the SceneNode specific data (represented in the <SceneNode> tag)
VDelegateInit(TiXmlElement * pData)38     virtual bool VDelegateInit(TiXmlElement* pData) { return true; }
39     virtual shared_ptr<SceneNode> VCreateSceneNode(void) = 0;  // factory method to create the appropriate scene node
40 
41     // editor stuff
VCreateBaseElement(void)42     virtual TiXmlElement* VCreateBaseElement(void) { return NULL; /*return new TiXmlElement(VGetName());*/ }
43     virtual void VCreateInheritedXmlElements(TiXmlElement* pBaseElement) = 0;
44 
45     ImageMap m_ImageMap;
46 
47     shared_ptr<SceneNode> m_pSceneNode;
48 
49     PositionComponent* m_pPositionComponent;
50 
51 private:
52     shared_ptr<SceneNode> GetSceneNode();
53 };
54 
55 //=================================================================================================
56 
57 
58 //=================================================================================================
59 // ActorRenderComponent Declaration
60 //
61 
62 class ActorRenderComponent : public BaseRenderComponent
63 {
64 public:
65     ActorRenderComponent();
66 
67     static const char* g_Name;
VGetName()68     virtual const char* VGetName() const override { return g_Name; }
69 
70     virtual bool VDelegateInit(TiXmlElement* pXmlData) override;
71 
72     virtual SDL_Rect VGetPositionRect() const override;
73 
GetCurrentImage()74     weak_ptr<Image> GetCurrentImage() { return m_CurrentImage; }
75     void SetImage(std::string imageName);
76 
SetMirrored(bool mirrored)77     void SetMirrored(bool mirrored) { m_IsMirrored = mirrored; }
78 
SetVisible(bool visible)79     inline void SetVisible(bool visible) { m_IsVisible = visible; }
IsVisible()80     inline bool IsVisible() { return m_IsVisible; }
IsMirrored()81     inline bool IsMirrored() { return m_IsMirrored; }
IsInverted()82     inline bool IsInverted() { return m_IsInverted; }
GetAlpha()83     inline int GetAlpha() { return m_Alpha; }
SetAlpha(int alpha)84     inline void SetAlpha(int alpha) { m_Alpha = alpha; }
GetColorMod()85     inline SDL_Color GetColorMod() { return m_ColorMod; }
SetColorMod(const SDL_Color & color)86     void SetColorMod(const SDL_Color& color) { m_ColorMod = color; }
87 
88 protected:
89     virtual shared_ptr<SceneNode> VCreateSceneNode() override;
90 
91     // Editor stuff
92     virtual void VCreateInheritedXmlElements(TiXmlElement* pBaseElement) override;
93 
94     shared_ptr<Image> m_CurrentImage;
95 
96 private:
97     bool m_IsVisible;
98     bool m_IsMirrored;
99     bool m_IsInverted;
100     int m_Alpha;
101     SDL_Color m_ColorMod;
102     int32 m_ZCoord;
103 };
104 
105 //=================================================================================================
106 
107 
108 //=================================================================================================
109 // TilePlaneRenderComponent Declaration
110 //
111 
112 enum TilePlaneRenderPosition
113 {
114     TilePlaneRenderPosition_Background,
115     TilePlaneRenderPosition_Action,
116     TilePlaneRenderPosition_Foreground
117 };
118 
119 typedef std::vector<Image*> TileImageList;
120 
121 struct TilePlaneProperties
122 {
123     std::string name;
124 
125     int tilePixelWidth;
126     int tilePixelHeight;
127 
128     int tilesOnAxisX;
129     int tilesOnAxisY;
130 
131     int planePixelWidth;
132     int planePixelHeight;
133 
134     int movementPercentX;
135     int movementPercentY;
136 
137     int fillColor;
138 
139     int zCoord;
140 
141     bool isMainPlane;
142     bool isDrawable;
143     bool isWrappedX;
144     bool isWrappedY;
145     bool isTileAutosized;
146 };
147 
148 // Helper struct
149 struct TileInfo
150 {
151     int tileId;
152     int x;
153     int y;
154 };
155 
156 typedef std::vector<int> TileList;
157 class TilePlaneRenderComponent : public BaseRenderComponent
158 {
159 public:
160     static const char* g_Name;
VGetName()161     virtual const char* VGetName() const override { return g_Name; }
162 
163     virtual bool VDelegateInit(TiXmlElement* pXmlData) override;
164 
165     virtual SDL_Rect VGetPositionRect() const override;
166 
GetTilePlaneProperties()167     const TilePlaneProperties* const GetTilePlaneProperties() const { return &m_PlaneProperties; }
GetTileImageList()168     const TileImageList* const GetTileImageList() const { return &m_TileImageList; }
169 
170 protected:
171     virtual shared_ptr<SceneNode> VCreateSceneNode() override;
172 
173     // Editor stuff
174     virtual void VCreateInheritedXmlElements(TiXmlElement* pBaseElement) override;
175 
176 private:
177     void ProcessMainPlaneTiles(const TileList& tileList);
178     TileList GetAllContinuousTiles(const TileList& tileList, int fromTileIdx);
179     TileInfo GetTileInfo(const TileList& tileList, int tileIdx);
180 
181     // Background, action, foreground
182     TilePlaneRenderPosition m_RenderLocation;
183 
184     std::string m_PlaneName;
185 
186     TilePlaneProperties m_PlaneProperties;
187 
188     // This is 1D array representing all tiles from top left to bottom right corner
189     TileImageList m_TileImageList;
190 
191     // Fill color
192     shared_ptr<Image> m_pFillImage;
193 
194     // How far does this plane span
195     SDL_Rect m_PositionRect;
196 };
197 
198 //=================================================================================================
199 
200 //=================================================================================================
201 // HUDRenderComponent Declaration
202 //
203 
204 class HUDRenderComponent : public ActorRenderComponent
205 {
206 public:
207     HUDRenderComponent();
208 
209     static const char* g_Name;
VGetName()210     virtual const char* VGetName() const override { return g_Name; }
211     virtual bool VDelegateInit(TiXmlElement* pXmlData) override;
212 
213     virtual SDL_Rect VGetPositionRect() const override;
214 
IsAnchoredRight()215     bool IsAnchoredRight() { return m_IsAnchoredRight; }
IsAnchoredBottom()216     bool IsAnchoredBottom() { return m_IsAnchoredBottom; }
217 
218 protected:
219     virtual shared_ptr<SceneNode> VCreateSceneNode() override;
220 
221     // Editor stuff
222     virtual void VCreateInheritedXmlElements(TiXmlElement* pBaseElement) override;
223 
224 private:
225     bool m_IsAnchoredRight;
226     bool m_IsAnchoredBottom;
227     std::string m_HUDElementKey;
228 };
229 
230 //=================================================================================================
231 
232 #endif
233