Jump to content

  • Log In with Google      Sign In   
  • Create Account

Adam Omega

Message Routing

Posted by , 03 January 2012 - - - - - - · 910 views
C++, architecture, indie and 2 more...
To continue in the series of our multi-threaded game engine, I feel the next topic of discussion should be messaging. To facilatate communication between modules we have devised a messaging system that runs in its own thread. This system routes packets of data or as we call them Envelopes between different subscribed functions for a given message id.

First our Envelope
#pragma once
/**
* \file file base name
* \author Adam Martin
* \date 2011-07-21
* \brief Template container for message data passed between cores.
*
*  Usage:
*  Envelope<int> e;
*  e.AddData(10);
*
*/
// Standard Includes
#include <vector>


// Library Includes
#include <boost/any.hpp>
#include <boost/utility/value_init.hpp>
#include <boost/lexical_cast.hpp>

// Local Includes
#include "EventLogger.h"


// Forward Declarations
class Entity;

// Typedefs


class Envelope {
public:
Envelope(void) { this->refCount = 1; }
~Envelope(void) { }
boost::any	 GetData	   (unsigned int index = 0)	   { return this->data.at(index); } // Get the data stored at index i
boost::any	 GetData	   (unsigned int index = 0) const { return this->data.at(index); } // Get the data stored at index i
bool		   GetDataBool   (unsigned int index = 0) { return TGetData<bool>		  (index); }
int			GetDataInt	(unsigned int index = 0) { return TGetData<int>		   (index); }
long		   GetDataLong   (unsigned int index = 0) { return TGetData<long>		  (index); }
unsigned int   GetDataUInt   (unsigned int index = 0) { return TGetData<unsigned int>  (index); }
float		  GetDataFloat  (unsigned int index = 0) { return TGetData<float>		 (index); }
std::string	GetDataString (unsigned int index = 0) { return TGetData<std::string>   (index); }
Entity*		GetDataEntityP(unsigned int index = 0);
void AddData	   (boost::any	 data) { this->data.push_back(data); } // Adds more data to the envelope
void AddDataBool   (bool		   data) { this->data.push_back(data); }
void AddDataInt	(int			data) { this->data.push_back(data); }
void AddDataUInt   (unsigned int   data) { this->data.push_back(data); }
void AddDataFloat  (float		  data) { this->data.push_back(data); }
void AddDataString (std::string	data) { this->data.push_back(data); }
void AddDataColor  (D3DXCOLOR	  data) { this->data.push_back(data); }
void AddDataEntityP(Entity*		data) { this->data.push_back(data); }

int msgid; // Used to identify the message type

private:

template <typename T>
T TGetData(unsigned int index) {
T value = boost::initialized_value;

try {
value = boost::any_cast<T>(GetData(index));
}
catch (boost::bad_any_cast e) {
EventLogger::printToFile(1, "Attempted to get the wrong type from Envelope at index " + boost::lexical_cast<std::string>(index) + ".");
}
catch (std::out_of_range e) {
EventLogger::printToFile(1, "Index " + boost::lexical_cast<std::string>(index) + " out of range when accessing Envelope data.");
}

return value;
}

std::vector<boost::any> data;
};

There are some generic getters and setters for various data types that handle the various casting.

Now we must dynamically create envelopes on the stack as our message thread will delete them after it is done.


Here is our messaging system called MessageRouter.

#pragma once

/**
* \file file base name
* \author Adam Martin
* \date 2011-010-28
* \brief  Message routing class to allow advanced subscription based messaging.
*
*
*
*/

// Standard Includes
#include <map>
#include <queue>
#include <vector>

// Library Includes
#include <boost/bind.hpp>
#include <boost/function.hpp>

// Local Includes

// Local Includes

// Forward Declarations
class Envelope;

// Typedefs
typedef boost::function<void (Envelope*)> subscriber;

enum CORE_MESSAGE : unsigned int {STARTUP = 0x0000, QUIT = 0x0099, SHUTDOWN = 0x0100, CREATE = 0x0001, LOADLIBRARY = 0x0002, MODULESTARTED = 0x0003};

