This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Entity Management Framework
  Submitted by



This COTD entry is a simple entity management framework I use in Shake-n-Bake Game Development Kit, my own personal game development toolkit. I primarily use it to manage dynamic objects in my game development projects. The framework is fairly straightforward and includes an EntityManager, abstract Entity class, and abstract EntityFactory class. EntityManage is a singleton that manages EntityFactory classes and active Entity classes. I opted to use entity factories to make it easier for me to load different types of factory implementations from DLLs or shared objects and plug them into the Entity Manager. This allows me to easily expand the different sorts of dynamic objects that appear in a game. I’m sure many of you have similar systems. I figured I’d just share this with the rest of you to share ideas and get some feedback. The code is released with BSD styled license, however if you do intent to use the code please drop me a note. I’m always interested in knowing when my code is useful to someone other than myself. Enjoy!

Currently browsing [snbfrx.zip] (19,907 bytes) - [include/snbfrx/entity.h] - (4,336 bytes)

/* ************************************************************************** */
// Programmer: Tobin Schwaiger-Hastanan (tobin@13hex.com)
// 
// Copyright (C) 2003 13Hex, Inc.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:
// 
//      * Redistributions of source code must retain the above copyright 
//        notice, this list of conditions and the following disclaimer.
// 
//      * Redistributions in binary form must reproduce the above 
//        copyright notice, this list of conditions and the following 
//        disclaimer in the documentation and/or other materials 
//        provided with the distribution. 
// 
//      * Neither the name of 13Hex, Inc. nor the names 
//        of its contributors may be used to endorse or promote 
//        products derived from this software without specific prior 
//        written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
/* ************************************************************************** */
#ifndef _ENTITY_DOT_H_INCLUDED_
#define _ENTITY_DOT_H_INCLUDED_

namespace snbfrx { // forward declaratin of the EntityFactory class. class EntityFactory;

/** * Abstract representation of a dynamic entity. */ class Entity { public: /** * Entity constructor. * * @param pfactory pointer to an EntityFactory that created this entity. */ Entity( EntityFactory* pfactory ); /** * Virtual destructor. */ virtual ~Entity();

/** * Sets the EntityFactory of the entity factory that created this * entity. * * @param pfactory pointer to an EntityFactory that created this entity. */ void setFactory( EntityFactory* pfactory ) { m_pfactory = pfactory; }

/** * Returns the entity factory that had created this entity. * * @return EntityFactory pointer to factory that had created this entity. */ EntityFactory* getFactory() const { return m_pfactory; }

/** * Pure virtual funtion that handles the entity update. * * Typically implementations of this method should handle things * like state management, AI updates, position/movement updates, * and collision detection for the entity. * * If the entity has expired/died, this method will return false * and the EntityManager managing this entity will handle the * destruction of the entity by handing it off to the EntityFactory's * resleaseEntity method. * * @return boolean value that specifies the "life" status of an entity. * true - entity is still alive and kicking. * false - entity has died and should be released/destroyed. */ virtual bool update() = 0;

/** * Pure virtual function that handles the entity rendering. * * This method is called by an EntityManager object when an * entity is alive and associated to that manager. */ virtual void render() = 0;

private: /** pointer to factory that had created this entity */ EntityFactory* m_pfactory; }; }

#endif

Currently browsing [snbfrx.zip] (19,907 bytes) - [include/snbfrx/entityfactory.h] - (4,704 bytes)

/* ************************************************************************** */
// Programmer: Tobin Schwaiger-Hastanan (tobin@13hex.com)
// 
// Copyright (C) 2003 13Hex, Inc.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:
// 
//      * Redistributions of source code must retain the above copyright 
//        notice, this list of conditions and the following disclaimer.
// 
//      * Redistributions in binary form must reproduce the above 
//        copyright notice, this list of conditions and the following 
//        disclaimer in the documentation and/or other materials 
//        provided with the distribution. 
// 
//      * Neither the name of 13Hex, Inc. nor the names 
//        of its contributors may be used to endorse or promote 
//        products derived from this software without specific prior 
//        written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
/* ************************************************************************** */
#ifndef _ENTITYFACTORY_DOT_H_INCLUDED_
#define _ENTITYFACTORY_DOT_H_INCLUDED_

