Jump to content

  • Log In with Google      Sign In   
  • Create Account

Chronicles of the Hieroglyph



Don't use Singletons for Event Managers!

Posted by , 21 February 2013 - - - - - - · 1,141 views
singleton
The most recent commit to the Hieroglyph 3 repository has reduced my usage of the simple event system in the engine. I think the reasons behind this can apply to others, so I wanted to make a post here about it and try to share my experiences with the community.

A long time ago, I added an event manager to the Hieroglyph codebase. This was really long time ago - something like 10 years ago... Anyway, it was added when I was still in my C++ infancy, and of course I chose to make the event manager a singleton. It was perfectly logical - why in the world would anyone want an event system where not every listener would receive all the messages it signed up for?

Naturally, my coding style has evolved along with time using the language. And naturally I developed a scenario where the event system would need to be replicated in several small mini-application systems (if you missed the last post, I am referring to my Glyphlets!). So I thought, I can just modify the event manager to not be a singleton anymore, and create them as standard objects for each glyphlet.

That would work fine, as long as I didn't make any assumptions in the code that there was only one event manager. Which I didn't do - the event listeners would grab a reference to the singleton at their startup, so essentially all of the event based functionality broke in a seemingly incoherent mess :(

So to dig myself out, I started to review all of the classes that were using the event manager at all. As it turns out, most of them didn't really need to be using events, and I modified them accordingly to use other mechanisms to achieve their messaging. At this point, I am now down to only having applications, Glyphlets (which are mini applications), and the camera classes using the event system. With the breadth of the issue reduced, I can now eliminate the singleton interface method and deal with the minimized problem in a comprehensible way.

The moral of the story is this - if you think you need a singleton, then implement the class as a normal object and create a single instance of it to use. Enforce the singleton on yourself, but don't mechanize it through the class. This way, you still respect the class' potential for being used in multiple instances, and it can be very easy to expand later on. By using the singleton interface, you limit future design decisions without much gain - at least in my view, it is better to trust yourself to follow the access rules than to force yourself into one design later on down the road!


Glyphlets

Posted by , 10 February 2013 - - - - - - · 767 views

As I alluded to last time around, one of the new features that I have been working on lately is Glyphlets. These little guys are basically an abstraction of an application, and only provide the bare minimum functionality that an app requires. The simplicity of the interface is demonstrated by the Glyphlet header file:
//--------------------------------------------------------------------------------
// This file is a portion of the Hieroglyph 3 Rendering Engine.  It is distributed
// under the MIT License, available in the root of this distribution and 
// at the following URL:
//
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) Jason Zink 
//--------------------------------------------------------------------------------
#ifndef Glyphlet_h
#define Glyphlet_h
//--------------------------------------------------------------------------------
#include "Log.h"
#include "Timer.h"
#include "EventManager.h"
#include "Scene.h"
//--------------------------------------------------------------------------------
namespace Glyph3
{
	class Glyphlet : public IEventListener
	{

	public:

		// The general glyphlet interface is defined by these four functions. 
		// Their individual implementations can perform whatever functionality
		// is needed for a given situation, thus no data members are assumed in
		// this base class.

		virtual void Initialize() = 0;
		virtual void Update( float dt ) = 0;
		virtual void Shutdown() = 0;
		virtual bool HandleEvent( EventPtr pEvent ) = 0;
	};
};
//--------------------------------------------------------------------------------
#endif // Glyphlet_h

While the interface is minimalist, these are only the methods that my framework requires to be able to 'play' a Glyphlet. In fact, the majority of the applications that I have in Hieroglyph 3 are able to be shoehorned into this simple interface. The latest commit to the Hieroglyph 3 repository has added a sample program which does just that - it uses the files from the ParticleStorm sample and creates a Glyphlet out of it. This is mostly accurate - I actually added another subclass that defines Glyphlets that have a single window interface, just to cut down on the amount of copy and paste that is needed to create another mini-application... but the general idea is still the same.

So what does adding a new level of abstraction for an application get me? For one, it means I don't need to create a new application and project for each demo effect or scene that I want to try out. In many respects, it allows me to have a little sandbox to create an app within an app. That sounds a bit silly at first, but it is actually quite an interesting capability. In fact, in the Glyphlets application, I actually created an Actor that can appear in a scene which displays the rendered output from a glyphlet - and then I added another one, allowing for viewing multiple glyphlets within one rendered window. Its fun, and it opens up some interesting possibilities.

To further push the system a bit further, I created a Lua based Glyphlet which uses my new Lua binding technique (its nothing special, but new to me...) to allow for totally customized scripted glyplets. So it seems that I will be able to rapidly iterate over multiple concepts fairly easily.

One thing that the implementation of this interface has demonstrated to me is that I made some unfortunate decisions about the use of Events and my EventManager class early on with Hieroglyph that I will have to correct in the near future. The EventManager was always assumed to be a Singleton up until a few weeks ago, and that doesn't jive when you have multiple 'isolated' instances of an application. So I will be dealing with that, and also trying to streamline and simplify the usage of my material system. I would like to have a cleaner API to use with my scripting system, so I'm trying to cook up some better arrangements for getting things rendered...

Overall, it has been a fun few weeks of development, and I'll be continuing on in the weeks to come!







PARTNERS