class MessageRouter {
public:
MessageRouter(void);
~MessageRouter(void);

void Subscribe(int id, std::shared_ptr<subscriber>& s); // Subscribe to message id, with subscriber function s
void Subscribe(std::vector<int>& ids, std::shared_ptr<subscriber> s); // Subscribe to all messages ids, with subscriber function s
void Unsubscribe(int id, std::shared_ptr<subscriber> s);
void Unsubscribe(std::vector<int>& ids, std::shared_ptr<subscriber> s);
void Send(Envelope* e, bool async = true); // Add a new envelope to the backlog, Envelopes must be dynamic memory to allow the dtor to free all unrouted messages safely. If sync is set to true the message is sent synchronously
void Route(void); // Thread to handle the backlog
void Shutdown(Envelope* e); // Used to shutdown this module after quit has been initiated
void Quit(Envelope* e); // Used when the application is quiting.
private:
std::map<int, std::vector<std::shared_ptr<subscriber>>> subscriptions; // a mapping of message ids to subscribers
std::queue<Envelope*> backlog;
bool routing;
};

We have some defined message ids at the top, the subscribe and unsubscribe functions next, our Send function with an option to send the message in a blocking synchronous way or asynchronous via the backlog, the Route function which is the threaded function, and a few subscribed functions.

The Route function continues to loop through the backlog until routing is false. Routing is set to false when the subscribed Quit function is called via a message sent to the message router with the message id CORE_MESSAGE::QUIT. Route loops through the backlog by popping the front Envelope from the queue, checking to see if anything is subscribed to the given message id, and then looping through all subscribers by calling the given function pointer and passing in the Envelope. After the loop it deletes the Envelope.

The Route function:


void MessageRouter::Route( void ) {
Envelope* e;
while (routing) {
while (!this->backlog.empty()) {
// Get the next Envelope from the queue and pop it from the queue
e = this->backlog.front();
this->backlog.pop();

// See if anyone is subscribed to the message id, get the functor, and call it
if (this->subscriptions.find(e->msgid) != this->subscriptions.end()) {
std::vector<std::shared_ptr<subscriber>> vec = this->subscriptions[e->msgid];
for(std::vector<std::shared_ptr<subscriber>>::iterator itr = vec.begin(); itr != vec.end(); ++itr) {
(*itr->get())(e);
}
}

// Free up memory
delete e;
}
Sleep(1);
}
}



NLS Engine Module Management

Posted by , 12 December 2011 - - - - - - · 512 views
engine, C++ and 2 more...
Last entry was about the basics, and the generic module interface. This time I think we need to discuss how the modules are loaded and managed.

Introducing the ModuleManager

#pragma once

/**
* \file file base name
* \author Adam Martin
* \date 2011-10-23
* \brief A manager class to load and start modules.
*
* A manager that can load/unload and start/stop modules at runtime through dynamically loaded
* libraries. The use of a common interface ModuleInterface allows us to have a uniform loading
* and starting procedure.
*/

// Standard Includes
#include <string>
#include <map>

// Library Includes
#include <boost/foreach.hpp> // Quick simple looping

// Local Includes
#include "../SharedBase/MessageRouter.h" // Needed in several locations throughout the header

// Forward Declarations
class GlobalProperties;
class ModuleInterface;
class EntityManager;

// Typedefs
typedef ModuleInterface* (*ModuleInstanceFactory)(GlobalProperties*, MessageRouter*, EntityManager*); // Used to find the address of the create system function
// Preprocessor selection based on OS
#ifdef _WIN32
#include <Windows.h>
typedef HMODULE DLLHANDLE;
#else
#include <dlfcn.h>
typedef void* DLLHANDLE;
#endif

class ModuleManager {
public:
	ModuleManager(GlobalProperties* gprops, MessageRouter* msgrouter, EntityManager* emgr);

	void Load(std::string name); // The name is required in order to load the new library
	void Unload(std::string name = "");

	void Update(double dt = 0.0f);

private:
	GlobalProperties* gprops;
	MessageRouter* msgrouter;
	EntityManager* emgr;

	std::map<std::string, ModuleInterface*> modules; // A mapping of each core to its given name
	std::map<std::string, DLLHANDLE> libraries; // A mapping of each loaded library to a given filename
};
*Note that any non-windows code as not been tested, and I really don't know if it works.

This is the meat of the module manager. Simple put you create an instance, call Load/Unload respectively to load a module with the given filename.

The actual meat of the code is as follows:

/**
* \file file base name
* \author Adam Martin
* \date 2011-10-23
* \brief A manager class to load and start modules.
*
* A manager that can load/unload and start/stop modules at runtime through dynamically loaded
* libraries. The use of a common interface ModuleInterface allows us to have a uniform loading
* and starting procedure.
*/

#include "ModuleManager.h"

// Standard Includes

// Library Includes
#include <boost/function.hpp>
#include <boost/any.hpp>

// Local Includes
#include "../sharedbase/ModuleInterface.h"
#include "../sharedbase/EventLogger.h"
#include "../sharedbase/EntityManager.h"
#include "../ScriptDLL/EntityFactory.h"