namespace snbfrx { /** * Abstract class EntityFactory that is used to create Entity objects. * * For more effecient handling of entities, I recommend that a pool of entities * are allocated upon factory initialization. When the creation of an entity * is requested, the createEntity method should search the pool to see if a * free entity is available and then return that entity. * * Likewise when an entity is released, the factory should reset the data * for the entity and return it to the pool. */ class EntityFactory { public: /** * Virtual constructor. */ virtual ~EntityFactory();

/** * returns the name of the entity factory. * The name is primarily used to manage factories in the EntityManager * class. * * @return constant reference to an stl string containing the factory name. */ const std::string& getName() const { return m_name; }

/** * returns the description of the entity factory. * * @return constant reference to an stl string containing the description. */ const std::string& getDescription() const { return m_description; }

/** * Pure virtual function that creates an entity from a specific factory. * * @return pointer to a created entity object. */ virtual Entity* createEntity() = 0; /** * Pure virtual function that releases an entity that this factory had * created. * * @param pentity pointer to an entity object that is to be released. */ virtual void releaseEntity( Entity* pentity ) = 0;

protected: /** * Protected method for setting the name of the factory. * * @param name constant reference to an stl string containing the factory name. */ void setName( std::string& name ) { m_name = name; }

/** * Protected method for setting the description of the factory. * * @param description constant reference to an stl string containing the * factory description. */ void setDescription( std::string& description ) { m_description = description; }

private: /** stl string containing the name of the factory */ std::string m_name; /** stl string containing the description of the factory */ std::string m_description; }; }

#endif

Currently browsing [snbfrx.zip] (19,907 bytes) - [include/snbfrx/entitymanager.h] - (7,602 bytes)

/* ************************************************************************** */
// Programmer: Tobin Schwaiger-Hastanan (tobin@13hex.com)
// 
// Copyright (C) 2003 13Hex, Inc.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:
// 
//      * Redistributions of source code must retain the above copyright 
//        notice, this list of conditions and the following disclaimer.
// 
//      * Redistributions in binary form must reproduce the above 
//        copyright notice, this list of conditions and the following 
//        disclaimer in the documentation and/or other materials 
//        provided with the distribution. 
// 
//      * Neither the name of 13Hex, Inc. nor the names 
//        of its contributors may be used to endorse or promote 
//        products derived from this software without specific prior 
//        written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
/* ************************************************************************** */
#ifndef _ENTITYMANAGER_DOT_H_INCLUDED_
#define _ENTITYMANAGER_DOT_H_INCLUDED_

