LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_event.h
Go to the documentation of this file.
1 /*
2  LibNRG - Networking for Real-time Games
3 
4  Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
25 #ifndef NRG_EVENT_H
26 #define NRG_EVENT_H
27 #include "nrg_core.h"
28 #include "nrg_queue.h"
29 
30 namespace nrg {
31 
33 typedef enum {
34  /* Client-side */
35  DISCONNECTED = 1,
36  ENTITY_UPDATED,
37  ENTITY_CREATED,
38  ENTITY_DESTROYED,
39 
40  /* Server-side */
41  PLAYER_JOIN,
42  PLAYER_LEAVE,
43  PLAYER_INPUT,
44 } EventType;
45 
48  uint8_t type;
49  const char* reason;
50 };
51 
52 class Entity;
53 
55 struct EntityEvent {
56  uint8_t type;
57  uint16_t eid;
58  uint16_t etype;
60 };
61 
62 class Player;
63 
65 struct PlayerEvent {
66  uint8_t type;
67  uint16_t id;
69 };
70 
72 union Event {
73  uint8_t type;
79  Event() : type(0){}
80 
82  Event(const DisconnectEvent& e) : dc(e){}
83 
85  Event(const EntityEvent& e) : entity(e){}
86 
88  Event(const PlayerEvent& e) : player(e){}
89 };
90 
92 class EventQueue {
93 public:
95  EventQueue() : queue(32){}
96 
98  void pushEvent(const Event& e){
99  queue.push(e);
100  }
101 
103  bool pollEvent(Event& e){
104  if(queue.empty()){
105  return false;
106  } else {
107  e = queue.pop();
108  return true;
109  }
110  }
111 
113  void clear(){
114  queue.clear();
115  }
116 private:
117  Queue<Event> queue;
118 };
119 
120 }
121 
122 #endif
void clear()
Removes all Events from the queue.
Definition: nrg_event.h:113
Event()
Default Constructor.
Definition: nrg_event.h:79
const char * reason
Statically-allocated reason for the disconnection.
Definition: nrg_event.h:49
Event(const DisconnectEvent &e)
Implicit conversion constructor from DisconnectEvent.
Definition: nrg_event.h:82
Simple queue class built on a std::vector.
Definition: nrg_queue.h:34
uint16_t id
The player's ID assigned by the library.
Definition: nrg_event.h:67
uint8_t type
Used to determine which event this is.
Definition: nrg_event.h:73
void pushEvent(const Event &e)
Add an event to the end of the queue.
Definition: nrg_event.h:98
Event raised when entities are updated, created, destroyed client-side - an alternative to the virtua...
Definition: nrg_event.h:55
Represents a connected client on the server.
Definition: nrg_player.h:34
Union to contain all the event types.
Definition: nrg_event.h:72
Event(const EntityEvent &e)
Implicit conversion constructor from EntityEvent.
Definition: nrg_event.h:85
Event raised on the server when a player joins or leaves.
Definition: nrg_event.h:65
Abstract class to be inherited by users of the library which acts as a container of one or more Field...
Definition: nrg_entity.h:37
Holds a queue of Event objects.
Definition: nrg_event.h:92
Event(const PlayerEvent &e)
Implicit conversion constructor from PlayerEvent.
Definition: nrg_event.h:88
uint16_t eid
The Entity's ID.
Definition: nrg_event.h:57
Entity * pointer
Pointer to the Entity - don't dereference it on ENTITY_DESTROYED!
Definition: nrg_event.h:59
Player * player
Pointer to the Player - don't dereference it on PLAYER_LEAVE!
Definition: nrg_event.h:68
bool pollEvent(Event &e)
Place the Event at the head of the queue into e - return true if this happened or false if the queue ...
Definition: nrg_event.h:103
EventQueue()
Default Constructor.
Definition: nrg_event.h:95
EntityEvent entity
For ENTITY_{UPDATED, CREATED, DESTROYED}.
Definition: nrg_event.h:75
uint8_t type
Will be DISCONNECTED.
Definition: nrg_event.h:48
A simple queue implementation.
uint8_t type
Will be PLAYER_{JOIN, LEAVE}.
Definition: nrg_event.h:66
uint8_t type
Will be ENTITY_{UPDATED, CREATED, DESTROYED}.
Definition: nrg_event.h:56
uint16_t etype
The Entity's user-defined type id.
Definition: nrg_event.h:58
Event raised when the Client becomes disconnected.
Definition: nrg_event.h:47
DisconnectEvent dc
For DISCONNECTED.
Definition: nrg_event.h:74
Common defines and includes used by all the other nrg header files.
PlayerEvent player
For PLAYER_{JOIN, LEAVE}.
Definition: nrg_event.h:76