Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCmpWaterManager.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 "ICmpWaterManager.h"
22 
24 #include "graphics/Terrain.h"
25 #include "renderer/Renderer.h"
26 #include "renderer/WaterManager.h"
28 
30 {
31 public:
32  static void ClassInit(CComponentManager& componentManager)
33  {
34  componentManager.SubscribeToMessageType(MT_Interpolate);
35  componentManager.SubscribeToMessageType(MT_TerrainChanged);
36  }
37 
39 
40  // Dynamic state:
41 
43 
44  static std::string GetSchema()
45  {
46  return "<a:component type='system'/><empty/>";
47  }
48 
49  virtual void Init(const CParamNode& UNUSED(paramNode))
50  {
52  }
53 
54  virtual void Deinit()
55  {
56  }
57 
58  virtual void Serialize(ISerializer& serialize)
59  {
60  serialize.NumberFixed_Unbounded("height", m_WaterHeight);
61  }
62 
63  virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize)
64  {
65  Init(paramNode);
66 
67  deserialize.NumberFixed_Unbounded("height", m_WaterHeight);
68  }
69 
70  virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
71  {
72  switch (msg.GetType())
73  {
74  case MT_Interpolate:
75  {
76  const CMessageInterpolate& msgData = static_cast<const CMessageInterpolate&> (msg);
78  g_Renderer.GetWaterManager()->m_WaterTexTimer += msgData.deltaSimTime;
79  break;
80  }
81  case MT_TerrainChanged:
82  {
83  // Tell the renderer to redraw the map.
85  {
86  const CMessageTerrainChanged& msgData = static_cast<const CMessageTerrainChanged&> (msg);
87  g_Renderer.GetWaterManager()->m_NeedInfoUpdate = true;
88  g_Renderer.GetWaterManager()->m_updatei0 = msgData.i0;
89  g_Renderer.GetWaterManager()->m_updatej0 = msgData.j0;
90  g_Renderer.GetWaterManager()->m_updatei1 = msgData.i1;
91  g_Renderer.GetWaterManager()->m_updatej1 = msgData.j1;
92 
93  GetSimContext().GetTerrain().MakeDirty(msgData.i0,msgData.j0,msgData.i1,msgData.j1,RENDERDATA_UPDATE_VERTICES);
94  }
95  break;
96  }
97  }
98  }
99 
100  virtual void SetWaterLevel(entity_pos_t h)
101  {
102  m_WaterHeight = h;
103 
104  // Tell the terrain it'll need to recompute its cached render data
106  }
107 
109  {
110  return m_WaterHeight;
111  }
112 
113  virtual float GetExactWaterLevel(float UNUSED(x), float UNUSED(z))
114  {
115  return m_WaterHeight.ToFloat();
116  }
117 };
118 
An entity initialisation parameter node.
Definition: ParamNode.h:112
void SubscribeToMessageType(MessageTypeId mtid)
Subscribe the current component type to the given message type.
entity_pos_t m_WaterHeight
A simple fixed-point number class.
Definition: Fixed.h:115
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:30
void NumberFixed_Unbounded(const char *name, fixed value)
Serialize a number.
Definition: ISerializer.h:191
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
void MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags)
Definition: Terrain.cpp:598
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
static std::string GetSchema()
virtual void Init(const CParamNode &paramNode)
#define RENDERDATA_UPDATE_VERTICES
Sent when terrain (texture or elevation) has been changed.
Definition: MessageTypes.h:282
virtual float GetExactWaterLevel(float x, float z)
Get the current water level at the given point.
virtual void NumberFixed_Unbounded(const char *name, fixed &out)
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
float deltaSimTime
Elapsed simulation time since previous interpolate, in seconds.
Definition: MessageTypes.h:134
#define g_Renderer
Definition: Renderer.h:61
static void ClassInit(CComponentManager &componentManager)
virtual void HandleMessage(const CMessage &msg, bool global)
virtual int GetType() const =0
float ToFloat() const
Convert to float. May be lossy - float can&#39;t represent all values.
Definition: Fixed.h:161
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:44
const CSimContext & GetSimContext() const
Definition: IComponent.h:52
static bool IsInitialised()
Definition: Singleton.h:63
virtual void Deinit()
virtual void SetWaterLevel(entity_pos_t h)
Set the height of the water level, as a constant value across the whole map.
static CFixed FromInt(int n)
Definition: Fixed.h:136
virtual entity_pos_t GetWaterLevel(entity_pos_t x, entity_pos_t z)
Get the current water level at the given point.
Prepare for rendering a new frame (set up model positions etc).
Definition: MessageTypes.h:122
CTerrain & GetTerrain() const
Definition: SimContext.cpp:51
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34
Class WaterManager: Maintain water settings and textures.
Definition: WaterManager.h:49
virtual void Serialize(ISerializer &serialize)