// Static class member initialization

// Class methods in the order they are defined within the class header


ModuleManager::ModuleManager( GlobalProperties* gprops, MessageRouter* msgrouter, EntityManager* emgr) : gprops(gprops), msgrouter(msgrouter), emgr(emgr) {

}

void ModuleManager::Load(std::string name) {
	char buf[256];
	
	if (this->libraries.find(name) == this->libraries.end()) {
		// Load the DLL ONLY if the libary has not already been loaded in the past.
#ifdef _WIN32
		HMODULE libdll = LoadLibrary(name.c_str());
#else
		void * libdll = dlopen(fname.c_str(), RTLD_LAZY);
#endif
		this->libraries[name] = libdll;

		if (libdll != NULL) {
			LOG(1, "Loaded library '" + name + "' successfully.");
		}
		else {
#ifdef _WIN32
			DWORD errcode = GetLastError();
			FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcode, 0, buf, 256, NULL);
#else
			buf = "Error loading library: " + name;
#endif
			LOG(4, buf);
			
			LOG(4, "Module loading aborted due to error.");
			return; // *NOTE: This early return may be considered bad style, but was added to maintain modularity between the DLL loader code and the module facotry loading code.
		}
	}
	else {
		LOG(2, "Library '" + name + "' already loaded, not reloading.");
	}

	{
		ModuleInstanceFactory fact;
#ifdef _WIN32
		fact = (ModuleInstanceFactory)GetProcAddress(this->libraries[name], "ModuleFactory");
#else
		fact = (ModuleInstanceFactory)dlsym(this->libraries[name], "ModuleFactory");
#endif

		if (fact != nullptr) {
			LOG(1, "Module factory acquired successfully.");
			
			ModuleInterface* module = fact(this->gprops, this->msgrouter, this->emgr);
			this->modules[name] = module;
		}
		else {
#ifdef _WIN32
			DWORD errcode = GetLastError();
			FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcode, 0, buf, 256, NULL);
#else
			buf = "Error loading module factory";
#endif
			LOG(4, buf);
			
			LOG(4, "Module loading aborted due to error.");
			
			Unload(name);
			
			return;
		}
	}
}

