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

Author Topic: Vector affecting performance in game  (Read 1481 times)

0 Members and 1 Guest are viewing this topic.

Junyi00

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Vector affecting performance in game
« on: June 30, 2016, 06:16:04 pm »
I am trying to hold a vector of projectiles which then i would loop at the end to draw it. I tried doing so but my game immediately freeze for a second the moment i try to add a projectile in into the vector! I finally realized that its vector.push_back() that is the one causing the weak game performance. I have tried using deque, list but none of them shown any improvement in performance.

Quote
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { // under while (window.isOpen()) {}
    if (player.canShoot) {
        player.shot();//timer related, not causing any issues
        fireball.setStartPos(player.getPos());
        fireball.start();//start animation, not causing issues

        if (pList.size() == 3) { //max 3 projectiles, pList is the vector
            pList.pop_front();
        }
        pList.push_back(fireball); //this causing issues
            
    }
}

Please help!
« Last Edit: June 30, 2016, 06:27:40 pm by Junyi00 »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Vector affecting performance in game
« Reply #1 on: June 30, 2016, 06:54:40 pm »

Quote
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))

Be careful with this line, isKeyPressed is dispatched several times even if the key was pressed and released.

Also test performance always in Release mode with all optimizations turned on.
I would like a spanish/latin community...
Problems building for Android? Look here

Junyi00

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Vector affecting performance in game
« Reply #2 on: July 01, 2016, 11:21:29 am »
Thank lots, everything is fine now. I'm still too new at this

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Vector affecting performance in game
« Reply #3 on: July 01, 2016, 07:29:04 pm »
Since you are using the vector as a queue, you may want to consider using std::queue instead. It works very similarly; you still get to push to the back and pop from the front - that's its sole purpose - and you may very well find it faster.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything