Reflective Factory (Maybe a Not So Beginner Topic)

Started by
13 comments, last by Shamino 18 years, 4 months ago
I would agree with you telastyn about writing too much code at once, I need to take it a few steps back..
----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------
Advertisement
Here is what I have that I know works, but I need to find a better method of doing this.

Object.h
#include "MS3D.h"#include "Player.h"#ifndef OBJECT_H#define OBJECT_Hclass Object{public:	bool MarkedForDeletion;	struct Location	{		float locx;		float locy;		float locz;	};};class Cross : public Object{public:	MS3DModel *Model1;};enum OBJECTS{	OBJ_CROSS};

World.h
#include "Object.h"#ifndef WORLD_H#define WORLD_Hextern std::vector<Object*> Scene_Objects;class World{public:	World();	~World();	int  AddObjectToWorld(int type);	void AddObject(Object * Obj);	void RemoveObjectFromWorld();	void RemoveAllObjectsFromWorld();	private:		float WWidth;	float WLength;	float WHeight;};#endif

World.cpp
#include "World.h"std::vector<Object*> Scene_Objects;std::vector<Object*>::iterator Iter;void World::AddObject(Object * Obj){	Scene_Objects.push_back(Obj);};int World::AddObjectToWorld(int type){    Object * Obj = NULL;    switch (type)    {    case OBJ_CROSS:        Obj = new Cross();        break;    }	    if (Obj != NULL)    {		World::AddObject(Obj);    }	return 0;};void World::RemoveObjectFromWorld(){	for (Iter = Scene_Objects.begin(); Iter!=Scene_Objects.end();){    if ((*Iter)->MarkedForDeletion)    {        delete *Iter;        Iter = Scene_Objects.erase(Iter);    }    else    {        ++Iter;    }}};void World::RemoveAllObjectsFromWorld(){	for ( size_t i = 0; i < Scene_Objects.size( ); ++i )	{ 		delete Scene_Objects;	}	Scene_Objects.clear();};


The ultimate goal is to create and manage a Scene_Objects vector that will be sent to the renderer for final compilation, it works, (i think), but it is clunky in the fact that if you were to add a new object, you have to change the OBJECTS enum in object.h, as well as add a switch statement to the AddObjectToWorld() function..

What can I possibly do to remedy this design problem?
----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------
Hi there,

I've had my management classes running flawlessly for a while now - i'm not too sure if it's the best way to do it, but it works fine for me.

Right, heer goes....

Every type of objects in my engine, ie. octree, scene object, character, particle emitter has it's own base class.

Above each base class I have a manager class.

The manager class uses a stack/queue of a size definable at execution.

when the engine starts, i have several script files that load all of the content, and the management classes automatically take care of the rest.

For scene objects, I only load one of each, so I define the following in the script
id path
MODEL 0 /.../.../model.3ds

and then define the properties of that object, such as it's physics properties, sound effects, etc.

then i create each instance, but only with the non generic data it requires. so..

INST 0
pos x, y, z
vel x, y, z
etc....

END_INST



So after parsing the script, the engine loads in one of each base object,

and then creates instances for each, only storing pointers to the generic data.

ATM i currently have a scene with over 150 instances of about 10 objects, with 10 characters all working fine like this, and at runtime if you want to add any scene objects, yuo just call the function _scenemanager->addObject( id, pos, vel ), and another object is thrown on the stack.

I know this is slightly different to what you are doing but I hope it helps.



That does help alot actually. I was actually wondering whether or not I had to load another model into memory for each object I created, apparently I can just create a new pointer to it, sweet.
----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------
So this is essentially what your scene_manager looks like, probably a more efficient AddScene_ObjectToWorld() function, but this is the gist of it..

#include "Scene_Manager.h"std::vector<Object*> Scene_Objects;std::vector<Object*>::iterator Iter;void Scene_Manager::AddScene_Object(Object * Obj){	Scene_Objects.push_back(Obj);};int Scene_Manager::AddScene_ObjectToWorld(int type){    Object * Obj = NULL;    switch (type)    {    case OBJ_CROSS:        Obj = new Cross();        break;	case OBJ_SHIP:		// dostuff		break;    }	    if (Obj != NULL)    {		Scene_Manager::AddScene_Object(Obj);    }	else	{		MessageBox( NULL, "No Class for the Case, Did you forget to add a class for your enum?", "Error", MB_OK | MB_ICONERROR );		return 0;	}	return 0;};void Scene_Manager::RemoveScene_ObjectFromWorld(){	for (Iter = Scene_Objects.begin(); Iter!=Scene_Objects.end();){    if ((*Iter)->MarkedForDeletion)    {        delete *Iter;        Iter = Scene_Objects.erase(Iter);    }    else    {        ++Iter;    }}};void Scene_Manager::RemoveAllScene_ObjectsFromWorld(){	for ( size_t i = 0; i < Scene_Objects.size( ); ++i )	{ 		delete Scene_Objects;	}	Scene_Objects.clear();};
----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------

This topic is closed to new replies.

Advertisement