void ModuleManager::Unload( std::string name /*= ""*/ ) {
	if (this->libraries.find(name) != this->libraries.end()) { // Lib WAS found
		if (this->modules.find(name) != this->modules.end()) { // Mod WAS found
			//Shutdown(name);
		}
		
#ifdef _WIN32
		if (FreeLibrary(this->libraries[name]) != 0) {
#else
		if (dlclose(this->libraries[name])) {
#endif
			this->libraries.erase(name);
		}
		else {
			LOG(4, "Unable to unload the library '" + name + "'!");
		}
	}
}

void ModuleManager::Update( double dt /*= 0.0f*/ ) {
	for (auto it = this->modules.begin(); it != this->modules.end(); ++it) {
		(*it).second->Update(dt);
	}
}
*Note LOG is located in a separate header and just logs a message with a certain log level.

In the loading process a function pointer to get an instance of the modules ModuleInterface is found and called. ModuleManager simple loads and unloads modules and there respective library files from memory.


Well...I have learned a lot

Posted by , 11 December 2011 - - - - - - · 463 views
engine, C++ and 3 more...
Some time about I posted about systems, and their respective managers. I still have used parts of that system, but at the same time I have evolved beyond the system approach and moved into using distinct and operate modules.

Let's define a few things first:
  • Module: any system or system-like representation that is loaded externally, and controls a specific system and/or certain types of components.
  • Component: any self-contained unit of code that is used to make up an entity. Entities are made up of many components ranging from 3d models, to sound effects, and even physics objects. They are extremely encapsulated, and they are never transferred beyond the barrier of the module.
  • Entity: the basic game object. All things inside the game that the player can interact with in some way is an entity. Entity are just a position, rotation, and scale structure to describe the root of the components that are linked to that entity. Entities also have a unique id used to link the components that make up that entity inside each module.
Those 3 things make up the core idea for the engine I (and another classmate) are currently working on. The concept is simple: create a different style of game engine that is modern, clean and encapsulated, and can be added to very easily.

The high-level design of the engine provides a generic interface through which all modules are accessed.
#pragma once
/*
	Author: Adam Martin
	Date: July 20 2011
	Description ModuleInterface class used as a common base for all modules.
*/

// Standard Includes
#include <string>
#include <map>

// Library Includes
#include <boost/any.hpp>

// Local Includes

// Forward Declarations
class GlobalProperties;
class ComponentInterface;
class MessageRouter;
class Envelope;
class EntityManager;
struct Entity;

// Typedefs

class ModuleInterface { // All systems derive from this
public:
	ModuleInterface() : { };
	~ModuleInterface(void) { }

	virtual void Update(double dt) = 0; // Called each game update with change in time (dt) in milliseconds since last update
    virtual void CreateComponent(std::string type, std::map< std::string, boost::any > &attributes, Entity* e) = 0; // Called to create a component of type (type), with the set of attributes (attributes), and with parent entity (e)

protected:
	double deltaAccumulator; // Accumulator for the change in time between each call to update
	std::map< int, ComponentInterface* > components; // Map components with a given Entity ID
};


Of course this isn't very useful on its own, so a few other class pointers are include to provide some communication and basic global data sharing. *Note the forward declarations were left in the original code block for the following.

The constructor becomes:
ModuleInterface(GlobalProperties* gprops, MessageRouter* msgrouter, EntityManager* emgr = nullptr) : msgrouter(msgrouter), gprops(gprops), emgr(emgr) { };

The actual message handling function:
virtual void Message(Envelope* e) = 0; // Handle message ID (msg). Messages are such as mouse click, keyboard, or os events. Envelope is a container to store the arguments of the message.
Some additional member variables:
MessageRouter* msgrouter;
 GlobalProperties* gprops;
 EntityManager* emgr;

The code for the engine code named NLS Engine (as the in Next Logical Step in engine development) is online and open sourced here NLS Engine on Bitbucket


Subsystems and their management.

Posted by , 23 January 2011 - - - - - - · 683 views

I am developing a component based system using the concept of subsystems for my current game. The system is the game (which controls level loading, timiing, etc) and the subsystems as things such as physics, audio, scripting, rendering, etc. Now each subsystem has its own data that it uses to perform its tasks. These subsystems however don't modify their data, but instead the use modifiers to change it. For example a render subsystem instance may contain a texture pointer, the RECT from the texture to use, and the destination RECT. Now we add a modifier for animation for example. This modifier can change the texture pointer or either source or destination RECTs.

Each subsystem is meant to be encapsulated and abstracted to the point that it can be used in any system (Direct X or OpenGL, PhysX or Havok, etc) with little modification. To achieve this we need to store instances of each subsystem inside a manager for that particular subsystem.
std::map< SubSystem*, std::map< std::string, std::string > > subsystems;
This does the trick. We use the map to store a pointer to the instance, and map that pointer to a properties name/value map. This allows use to store and retrieve information about the particular instance, such as an id, name, size, etc.

To register a particular instance we can use this simple function:
void SysMgr::RegisterSubSystem( SubSystem* s )
{
	s->Register(this);
	this->subsystems[s]["name"] = s->GetName();
}
SubSystem::GetName() returns a std::string with the instance or subsystem name. We also pass along a pointer to the manager so the instance can set or remove propeties or callbacks. To accomplish that task we use:
void SysMgr::SetProperty( SubSystem* s, std::string name, std::string value )
{
	this->subsystems[s][name] = value;
}
void SysMgr::RemoveProperty( SubSystem* s, std::string name )
{
	this->subsystems[s].erase(name);
}
Simple and effective.

I have attached the manager and subsystem code below. These are virtual classes and will need to be subclasses to your own needs. I will try and post an example subsystem later today or tomorrow to give a feel for how to use the system. Modifiers are not included as that aspect isn't passed the idea stage quite yet.

Attached Files




A*, a first attempt, and learned tips.

Posted by , 19 January 2011 - - - - - - · 432 views

So I am creating a simple puzzle game with basic path-finding in a 2d environment, and I figured the simplest place to start would be an A* algorithm. After finding a few web sources I sat down and just started programming. After 15 minutes or so I managed to craft a basic heuristic A* algorithm.

Here is my NodeMap header:
#pragma once

#include <map>
#include <string>
#include <vector>

enum STATE {SOLID, PICKUP, FREE};

struct Coord {
	int x,y;
	bool operator<(const Coord& c) const {
		if (x != c.x)
		{
			return x < c.x;
		}
		else
		{
			return y < c.y;
		}

	}
};

struct Node {
	int id;
	STATE state;
	Node(int i = 0, STATE s = FREE) : id(i), state(s) {	}
};

struct pathNode {
	Node n;
	int f, g;
	float h;
	Coord pos;
	pathNode* parent;
};

struct Tiles {
	std::string fname;
	int x, y, w, h;
};

typedef std::map< Coord, Node > NodeGrid;
typedef std::map< Coord, pathNode > PathNodeGrid;

class NodeMap {
public:
	NodeMap() {

	}

	~NodeMap() {

	}

	// Retrieves the node at x,y
	Node getNode(Coord c) {
		return this->grid[c];
	}

	// Sets the specified node at x,y to n
	void setNode(Coord c, Node n) {
		this->grid[c] = n;
	}

	// Shortcut to set the node at x,y to an empty,free node
	void removeNode(Coord c) {
		setNode(c, Node(0, FREE));
	}

	// Retrieves the begin iterator
	NodeGrid::iterator begin() {
		return this->grid.begin();
	}
	
	// Retrieves the end iterator
	NodeGrid::iterator end() {
		return this->grid.begin();
	}

	// Finds the path to the target node starting at x,y
	void FindPath(Coord player);
	
	// Get the iterator to the path
	std::vector<Coord>::iterator GetPath() {
		if (path.size() > 0) {
			return path.begin();
		}
	}

	void SetGoal(int x, int y) {
		this->goal.x = x;
		this->goal.y = y;
	}
private:
	NodeGrid grid;
	std::vector<Coord> path;
	Coord goal;
	PathNodeGrid openList;
	PathNodeGrid closedList;
};


Here is the guts of the A*. The input is simply where the player is now. Previously you will need to set the goal by calling NodeMap::SetGoal.
#include "NodeMap.h"


void NodeMap::FindPath(Coord player) {

	pathNode playerNode = {this->grid[player], 0,0,0, player, nullptr};
	pathNode targetNode;
	this->openList[player] = playerNode;

	while (this->openList.size() > 0) {
		// Find the lowest f cost in the this->openList,
		// set it as our loc node,
		// and move it to the this->closedList
		PathNodeGrid::iterator lowest = this->openList.begin();
		for (PathNodeGrid::iterator itr = this->openList.begin(); itr != this->openList.end(); ++itr) {
			if (itr->second.f < lowest->second.f) {
				lowest = itr;
			}
		}

    	if (lowest == openList.end()) { // Make sure we have a valid element
    	    	return;
    	}

		this->closedList.insert(*lowest);
		pathNode* curNode = &this->closedList[lowest->second.pos]; // grab the pointer to the closed list item as the one in open list will be removed
		this->openList.erase(lowest);
		if ((curNode->pos.x == goal.x) && (curNode->pos.y == goal.y)) {
			targetNode = *curNode;
			// We have reach out goal so lets store the path
			pathNode* parent = &targetNode;
			while (parent != nullptr) {
				this->path.push_back(parent->pos);
				parent = parent->parent;
			}
			return;
		}


		// Get the surrounding nodes 
		pathNode temp;
		int cost;
		Coord loc = player;
		for (int x = -1; x < 2; ++x) {
			for (int y = -1; y < 2; ++y) {
				if ((x == 0) && (y == 0)) { // We don't need to check the current node
					continue;
				} else if ((x == 0) || (y == 0)) {
					cost = 10;
				} else {
					cost = 14;
				}
				loc.x = curNode->pos.x + x;
				loc.y = curNode->pos.y + y;
				if (this->grid.find(loc) != this->grid.end()) { // Node at coords exists
					if ((this->openList.find(loc) == this->openList.end()) && (this->closedList.find(loc) == this->closedList.end())) { // Not on open or closed list
						if (this->grid[loc].state == FREE) {
							temp.g = cost + curNode->g;
							temp.h = sqrt((float)((this->goal.x - curNode->pos.x)^2 + (this->goal.y - curNode->pos.y)^2));
							temp.f = (float)temp.g + temp.h;
							temp.pos = loc;
							temp.parent = curNode;
							this->openList[loc] = temp;
						}
					} else if (this->openList.find(loc) != this->openList.end()) {
						if (this->openList[loc].g > (curNode->g + cost)) {
							this->openList[loc].parent = curNode;
							this->openList[loc].g = (curNode->g + cost);
							this->openList[loc].f = (float)this->openList[loc].g + this->openList[loc].h;
						}
					}
				}
			}
		}
	}
}

My choice in using a map for my open and closed list is what I am going to talk about. First when doing the open list, you don't need to sort it as most tutorials say. This is just a waste of time. you need to iterate over the list one way or another to find the smallest f. By sorting by f values you my end up going through the whole list anyway. Another tip is to always look for the quick out. I am constantly checking to make sure the coord exists to exit the loop early. This may seem like a waste, but if I can save several inner loops I will.





December 2016 »

S M T W T F S
    123
456 7 8910
11121314151617
18192021222324
25262728293031

Recent Entries

Recent Comments