Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


ptchaos

Member Since 04 Nov 2012
Offline Last Active Mar 19 2014 04:45 PM

Topics I've Started

Best way to use zIndex

24 February 2013 - 09:53 AM

Greetings,

 

I have a list of objects that have a property zIndex, which indicates the order the objects are processed (updated/draw).

 

Whats the best way to process them in the correct order? Sort the vector each time I push a new object? Use another kind of container?

 

Think about operative system windows. Usual they are showed in the order they are opened (with some exceptions ofc). But when you click on one in the back, it activates and passes to the front. Does it remove from the list and pushes again so its the last to be drawn?

 

Think in a top-down game. You have the ground tiles, then ground objects, then flying objects. Do you have separated lists for each layer?

 

It probably depends on the use, but whats the usual approach to this?

 

 

Thanks,

 

chaos


Error accessing vector member

04 November 2012 - 10:43 AM

Greetings,
been a long time in manage world and now I'm going back to C++.

Trying to do a small object management, but I'm having an error that I can't figure out.

Reduced the project to a smallest possible to show my issue:
(the identation got a bit messed up on copy/paste)

#include <iostream>
#include <vector>
using namespace std;
//
class cControl
{
public:
	virtual void Draw()
	{		
			cout << "Control" << endl;
	};
};
//
class cLabel: public cControl
{
	public:
		cLabel()
		{
		}
		//
		void Draw()
		{
			cout << "Label" << endl;
		}
};
//
class cControls
{
	public:
		vector<cControl*> listControls;
	  
		//
	cLabel CreateLabel()
	{
		cLabel label;
		// #1
		//this->AddControl(&label);
		return(label);
	}
		  
	//
	void AddControl(cControl *control)
	{
		this->listControls.push_back(control);
	}
	//
	void Draw()
	{
		for(cControl *ctrl: listControls)
			ctrl->Draw();
	}
};
//
int main()
{
	cControls controls;
  
	cLabel label1=controls.CreateLabel();
	// #2
	controls.AddControl(&label1);
	//
	controls.Draw();
	//
	cin.get();
	//
	return(EXIT_SUCCESS);
}


The point is to have a base control class that others will derive (label, textbox, line, panel, etc).
I've pointed the problem with #1 and #2.
If I add the control to the list in the main function (#2), it works fine and it calls Draw from label correctly.
But if I comment #2 and uncomment #1, so it adds automatically to list when creating label, it crashes when trying to call Draw from label.

From what I've checked, when the label is added to the vector inside the CreateLabel function, something changes when it gets out of that function, which makes unaccessible when calling the Draw.

Can anyone point me out to the correct path?

Thanks,
chaos

PARTNERS