Leviathan  0.8.0.0
Leviathan game engine
Event.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "Event.h"
3 
4 #include "Exceptions.h"
5 #include <boost/assign/list_of.hpp>
6 using namespace Leviathan;
7 // ------------------------------------ //
9  Type(type), Data(data)
10 {
11  // Check that types that require values have values //
12  if(!Data) {
13  // Check that the event has data //
15 
16  throw InvalidArgument("Event that requires data, didn't get it");
17  }
18  }
19 }
20 
22 {
23  // Delete our data //
25 }
26 // ------------------------------------ //
28 {
29  return Type;
30 }
31 // ------------------------------------ //
32 #ifdef LEVIATHAN_USING_SFML
33 DLLEXPORT void Leviathan::Event::AddDataToPacket(sf::Packet& packet) const
34 {
35  // Add the type first //
36  packet << (int)Type;
37 
38  // Add the data object //
39  if(Data)
40  Data->AddDataToPacket(packet);
41 }
42 
44 {
45  // Get the type from the packet //
46  int tmptype;
47 
48  if(!(packet >> tmptype)) {
49 
50  throw InvalidArgument("packet has invalid format");
51  }
52 
53  // Set our type //
54  Type = static_cast<EVENT_TYPE>(tmptype);
55 
56  // Load based on type //
57  switch(Type) {
59  case EVENT_TYPE_TICK: {
60  Data = new FloatEventData(packet);
61  break;
62  }
63  default:
64  // No data required //
65  Data = NULL;
66  }
67 }
68 #endif // LEVIATHAN_USING_SFML
69 // ------------------------------------ //
71 {
72  if(Type == EVENT_TYPE_TICK || Type == EVENT_TYPE_FRAME_BEGIN)
73  return static_cast<FloatEventData*>(Data);
74  return NULL;
75 }
76 // ------------------ GenericEvent ------------------ //
78  const std::string& type, const NamedVars& copyvals) :
79  TypeStr(new std::string(type)),
80  Variables(new NamedVars(copyvals))
81 {}
82 
84  std::string* takeownershipstr, NamedVars* takeownershipvars) :
85  TypeStr(takeownershipstr),
86  Variables(takeownershipvars)
87 {}
88 
90  TypeStr(new std::string(type)), Variables(new NamedVars())
91 {}
92 
94 {
95  // release memory //
96  SAFE_DELETE(TypeStr);
97  SAFE_RELEASE(Variables);
98 }
99 // ------------------------------------ //
100 #ifdef LEVIATHAN_USING_SFML
102 {
103  // Load data from the packet //
104  std::unique_ptr<std::string> tmpstr(new std::string());
105  if(!(packet >> *tmpstr)) {
106 
107  throw InvalidArgument("packet has invalid format");
108  }
109 
110  // Try to get the named variables //
111  std::unique_ptr<NamedVars> tmpvars(new NamedVars(packet));
112 
113  // Take the string away from the smart pointer //
114  TypeStr = tmpstr.release();
115 
116  // Take the variables away //
117  Variables = tmpvars.release();
118 }
119 
121 {
122  // Add data to the packet //
123  packet << *TypeStr;
124 
125  Variables->AddDataToPacket(packet);
126 }
127 #endif // LEVIATHAN_USING_SFML
128 // ------------------------------------ //
130 {
131  return TypeStr;
132 }
133 
135 {
136  return *TypeStr;
137 }
138 
140 {
141  return *Variables;
142 }
143 
145 {
146  return Variables;
147 }
148 // ------------------------------------ //
150 {
151  Variables->AddRef();
152  return Variables;
153 }
154 // ------------------ FloatEventData ------------------ //
155 #ifdef LEVIATHAN_USING_SFML
157 {
158 
159  packet >> FloatDataValue;
160 
161  if(!packet)
162  throw InvalidArgument("packet has invalid format");
163 }
164 
166 {
167  packet << FloatDataValue;
168 }
169 #endif // LEVIATHAN_USING_SFML
170 
171 DLLEXPORT Leviathan::FloatEventData::FloatEventData(float elapsed) : FloatDataValue(elapsed) {}
172 // ------------------ BaseEventData ------------------ //
DLLEXPORT GenericEvent(sf::Packet &packet)
Constructs this object from a packet.
Definition: Event.cpp:101
DLLEXPORT std::string * GetTypePtr()
Returns the TypeStr ptr.
Definition: Event.cpp:129
EVENT_TYPE Type
Events type.
Definition: Event.h:133
DLLEXPORT NamedVars * GetVariables()
Returns a direct pointer to this objects variables.
Definition: Event.cpp:144
EVENT_TYPE
Engine events that are triggered at certain times.
Definition: Event.h:12
DLLEXPORT FloatEventData(float elapsed)
Loads from a packet.
Definition: Event.cpp:171
BaseEventData * Data
Direct pointer to the data.
Definition: Event.h:136
DLLEXPORT std::string GetType() const
Returns the name of the event.
Definition: Event.cpp:134
#define SAFE_RELEASE(x)
Definition: Define.h:134
DLLEXPORT FloatEventData * GetFloatDataForEvent() const
Gets the data if this is an event that has only one integer data member.
Definition: Event.cpp:70
DLLEXPORT const NamedVars GetVariablesConst() const
Gets this event's variables.
Definition: Event.cpp:139
DLLEXPORT EVENT_TYPE GetType() const
Gets the Type of the event.
Definition: Event.cpp:27
DLLEXPORT NamedVars * GetNamedVarsRefCounted()
Proxy for script to get Variables.
Definition: Event.cpp:149
DLLEXPORT Event(sf::Packet &packet)
Loads this event from a packet.
Definition: Event.cpp:43
DLLEXPORT void AddDataToPacket(sf::Packet &packet) const
Saves this event to a packet.
Definition: Event.cpp:33
FORCE_INLINE void AddRef() const
DLLEXPORT ~Event()
Definition: Event.cpp:21
virtual ~BaseEventData()
Definition: Event.cpp:173
DLLEXPORT ~GenericEvent()
Definition: Event.cpp:93
#define DLLEXPORT
Definition: Include.h:84
Data for EVENT_TYPE_ENGINE_TICK and all others that have only int data.
Definition: Event.h:90
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
#define SAFE_DELETE(x)
Definition: Define.h:149
Base type for all event data types.
Definition: Event.h:79
virtual void AddDataToPacket(sf::Packet &packet)
Adds this to a packet for retrieving it later.
Definition: Event.cpp:165
DLLEXPORT void AddDataToPacket(sf::Packet &packet) const
Serializes this event to a packet.
Definition: Event.cpp:120