Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - samartheory

Pages: [1]
1
General / Ghost Entries Inside The Vector!!
« on: June 23, 2022, 11:12:27 pm »
So I am just getting started with SFML, and trying to make a simple physics engine. While creating multiple objects of a custom struct (which contains sf::CircularShape and some other variables) whenver I press the left key button it should create a new object of my custom class and push it to a vector and at the end of this journey the vector should have the its size equal to 1 (vector.size() = 1) but instead the size increases by greater than 1 that means I am getting some entries from idk where

       //verlet is the custom struct
        vector<verlet> all;
        float radius = 50.f;
        //event loop / game loop
        while (window.isOpen())
        {
                sf::Event ent;
                //if there are any pending events
                while (window.pollEvent(ent))
                {
                        if (ent.type == sf::Event::Closed)
                                window.close();
                }
                //to create a circle
               //*********THE BELOW IF STATEMENT IS CREATING PROBLEM******************
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        sf::Vector2i localPosition = sf::Mouse::getPosition(window);
                        verlet cir(radius, (sf::Vector2f)localPosition);
                        all.push_back(cir);
                }
                //clear all circles
     
                else if (sf::Mouse::isButtonPressed(sf::Mouse::Right))
                {
                        all.clear();
                }

               
       
                window.clear();
                cout << all.size() << endl;
                window.draw(ground);
                window.display();
        }
 

Any help will be appreciated!

Pages: [1]