namespace snbfrx { /** * Singleton EntityManager used to manage EntityFactory and Entity classes. * * This calls allows you to manage EntityFactory classes by name. You can * look up and get name factories classes. The factories are used to instantiate * entity objects. Entities are any dynamic elements that may be in your game that * need to be managed through a simple update and render methods. * * When you add an entity */ class EntityManager { private: /** * Default constructor, private to protect the singleton implementation. */ EntityManager(); /** * Unimplemented copy constructor to keep instances of EntityManager objects * from being copied. */ EntityManager( const EntityManager& ); public:

/** * Virtual destructor. */ virtual ~EntityManager();

/** * Singleton instantiation interface. This method returns * a single instance of an entity manager. * * @return Pointer to an instantiated Entity Manager. */ static EntityManager* getInstance(); /** * Determines if the EntityManager already has a factory registered * under a specific name. * * @param name string object containing the name to look up. * @return bool value specifying if a factory exists by a given name. */ bool hasFactory( const std::string& name ) const;

/** * Determines if factory object has already been registered in the EntityManager. * * @param pfactory pointer to an EntityFactory to check existance of. * @return bool value specifying if a factory exists. */ bool hasFactory( EntityFactory* pfactory ) const;

/** * Registers a factory under a specific name. * * @param name name of factory being registered. * @param pfactory point to factory to be register. * * @return pointer to the factory added. If null, factory could not be added. * user should check to see if a factory exists of the same name or the * factory object has already be registered. */ EntityFactory* registerFactory( const std::string& name, EntityFactory* pfactory );

/** * Gets a factory of a specific name. * * @param name name of factory to get. * @return pointer to Entity Factory. If the factory does not exist, * null is returned. */ EntityFactory* getFactory( const std::string& name ) const; /** * Erases a registered entity factory from the manager. * This method does not do deletion of the object. * * @param name name of the factory to erase. * @return point to the factory removed from the manager. */ EntityFactory* eraseFactory( const std::string& name );

/** * Gets all registered factories assigned to the manager. * * @return a set of pointers to Entity factories. */ const std::set<EntityFactory*>& getFactories() const { return m_factorySet; }

/** * Checks to see if an active entity is already being managed. * * @param pentity pointer to entity to check. * @return bool value specifying if entity exists or not. * true - entity exists. * false - entity does not exist. */ bool hasEntity( Entity* pentity ) const;

/** * Adds an entity to the manager. * * NOTE: only unique entities may be managed, * two of the same objects cannot be added. * * @param pentity pointer to entity object to be added. * @return pointer to entity added. If entity already is managed, * null is returned. */ Entity* addEntity( Entity* pentity );

/** * Erases entity from manager. * This method does not release the entity, see the update method * for more info about releasing entities. * * @param pentity pointer to entity to be removed. * @return pointer to entity removed from manager. */ Entity* eraseEntity( Entity* pentity ); /** * Gets all active entities managed by manager. * * @return set of points to active entity objects. */ const std::set<Entity*>& getEntities() const { return m_entitySet; }

/** * Updates all active entities. */ void update();

/** * Renders all active entities. */ void render(); private:

/** static pointer to an instance of the Entity Manager. */ static EntityManager* m_pinstance;

/** map of registered entity factories */ std::map<std::string, EntityFactory*> m_factoryMap;

/** set of registered factories */ std::set<EntityFactory*> m_factorySet;

/** set of active entities */ std::set<Entity*> m_entitySet; }; }

#endif

Currently browsing [snbfrx.zip] (19,907 bytes) - [include/snbfrx.h] - (2,138 bytes)

/* ************************************************************************** */
// Programmer: Tobin Schwaiger-Hastanan (tobin@13hex.com)
// 
// Copyright (C) 2003 13Hex, Inc.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:
// 
//      * Redistributions of source code must retain the above copyright 
//        notice, this list of conditions and the following disclaimer.
// 
//      * Redistributions in binary form must reproduce the above 
//        copyright notice, this list of conditions and the following 
//        disclaimer in the documentation and/or other materials 
//        provided with the distribution. 
// 
//      * Neither the name of 13Hex, Inc. nor the names 
//        of its contributors may be used to endorse or promote 
//        products derived from this software without specific prior 
//        written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
/* ************************************************************************** */
#ifndef _SNBFRX_DOT_H_INCLUDED_
#define _SNBFRX_DOT_H_INCLUDED_

#include <string> #include <map> #include <set>

#include <snbfrx/entity.h> #include <snbfrx/entityfactory.h> #include <snbfrx/entitymanager.h>

#endif

Currently browsing [snbfrx.zip] (19,907 bytes) - [samples/snbfrx/simple/main.cpp] - (1,007 bytes)

#include <set>
#include <vector>
using namespace std;

#include <snbfrx.h> using namespace snbfrx;

#include "simpleentity.h" #include "simpleentityfactory.h"

// simple sample showing basic usage of the entity manager... void main() { // get instance of singleton EntityManager. EntityManager* em = EntityManager::getInstance(); // instantiate SimpleEntityFactory SimpleEntityFactory factory( 10 ); // register SimpleEntityFactory em->registerFactory( factory.getName(), &factory );

// insert 10 entities into entity manager. for( int i = 0; i < 10; i ++ ) em->addEntity( factory.createEntity() ); // get active entity set const set<Entity*>& entities = em->getEntities();

