Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Independant Objects

Status
Not open for further replies.

ITHODVGK

Programmer
May 7, 2003
5
IN
I have devised a new Flood Filling Algorithm but it seems to have a problem...
I have created a class Spawn ...

Code:
typedef enum DIR { HORZ, VERT } DIRECTION;
class Spawn	
       {
	public:
		Spawn(void);
		virtual ~Spawn(void);
		int x;
		int y;
		DIRECTION d;
		Spawn(int x, int y, DIRECTION d, COLORREF oldColor, COLORREF newColor, Spawn* pParent);
		Spawn* m_pParent;
		bool childTerminated;
	};

When a bounded Polygon is drawn on the screen, the user has to click on the interior of the Polygon. Then the following code is executed ...

Code:
Spawn* Initiator = new Spawn();
Spawn* Iterator  = new Spawn(point.x, point.y, HORZ, oldColor, fillColor, Initiator);

This calls the constructor of the Spawn class...

Code:
Spawn::Spawn(int x, int y, DIRECTION d, COLORREF oldColor, COLORREF newColor, Spawn* pParent): x(x), y(y), d(d), m_pParent(pParent), childTerminated(false)
{
	int mx, my;
	DIRECTION next;
	bool jump = false;
	switch (d) {
		case HORZ : mx = 1;
  	  		    my = 0;
 			    next = VERT;	
 			    break;
		case VERT : mx = 0;		
			    my = 1;
			    next = HORZ;
			    break;
	}
	
  	CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
       	CXFillView * pView = (CXFillView*)pFrame->GetActiveView();
	CClientDC dc(pView);	COLORREF pix = dc.GetPixel(x,y);
	if ((pix != newColor) || (pix == oldColor)) {
	        int i,j;
		dc.SetPixel(x, y, newColor);
		Spawn* s;
		for (i = x, j = y ; dc.GetPixel(i, j) != newColor ; i -= mx, j -= my)
 			s = new Spawn(i, j, next, oldColor, newColor, this);					
		for (i = x, j = y ; dc.GetPixel(i, j) != newColor ; i += mx, j += my)
			s = new Spawn(i, j, next, oldColor, newColor,this);

		while (!childTerminated);
	}

	m_pParent->childTerminated = true;
	delete this;
}

What I want to do is that create an object for every point. When an object is created, based on the value of d (HORZ or VERT) it checks all the adjacent points horizontally or vertically and create objects of those points. the constructor for these points would spawn additional objects.
In order to prevent memory shortage I want to delete the object as soon as it has completed checking (left and right) or (top and bottom) neighbours.
I want each object to be independant of all the other objects in existence.
The problem with the above given code is that as soon as the parent object ejects out of the constructor code all the child objects get terminated w/o finishing their jobs. That why I added the childTerminated boolean variable. I also put an empty while loop which waits until the child objects have finished their jobs. But this loop hangs the system.

For those of u who are well familiar with Flash ActionScript programming, I want to do something similar to the duplicateMovieClip function,the movie clip having an onClipEvent(enterFrame) function. This would create movie objects which would be playing independantly.

HELP ME...

 
If you're going to delete a "parent" and want the "child" to live on, it might be best to rearrange the different instantiations into a broader data structure. Maybe an STL list or something, where you can insert or delete objects as needed. I.E. have the implementation of all of the different objects be independant of an instantiation the original object.

Using this method, you can have a Spawn object send messages to your data structure to create new Spawns, then simply delete the original.
 
But how can I separate the implementation of the objects from their instantiation in this case.
After creating the object it should automatically check its neigboring pixels and create more Spawns. This can only be done it its constructor.
Do you have any other idea?
 
The instantiation would have to be separate from the implementation if you want to delete "parents". I would recommend a setup something like this:

Class Spawn
constructor
function check_pixels()

Class Spawn_Structure
function create_spawn( given pixels )
{
New Spawn x
if x.check_pixels() = true //if it is determined that another spawn is needed
create_spawn( pixels ) //create new spawn @ proper position
}

In other words, have a class Spawn that is able to send a message saying whether or not a new Spawn should be created. Then you can have a Spawn_Structure class that checks the message, and creates new Spawn(s) as necessary. This way, the structure of Spawns is independant of any single "parent", and the structure won't collapse if the parent is gone.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top