This topic is now archived and is closed to further replies.


Creating Independant Objects

Recommended Posts

I have devised a new Flood Filling Algorithm but it seems to have a problem... I have created a class Spawn ...
typedef enum DIR { HORZ, VERT } DIRECTION;

class Spawn
		virtual ~Spawn(void);
		int x;
		int y;
		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 ...
Spawn* Initiator = new Spawn();
Spawn* Iterator  = new Spawn(point.x, point.y, HORZ, oldColor, fillColor, Initiator);
This calls the constructor of the Spawn class...
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;


	bool jump = false;

	switch (d) {
		case HORZ : mx = 1;
					my = 0;
					next = VERT;

		case VERT : mx = 0;
					my = 1;
					next = HORZ;

	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)
			Spawn* 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... "There is a Bug in every Code!"

Share this post

Link to post
Share on other sites