ofxEngine

As much as I don’t like starting new projects while working on others, I have started what I call ofxEngine. The purpose is to provide a runtime engine for even further simplified interactions between objects in a nodelike fashion. My inspiration for this was Valve Software’s Source engine (from Half-Life 2, great game to try out for physics, artwork, and coding buffs, as it excels at all of these) and the amazingly easy-for-me-to-learn entity system they have implemented, as well as having some ideas influenced by the DirectShow multimedia filter system.

The library does not introduce any new technologies or techniques other than sheer organization.

There are five classes introduced in ofxEngine:
ofxEngine, ofxBaseObject, ofxBaseMessage, ofxBaseModule, and ofxBaseEntity.

The ofxEngine is the controller.

The ofxBaseMessage carries information between objects (entities and modules).

The ofxBaseModule is meant to provide custom interfaces with external processes or systems. (Think of a rendering device or a world manager.)

The ofxBaseEntity serves as an entity, or a “thing” in the program which holds high-level information and processes and can communicate with modules and other entities. (Think of a game character.)

The ofxBaseObject is an abstract base class that ofxBaseEntity and ofxBaseModule derive from to allow entity-to-module communication through downcasting.

I am at this time almost complete with the code, and I will try to upload the first full code version when I am finished.

Update:

I have decided to redesign the ofxEngine class as a derivative of the ofxThread class. I am using the OF pre-release 0.05 FAT C::B Win32 version of ofxThread, please let me know if the implementation has been changed in newer subversion releases.

I am almost finished with the implementation at this point; but I have to keep up with school, FIRST-Robotics, and work. Grrr, more projects…

Anyways, I will release the update when it is finished, possibly before the end of the weekend.

This sounds great! Threading is pretty important for a game engine. I’m looking forward to your code. What would be even better is if you could post a demo application to give people an example of the engine in use.

Hey! Thanks for the reply. I’m already planning out an example app for ofxEngine, but I need some ideas for how best to exemplify it.

Any suggestions? I don’t want to get into another game project just yet, however, so hold off on those ideas.

Also, I want to get some feedback on some preliminary code fragments.

Here’s some listing for an abstract object class:

  
  
//=== baseobject.h ===  
#pragma once  
  
#include "engine.h"  
  
class ofxEngine;  
  
typedef unsigned long ofxModuleID;  
typedef unsigned long ofxEntityID;  
typedef unsigned long ofxMessageID;  
  
typedef union {  
	ofxModuleID _mid;  
	ofxEntityID _eid;  
	unsigned long _id;  
} ofxObjectID;  
  
class ofxBaseObject  
{  
public:  
	friend class ofxEngine, ofxBaseMessage;  
  
	ofxBaseObject() :_engine(NULL), _name(NULL) {}  
	virtual ~ofxBaseObject(); // Calls unload() and cleanup()  
  
	bool load(); // Calls doLoad() to specify loading methods  
	bool isLoaded();  
	bool unload(); // Calls doUnload() to specify unloading methods  
  
	bool setName(string name);  
	string getName() const;  
  
	bool setEngine(ofxEngine* engine);  
	ofxEngine* getEngine() const;  
  
	virtual ofxMessageID fireMessage(ofxBaseObject* target, void* data) = 0; // Calls createMessage() to construct a message, then fires it.  
	  
private:  
	virtual ofxObjectID doLoad() = 0;  
	virtual bool doUnload() = 0;  
  
protected:  
	virtual void onMessage(ofxBaseMessage* message) = 0; // Called from ofxEngine::fireMessage(), receives fired message.  
	virtual void cleanup() = 0;  
	virtual ofxBaseMessage* createMessage(ofxBaseObject* target, void* data) = 0;  
  
	ofxObjectID _id; // Assigned in the engine's loading process  
	string _name; // Assigned by the user; cannot be changed while loaded.  
	ofxEngine* _engine; // Assigned by the user; cannot be changed while loaded.  
};  
  

EDIT: Also, this isn’t necessarily a game engine per se; after completing this, I am going to make a game engine using this, but at the current point it is simply an organizational engine. I could imagine this being adapted to many different programs. Heh, it’s kind of like a framework inside a framework.

Hey everyone! I have been thinking of implementing a managed multithreading system to ofxEngine.

The way it is currently set up is that the ofxEngine has a single thread (it is derived from ofxThread, after all) which runs all messages, which each trigger the onMessage() function within each target object, running it inside the thread.

The way I was thinking of setting it up was to make the ofxEngine class a threaded manager of multiple other threads, or ofxEngineThread objects. The lists of objects & messages are still contained within the ofxEngine, but they are processed by the ofxEngineThreads. When the ofxEngine receives a message from the fireMessage() function call, it checks to see if any of the ofxEngineThreads are currently available, then it adds the ID to that thread. When each ofxEngineThread runs, it rattles through it’s list of IDs, locks the respective message and it’s target, calls the target’s onMessage() function, then unlocks and deletes the message. This way, the ofxEngine only REALLY has to lock when a message is:

  • being fired into the ofxEngine
  • being referenced by an ofxEngineThread, and
  • being removed and deleted after being processed.
    Threads can be run by calling the member function start() directly, or can be automated from the ofxEngine to run either by a timeframe (normal priority) or immediately when a single message enters (realtime priority). Even if all threads are running, a message can be reserved by a second list of IDs in the ofxEngine to be passed into a thread when it becomes available. Plus, all setter methods of the messages cancel after the message is fired, and all getter methods are thread-safe.

A very basic diagram is shown below: