Permalink
Browse files

Changed the way GameInterfaces work, and they actually work now

  • Loading branch information...
1 parent 155c8eb commit c5c395307ab731d078c20f0312e842adc690f9ad @deek0146 committed May 28, 2012
@@ -24,5 +24,6 @@ void AbstractFactoryBase::build(PropertyBagInterface* _parameters)
void AbstractFactoryBase::registerActions(GameComponentType* _type)
{
- _type->createActionHandle<AbstractFactoryBase, PropertyBagInterface>("build", &AbstractFactoryBase::build);
+ _type->exposeInterface<AbstractFactoryBase, FactoryInterface>();
+ //_type->createActionHandle<AbstractFactoryBase, PropertyBagInterface>("build", &AbstractFactoryBase::build);
}
@@ -5,6 +5,7 @@
#include <istream>
#include <GameObject.h>
#include <Objects/GameInterfaces/PropertyBagInterface.h>
+#include <Objects/GameInterfaces/FactoryInterface.h>
#include <AbstractFactory/FactoryParameters.h>
class FactoryParameters;
class FactoryLoader;
@@ -14,7 +15,7 @@ class EventsListener;
class AbstractFactories;
class PropertyBagInterface;
-class AbstractFactoryBase : public TypedGameComponent<AbstractFactoryBase>
+class AbstractFactoryBase : public TypedGameComponent<AbstractFactoryBase>, public FactoryInterface
{
public:
AbstractFactoryBase(const std::string _name);
@@ -15,6 +15,6 @@ GameComponent* Compilation::useFactory(PropertyBagInterface* _parameters)
AbstractFactoryBase* Compilation::gameObjectFactory()
{
- //static AbstractFactoryBase* factory = dynamic_cast<AbstractFactoryBase*>(DefaultFactories::global()->getNode("GameObject"));
- //return factory;
+ static AbstractFactoryBase* factory = dynamic_cast<AbstractFactoryBase*>(DefaultFactories::global()->getNode("GameObject"));
+ return factory;
}
@@ -25,10 +25,8 @@ void FactoryFactory<Factory>::registerActions(GameComponentType* _type)
template <typename Factory>
GameComponent* FactoryFactory<Factory>::privateUseFactory(PropertyBagInterface* _parameters, GameComponent* _parent)
{
- _parameters->setParent(_parent);
Factory* factory = new Factory;
factory->baseInit(_parameters->getProductName(), _parameters);
- _parameters->setProduct(factory);
_parent->attachChild(factory);
return factory;
}
@@ -6,6 +6,7 @@ FactoryParameters::FactoryParameters(bool logUndefined)
//ctor
product = nullptr;
parent = nullptr;
+ name = "no name programmed";
}
FactoryParameters::~FactoryParameters()
@@ -48,10 +48,13 @@ class FactoryParameters: public PropertyBagInterface
GameComponent* getParent(){return parent;}
void setParent(GameComponent* _parent){parent = _parent;}
+ std::string getProductName(){return name;}
+
protected:
private:
//std::unordered_map<std::string, float> _parameters;
TypeTable table;
+ std::string name;
GameComponent* product;
GameComponent* parent;
};
@@ -20,7 +20,7 @@ GameComponent* InstanceParameters::build(GameComponent* _parent)
std::cout << _parent->getName() << std::endl;
//assert(_parent->getParentNode() != _parent);
//assert(_parent->getParentNode() != _parent->getParentNode()->getParentNode());
- GameComponentPropertyBagInterface interface(this);
+ PropertyBagInterface* interface = this;
GameComponent* factories = Filesystem::global()->getNode("/dev/defaultFactories");
/*std::cout << factories->getName() << ":" << std::endl;
for (auto child = factories->getChildren(); child; child = child->getNext())
@@ -34,17 +34,17 @@ GameComponent* InstanceParameters::build(GameComponent* _parent)
}
catch (...)
{
- factory = Filesystem::global()->getNode("/dev/game/factories")->getNode(type);
+ factory = Filesystem::global()->getNode("dev/game/factories")->getNode(type);
//factory = _parent->getParentNode()->getParentNode()->getNode("factories")->getNode(type);
}
FactoryInterface* factoryInterface = nullptr;
factoryInterface = factory->getInterface<FactoryInterface>();
//assert(dynamic_cast<AbstractFactoryBase<GameComponent>*>(factory));
//auto castFactory = static_cast<AbstractFactoryBase<GameComponent>*>(factory);
AbstractFactoryBase* castFactory = dynamic_cast<AbstractFactoryBase*>(factory);
- //GameComponent* product = factoryInterface->use(&interface, _parent);
+ GameComponent* product = factoryInterface->use(interface, _parent);
/// FIXME this needs to use the interfaces
- GameComponent* product = castFactory->use(&interface, _parent);
+ //GameComponent* product = castFactory->use(&interface, _parent);
for (GameComponent* child = getChildren(); child; child = child->getNext())
{
assert(dynamic_cast<InstanceParameters*>(child));
@@ -63,4 +63,5 @@ void InstanceParameters::setType(const std::string& _type)
void InstanceParameters::registerActions(GameComponentType* _type)
{
+ _type->exposeInterface<InstanceParameters, PropertyBagInterface>();
}
@@ -3,11 +3,12 @@
#include <Types/Vec2f.h>
#include <Types/TypeTable.h>
#include <Objects/GameComponent.h>
+#include <Objects/GameInterfaces/PropertyBagInterface.h>
#include <unordered_map>
#include <string>
class PropertyBagSerializer;
-class InstanceParameters : public TypedGameComponent<InstanceParameters>
+class InstanceParameters : public TypedGameComponent<InstanceParameters>, public PropertyBagInterface
{
public:
InstanceParameters(const std::string& _name);
@@ -53,6 +54,7 @@ class InstanceParameters : public TypedGameComponent<InstanceParameters>
}
static void registerActions(GameComponentType* _type);
+ std::string getProductName(){return getName();}
protected:
private:
//std::unordered_map<std::string, float> _parameters;
View
@@ -33,6 +33,8 @@ namespace Editor
Editor::~Editor()
{
+ delete data;
+ delete parser;
}
void Editor::init()
@@ -43,6 +45,12 @@ namespace Editor
executeCommand(&command);
command = "new Compilation named Crate in factories"; // ? What is "Type" and why does it already exist
executeCommand(&command);
+
+ /*command = "new Body named body in factories/Crate";
+ executeCommand(&command);
+ command = "new Skin named skin in factories/Crate";
+ executeCommand(&command);*/
+
command = "new Crate named crate in entities";
//executeCommand(&command);
CEGUI::Window* root = CEGUI::System::getSingleton().getGUISheet();
@@ -79,6 +87,10 @@ namespace Editor
assert(_parameters->getType() == "Folder");
GameComponent* dev = Filesystem::global()->getNode("dev");
GameComponent* game = _parameters->build(dev);
+ try {
+ delete Filesystem::global()->getNode("game");
+ } catch (...){}
+ Filesystem::global()->attachChild(game);
/*factories->getType()->getActionHandle("build")->execute<InstanceParameters>(factories, _parameters);
GameComponent* game = Filesystem::global()->getNode("/game");*/
}
View
@@ -2,6 +2,7 @@
#include <Graphics/GraphicsManager.h>
#include <Physics/PhysicsManager.h>
#include <Input/InputManager.h>
+#include <Filesystem/Filesystem.h>
#include <GameModes/PlayMode.h>
#include <GameModes/ShooterGame.h>
#include <GameModes/CarneGame.h>
@@ -53,7 +54,7 @@ Game::~Game()
#include <GL/gl.h>
void Game::run()
{
- while (gameMode->update());
+ while (static_cast<GameMode*>(Filesystem::global()->getNode("gameMode"))->update());
delete gameMode;
}
@@ -8,7 +8,7 @@
#include <cassert>
GameMode::GameMode()
- :TypedGameComponent<GameMode>("game", Filesystem::global(), true)
+ :TypedGameComponent<GameMode>("gameMode", Filesystem::global(), true)
{
//ctor
}
@@ -3,14 +3,13 @@
#include <Level/PropertyBagData.h>
FactoryData::FactoryData(const char* _type, const char* _name, const char* _product, PropertyBagData* _propertyBag, const std::vector<FactoryData*>& _childFactories)
-:loader(nullptr)
{
//ctor
product = _product;
propertyBag = _propertyBag;
childFactories = _childFactories;
- loader.setType(_type);
- loader.setName(_name);
+ //loader.setType(_type);
+ //loader.setName(_name);
}
FactoryData::~FactoryData()
View
@@ -16,7 +16,7 @@ class FactoryData : public LoadedData
protected:
private:
void virtualSave(XmlDataSaver* _saver, const std::string* _address);
- CppFactoryLoader loader;
+ //CppFactoryLoader loader;
PropertyBagData* propertyBag;
std::string product;
std::vector<FactoryData*> childFactories;
@@ -26,6 +26,14 @@ GameComponent::GameComponent(GameComponentType* _type, const std::string& _name,
}
else
{
+ try
+ {
+ //Filesystem::global()->getOrphanList()->attachOrphan(this);
+ //assert("Object flagged as not an orphan, but orphan list exists" == nullptr);
+ }
+ catch (...)
+ {
+ }
}
}
@@ -133,6 +133,7 @@ const std::string& generateName()
template <typename Type>
Type* GameComponent::getInterface()
{
+ return type->getInterface<Type>(this);
}
template <typename Type>
GameComponentType TypedGameComponent<Type>::type(generateName<Type>());
@@ -103,7 +103,13 @@ class GameComponentType
ActionHandle* getActionHandle(const std::string& _name);
template <typename VariableType>
VariableHandle<VariableType>* getVariableHandle(const std::string& _name);
- GameInterface* getInterface(const std::string& _name);
+
+ template <typename InterfaceType>
+ InterfaceType* getInterface(GameComponent* _component);
+ GameInterface* getInterface(GameComponent* _component, const std::string& _name);
+ template <typename DerivedType, typename InterfaceType>
+ void exposeInterface();
+
template <typename Type>
Type* getInterface();
static EventHandle* staticGetEventHandle(const std::string& _name);
@@ -127,11 +133,14 @@ class GameComponentType
std::unordered_map<std::string,ActionHandle*> actionHandles;
std::unordered_map<std::string,VariableHandleBase*> variableHandles;
std::unordered_map<std::string,GameInterfaceType*> interfaceHandles;
+ std::unordered_map<unsigned int, void*> interfaceOffsets; /// FIXME should make this a vector
static std::unordered_map<std::string,EventHandle*>& g_EventHandles();
static std::unordered_map<std::string,ActionHandle*>& g_ActionHandles();
static std::unordered_map<std::string,VariableHandleBase*>& g_VariableHandles();
};
+#include <Objects/GameInterface.h>
+#include <Types/PointerInt.h>
template <typename DerivedObject>
void GameComponentType::createActionHandle(const std::string& _name, void (DerivedObject::*_action)())
@@ -210,3 +219,36 @@ void ActionHandle::execute(GameComponent* _object, EventObject* _eventObject)
{
execute(_object, static_cast<void*>(_object), getObjectHandle<EventObject>());
}
+
+template <typename InterfaceType>
+InterfaceType* GameComponentType::getInterface(GameComponent* _object)
+{
+ unsigned int handle = InterfaceType::interfaceHandle();
+ auto iter = interfaceOffsets.find(handle);
+ if (iter == interfaceOffsets.end())
+ throw -1; /// FIXME report a proper error
+ //else
+ assert(sizeof(POINTER_INT) == sizeof(void*));
+ POINTER_INT offset = reinterpret_cast<POINTER_INT>(iter->second);
+ POINTER_INT object = reinterpret_cast<POINTER_INT>(_object);
+ POINTER_INT final = object + offset;
+ InterfaceType* ret = reinterpret_cast<InterfaceType*>(final);
+ assert(dynamic_cast<TypedGameInterface<InterfaceType>*>(ret));
+ return ret;
+}
+
+template <typename DerivedType, typename InterfaceType>
+void GameComponentType::exposeInterface()
+{
+ unsigned int handle = InterfaceType::interfaceHandle();
+ assert(interfaceOffsets.find(handle) == interfaceOffsets.end());
+ char* charNull = reinterpret_cast<char*>(0x100);
+ DerivedType* null = reinterpret_cast<DerivedType*>(charNull);
+ assert(reinterpret_cast<unsigned int>(static_cast<GameComponent*>(null)) == 0x100);
+ InterfaceType* offset = null;
+ POINTER_INT intOffset = reinterpret_cast<POINTER_INT>(offset);
+ assert(intOffset > sizeof(void*) + 0x100 && intOffset < 0x200); /// Sanity check, upper bound could be increased
+ assert(reinterpret_cast<unsigned int>(static_cast<DerivedType*>(offset)) == 0x100);
+ intOffset -= 0x100;
+ interfaceOffsets[handle] = reinterpret_cast<void*>(intOffset);
+}
@@ -43,10 +43,19 @@ class TypedGameInterface: public GameInterface
registrar.check();
}
GameInterfaceType* getType(){return &type;}
- static DerivedType* createInterface(GameComponent* _component);
- static GameInterface* factory(GameComponent* _component){return DerivedType::createInstance(_component);}
+ static DerivedType* factory(GameComponent* _component){return DerivedType::createInstance(_component);}
+ unsigned int getInterfaceHandle(){return interfaceHandle();}
+ static unsigned int interfaceHandle()
+ {
+ static unsigned int handle = newInterfaceHandle();
+ return handle;
+ }
private:
- static GameInterface* createInstance(GameComponent* _component){return new DerivedType(_component);}
+ static unsigned int newInterfaceHandle()
+ {
+ static unsigned int handle = 0;
+ return handle++;
+ }
static GameInterfaceType type;
friend class AutoInstantiateInterface<DerivedType>;
const static AutoInstantiateInterface<DerivedType> registrar;
@@ -76,6 +85,6 @@ template <typename DerivedType>
void TypedGameInterface<DerivedType>::baseRegister()
{
//interfaces[DerivedType::name()] = &type;
- type.selfRegister(&TypedGameInterface<DerivedType>::factory);
+ //type.selfRegister(&TypedGameInterface<DerivedType>::untypedCreateInstance);
DerivedType::derivedRegister();
}
@@ -2,30 +2,13 @@
#include <Objects/GameComponent.h>
#include <Objects/GameInterfaces/PropertyBagInterface.h>
-FactoryInterface::FactoryInterface(GameComponent* _object)
+FactoryInterface::FactoryInterface()
{
- object = _object;
}
-
FactoryInterface::~FactoryInterface()
{
}
-GameComponent* FactoryInterface::use(PropertyBagInterface* _parameters, GameComponent* _parent)
-{
- //assert(useHandle == object->getType()->getActionHandle("build"));
- useHandle = object->getType()->getActionHandle("build"); /// FIXME this is inefficient
- _parameters->setParent(_parent);
- useHandle->execute<PropertyBagInterface>(object, _parameters);
- assert(_parameters->getProduct()->getParentNode() == _parameters->getParent());
- return _parameters->getProduct();
-}
-
void FactoryInterface::derivedRegister()
{
- std::cout << "Getting build handle" << std::endl;
- useHandle = GameComponentType::staticGetActionHandle("build");
- assert(useHandle);
}
-
-ActionHandle* FactoryInterface::useHandle;
@@ -2,16 +2,13 @@
#include <Objects/GameInterface.h>
class PropertyBagInterface;
-class ActionHandle;
class FactoryInterface: public TypedGameInterface<FactoryInterface>
{
public:
- FactoryInterface(GameComponent* _object);
+ FactoryInterface();
~FactoryInterface();
+ virtual GameComponent* use(PropertyBagInterface* _params, GameComponent* _parent)=0;
static void derivedRegister();
- GameComponent* use(PropertyBagInterface*, GameComponent* _parent);
private:
- GameComponent* object;
- static ActionHandle* useHandle;
};
Oops, something went wrong. Retry.

0 comments on commit c5c3953

Please sign in to comment.