Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCmpOverlayRenderer.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
21 #include "ICmpOverlayRenderer.h"
22 
23 #include "ICmpPosition.h"
25 
26 #include "graphics/Overlay.h"
28 #include "renderer/Renderer.h"
29 
31 {
32 public:
33  static void ClassInit(CComponentManager& componentManager)
34  {
35  componentManager.SubscribeToMessageType(MT_Interpolate);
36  componentManager.SubscribeToMessageType(MT_RenderSubmit);
37  }
38 
40 
41  // Currently-enabled set of sprites
42  std::vector<SOverlaySprite> m_Sprites;
43 
44  // For each entry in m_Sprites, store the offset of the sprite from the unit's position
45  // (so we can recompute the sprite position after the unit moves)
46  std::vector<CVector3D> m_SpriteOffsets;
47 
48  // Whether the sprites should be drawn (only valid between Interpolate and RenderSubmit)
49  bool m_Enabled;
50 
51  static std::string GetSchema()
52  {
53  return "<a:component type='system'/><empty/>";
54  }
55 
56  virtual void Init(const CParamNode& UNUSED(paramNode))
57  {
58  }
59 
60  virtual void Deinit()
61  {
62  }
63 
64  virtual void Serialize(ISerializer& UNUSED(serialize))
65  {
66  // TODO: should we do anything here?
67  // or should we expect other components to reinitialise us
68  // after deserialization?
69  }
70 
71  virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize))
72  {
73  Init(paramNode);
74  }
75 
76  virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
77  {
78  switch (msg.GetType())
79  {
80  case MT_Interpolate:
81  {
82  const CMessageInterpolate& msgData = static_cast<const CMessageInterpolate&> (msg);
83  Interpolate(msgData.deltaSimTime, msgData.offset);
84  break;
85  }
86  case MT_RenderSubmit:
87  {
88  const CMessageRenderSubmit& msgData = static_cast<const CMessageRenderSubmit&> (msg);
89  RenderSubmit(msgData.collector);
90  break;
91  }
92  }
93  }
94 
95  virtual void Reset()
96  {
97  m_Sprites.clear();
98  m_SpriteOffsets.clear();
99  }
100 
101  virtual void AddSprite(VfsPath textureName, CFixedVector2D corner0, CFixedVector2D corner1, CFixedVector3D position)
102  {
103  CTextureProperties textureProps(textureName);
104 
105  SOverlaySprite sprite;
106  sprite.m_Texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
107  sprite.m_X0 = corner0.X.ToFloat();
108  sprite.m_Y0 = corner0.Y.ToFloat();
109  sprite.m_X1 = corner1.X.ToFloat();
110  sprite.m_Y1 = corner1.Y.ToFloat();
111 
112  m_Sprites.push_back(sprite);
113  m_SpriteOffsets.push_back(CVector3D(position));
114  }
115 
116  void Interpolate(float UNUSED(frameTime), float frameOffset)
117  {
118  // Skip all the following computations if we have no sprites
119  if (m_Sprites.empty())
120  {
121  m_Enabled = false;
122  return;
123  }
124 
125  // Disable rendering of the unit if it has no position
126  CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle());
127  if (!cmpPosition || !cmpPosition->IsInWorld())
128  {
129  m_Enabled = false;
130  return;
131  }
132 
133  // Find the precise position of the unit
134  CMatrix3D transform(cmpPosition->GetInterpolatedTransform(frameOffset, false));
135  CVector3D position(transform.GetTranslation());
136 
137  // Move all the sprites to the desired offset relative to the unit
138  for (size_t i = 0; i < m_Sprites.size(); ++i)
139  m_Sprites[i].m_Position = position + m_SpriteOffsets[i];
140 
141  m_Enabled = true;
142  }
143 
144  void RenderSubmit(SceneCollector& collector)
145  {
146  if (!m_Enabled)
147  return;
148 
149  for (size_t i = 0; i < m_Sprites.size(); ++i)
150  collector.Submit(&m_Sprites[i]);
151  }
152 };
153 
An entity initialisation parameter node.
Definition: ParamNode.h:112
void SubscribeToMessageType(MessageTypeId mtid)
Subscribe the current component type to the given message type.
std::vector< SOverlaySprite > m_Sprites
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:30
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
float m_X1
Definition: Overlay.h:141
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
Class OverlayRenderer: Render various bits of data that overlay the game world (selection circles...
Represents the filename and GL parameters of a texture, for passing to CTextureManager::CreateTexture...
Add renderable objects to the scene collector.
Definition: MessageTypes.h:145
Billboard sprite overlay, with world-space coordinates, rendered on top of all other objects...
Definition: Overlay.h:137
virtual CMatrix3D GetInterpolatedTransform(float frameOffset, bool forceFloating)=0
Get the current interpolated transform matrix, for rendering.
static void ClassInit(CComponentManager &componentManager)
void Interpolate(float frameTime, float frameOffset)
virtual bool IsInWorld()=0
Returns true if the entity currently exists at a defined position in the world.
static std::string GetSchema()
float deltaSimTime
Elapsed simulation time since previous interpolate, in seconds.
Definition: MessageTypes.h:134
virtual void Reset()
Delete all sprites that have been previously added.
This interface accepts renderable objects.
Definition: Scene.h:82
#define g_Renderer
Definition: Renderer.h:61
float m_X0
Definition: Overlay.h:141
CEntityHandle GetEntityHandle() const
Definition: IComponent.h:45
virtual void AddSprite(VfsPath textureName, CFixedVector2D corner0, CFixedVector2D corner1, CFixedVector3D position)
Add a new textured billboard sprite to be rendered.
virtual void HandleMessage(const CMessage &msg, bool global)
virtual int GetType() const =0
float m_Y1
Definition: Overlay.h:141
Definition: path.h:75
void RenderSubmit(SceneCollector &collector)
float ToFloat() const
Convert to float. May be lossy - float can&#39;t represent all values.
Definition: Fixed.h:161
float offset
Range [0, 1] (inclusive); fractional time of current frame between previous/next simulation turns...
Definition: MessageTypes.h:136
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:44
A simplified syntax for accessing entity components.
Definition: CmpPtr.h:55
SceneCollector & collector
Definition: MessageTypes.h:155
float m_Y0
Definition: Overlay.h:141
CTexturePtr m_Texture
Definition: Overlay.h:139
Prepare for rendering a new frame (set up model positions etc).
Definition: MessageTypes.h:122
virtual void Serialize(ISerializer &serialize)
virtual void Submit(CPatch *patch)=0
Submit a terrain patch that is part of the scene.
Interface for rendering &#39;overlay&#39; objects (typically sprites), automatically positioned relative to t...
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34
virtual void Init(const CParamNode &paramNode)
std::vector< CVector3D > m_SpriteOffsets