ptchaos

Member
  • Content count

    5
  • Joined

  • Last visited

Community Reputation

135 Neutral

About ptchaos

  • Rank
    Newbie
  1. Best way to use zIndex

    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
  2. Error accessing vector member

    /bow Thanks for taking time to give suggestions, you guys are awesome. That forward "thing" looks interesting. I need to invest more time in the new features. ~10 years in manage code and coming back feels a totally new language [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] Thanks again [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] chaos
  3. Error accessing vector member

    [quote name='Trienco' timestamp='1352092874' post='4997443'] Of course now you have to ask yourself who is responsible for cleaning it up and when should that happen. Rule of thumb: for every "new" that isn't assigned to a smart pointer, there must be one (and only one) "delete".[/quote] I'm deleting in the destructor of controls class. Don't know if its enough. Something like this: [code]~cControls() { for(cControl *ctrl : this->listControls) delete ctrl; }[/code] [quote name='Servant of the Lord' timestamp='1352095112' post='4997451'] So, either you need to A) manually micro-manage your memory, or B) Let C++ manage your memory for you using smart pointers. [/quote] Yeah I know. I still have in my checkup list the use of smart pointers, since I've seen that's whats being used to ease memory management. [quote name='Servant of the Lord' timestamp='1352095112' post='4997451']Why's every one of your classes start with the lowercase letter 'c', and what does it stand for? If 'c' stands for 'class', that's a little bit redundant don't you think? [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] ([size=2]See:[/size] [size=2][[url="http://www.jelovic.com/articles/stupid_naming.htm"]CStupidClassName[/url]] [/size][size=2]Sometimes programmers see someone else do something, don't know why it's there, adopt the practice, and then spread that practice to other programmers[/size]) [/quote] LOL, I've been using that for a while, didn't knew it was a thing. I usually use the c as a prefix for instanced classes and without the c for static (singleton). Thanks for the comments guys, chaos
  4. Error accessing vector member

    [quote name='SiCrane' timestamp='1352047907' post='4997216'] Your cLabel objects are created on the stack and you are storing pointers to them in the vectors. This is bad juju as they get destroyed when the function that contains them exits. Two possible fixes are either heap allocate them with new or store by value rather than by pointer. [/quote] Awesome SiCrane, thank you so much. I've changed to: [code] cLabel *CreateLabel() { cLabel *pLabel=new cLabel(); // #1 this->AddControl(pLabel); return(pLabel); } [/code] Seems to be working fine now Thanks, chaos
  5. 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) [code] #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); } [/code] 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