// while there are entities in the set... while( entities.size() != 0 ) { // update and render all active entities. em->update(); em->render(); }

// delte manager. delete em; }

Currently browsing [snbfrx.zip] (19,907 bytes) - [samples/snbfrx/simple/simpleentity.cpp] - (1,009 bytes)

#include <stdlib.h>
#include <iostream>
using namespace std;

#include <snbfrx.h> using namespace snbfrx;

#include "simpleentity.h"

// call the entity constructor SimpleEntity::SimpleEntity( int id, EntityFactory* pfactory ):Entity( pfactory ) { // set up the id m_id = id;

// set up health reset(); }

SimpleEntity::~SimpleEntity() {

}

void SimpleEntity::reset() { // assign a random value for the health. m_health = rand() & 0xFF; }

bool SimpleEntity::update() {

// decrease health point each update. m_health --;

// if health is less or equal to zero, then entity is dead. if( m_health <= 0 ) cout << "SimpleEntity::update() -- entity(" << m_id << ") has died." << endl;

return m_health > 0; }

void SimpleEntity::render() { // render entity status. cout << "SimpleEntity::render() -- entity(" << m_id << ") is still alive with "; cout << m_health << " health left." << endl; }

Currently browsing [snbfrx.zip] (19,907 bytes) - [samples/snbfrx/simple/simpleentity.h] - (486 bytes)

#ifndef _SIMPLEENTITY_DOT_H_INCLUDED_
#define _SIMPLEENTITY_DOT_H_INCLUDED_

// Implementation of a simple entity class SimpleEntity:public snbfrx::Entity { public: SimpleEntity( int id, EntityFactory* pfactory ); virtual ~SimpleEntity();

// resets the health of the entity void reset();

virtual bool update(); virtual void render();

private: // entity id int m_id; // health remaining int m_health; };

#endif

Currently browsing [snbfrx.zip] (19,907 bytes) - [samples/snbfrx/simple/simpleentityfactory.cpp] - (1,818 bytes)

#include <string>
#include <vector>
using namespace std;

#include <snbfrx.h> using namespace snbfrx;

#include "simpleentity.h" #include "simpleentityfactory.h"

SimpleEntityFactory::SimpleEntityFactory( int poolsize ) { // sets up name and description. setName( string( "SimpleEntityFactory" ) ); setDescription( string( "SimpleEntityFactory" ) );

// creates entities for the pool. for( int i = 0; i < poolsize; i ++ ) m_freeEntity.push_back( new SimpleEntity( i, this ) ); }

SimpleEntityFactory::~SimpleEntityFactory() { // delete all used entities for( set<SimpleEntity*>::iterator it = m_usedEntity.begin(); it!= m_usedEntity.end(); it ++ ) { SimpleEntity* pentity = *it; delete pentity; }

// delete all free entities for( vector<SimpleEntity*>::iterator it = m_freeEntity.begin(); it!= m_freeEntity.end(); it ++ ) { SimpleEntity* pentity = *it; delete pentity; } }

Entity* SimpleEntityFactory::createEntity() { SimpleEntity* pentity = NULL; // check if there are any freen entities.. if( m_freeEntity.size() != 0 ) { // free entity found. pentity = m_freeEntity.back();

// pop entity off of free entity vector. m_freeEntity.pop_back();

// add entity to used set. m_usedEntity.insert( pentity ); } // reset values of entity. pentity->reset();

// return new entity. return pentity; }

void SimpleEntityFactory::releaseEntity( Entity* pentity ) { // remove entity from the used set. m_usedEntity.erase( (SimpleEntity*) pentity );

// push the entit back on the free entity list. m_freeEntity.push_back( (SimpleEntity*) pentity ); }

Currently browsing [snbfrx.zip] (19,907 bytes) - [samples/snbfrx/simple/simpleentityfactory.h] - (709 bytes)

#ifndef _SIMPLEENTITYFACTORY_DOT_H_INCLUDED_
#define _SIMPLEENTITYFACTORY_DOT_H_INCLUDED_

// simple pooled entity factory. class SimpleEntityFactory:public snbfrx::EntityFactory { public: // constructor, takes in a pool size parameter specifying // the number of entities in the pool SimpleEntityFactory( int poolsize = 10 ); // destructor virtual ~SimpleEntityFactory();

virtual snbfrx::Entity* createEntity(); virtual void releaseEntity( snbfrx::Entity* pentity );

private: // vector of free unused entities. std::vector< SimpleEntity* > m_freeEntity;

// set of used entities. std::set< SimpleEntity* > m_usedEntity; }; #endif

Currently browsing [snbfrx.zip] (19,907 bytes) - [snbfrx/source/entity.cpp] - (2,260 bytes)

/* ************************************************************************** */
// Programmer: Tobin Schwaiger-Hastanan (tobin@13hex.com)
// 
// Copyright (C) 2003 13Hex, Inc.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:
// 
//      * Redistributions of source code must retain the above copyright 
//        notice, this list of conditions and the following disclaimer.
// 
//      * Redistributions in binary form must reproduce the above 
//        copyright notice, this list of conditions and the following 
//        disclaimer in the documentation and/or other materials 
//        provided with the distribution. 
// 
//      * Neither the name of 13Hex, Inc. nor the names 
//        of its contributors may be used to endorse or promote 
//        products derived from this software without specific prior 
//        written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
/* ************************************************************************** */
#include "snbfrx/entity.h"
using namespace snbfrx;

/** * Entity constructor. * * @param pfactory pointer to an EntityFactory that created this entity. */ Entity::Entity( EntityFactory* pfactory ) { setFactory( pfactory ); }

/** * Virtual destructor. */ Entity::~Entity() { // as of right now, does nothing. }

Currently browsing [snbfrx.zip] (19,907 bytes) - [snbfrx/source/entityfactory.cpp] - (2,085 bytes)

/* ************************************************************************** */
// Programmer: Tobin Schwaiger-Hastanan (tobin@13hex.com)
// 
// Copyright (C) 2003 13Hex, Inc.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:
// 
//      * Redistributions of source code must retain the above copyright 
//        notice, this list of conditions and the following disclaimer.
// 
//      * Redistributions in binary form must reproduce the above 
//        copyright notice, this list of conditions and the following 
//        disclaimer in the documentation and/or other materials 
//        provided with the distribution. 
// 
//      * Neither the name of 13Hex, Inc. nor the names 
//        of its contributors may be used to endorse or promote 
//        products derived from this software without specific prior 
//        written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
/* ************************************************************************** */
#include <string>
using namespace std;

#include "snbfrx/entity.h" #include "snbfrx/entityfactory.h" using namespace snbfrx;

EntityFactory::~EntityFactory() {

}


Currently browsing [snbfrx.zip] (19,907 bytes) - [snbfrx/source/entitymanager.cpp] - (7,627 bytes)

/* ************************************************************************** */
// Programmer: Tobin Schwaiger-Hastanan (tobin@13hex.com)
// 
// Copyright (C) 2003 13Hex, Inc.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:
// 
//      * Redistributions of source code must retain the above copyright 
//        notice, this list of conditions and the following disclaimer.
// 
//      * Redistributions in binary form must reproduce the above 
//        copyright notice, this list of conditions and the following 
//        disclaimer in the documentation and/or other materials 
//        provided with the distribution. 
// 
//      * Neither the name of 13Hex, Inc. nor the names 
//        of its contributors may be used to endorse or promote 
//        products derived from this software without specific prior 
//        written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
/* ************************************************************************** */
#include <string>
#include <map>
#include <set>
using namespace std;

#include "snbfrx/entity.h" #include "snbfrx/entityfactory.h" #include "snbfrx/entitymanager.h" using namespace snbfrx; /** static pointer to an instance of the Entity Manager. */ EntityManager* EntityManager::m_pinstance = NULL;

/** * Default constructor, private to protect the singleton implementation. */ EntityManager::EntityManager() {

}

/** * Virtual destructor. */ EntityManager::~EntityManager() {

}

/** * Singleton instantiation interface. This method returns * a single instance of an entity manager. * * @return Pointer to an instantiated Entity Manager. */ EntityManager* EntityManager::getInstance() { if( m_pinstance == NULL ) m_pinstance = new EntityManager();

return m_pinstance; }

/** * Determines if the EntityManager already has a factory registered * under a specific name. * * @param name string object containing the name to look up. * @return bool value specifying if a factory exists by a given name. */ bool EntityManager::hasFactory( const string& name ) const { return m_factoryMap.find( name ) != m_factoryMap.end(); }

/** * Determines if factory object has already been registered in the EntityManager. * * @param pfactory pointer to an EntityFactory to check existance of. * @return bool value specifying if a factory exists. */ bool EntityManager::hasFactory( EntityFactory* pfactory ) const { return m_factorySet.find( pfactory ) != m_factorySet.end(); }

/** * Registers a factory under a specific name. * * @param name name of factory being registered. * @param pfactory point to factory to be register. * * @return pointer to the factory added. If null, factory could not be added. * user should check to see if a factory exists of the same name or the * factory object has already be registered. */ EntityFactory* EntityManager::registerFactory( const string& name, EntityFactory* pfactory ) { if( hasFactory( name ) || hasFactory( pfactory ) ) return NULL;

m_factoryMap[ name ] = pfactory; m_factorySet.insert( pfactory );

return pfactory; }

/** * Gets a factory of a specific name. * * @param name name of factory to get. * @return pointer to Entity Factory. If the factory does not exist, * null is returned. */ EntityFactory* EntityManager::getFactory( const string& name ) const { map<string, EntityFactory*>::const_iterator it = m_factoryMap.find( name ); return (*it).second; }

/** * Erases a registered entity factory from the manager. * This method does not do deletion of the object. * * @param name name of the factory to erase. * @return point to the factory removed from the manager. */ EntityFactory* EntityManager::eraseFactory( const string& name ) { EntityFactory* pfactory = NULL; if( hasFactory( name ) || hasFactory( pfactory ) ) { pfactory = m_factoryMap[ name ]; m_factoryMap.erase( name ); m_factorySet.erase( pfactory ); }

return pfactory; }

/** * Checks to see if an active entity is already being managed. * * @param pentity pointer to entity to check. * @return bool value specifying if entity exists or not. * true - entity exists. * false - entity does not exist. */ bool EntityManager::hasEntity( Entity* pentity ) const { return m_entitySet.find( pentity ) != m_entitySet.end(); }

/** * Adds an entity to the manager. * * NOTE: only unique entities may be managed, * two of the same objects cannot be added. * * @param pentity pointer to entity object to be added. * @return pointer to entity added. If entity already is managed, * null is returned. */ Entity* EntityManager::addEntity( Entity* pentity ) { if( hasEntity( pentity ) ) return NULL;

m_entitySet.insert( pentity ); return pentity; }

/** * Erases entity from manager. * This method does not release the entity, see the update method * for more info about releasing entities. * * @param pentity pointer to entity to be removed. * @return pointer to entity removed from manager. */ Entity* EntityManager::eraseEntity( Entity* pentity ) { Entity* retval = NULL; if( hasEntity( pentity ) ) { m_entitySet.erase( pentity ); retval = pentity; } return retval; }

/** * Updates all active entities. */ void EntityManager::update() { set<Entity*> entities( m_entitySet.begin(), m_entitySet.end() );

// for each active entity... for( set<Entity*>::iterator it = entities.begin(); it != entities.end(); it ++ ) { Entity* pentity = *it;

// update entity. if entity has died, // remove it from the active entity set if( !pentity->update() ) { eraseEntity( pentity ); // gets the parent factory and releases the entity // with that factory. EntityFactory* pfactory = pentity->getFactory(); if( pfactory != NULL ) pfactory->releaseEntity( pentity ); } } }

/** * Renders all active entities. */ void EntityManager::render() { // for each active entity... for( set<Entity*>::iterator it = m_entitySet.begin(); it != m_entitySet.end(); it ++ ) { Entity* pentity = *it; // render entity pentity->render(); } }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.